1 
2 /*
3 #    Sfront, a SAOL to C translator
4 #    This file: std audio driver for sfront
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 /*           stdin/stdout file audio driver for sfront          */
43 /*      reads/write 16-bit signed shorts in native format       */
44 /*                     to/from stdout/stdin                     */
45 /****************************************************************/
46 
47 #include <stdio.h>
48 #include <string.h>
49 
50 #if defined(ASYS_HASOUTPUT)
51 
52 /* global variables, must start with asys_ */
53 
54 FILE * asyso_fd;     /* output file pointer */
55 char * asyso_name;   /* name of file  */
56 int asyso_srate;    /* sampling rate */
57 int asyso_channels; /* number of channels */
58 int asyso_size;    /* number of samples in a buffer */
59 int asyso_nsamp;    /* total number of samples written */
60 short * asyso_buf;   /* output buffer */
61 
62 #endif
63 
64 #if defined(ASYS_HASINPUT)
65 
66 /* global variables, must start with asys_ */
67 
68 FILE * asysi_fd;     /* input file pointer */
69 char * asysi_name;   /* name of file  */
70 int asysi_srate;    /* sampling rate */
71 int asysi_channels; /* number of channels */
72 int asysi_size;  /* number of samples in a buffer */
73 int asysi_nsamp;    /* total number of samples written */
74 short * asysi_buf;   /* output buffer */
75 
76 #endif
77 
78 #if defined(ASYS_HASOUTPUT)
79 
80 /****************************************************************/
81 /*        core routine for audio output setup                   */
82 /****************************************************************/
83 
84 int asyso_setup(int srate, int ochannels, int osize, char * oname)
85 
86 {
87   asyso_name = strcpy((char *) calloc(7, sizeof(char)), "stdout");
88   asyso_fd = stdout;
89   asyso_srate = srate;
90   asyso_channels = ochannels;
91   asyso_size = osize;
92   asyso_nsamp = 0;
93   asyso_buf = (short *)calloc(osize, sizeof(short));
94   return ASYS_DONE;
95 }
96 
97 #endif
98 
99 #if defined(ASYS_HASINPUT)
100 
101 /****************************************************************/
102 /*        core routine for audio input setup                   */
103 /****************************************************************/
104 
105 int asysi_setup(int srate, int ichannels, int isize, char * iname)
106 
107 {
108   asysi_name = strcpy((char *) calloc(6, sizeof(char)), "stdin");
109   asysi_fd = stdin;
110   asysi_srate = srate;
111   asysi_channels = ichannels;
112   asysi_size = isize;
113   asysi_nsamp = 0;
114   asysi_buf = (short *)malloc(sizeof(short)*isize);
115   return ASYS_DONE;
116 }
117 
118 #endif
119 
120 #if (defined(ASYS_HASOUTPUT) && !defined(ASYS_HASINPUT))
121 
122 /****************************************************************/
123 /*        sets up audio output for a given srate/channels       */
124 /****************************************************************/
125 
126 int asys_osetup(int srate, int ochannels, int osample,
127                 char * oname, int toption)
128 
129 
130 {
131   return asyso_setup(srate, ochannels, ASYS_OCHAN*EV(ACYCLE), oname);
132 }
133 
134 #endif
135 
136 
137 #if (!defined(ASYS_HASOUTPUT) && defined(ASYS_HASINPUT))
138 
139 /****************************************************************/
140 /*        sets up audio input for a given srate/channels       */
141 /****************************************************************/
142 
143 int asys_isetup(int srate, int ichannels, int isample,
144                 char * iname, int toption)
145 
146 {
147   return asysi_setup(srate, ichannels, ASYS_ICHAN*EV(ACYCLE), iname);
148 }
149 
150 #endif
151 
152 
153 #if (defined(ASYS_HASOUTPUT) && defined(ASYS_HASINPUT))
154 
155 /****************************************************************/
156 /*   sets up audio input and output for a given srate/channels  */
157 /****************************************************************/
158 
159 int asys_iosetup(int srate, int ichannels, int ochannels,
160                  int isample, int osample,
161                  char * iname, char * oname, int toption)
162 
163 {
164 
165   if (asysi_setup(srate, ichannels, ASYS_ICHAN*EV(ACYCLE), iname) != ASYS_DONE)
166     return ASYS_ERROR;
167   return asyso_setup(srate, ochannels, ASYS_OCHAN*EV(ACYCLE), oname);
168 
169 }
170 
171 #endif
172 
173 
174 #if defined(ASYS_HASOUTPUT)
175 
176 /****************************************************************/
177 /*             shuts down audio output system                   */
178 /****************************************************************/
179 
180 void asyso_shutdown(void)
181 
182 {
183   char name[1024];
184 
185   fclose(asyso_fd);
186   strcpy(name,asyso_name);
187   strcat(name,".info");
188   asyso_fd = fopen(name,"w");
189   fprintf(asyso_fd,"%i\n",asyso_srate);
190   fprintf(asyso_fd,"%i\n",asyso_channels);
191   fprintf(asyso_fd,"%i\n",asyso_nsamp);
192   fclose(asyso_fd);
193 }
194 
195 #endif
196 
197 #if defined(ASYS_HASINPUT)
198 
199 /****************************************************************/
200 /*               shuts down audio input system                  */
201 /****************************************************************/
202 
203 void asysi_shutdown(void)
204 
205 {
206   char name[1024];
207 
208   fclose(asysi_fd);
209   strcpy(name, asysi_name);
210   strcat(name,".info");
211   asysi_fd = fopen(name,"w");
212   fprintf(asysi_fd,"%i\n", asysi_srate);
213   fprintf(asysi_fd,"%i\n", asysi_channels);
214   fprintf(asysi_fd,"%i\n", asysi_nsamp);
215   fclose(asysi_fd);
216 }
217 
218 #endif
219 
220 #if (defined(ASYS_HASOUTPUT)&&(!defined(ASYS_HASINPUT)))
221 
222 /****************************************************************/
223 /*                    shuts down audio output                   */
224 /****************************************************************/
225 
226 void asys_oshutdown(void)
227 
228 {
229   asyso_shutdown();
230 }
231 
232 #endif
233 
234 #if (!defined(ASYS_HASOUTPUT)&&(defined(ASYS_HASINPUT)))
235 
236 /****************************************************************/
237 /*              shuts down audio input device                   */
238 /****************************************************************/
239 
240 void asys_ishutdown(void)
241 
242 {
243   asysi_shutdown();
244 }
245 
246 #endif
247 
248 #if (defined(ASYS_HASOUTPUT)&&(defined(ASYS_HASINPUT)))
249 
250 /****************************************************************/
251 /*              shuts down audio input and output device        */
252 /****************************************************************/
253 
254 void asys_ioshutdown(void)
255 
256 {
257   asysi_shutdown();
258   asyso_shutdown();
259 }
260 
261 #endif
262 
263 #if defined(ASYS_HASOUTPUT)
264 
265 
266 
267 /****************************************************************/
268 /*        creates buffer, and generates starting silence        */
269 /****************************************************************/
270 
271 int asys_preamble(ASYS_OTYPE * asys_obuf[], int * osize)
272 
273 {
274   int i;
275 
276   *asys_obuf = asyso_buf;
277   *osize = asyso_size;
278   return ASYS_DONE;
279 }
280 
281 
282 /****************************************************************/
283 /*               sends one frame of audio to output             */
284 /****************************************************************/
285 
286 int asys_putbuf(ASYS_OTYPE * asys_obuf[], int * osize)
287 
288 {
289   if (rwrite(*asys_obuf, sizeof(short), *osize, asyso_fd) != *osize)
290     return ASYS_ERROR;
291   asyso_nsamp += *osize;
292   *osize = asyso_size;
293   return ASYS_DONE;
294 }
295 
296 #endif
297 
298 #if defined(ASYS_HASINPUT)
299 
300 /****************************************************************/
301 /*               sends one frame of audio to output             */
302 /****************************************************************/
303 
304 int asys_getbuf(ASYS_ITYPE * asys_ibuf[], int * isize)
305 
306 {
307   if (*asys_ibuf == NULL)
308     *asys_ibuf = asysi_buf;
309   *isize = (int)rread(*asys_ibuf, sizeof(short), asysi_size, asysi_fd);
310   asysi_nsamp += *isize;
311   return ASYS_DONE;
312 }
313 
314 #endif
315