File: sox.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (311 lines) | stat: -rw-r--r-- 7,240 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*****************************************************
 * Modified code from sox for Scilab to read/write 
 * wav files 
 *
 * July 5, 1991
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 * 1999 Copyright ENPC 
 *****************************************************/

#include "st.h"
#include <string.h>

#ifndef WIN32
#include <sys/types.h>
#include <sys/stat.h>
#endif

#include <ctype.h>
#include <errno.h>
#include "../machine.h"
#include "sox.h" 

#define Abs(x) ( ( (x) >= 0) ? (x) : -( x) )
#define Min(x,y) ( ( (x) < (y))  ? (x) : (y) )
extern void sciprint __PARAMS((char *fmt, ...));
struct soundstream informat;
ft_t ft;

static void checkformat __PARAMS((ft_t ft));
static void cleanup  __PARAMS((void));
static void init  __PARAMS((void));
static int filetype __PARAMS((int fd));



/************************************************************
 * loadwave 
 * --------
 * read a wav file and store results in res
 * if flag == 1, res is read 
 * if flag == 0, size of res is computed but res is not read
 ************************************************************/


int C2F(loadwave)(char * filename,double *res, integer * size_res,
		  integer flag,WavInfo *Wi, integer *ierr)
{
  long i,size_max;
#if defined(__alpha)|defined(__ia64__)
  int buf[BUFSIZ];
#else
  long buf[BUFSIZ];
#endif
  int olen;
  double *res1;
  *ierr=0;
  init();
  /* Get input format options */
  ft = &informat;
  ft->ierr=*ierr;
  /* Get input file */
  if ((ft->fp = fopen(filename, READBINARY)) == NULL)
    {
      sciprint("Can't open input file '%s': %s\r\n", 
	       filename, strerror(errno));
      *ierr=1;
      return 0;
    }
  ft->filename = filename;
#if defined(DOS) || defined(__OS2__) || defined(WIN32) || defined (__MWERKS__)
  informat.seekable  = 1;
#else
  informat.seekable  = (filetype(fileno(informat.fp)) == S_IFREG);
#endif
  informat.comment = informat.filename;

  /* Read and write starters can change their formats. */
  wavstartread(&informat,Wi,flag);
  if ( ft->ierr > 0 ) 
    {
      sciprint("Error while reading \r\n");
      *ierr=1;
      return 0;
    }
  checkformat(&informat);
  if ( ft->ierr > 0 ) 
    {
      sciprint("Error while reading \r\n");
      *ierr=1;
      return 0;
    }
  if ( flag == 1) 
    {
      sciprint("Input file: using sample rate %lu\r\n", 
	       informat.info.rate);
      sciprint("\tsize %s, style %s, %d %s\r\n",
	       sizes[informat.info.size], 
	       styles[informat.info.style], informat.info.channels, 
	       (informat.info.channels > 1) ? "channels" : "channel");
  }
  /* read chunk */
  size_max = *size_res ; 
  *size_res  = 0;
  olen = 1;
  res1=res;
  while ( olen > 0 )
    {
      olen = wavread(&informat,buf, (long) BUFSIZ);
      if ( ft->ierr > 0 ) 
	{
	  sciprint("Error while reading \r\n");
	  *ierr=1;
	  return 0;
	}
      *size_res += olen ;
      if (flag == 1 &&  *size_res > size_max ) 
	{
	  sciprint(" Sorry wav file too big \r\n");
	  return 0;
	}
      /** sciprint("2 premier nombres du bloc \r\n"); **/
      if (flag == 1) 
	for ( i = 0 ; i < olen ; i++ ) 
	  {
	    *res1++ = buf[i];
	  }
    }
  
  fclose(informat.fp);

  if ( flag == 1 ) 
    {
      for ( i = 0 ; i < *size_res ; i++ ) 
	{
	  if ( informat.info.size /8 == 1 )
	    res[i] = (res[i]-128)/128;
	  else 
	    res[i] = ((res[i])/32768)/65536;
	}
    }
  *ierr= ft->ierr;
  return 0 ;
}

/************************************************************
 * savewave 
 ************************************************************/

int C2F(savewave)(char * filename,double *res,integer * rate,
		  integer *size_res,integer *nchannels, integer *ierr)
{
  long buf[BUFSIZ];
  long i,size_max;
  int count;
  double m,*loc;
  *ierr=0;
  init();
  /* Get input format options */
  ft = &informat;
  ft->ierr=*ierr;
  /* Get input file */
  if ((ft->fp = fopen(filename, WRITEBINARY)) == NULL)
    {
      sciprint("Can't open output file '%s': %s\r\n", 
	   filename, strerror(errno));
      *ierr=1;
      return 0;
    }

  ft->filename = filename;
#if defined(DOS) || defined(__OS2__) || defined(WIN32) || defined (__MWERKS__)
  informat.seekable  = 1;
#else
  informat.seekable  = (filetype(fileno(informat.fp)) == S_IFREG);
#endif
  informat.comment = informat.filename;
  
  /* changing the formats. */
  informat.info.size = WORDSCI;
  informat.info.rate = *rate ;
  informat.info.style = SIGN2;
  informat.info.channels = *nchannels;

  m=1.0;
  /* 
  m=res[0];
  for ( i = 0 ; i < *size_res ; i++) 
    {
      if ( Abs(res[i]) > m ) m = Abs(res[i]);
    }
  */
  wavstartwrite(&informat);
  if ( ft->ierr > 0 ) 
    {
      *ierr= ft->ierr;
      cleanup();
      return 0;
    }
  /* read chunk */
  size_max = *size_res ; 
  *size_res  = 0;
  count = 0 ;
  loc= res;
  while ( count < size_max ) 
    {
      double x;
      long int len, num;
      len = count + BUFSIZ;
      len = ( len > size_max ) ? size_max : len;
      for ( i = count ; i < len   ; i++ ) 
	{
	  /* x= v/m*2**(31-1); */
	  x= (*loc++)/m*2147483647;
	  buf[i-count ] = (long) x;
	  /** if (i < 2) sciprint("Ecriture d'un long %f %ld\r\n", x,buf[i-count]); **/
	}
      num = len - count ;
      wavwrite(&informat,buf, num );
      if ( ft->ierr > 0 ) 
	{
	  *ierr= ft->ierr;
	  cleanup();
	  return 0;
	}
      count = len;
    }
  wavstopwrite(&informat);
  fclose(informat.fp);
  res[0] = ((double)(size_max))/((double) (*rate));
  *ierr= ft->ierr;
  return 0;
}


void init(void) {
  /* init files */
  informat.info.rate      = 0;
  informat.info.size      = -1;
  informat.info.style     = -1;
  informat.info.channels  = -1;
  informat.comment   = NULL;
  informat.swap      = 0;
  informat.filetype  = "wav";
  informat.fp        = stdin;
  informat.filename  = "input";
}

/* 
 * Process input file -> effect table -> output file
 *	one buffer at a time
 */
#if defined(DOS) || defined(__OS2__) || defined(WIN32) || defined (__MWERKS__)
int filetype(int fd) { return 0;}
#else 
int filetype(int fd)
{
  struct stat st;
  fstat(fd, &st);
  return st.st_mode & S_IFMT;
}
#endif

/* called from util.c:fail */

void cleanup(void)
{
  if (informat.fp)
    fclose(informat.fp);
}

/* check that all settings have been given */

static void checkformat(ft_t ft)
{
  if (ft->info.rate == 0)
    {
      sciprint("Sampling rate for %s file was not given\r\n", ft->filename);
      ft->ierr=1;
      return;
    }
  if ((ft->info.rate < 100) || (ft->info.rate > 50000))
    {
      sciprint("Sampling rate %lu for %s file is bogus\r\n", 
	   ft->info.rate, ft->filename);
      ft->ierr=1;
      return;
    }

  if (ft->info.size == -1)
    {
      sciprint("Data size was not given for %s file\r\n", ft->filename);
      ft->ierr=1;
      return;
    }
  if (ft->info.style == -1 && ft->info.size != FLOATSCI)
    {
      sciprint("Data style was not given for %s file\r\n", ft->filename);
      ft->ierr=1;
      return;
    }
  /* it's so common, might as well default */
  if (ft->info.channels == -1)
    ft->info.channels = 1;
}