1 /* $Header: /home/cvs/wavplay/recplay.c,v 1.2 1999/12/04 00:01:20 wwg Exp $
2 * Warren W. Gay VE3WWG Sun Feb 16 20:14:02 1997
3 *
4 * RECORD/PLAY MODULE FOR WAVPLAY :
5 *
6 * X LessTif WAV Play :
7 *
8 * Copyright (C) 1997 Warren W. Gay VE3WWG
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 * Public License for more details (see enclosed file COPYING).
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Send correspondance to:
24 *
25 * Warren W. Gay VE3WWG
26 *
27 * Email:
28 * ve3wwg@yahoo.com
29 * wgay@mackenziefinancial.com
30 *
31 * $Log: recplay.c,v $
32 * Revision 1.2 1999/12/04 00:01:20 wwg
33 * Implement wavplay-1.4 release changes
34 *
35 * Revision 1.1.1.1 1999/11/21 19:50:56 wwg
36 * Import wavplay-1.3 into CVS
37 *
38 * Revision 1.3 1997/04/19 01:24:22 wwg
39 * 1.0pl2 : Removed the extraneous ' after Hz in the wav info
40 * display. Also removed some extraneous ';' from the same
41 * series of prints.
42 *
43 * Revision 1.2 1997/04/17 23:42:02 wwg
44 * Added #include <errno.h> for 1.0pl2 fix.
45 *
46 * Revision 1.1 1997/04/14 00:18:31 wwg
47 * Initial revision
48 *
49 */
50 static const char rcsid[] = "@(#)recplay.c $Revision: 1.2 $";
51
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include <stdlib.h>
55 #ifndef FREEBSD
56 #include <malloc.h>
57 #endif
58 #include <string.h>
59 #include <fcntl.h>
60 #include <errno.h>
61 #include <sys/stat.h>
62 #ifndef FREEBSD
63 #include <linux/soundcard.h>
64 #else
65 #include <sys/soundcard.h>
66 #endif
67 #include "wavplay.h"
68 #include "server.h"
69
70 static int um = 0666; /* Current umask() value */
71 static ErrFunc v_erf; /* Error function for reporting */
72
73 /*
74 * Play a series of WAV files:
75 */
76 int
wavplay(WavPlayOpts * wavopts,char ** argv,ErrFunc erf)77 wavplay(WavPlayOpts *wavopts,char **argv,ErrFunc erf) {
78 WAVFILE *wfile; /* Opened wav file */
79 DSPFILE *dfile = NULL; /* Opened /dev/dsp device */
80 const char *Pathname; /* Pathname of the open WAV file */
81 int e; /* Saved error code */
82
83 if ( erf != NULL ) /* If called from external module.. */
84 v_erf = erf; /* ..set error reporting function */
85
86 if ( (Pathname = *argv) == NULL )
87 Pathname = "-"; /* Standard input */
88 else Pathname = *argv++; /* Point to first pathname on command line */
89
90 /*
91 * Play each Pathname:
92 */
93 do {
94 if ( cmdopt_x )
95 fprintf(stderr,"Playing WAV file %s :\n",Pathname);
96
97 /*
98 * Open the wav file for read, unless its stdin:
99 */
100 if ( (wfile = WavOpenForRead(Pathname,v_erf)) == NULL )
101 goto errxit;
102
103 /*
104 * Merge in command line option overrides:
105 */
106 WavReadOverrides(wfile,wavopts);
107
108 /*
109 * Send current settings to client when in server mode:
110 */
111 if ( clntIPC >= 0 )
112 toclnt_settings(0,wfile,wavopts);
113
114 /*
115 * Report the file details, unless in quiet mode:
116 */
117 if ( !wavopts->bQuietMode && clntIPC < 0 ) {
118 printf("Pathname:\t%s\n",wfile->Pathname);
119 printf("Device:\t\t%s\n",env_AUDIODEV);
120 printf("Sampling Rate:\t%lu Hz\n",(unsigned long)wfile->wavinfo.SamplingRate);
121 printf("Mode:\t\t%s\n",wfile->wavinfo.Channels == Mono ? "Mono" : "Stereo");
122 printf("Samples:\t%lu\n",(unsigned long)wfile->wavinfo.Samples);
123 printf("Bits:\t\t%u\n\n",(unsigned)wfile->wavinfo.DataBits);
124 }
125
126 if ( !wavopts->bInfoMode ) {
127 /*
128 * If not -i mode, play the file:
129 */
130 if ( (dfile = OpenDSP(wfile,O_WRONLY,v_erf)) == NULL )
131 goto errxit;
132
133 if ( PlayDSP(dfile,wfile,svr_work_proc,v_erf) )
134 goto errxit;
135
136 if ( CloseDSP(dfile,v_erf) ) { /* Close /dev/dsp */
137 dfile = NULL; /* Mark it as closed */
138 goto errxit;
139 }
140 }
141
142 dfile = NULL; /* Mark it as closed */
143 if ( WavClose(wfile,v_erf) ) /* Close the wav file */
144 wfile = NULL; /* Mark the file as closed */
145 wfile = NULL; /* Mark the file as closed */
146 } while ( (Pathname = *argv++) != NULL );
147
148 return 0;
149
150 /*
151 * Error exit:
152 */
153 errxit: e = errno; /* Save errno value */
154 if ( wfile != NULL )
155 WavClose(wfile,NULL); /* Don't report errors here */
156 if ( dfile != NULL )
157 CloseDSP(dfile,NULL); /* Don't report errors here */
158 errno = e; /* Restore error code */
159 return -1;
160 }
161
162 /*
163 * Record a WAV file:
164 */
165 int
wavrecd(WavPlayOpts * wavopts,char * Pathname,void (* erf)(const char * format,va_list ap))166 wavrecd(WavPlayOpts *wavopts,char *Pathname,void (*erf)(const char *format,va_list ap)) {
167 WAVFILE *wfile; /* Opened wav file */
168 DSPFILE *dfile = NULL; /* Opened /dev/dsp device */
169 int e; /* Saved error code */
170 UInt32 samples; /* The number of samples to record */
171 int bServerMode = 0; /* True if in server mode */
172
173 if ( erf != NULL ) /* If called from external module.. */
174 v_erf = erf; /* .. then set error reporting function */
175
176 samples = (UInt32) wavopts->Seconds * (UInt32) wavopts->SamplingRate.optValue;
177
178 /*
179 * Open the wav file for read, unless its stdin:
180 */
181 if ( Pathname != NULL ) {
182 if ( (wfile = WavOpenForWrite(Pathname,wavopts->Channels.optValue,
183 wavopts->SamplingRate.optValue,wavopts->DataBits.optValue,samples,
184 v_erf)) == NULL ) {
185 goto errxit;
186 }
187 } else {
188 bServerMode = 1; /* We're in server mode... */
189 Pathname = svr.path; /* so this is the pathname.. */
190 wfile = svr.wfile; /* And the file is already opened */
191 }
192
193 if ( (dfile = OpenDSP(wfile,O_RDONLY,v_erf)) == NULL )
194 goto errxit;
195
196 if ( RecordDSP(dfile,wfile,samples,svr_work_proc,v_erf) )
197 goto errxit;
198
199 if ( CloseDSP(dfile,v_erf) ) { /* Close /dev/dsp */
200 dfile = NULL; /* Mark it as closed */
201 goto errxit;
202 }
203 dfile = NULL; /* Mark it as closed */
204
205 WavClose(wfile,v_erf); /* Close the wav file */
206 wfile = NULL; /* Mark the file as closed */
207
208 if ( bServerMode )
209 svr.wfile = NULL; /* Indicate no open file now */
210
211 return 0;
212
213 /*
214 * Error exit:
215 */
216 errxit: e = errno; /* Save errno value */
217 if ( wfile != NULL )
218 WavClose(wfile,NULL);
219 if ( dfile != NULL )
220 CloseDSP(dfile,NULL);
221 errno = e; /* Restore error code */
222 return -1;
223 }
224
225 /*
226 * Record or Play WAV files:
227 */
228 int
recplay(WavPlayOpts * wavopts,char ** argv,ErrFunc erf)229 recplay(WavPlayOpts *wavopts,char **argv,ErrFunc erf) {
230 int rc; /* Return code */
231 int e; /* Saved errno value */
232
233 v_erf = erf; /* Error function to use */
234
235 /*
236 * Find out what are umask setting is :
237 */
238 um = umask(0666); /* Discover umask setting by changing it.. */
239 umask(um); /* Now Restore umask setting */
240
241 /*
242 * Lock the device :
243 */
244 if ( wavopts->ipc >= 0 && LockDSP(wavopts->ipc,wavopts->Mode==OprPlay?0:1,v_erf,0) )
245 return -1; /* Failed */
246
247 if ( wavopts->Mode == OprPlay )
248 rc = wavplay(wavopts,argv,NULL); /* Play samples */
249 else rc = wavrecd(wavopts,*argv,NULL); /* Record samples */
250
251 e = errno; /* Save errno value */
252
253 /*
254 * Unlock the device:
255 */
256 if ( wavopts->ipc >= 0 && UnlockDSP(wavopts->ipc,wavopts->Mode==OprPlay?0:1,v_erf) )
257 if ( !rc ) {
258 e = errno;
259 rc = -1;
260 }
261
262 errno = e;
263 return rc;
264 }
265
266 /* $Source: /home/cvs/wavplay/recplay.c,v $ */
267