1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc * recwave.c
3433d6423SLionel Sambuc *
4433d6423SLionel Sambuc * Record sound files in wave format. Only MicroSoft PCM is supported.
5433d6423SLionel Sambuc *
6433d6423SLionel Sambuc * Michel R. Prevenier.
7433d6423SLionel Sambuc */
8433d6423SLionel Sambuc
9433d6423SLionel Sambuc #include <errno.h>
10433d6423SLionel Sambuc #include <sys/types.h>
11433d6423SLionel Sambuc #include <sys/stat.h>
12433d6423SLionel Sambuc #include <termios.h>
13433d6423SLionel Sambuc #include <stdlib.h>
14433d6423SLionel Sambuc #include <fcntl.h>
15433d6423SLionel Sambuc #include <stdio.h>
16433d6423SLionel Sambuc #include <unistd.h>
17433d6423SLionel Sambuc #include <string.h>
18433d6423SLionel Sambuc #include <signal.h>
19433d6423SLionel Sambuc #include <sys/ioctl.h>
20433d6423SLionel Sambuc #include <minix/sound.h>
21433d6423SLionel Sambuc
22433d6423SLionel Sambuc int main(int argc, char **argv);
23433d6423SLionel Sambuc void usage(void);
24433d6423SLionel Sambuc void write_wave_header(void);
25433d6423SLionel Sambuc void terminate(int s);
26433d6423SLionel Sambuc
27433d6423SLionel Sambuc
28433d6423SLionel Sambuc /******* Wave format definitions *********/
29433d6423SLionel Sambuc
30433d6423SLionel Sambuc #define RIFF_ID 0x46464952
31433d6423SLionel Sambuc #define WAVE_ID1 0x45564157
32433d6423SLionel Sambuc #define WAVE_ID2 0x20746D66
33433d6423SLionel Sambuc #define DATA_ID 0x61746164
34433d6423SLionel Sambuc #define MS_PCM_FORMAT 0x0001
35433d6423SLionel Sambuc
36433d6423SLionel Sambuc #define WORD short
37433d6423SLionel Sambuc #define DWORD unsigned long
38433d6423SLionel Sambuc
39433d6423SLionel Sambuc struct RIFF_fields
40433d6423SLionel Sambuc {
41433d6423SLionel Sambuc DWORD RIFF_id;
42433d6423SLionel Sambuc DWORD RIFF_len;
43433d6423SLionel Sambuc DWORD WAVE_id1;
44433d6423SLionel Sambuc DWORD WAVE_id2;
45433d6423SLionel Sambuc DWORD data_ptr;
46433d6423SLionel Sambuc } r_fields;
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc struct common_fields
49433d6423SLionel Sambuc {
50433d6423SLionel Sambuc WORD FormatTag;
51433d6423SLionel Sambuc WORD Channels;
52433d6423SLionel Sambuc DWORD SamplesPerSec;
53433d6423SLionel Sambuc DWORD AvgBytesPerSec;
54433d6423SLionel Sambuc WORD BlockAlign;
55433d6423SLionel Sambuc } c_fields;
56433d6423SLionel Sambuc
57433d6423SLionel Sambuc struct specific_fields
58433d6423SLionel Sambuc {
59433d6423SLionel Sambuc WORD BitsPerSample;
60433d6423SLionel Sambuc } s_fields;
61433d6423SLionel Sambuc
62433d6423SLionel Sambuc DWORD data_id;
63433d6423SLionel Sambuc DWORD data_len;
64433d6423SLionel Sambuc
65433d6423SLionel Sambuc /******** End of wave format definitions *********/
66433d6423SLionel Sambuc
67433d6423SLionel Sambuc /* Default recording values */
68433d6423SLionel Sambuc unsigned int sign = 0;
69433d6423SLionel Sambuc unsigned int bits = 8;
70433d6423SLionel Sambuc unsigned int stereo = 0;
71433d6423SLionel Sambuc unsigned int rate = 22050;
72433d6423SLionel Sambuc
73433d6423SLionel Sambuc int old_stdin;
74433d6423SLionel Sambuc struct termios old_tty, new_tty;
75433d6423SLionel Sambuc int audio, file;
76433d6423SLionel Sambuc
usage()77433d6423SLionel Sambuc void usage()
78433d6423SLionel Sambuc {
79433d6423SLionel Sambuc fprintf(stderr, "Usage: recwav [-b -s -r] file_name\n");
80433d6423SLionel Sambuc exit(-1);
81433d6423SLionel Sambuc }
82433d6423SLionel Sambuc
terminate(int s)83*3bdcd288SJacob Adams void terminate(int s)
84433d6423SLionel Sambuc {
85433d6423SLionel Sambuc /* Restore terminal parameters */
86433d6423SLionel Sambuc tcsetattr(0, TCSANOW, &old_tty);
87433d6423SLionel Sambuc (void) fcntl(0,F_SETFL,old_stdin);
88433d6423SLionel Sambuc close(audio);
89433d6423SLionel Sambuc close(file);
90433d6423SLionel Sambuc exit(0);
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc
write_wave_header()93433d6423SLionel Sambuc void write_wave_header()
94433d6423SLionel Sambuc {
95433d6423SLionel Sambuc /* RIFF fields */
96433d6423SLionel Sambuc r_fields.RIFF_id = RIFF_ID;
97433d6423SLionel Sambuc r_fields.WAVE_id1 = WAVE_ID1;
98433d6423SLionel Sambuc r_fields.WAVE_id2 = WAVE_ID2;
99433d6423SLionel Sambuc r_fields.data_ptr = 16;
100433d6423SLionel Sambuc r_fields.RIFF_len = 20 + r_fields.data_ptr + data_len;
101433d6423SLionel Sambuc
102433d6423SLionel Sambuc /* MicroSoft PCM specific fields */
103433d6423SLionel Sambuc s_fields.BitsPerSample = bits;
104433d6423SLionel Sambuc
105433d6423SLionel Sambuc /* Common fields */
106433d6423SLionel Sambuc c_fields.FormatTag = MS_PCM_FORMAT;
107433d6423SLionel Sambuc c_fields.Channels = stereo + 1;
108433d6423SLionel Sambuc c_fields.SamplesPerSec = rate;
109433d6423SLionel Sambuc c_fields.AvgBytesPerSec = c_fields.Channels * rate * (bits / 8);
110433d6423SLionel Sambuc c_fields.BlockAlign = c_fields.Channels * (bits / 8);
111433d6423SLionel Sambuc
112433d6423SLionel Sambuc /* Data chunk */
113433d6423SLionel Sambuc data_id = DATA_ID;
114433d6423SLionel Sambuc
115433d6423SLionel Sambuc /* Write wave-file header */
116433d6423SLionel Sambuc lseek(file, 0L, SEEK_SET);
117433d6423SLionel Sambuc write(file, &r_fields, 20);
118433d6423SLionel Sambuc write(file, &c_fields, 14);
119433d6423SLionel Sambuc write(file, &s_fields, 2);
120433d6423SLionel Sambuc write(file, &data_id, sizeof(data_id));
121433d6423SLionel Sambuc write(file, &data_len, sizeof(data_len));
122433d6423SLionel Sambuc }
123433d6423SLionel Sambuc
124433d6423SLionel Sambuc
main(int argc,char * argv[])125*3bdcd288SJacob Adams int main(int argc, char* argv[])
126433d6423SLionel Sambuc {
127433d6423SLionel Sambuc unsigned int fragment_size;
128433d6423SLionel Sambuc char *buffer, *file_name;
129433d6423SLionel Sambuc char c;
130433d6423SLionel Sambuc int i;
131433d6423SLionel Sambuc
132433d6423SLionel Sambuc /* Read parameters */
133433d6423SLionel Sambuc if (argc < 2) usage();
134433d6423SLionel Sambuc
135433d6423SLionel Sambuc i = 1;
13625223c2aSJacob Adams while ((i < argc) && (argv[i][0] == '-'))
137433d6423SLionel Sambuc {
138433d6423SLionel Sambuc if (strncmp(argv[i], "-b", 2) == 0)
139433d6423SLionel Sambuc bits = atoi(argv[i] + 2);
140433d6423SLionel Sambuc else if (strncmp(argv[i], "-s", 2) == 0)
141433d6423SLionel Sambuc stereo = atoi(argv[i] + 2);
142433d6423SLionel Sambuc else if (strncmp(argv[i], "-r", 2) == 0)
143433d6423SLionel Sambuc rate = (unsigned int) atol(argv[i] + 2);
144433d6423SLionel Sambuc else usage();
145433d6423SLionel Sambuc i++;
146433d6423SLionel Sambuc }
147433d6423SLionel Sambuc if (i == argc) usage();
148433d6423SLionel Sambuc
149433d6423SLionel Sambuc file_name = argv[i];
150433d6423SLionel Sambuc
151433d6423SLionel Sambuc /* Some sanity checks */
152433d6423SLionel Sambuc if ((bits != 8 && bits != 16) ||
153433d6423SLionel Sambuc (rate < 4000 || rate > 44100) ||
154433d6423SLionel Sambuc (stereo != 0 && stereo != 1))
155433d6423SLionel Sambuc {
156433d6423SLionel Sambuc fprintf(stderr, "Invalid parameters\n");
157433d6423SLionel Sambuc exit(-1);
158433d6423SLionel Sambuc }
159433d6423SLionel Sambuc
160433d6423SLionel Sambuc /* Open DSP */
161433d6423SLionel Sambuc if ((audio = open("/dev/rec", O_RDWR)) < 0)
162433d6423SLionel Sambuc {
163433d6423SLionel Sambuc fprintf(stderr, "Cannot open /dev/rec\n");
164433d6423SLionel Sambuc exit(-1);
165433d6423SLionel Sambuc }
166433d6423SLionel Sambuc
167433d6423SLionel Sambuc /* Get maximum fragment size and try to allocate a buffer */
168433d6423SLionel Sambuc ioctl(audio, DSPIOMAX, &fragment_size);
169433d6423SLionel Sambuc if ((buffer = malloc(fragment_size)) == (char *) 0)
170433d6423SLionel Sambuc {
171433d6423SLionel Sambuc fprintf(stderr, "Cannot allocate buffer\n");
172433d6423SLionel Sambuc exit(-1);
173433d6423SLionel Sambuc }
174433d6423SLionel Sambuc
175433d6423SLionel Sambuc /* Set sample parameters */
176433d6423SLionel Sambuc ioctl(audio, DSPIOSIZE, &fragment_size);
177433d6423SLionel Sambuc ioctl(audio, DSPIOSTEREO, &stereo);
178433d6423SLionel Sambuc ioctl(audio, DSPIORATE, &rate);
179433d6423SLionel Sambuc ioctl(audio, DSPIOBITS, &bits);
180433d6423SLionel Sambuc sign = (bits == 16 ? 1 : 0);
181433d6423SLionel Sambuc ioctl(audio, DSPIOSIGN, &sign);
182433d6423SLionel Sambuc
183433d6423SLionel Sambuc /* Create sample file */
184433d6423SLionel Sambuc if ((file = creat(file_name, 511)) < 0)
185433d6423SLionel Sambuc {
186433d6423SLionel Sambuc fprintf(stderr, "Cannot create %s\n", argv[1]);
187433d6423SLionel Sambuc exit(-1);
188433d6423SLionel Sambuc }
189433d6423SLionel Sambuc /* Skip wave header */
190433d6423SLionel Sambuc lseek(file, (long)(sizeof(r_fields) +
191433d6423SLionel Sambuc sizeof(c_fields) +
192433d6423SLionel Sambuc sizeof(s_fields) +
193433d6423SLionel Sambuc sizeof(data_id) +
194433d6423SLionel Sambuc sizeof(data_len)), SEEK_SET);
195433d6423SLionel Sambuc
196433d6423SLionel Sambuc printf("\nBits per sample : %u\n", bits);
197433d6423SLionel Sambuc printf("Stereo : %s\n", (stereo == 1 ? "yes" : "no"));
198433d6423SLionel Sambuc printf("Samples per second: %u\n", rate);
199433d6423SLionel Sambuc
200433d6423SLionel Sambuc /* Set terminal parameters and remember the old ones */
201433d6423SLionel Sambuc tcgetattr(0, &old_tty);
202433d6423SLionel Sambuc new_tty = old_tty;
203433d6423SLionel Sambuc new_tty.c_lflag &= ~(ICANON|ECHO);
204433d6423SLionel Sambuc old_stdin = fcntl(0, F_GETFL);
205433d6423SLionel Sambuc
206433d6423SLionel Sambuc /* Catch break signal to be able to restore terminal parameters in case
207433d6423SLionel Sambuc * of a user interrupt
208433d6423SLionel Sambuc */
209433d6423SLionel Sambuc signal(SIGINT, terminate);
210433d6423SLionel Sambuc
211433d6423SLionel Sambuc /* Go to non-blocking mode */
212433d6423SLionel Sambuc tcsetattr(0, TCSANOW, &new_tty);
213433d6423SLionel Sambuc (void) fcntl(0, F_SETFL, old_stdin | O_NONBLOCK);
214433d6423SLionel Sambuc
215433d6423SLionel Sambuc printf("\nPress spacebar to start sampling...\n");
216433d6423SLionel Sambuc while(!(read(0, &c, 1) == 1 && c == ' '));
217433d6423SLionel Sambuc
218433d6423SLionel Sambuc printf("Sampling, press spacebar to stop...\n");
219433d6423SLionel Sambuc while(!(read(0, &c, 1) == 1 && c == ' '))
220433d6423SLionel Sambuc {
221433d6423SLionel Sambuc /* Read sample fragment and write to sample file */
222433d6423SLionel Sambuc read(audio, buffer, fragment_size);
223433d6423SLionel Sambuc write(file, buffer, fragment_size);
224433d6423SLionel Sambuc data_len+= fragment_size;
225433d6423SLionel Sambuc }
226433d6423SLionel Sambuc printf("%ld bytes sampled. \n\n", data_len);
227433d6423SLionel Sambuc
228433d6423SLionel Sambuc /* Construct the wave header in front of the raw sample data */
229433d6423SLionel Sambuc write_wave_header();
230433d6423SLionel Sambuc
231433d6423SLionel Sambuc /* Restore terminal parameters and exit */
232433d6423SLionel Sambuc terminate(1);
233433d6423SLionel Sambuc }
234