source: trunk/minix/commands/ibm/recwave.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 5.3 KB
Line 
1/*
2 * recwave.c
3 *
4 * Record sound files in wave format. Only MicroSoft PCM is supported.
5 *
6 * Michel R. Prevenier.
7 */
8
9#include <errno.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <termios.h>
13#include <stdlib.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <string.h>
18#include <signal.h>
19#include <sys/ioctl.h>
20#include <minix/sound.h>
21
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));
26
27
28/******* Wave format definitions *********/
29
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
35
36#define WORD short
37#define DWORD unsigned long
38
39struct RIFF_fields
40{
41 DWORD RIFF_id;
42 DWORD RIFF_len;
43 DWORD WAVE_id1;
44 DWORD WAVE_id2;
45 DWORD data_ptr;
46} r_fields;
47
48struct common_fields
49{
50 WORD FormatTag;
51 WORD Channels;
52 DWORD SamplesPerSec;
53 DWORD AvgBytesPerSec;
54 WORD BlockAlign;
55} c_fields;
56
57struct specific_fields
58{
59 WORD BitsPerSample;
60} s_fields;
61
62DWORD data_id;
63DWORD data_len;
64
65/******** End of wave format definitions *********/
66
67/* Default recording values */
68unsigned int sign = 0;
69unsigned int bits = 8;
70unsigned int stereo = 0;
71unsigned int rate = 22050;
72unsigned int time = 10;
73
74int old_stdin;
75struct termios old_tty, new_tty;
76int audio, file;
77
78void usage()
79{
80 fprintf(stderr, "Usage: recwav [-b -s -r] file_name\n");
81 exit(-1);
82}
83
84void terminate(s)
85int s;
86{
87 /* Restore terminal parameters */
88 tcsetattr(0, TCSANOW, &old_tty);
89 (void) fcntl(0,F_SETFL,old_stdin);
90 close(audio);
91 close(file);
92 exit(0);
93}
94
95void write_wave_header()
96{
97 /* RIFF fields */
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;
103
104 /* MicroSoft PCM specific fields */
105 s_fields.BitsPerSample = bits;
106
107 /* Common fields */
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);
113
114 /* Data chunk */
115 data_id = DATA_ID;
116
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));
124}
125
126
127void main(argc, argv)
128int argc;
129char **argv;
130{
131 unsigned int fragment_size;
132 char *buffer, *file_name;
133 char c;
134 int i;
135
136 /* Read parameters */
137 if (argc < 2) usage();
138
139 i = 1;
140 while (argv[i][0] == '-' && i < argc)
141 {
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);
148 else usage();
149 i++;
150 }
151 if (i == argc) usage();
152
153 file_name = argv[i];
154
155 /* Some sanity checks */
156 if ((bits != 8 && bits != 16) ||
157 (rate < 4000 || rate > 44100) ||
158 (stereo != 0 && stereo != 1))
159 {
160 fprintf(stderr, "Invalid parameters\n");
161 exit(-1);
162 }
163
164 /* Open DSP */
165 if ((audio = open("/dev/rec", O_RDWR)) < 0)
166 {
167 fprintf(stderr, "Cannot open /dev/rec\n");
168 exit(-1);
169 }
170
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)
174 {
175 fprintf(stderr, "Cannot allocate buffer\n");
176 exit(-1);
177 }
178
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);
186
187 /* Create sample file */
188 if ((file = creat(file_name, 511)) < 0)
189 {
190 fprintf(stderr, "Cannot create %s\n", argv[1]);
191 exit(-1);
192 }
193 /* Skip wave header */
194 lseek(file, (long)(sizeof(r_fields) +
195 sizeof(c_fields) +
196 sizeof(s_fields) +
197 sizeof(data_id) +
198 sizeof(data_len)), SEEK_SET);
199
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);
203
204 /* Set terminal parameters and remember the old ones */
205 tcgetattr(0, &old_tty);
206 new_tty = old_tty;
207 new_tty.c_lflag &= ~(ICANON|ECHO);
208 old_stdin = fcntl(0, F_GETFL);
209
210 /* Catch break signal to be able to restore terminal parameters in case
211 * of a user interrupt
212 */
213 signal(SIGINT, terminate);
214
215 /* Go to non-blocking mode */
216 tcsetattr(0, TCSANOW, &new_tty);
217 (void) fcntl(0, F_SETFL, old_stdin | O_NONBLOCK);
218
219 printf("\nPress spacebar to start sampling...\n");
220 while(!(read(0, &c, 1) == 1 && c == ' '));
221
222 printf("Sampling, press spacebar to stop...\n");
223 while(!(read(0, &c, 1) == 1 && c == ' '))
224 {
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;
229 }
230 printf("%ld bytes sampled. \n\n", data_len);
231
232 /* Construct the wave header in front of the raw sample data */
233 write_wave_header();
234
235 /* Restore terminal parameters and exit */
236 terminate(1);
237}
Note: See TracBrowser for help on using the repository browser.