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");
86 /* Restore terminal parameters */
87 tcsetattr(0, TCSANOW
, &old_tty
);
88 (void) fcntl(0,F_SETFL
,old_stdin
);
94 void write_wave_header()
97 r_fields
.RIFF_id
= RIFF_ID
;
98 r_fields
.WAVE_id1
= WAVE_ID1
;
99 r_fields
.WAVE_id2
= WAVE_ID2
;
100 r_fields
.data_ptr
= 16;
101 r_fields
.RIFF_len
= 20 + r_fields
.data_ptr
+ data_len
;
103 /* MicroSoft PCM specific fields */
104 s_fields
.BitsPerSample
= bits
;
107 c_fields
.FormatTag
= MS_PCM_FORMAT
;
108 c_fields
.Channels
= stereo
+ 1;
109 c_fields
.SamplesPerSec
= rate
;
110 c_fields
.AvgBytesPerSec
= c_fields
.Channels
* rate
* (bits
/ 8);
111 c_fields
.BlockAlign
= c_fields
.Channels
* (bits
/ 8);
116 /* Write wave-file header */
117 lseek(file
, 0L, SEEK_SET
);
118 write(file
, &r_fields
, 20);
119 write(file
, &c_fields
, 14);
120 write(file
, &s_fields
, 2);
121 write(file
, &data_id
, sizeof(data_id
));
122 write(file
, &data_len
, sizeof(data_len
));
130 unsigned int fragment_size
;
131 char *buffer
, *file_name
;
135 /* Read parameters */
136 if (argc
< 2) usage();
139 while (argv
[i
][0] == '-' && i
< argc
)
141 if (strncmp(argv
[i
], "-b", 2) == 0)
142 bits
= atoi(argv
[i
] + 2);
143 else if (strncmp(argv
[i
], "-s", 2) == 0)
144 stereo
= atoi(argv
[i
] + 2);
145 else if (strncmp(argv
[i
], "-r", 2) == 0)
146 rate
= (unsigned int) atol(argv
[i
] + 2);
150 if (i
== argc
) usage();
154 /* Some sanity checks */
155 if ((bits
!= 8 && bits
!= 16) ||
156 (rate
< 4000 || rate
> 44100) ||
157 (stereo
!= 0 && stereo
!= 1))
159 fprintf(stderr
, "Invalid parameters\n");
164 if ((audio
= open("/dev/rec", O_RDWR
)) < 0)
166 fprintf(stderr
, "Cannot open /dev/rec\n");
170 /* Get maximum fragment size and try to allocate a buffer */
171 ioctl(audio
, DSPIOMAX
, &fragment_size
);
172 if ((buffer
= malloc(fragment_size
)) == (char *) 0)
174 fprintf(stderr
, "Cannot allocate buffer\n");
178 /* Set sample parameters */
179 ioctl(audio
, DSPIOSIZE
, &fragment_size
);
180 ioctl(audio
, DSPIOSTEREO
, &stereo
);
181 ioctl(audio
, DSPIORATE
, &rate
);
182 ioctl(audio
, DSPIOBITS
, &bits
);
183 sign
= (bits
== 16 ? 1 : 0);
184 ioctl(audio
, DSPIOSIGN
, &sign
);
186 /* Create sample file */
187 if ((file
= creat(file_name
, 511)) < 0)
189 fprintf(stderr
, "Cannot create %s\n", argv
[1]);
192 /* Skip wave header */
193 lseek(file
, (long)(sizeof(r_fields
) +
197 sizeof(data_len
)), SEEK_SET
);
199 printf("\nBits per sample : %u\n", bits
);
200 printf("Stereo : %s\n", (stereo
== 1 ? "yes" : "no"));
201 printf("Samples per second: %u\n", rate
);
203 /* Set terminal parameters and remember the old ones */
204 tcgetattr(0, &old_tty
);
206 new_tty
.c_lflag
&= ~(ICANON
|ECHO
);
207 old_stdin
= fcntl(0, F_GETFL
);
209 /* Catch break signal to be able to restore terminal parameters in case
210 * of a user interrupt
212 signal(SIGINT
, terminate
);
214 /* Go to non-blocking mode */
215 tcsetattr(0, TCSANOW
, &new_tty
);
216 (void) fcntl(0, F_SETFL
, old_stdin
| O_NONBLOCK
);
218 printf("\nPress spacebar to start sampling...\n");
219 while(!(read(0, &c
, 1) == 1 && c
== ' '));
221 printf("Sampling, press spacebar to stop...\n");
222 while(!(read(0, &c
, 1) == 1 && c
== ' '))
224 /* Read sample fragment and write to sample file */
225 read(audio
, buffer
, fragment_size
);
226 write(file
, buffer
, fragment_size
);
227 data_len
+= fragment_size
;
229 printf("%ld bytes sampled. \n\n", data_len
);
231 /* Construct the wave header in front of the raw sample data */
234 /* Restore terminal parameters and exit */