Debian Codegraph @

testsuite/record_in_noise_main.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2004 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: January 2004 */
35 /*************************************************************************/
36 /* */
37 /* Record a waveform and save to give file, while playing noise at */
38 /* specified volume all the time */
39 /* */
40 /*************************************************************************/
41 #include <unistd.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/ioctl.h>
47 #include <sys/soundcard.h>
48 #include <fcntl.h>
49 #include "cst_wave.h"
50 #include "cst_audio.h"
51 #include "cst_args.h"
52
53 int desired_rate = 16000;
54 float desired_time = -1;
55 int still_record = 1;
56
57 static const char * const vw_audio_device = "/dev/dsp";
58 static int audio_set_sample_rate_vw(int afd,int sample_rate)
59 {
60 int fmt;
61 int sfmts;
62 int stereo=0;
63 int sstereo=0;
64 int osample_rate;
65 int channels=1;
66
67 ioctl(afd,SNDCTL_DSP_RESET,0);
68 sstereo = stereo;
69 ioctl(afd,SNDCTL_DSP_STEREO,&sstereo);
70 /* Some devices don't do mono even when you ask them nicely */
71 if (sstereo != stereo)
72 osample_rate = sample_rate / 2;
73 else
74 osample_rate = sample_rate;
75 ioctl(afd,SNDCTL_DSP_SPEED,&osample_rate);
76 ioctl(afd,SNDCTL_DSP_CHANNELS,&channels);
77 ioctl(afd,SNDCTL_DSP_GETFMTS,&sfmts);
78
79 if (sfmts == AFMT_U8)
80 fmt = AFMT_U8; // its really an 8 bit only device
81 else if (CST_LITTLE_ENDIAN)
82 fmt = AFMT_S16_LE;
83 else
84 fmt = AFMT_S16_BE;
85
86 ioctl(afd,SNDCTL_DSP_SETFMT,&fmt);
87
88 if (fmt == AFMT_U8)
89 return -1;
90 else
91 return 0;
92 }
93
94
95 void sigint_handler(int a)
96 {
97 still_record = 0;
98 }
99
100 int main(int argc, char **argv)
101 {
102 /* need that cool argument parser */
103 cst_wave *w, *noise;
104 cst_features *args;
105 cst_val *files;
106 int r, n, d, i, j, m;
107 int desired_samples;
108 float nscale;
109 const char *ofile;
110 const char *nfile;
111
112 args = new_features();
113 files =
114 cst_args(argv,argc,
115 "usage: record_in_noise OPTIONS\n"
116 "Record a waveform while playing noise\n"
117 "-noise <string> waveform contain noise\n"
118 "-nscale <float> noise scale\n"
119 "-f <int> frequency for recording\n"
120 "-o <string> Output filename for recording\n"
121 "-t <float> Desired time in seconds\n",
122 args);
123
124 desired_rate = get_param_int(args,"-f",16000);
125 desired_time = get_param_float(args,"-t",3.0);
126 ofile = get_param_string(args,"-o","record.wav");
127 nfile = get_param_string(args,"-noise","noise.wav");
128 nscale = get_param_float(args,"-nscale",1.0);
129
130 noise = new_wave();
131 if (cst_wave_load_riff(noise,nfile) < 0)
132 {
133 cst_error();
134 }
135 cst_wave_rescale(noise,(int)(65535.0 * nscale));
136
137 r = open(vw_audio_device,O_RDWR);
138 audio_set_sample_rate_vw(r,desired_rate);
139
140 signal(SIGINT,sigint_handler);
141
142 w = new_wave();
143 w->sample_rate = desired_rate;
144 desired_samples = desired_time * desired_rate;
145
146 cst_wave_resize(w,desired_samples,1);
147
148 d = 256; n = d;
149 for (i=0,j=0;
150 still_record && ((desired_time < 0) || (i < desired_samples));
151 i+=n,j+=m)
152 {
153 if (j+256 >= noise->num_samples)
154 j = 0;
155 if (desired_time < 0)
156 {
157 if (i+n > w->num_samples)
158 cst_wave_resize(w,w->num_samples*1.25,1);
159 }
160 else if (i+n > w->num_samples)
161 d = w->num_samples - i;
162
163 m = write(r,&noise->samples[j],d*sizeof(short));
164 n = read(r,&w->samples[i],d*sizeof(short));
165 n /= 2;
166 m /= 2;
167 }
168
169 cst_wave_resize(w,i,1);
170
171 cst_wave_save_riff(w,ofile);
172
173 if (desired_time < 0)
174 printf("wrote %d samples %f to %s\n",
175 i,(float)i/(float)desired_rate,ofile);
176
177 return 0;
178 }