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 _PROTOTYPE (void main
, (int argc
, char **argv
));
23 _PROTOTYPE (void usage
, (void));
24 _PROTOTYPE ( void write_wave_header
, (void));
25 _PROTOTYPE ( 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;
72 unsigned int time
= 10;
75 struct termios old_tty
, new_tty
;
80 fprintf(stderr
, "Usage: recwav [-b -s -r] file_name\n");
87 /* Restore terminal parameters */
88 tcsetattr(0, TCSANOW
, &old_tty
);
89 (void) fcntl(0,F_SETFL
,old_stdin
);
95 void write_wave_header()
98 r_fields
.RIFF_id
= RIFF_ID
;
99 r_fields
.WAVE_id1
= WAVE_ID1
;
100 r_fields
.WAVE_id2
= WAVE_ID2
;
101 r_fields
.data_ptr
= 16;
102 r_fields
.RIFF_len
= 20 + r_fields
.data_ptr
+ data_len
;
104 /* MicroSoft PCM specific fields */
105 s_fields
.BitsPerSample
= bits
;
108 c_fields
.FormatTag
= MS_PCM_FORMAT
;
109 c_fields
.Channels
= stereo
+ 1;
110 c_fields
.SamplesPerSec
= rate
;
111 c_fields
.AvgBytesPerSec
= c_fields
.Channels
* rate
* (bits
/ 8);
112 c_fields
.BlockAlign
= c_fields
.Channels
* (bits
/ 8);
117 /* Write wave-file header */
118 lseek(file
, 0L, SEEK_SET
);
119 write(file
, &r_fields
, 20);
120 write(file
, &c_fields
, 14);
121 write(file
, &s_fields
, 2);
122 write(file
, &data_id
, sizeof(data_id
));
123 write(file
, &data_len
, sizeof(data_len
));
127 void main(argc
, argv
)
131 unsigned int fragment_size
;
132 char *buffer
, *file_name
;
136 /* Read parameters */
137 if (argc
< 2) usage();
140 while (argv
[i
][0] == '-' && i
< argc
)
142 if (strncmp(argv
[i
], "-b", 2) == 0)
143 bits
= atoi(argv
[i
] + 2);
144 else if (strncmp(argv
[i
], "-s", 2) == 0)
145 stereo
= atoi(argv
[i
] + 2);
146 else if (strncmp(argv
[i
], "-r", 2) == 0)
147 rate
= (unsigned int) atol(argv
[i
] + 2);
151 if (i
== argc
) usage();
155 /* Some sanity checks */
156 if ((bits
!= 8 && bits
!= 16) ||
157 (rate
< 4000 || rate
> 44100) ||
158 (stereo
!= 0 && stereo
!= 1))
160 fprintf(stderr
, "Invalid parameters\n");
165 if ((audio
= open("/dev/rec", O_RDWR
)) < 0)
167 fprintf(stderr
, "Cannot open /dev/rec\n");
171 /* Get maximum fragment size and try to allocate a buffer */
172 ioctl(audio
, DSPIOMAX
, &fragment_size
);
173 if ((buffer
= malloc(fragment_size
)) == (char *) 0)
175 fprintf(stderr
, "Cannot allocate buffer\n");
179 /* Set sample parameters */
180 ioctl(audio
, DSPIOSIZE
, &fragment_size
);
181 ioctl(audio
, DSPIOSTEREO
, &stereo
);
182 ioctl(audio
, DSPIORATE
, &rate
);
183 ioctl(audio
, DSPIOBITS
, &bits
);
184 sign
= (bits
== 16 ? 1 : 0);
185 ioctl(audio
, DSPIOSIGN
, &sign
);
187 /* Create sample file */
188 if ((file
= creat(file_name
, 511)) < 0)
190 fprintf(stderr
, "Cannot create %s\n", argv
[1]);
193 /* Skip wave header */
194 lseek(file
, (long)(sizeof(r_fields
) +
198 sizeof(data_len
)), SEEK_SET
);
200 printf("\nBits per sample : %u\n", bits
);
201 printf("Stereo : %s\n", (stereo
== 1 ? "yes" : "no"));
202 printf("Samples per second: %u\n", rate
);
204 /* Set terminal parameters and remember the old ones */
205 tcgetattr(0, &old_tty
);
207 new_tty
.c_lflag
&= ~(ICANON
|ECHO
);
208 old_stdin
= fcntl(0, F_GETFL
);
210 /* Catch break signal to be able to restore terminal parameters in case
211 * of a user interrupt
213 signal(SIGINT
, terminate
);
215 /* Go to non-blocking mode */
216 tcsetattr(0, TCSANOW
, &new_tty
);
217 (void) fcntl(0, F_SETFL
, old_stdin
| O_NONBLOCK
);
219 printf("\nPress spacebar to start sampling...\n");
220 while(!(read(0, &c
, 1) == 1 && c
== ' '));
222 printf("Sampling, press spacebar to stop...\n");
223 while(!(read(0, &c
, 1) == 1 && c
== ' '))
225 /* Read sample fragment and write to sample file */
226 read(audio
, buffer
, fragment_size
);
227 write(file
, buffer
, fragment_size
);
228 data_len
+= fragment_size
;
230 printf("%ld bytes sampled. \n\n", data_len
);
232 /* Construct the wave header in front of the raw sample data */
235 /* Restore terminal parameters and exit */