1 
2 /*
3 #    Sfront, a SAOL to C translator
4 #    This file: View (a Chipmunk tool) .dat file driver
5 #
6 # Copyright (c) 1999-2006, Regents of the University of California
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions are
11 # met:
12 #
13 #  Redistributions of source code must retain the above copyright
14 #  notice, this list of conditions and the following disclaimer.
15 #
16 #  Redistributions in binary form must reproduce the above copyright
17 #  notice, this list of conditions and the following disclaimer in the
18 #  documentation and/or other materials provided with the distribution.
19 #
20 #  Neither the name of the University of California, Berkeley nor the
21 #  names of its contributors may be used to endorse or promote products
22 #  derived from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #
36 #    Maintainer: John Lazzaro, lazzaro@cs.berkeley.edu
37 */
38 
39 
40 /****************************************************************/
41 /****************************************************************/
42 /*             raw file audio driver for sfront                 */
43 /*       reads/writes 16-bit signed shorts in native format     */
44 /****************************************************************/
45 
46 #include <stdio.h>
47 #include <string.h>
48 
49 #if defined(ASYS_HASOUTPUT)
50 
51 /* default name for output audio file */
52 #define ASYSO_DEFAULTNAME "output"
53 
54 /* global variables, must start with asyso_ */
55 
56 FILE * asyso_fd[ASYS_OCHAN]; /* output file pointer */
57 int asyso_srate;            /* sampling rate */
58 float asyso_stime;           /* sample period */
59 int asyso_channels;         /* number of channels */
60 int asyso_size;             /* number of samples in a buffer */
61 int asyso_nsamp;            /* total number of samples written */
62 ASYS_OTYPE * asyso_buf;      /* location for output buffer */
63 #endif
64 
65 #if defined(ASYS_HASINPUT)
66 
67 /* default name for output audio file */
68 #define ASYSI_DEFAULTNAME "input"
69 
70 /* global variables, must start with asysi_ */
71 
72 FILE * asysi_fd;     /* input file pointer */
73 char * asysi_name;   /* name of file  */
74 int asysi_srate;    /* sampling rate */
75 int asysi_channels; /* number of channels */
76 int asysi_size;    /* number of samples in a buffer */
77 int asysi_nsamp;    /* total number of samples written */
78 ASYS_ITYPE * asysi_buf;   /* location for input buffer */
79 
80 #endif
81 
82 #if defined(ASYS_HASOUTPUT)
83 
84 /****************************************************************/
85 /*        core routine for audio output setup                   */
86 /****************************************************************/
87 
asyso_setup(int srate,int ochannels,int osample,int osize,char * name)88 int asyso_setup(int srate, int ochannels, int osample,
89 		int osize, char * name)
90 
91 
92 {
93   int i;
94   char * cname;
95   char * nname;
96 
97   /* note: ASYS_OCHAN holds same value as ochannels */
98 
99   if (name == NULL)
100     return ASYS_ERROR;
101   else
102     {
103       if (ASYS_OCHAN == 1)
104 	{
105 	  asyso_fd[0] = fopen(name,"w");
106 	  if (asyso_fd[0] == NULL)
107 	    return ASYS_ERROR;
108 	}
109       else
110 	{
111 	  cname = strcpy((char *)calloc((strlen(name)+1),sizeof(char)), name);
112 	  cname[((int)strlen(cname))-4]='\0';
113 	  nname = (char *) malloc((int)strlen(cname)+8);
114 	  for(i=0;i < ASYS_OCHAN;i++)
115 	    {
116 	      sprintf(nname,"%s%i.dat",cname,i+1);
117 	      asyso_fd[i] = fopen(nname,"w");
118 	      if (asyso_fd[i] == NULL)
119 		return ASYS_ERROR;
120 	    }
121 	}
122     }
123 
124   for(i=0;i<ASYS_OCHAN;i++)
125     {
126       fprintf(asyso_fd[i],"1\n");
127       fprintf(asyso_fd[i],"# Sa.c channel %i: sample rate %i\n\n",i+1,srate);
128       fprintf(asyso_fd[i],"pairs\n");
129       fprintf(asyso_fd[i],"ch%i\n",i+1);
130       fprintf(asyso_fd[i],"*\n");
131       fprintf(asyso_fd[i],"*\n");
132     }
133 
134   asyso_srate = srate;
135   asyso_stime = 1.0F/srate;
136   asyso_channels = ochannels;
137   asyso_size = osize;
138   asyso_nsamp = 0;
139   asyso_buf = (ASYS_OTYPE *)calloc(osize, sizeof(ASYS_OTYPE));
140   return ASYS_DONE;
141 }
142 
143 #endif
144 
145 #if defined(ASYS_HASINPUT)
146 
147 /****************************************************************/
148 /*        core routine for audio input setup                   */
149 /****************************************************************/
150 
asysi_setup(int srate,int ichannels,int isample,int isize,char * name)151 int asysi_setup(int srate, int ichannels, int isample,
152 		int isize, char * name)
153 
154 
155 {
156   char strbuf[128];
157   char * ret;
158   char * val;
159 
160   if (name == NULL)
161     val = ASYSI_DEFAULTNAME;
162   else
163     val = name;
164   asysi_name = strcpy((char *) calloc((strlen(val)+1),sizeof(char)), val);
165   asysi_fd = fopen(asysi_name,"r");
166   if (asysi_fd == NULL)
167     return ASYS_ERROR;
168 
169   ret = fgets(strbuf, 128, asysi_fd);
170   while ((strstr(strbuf,"pairs") == NULL)||(strbuf[0]=='#'))
171     {
172       if (ret == NULL)
173 	return ASYS_ERROR;
174       ret = fgets(strbuf, 128, asysi_fd);
175     }
176 
177   if (fgets(strbuf, 128, asysi_fd) == NULL)
178     return ASYS_ERROR;
179   if (fgets(strbuf, 128, asysi_fd) == NULL)
180     return ASYS_ERROR;
181   if (fgets(strbuf, 128, asysi_fd) == NULL)
182     return ASYS_ERROR;
183 
184   asysi_srate = srate;
185   asysi_channels = ichannels;
186   asysi_size = isize;
187   asysi_nsamp = 0;
188   asysi_buf = (ASYS_ITYPE *)malloc(sizeof(ASYS_ITYPE)*isize);
189   return ASYS_DONE;
190 
191 }
192 
193 #endif
194 
195 #if (defined(ASYS_HASOUTPUT) && !defined(ASYS_HASINPUT))
196 
197 /****************************************************************/
198 /*        sets up audio output for a given srate/channels       */
199 /****************************************************************/
200 
asys_osetup(int srate,int ochannels,int osample,char * oname,int toption)201 int asys_osetup(int srate, int ochannels, int osample,
202                 char * oname, int toption)
203 
204 {
205   return asyso_setup(srate, ochannels, osample, ASYS_OCHAN*EV(ACYCLE), oname);
206 }
207 
208 #endif
209 
210 
211 #if (!defined(ASYS_HASOUTPUT) && defined(ASYS_HASINPUT))
212 
213 /****************************************************************/
214 /*        sets up audio input for a given srate/channels       */
215 /****************************************************************/
216 
asys_isetup(int srate,int ichannels,int isample,char * iname,int toption)217 int asys_isetup(int srate, int ichannels, int isample,
218                 char * iname, int toption)
219 
220 {
221   return asysi_setup(srate, ichannels, isample, ASYS_ICHAN*EV(ACYCLE), iname);
222 }
223 
224 #endif
225 
226 
227 #if (defined(ASYS_HASOUTPUT) && defined(ASYS_HASINPUT))
228 
229 /****************************************************************/
230 /*   sets up audio input and output for a given srate/channels  */
231 /****************************************************************/
232 
asys_iosetup(int srate,int ichannels,int ochannels,int isample,int osample,char * iname,char * oname,int toption)233 int asys_iosetup(int srate, int ichannels, int ochannels,
234                  int isample, int osample,
235                  char * iname, char * oname, int toption)
236 
237 {
238   if (asysi_setup(srate, ichannels, isample, ASYS_ICHAN*EV(ACYCLE),
239 		  iname) != ASYS_DONE)
240     return ASYS_ERROR;
241   return asyso_setup(srate, ochannels, osample, ASYS_OCHAN*EV(ACYCLE), oname);
242 }
243 
244 #endif
245 
246 #if defined(ASYS_HASOUTPUT)
247 
248 /****************************************************************/
249 /*             shuts down audio output system                   */
250 /****************************************************************/
251 
asyso_shutdown(void)252 void asyso_shutdown(void)
253 
254 {
255   int i;
256 
257   for (i=0;i<ASYS_OCHAN;i++)
258     {
259       fprintf(asyso_fd[i],"\n");
260       fclose(asyso_fd[i]);
261     }
262 }
263 
264 #endif
265 
266 #if defined(ASYS_HASINPUT)
267 
268 /****************************************************************/
269 /*               shuts down audio input system                  */
270 /****************************************************************/
271 
asysi_shutdown(void)272 void asysi_shutdown(void)
273 
274 {
275   fclose(asysi_fd);
276 }
277 
278 #endif
279 
280 
281 #if (defined(ASYS_HASOUTPUT)&&(!defined(ASYS_HASINPUT)))
282 
283 /****************************************************************/
284 /*                    shuts down audio output                   */
285 /****************************************************************/
286 
asys_oshutdown(void)287 void asys_oshutdown(void)
288 
289 {
290   asyso_shutdown();
291 }
292 
293 #endif
294 
295 #if (!defined(ASYS_HASOUTPUT)&&(defined(ASYS_HASINPUT)))
296 
297 /****************************************************************/
298 /*              shuts down audio input device                   */
299 /****************************************************************/
300 
asys_ishutdown(void)301 void asys_ishutdown(void)
302 
303 {
304   asysi_shutdown();
305 }
306 
307 #endif
308 
309 #if (defined(ASYS_HASOUTPUT)&&(defined(ASYS_HASINPUT)))
310 
311 /****************************************************************/
312 /*              shuts down audio input and output device        */
313 /****************************************************************/
314 
asys_ioshutdown(void)315 void asys_ioshutdown(void)
316 
317 {
318   asysi_shutdown();
319   asyso_shutdown();
320 }
321 
322 #endif
323 
324 
325 #if defined(ASYS_HASOUTPUT)
326 
327 
328 
329 /****************************************************************/
330 /*        creates buffer, and generates starting silence        */
331 /****************************************************************/
332 
asys_preamble(ASYS_OTYPE * asys_obuf[],int * osize)333 int asys_preamble(ASYS_OTYPE * asys_obuf[], int * osize)
334 
335 {
336   int i;
337 
338   *asys_obuf = asyso_buf;
339   *osize = asyso_size;
340   return ASYS_DONE;
341 }
342 
343 
344 /****************************************************************/
345 /*               sends one frame of audio to output             */
346 /****************************************************************/
347 
asys_putbuf(ASYS_OTYPE * asys_obuf[],int * osize)348 int asys_putbuf(ASYS_OTYPE * asys_obuf[], int * osize)
349 
350 
351 {
352   int i;
353 
354   for (i = 0; i < *osize; i++)
355     {
356       fprintf(asyso_fd[i % ASYS_OCHAN], "%f %f\n", asyso_nsamp*asyso_stime,
357 	      (float)((*asys_obuf)[i]));
358       if (!(i%ASYS_OCHAN))
359 	asyso_nsamp++;
360     }
361 
362   return ASYS_DONE;
363 }
364 
365 #endif
366 
367 #if defined(ASYS_HASINPUT)
368 
369 /****************************************************************/
370 /*               gets one frame of audio from input             */
371 /****************************************************************/
372 
asys_getbuf(ASYS_ITYPE * asys_ibuf[],int * isize)373 int asys_getbuf(ASYS_ITYPE * asys_ibuf[], int * isize)
374 
375 {
376   float x, y;
377   int i;
378 
379   if (*asys_ibuf == NULL)
380     *asys_ibuf = asysi_buf;
381   *isize = 0;
382   while (*isize < asysi_size)
383     {
384       if (fscanf(asysi_fd,"%f%f",&x, &y) != 2)
385 	break;
386       for (i = 0; i < asysi_channels; i++)
387 	(*asys_ibuf)[(*isize)++] = (ASYS_ITYPE) y;
388     }
389   asysi_nsamp += *isize;
390   return ASYS_DONE;
391 }
392 
393 #endif
394 
395