4 * Record sound files in wave format. Only MicroSoft PCM is supported.
10 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <minix/sound.h>
22 int main(int argc
, char **argv
);
24 void write_wave_header(void);
25 void terminate(int s
);
28 /******* Wave format definitions *********/
30 #define RIFF_ID 0x46464952
31 #define WAVE_ID1 0x45564157
32 #define WAVE_ID2 0x20746D66
33 #define DATA_ID 0x61746164
34 #define MS_PCM_FORMAT 0x0001
37 #define DWORD unsigned long
57 struct specific_fields
65 /******** End of wave format definitions *********/
67 /* Default recording values */
68 unsigned int sign
= 0;
69 unsigned int bits
= 8;
70 unsigned int stereo
= 0;
71 unsigned int rate
= 22050;
74 struct termios old_tty
, new_tty
;
79 fprintf(stderr
, "Usage: recwav [-b -s -r] file_name\n");
85 /* Restore terminal parameters */
86 tcsetattr(0, TCSANOW
, &old_tty
);
87 (void) fcntl(0,F_SETFL
,old_stdin
);
93 void write_wave_header()
96 r_fields
.RIFF_id
= RIFF_ID
;
97 r_fields
.WAVE_id1
= WAVE_ID1
;
98 r_fields
.WAVE_id2
= WAVE_ID2
;
99 r_fields
.data_ptr
= 16;
100 r_fields
.RIFF_len
= 20 + r_fields
.data_ptr
+ data_len
;
102 /* MicroSoft PCM specific fields */
103 s_fields
.BitsPerSample
= bits
;
106 c_fields
.FormatTag
= MS_PCM_FORMAT
;
107 c_fields
.Channels
= stereo
+ 1;
108 c_fields
.SamplesPerSec
= rate
;
109 c_fields
.AvgBytesPerSec
= c_fields
.Channels
* rate
* (bits
/ 8);
110 c_fields
.BlockAlign
= c_fields
.Channels
* (bits
/ 8);
115 /* Write wave-file header */
116 lseek(file
, 0L, SEEK_SET
);
117 write(file
, &r_fields
, 20);
118 write(file
, &c_fields
, 14);
119 write(file
, &s_fields
, 2);
120 write(file
, &data_id
, sizeof(data_id
));
121 write(file
, &data_len
, sizeof(data_len
));
125 int main(int argc
, char* argv
[])
127 unsigned int fragment_size
;
128 char *buffer
, *file_name
;
132 /* Read parameters */
133 if (argc
< 2) usage();
136 while ((i
< argc
) && (argv
[i
][0] == '-'))
138 if (strncmp(argv
[i
], "-b", 2) == 0)
139 bits
= atoi(argv
[i
] + 2);
140 else if (strncmp(argv
[i
], "-s", 2) == 0)
141 stereo
= atoi(argv
[i
] + 2);
142 else if (strncmp(argv
[i
], "-r", 2) == 0)
143 rate
= (unsigned int) atol(argv
[i
] + 2);
147 if (i
== argc
) usage();
151 /* Some sanity checks */
152 if ((bits
!= 8 && bits
!= 16) ||
153 (rate
< 4000 || rate
> 44100) ||
154 (stereo
!= 0 && stereo
!= 1))
156 fprintf(stderr
, "Invalid parameters\n");
161 if ((audio
= open("/dev/rec", O_RDWR
)) < 0)
163 fprintf(stderr
, "Cannot open /dev/rec\n");
167 /* Get maximum fragment size and try to allocate a buffer */
168 ioctl(audio
, DSPIOMAX
, &fragment_size
);
169 if ((buffer
= malloc(fragment_size
)) == (char *) 0)
171 fprintf(stderr
, "Cannot allocate buffer\n");
175 /* Set sample parameters */
176 ioctl(audio
, DSPIOSIZE
, &fragment_size
);
177 ioctl(audio
, DSPIOSTEREO
, &stereo
);
178 ioctl(audio
, DSPIORATE
, &rate
);
179 ioctl(audio
, DSPIOBITS
, &bits
);
180 sign
= (bits
== 16 ? 1 : 0);
181 ioctl(audio
, DSPIOSIGN
, &sign
);
183 /* Create sample file */
184 if ((file
= creat(file_name
, 511)) < 0)
186 fprintf(stderr
, "Cannot create %s\n", argv
[1]);
189 /* Skip wave header */
190 lseek(file
, (long)(sizeof(r_fields
) +
194 sizeof(data_len
)), SEEK_SET
);
196 printf("\nBits per sample : %u\n", bits
);
197 printf("Stereo : %s\n", (stereo
== 1 ? "yes" : "no"));
198 printf("Samples per second: %u\n", rate
);
200 /* Set terminal parameters and remember the old ones */
201 tcgetattr(0, &old_tty
);
203 new_tty
.c_lflag
&= ~(ICANON
|ECHO
);
204 old_stdin
= fcntl(0, F_GETFL
);
206 /* Catch break signal to be able to restore terminal parameters in case
207 * of a user interrupt
209 signal(SIGINT
, terminate
);
211 /* Go to non-blocking mode */
212 tcsetattr(0, TCSANOW
, &new_tty
);
213 (void) fcntl(0, F_SETFL
, old_stdin
| O_NONBLOCK
);
215 printf("\nPress spacebar to start sampling...\n");
216 while(!(read(0, &c
, 1) == 1 && c
== ' '));
218 printf("Sampling, press spacebar to stop...\n");
219 while(!(read(0, &c
, 1) == 1 && c
== ' '))
221 /* Read sample fragment and write to sample file */
222 read(audio
, buffer
, fragment_size
);
223 write(file
, buffer
, fragment_size
);
224 data_len
+= fragment_size
;
226 printf("%ld bytes sampled. \n\n", data_len
);
228 /* Construct the wave header in front of the raw sample data */
231 /* Restore terminal parameters and exit */