1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
* OpenAL Recording Example
*
* Copyright (c) 2017 by Chris Robinson <chris.kcat@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* This file contains a relatively simple recorder. */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "AL/al.h"
#include "AL/alc.h"
#include "AL/alext.h"
#include "common/alhelpers.h"
#if defined(_WIN64)
#define SZFMT "%I64u"
#elif defined(_WIN32)
#define SZFMT "%u"
#else
#define SZFMT "%zu"
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
static float msvc_strtof(const char *str, char **end)
{ return (float)strtod(str, end); }
#define strtof msvc_strtof
#endif
static void fwrite16le(ALushort val, FILE *f)
{
ALubyte data[2] = { (ALubyte)(val&0xff), (ALubyte)((val>>8)&0xff) };
fwrite(data, 1, 2, f);
}
static void fwrite32le(ALuint val, FILE *f)
{
ALubyte data[4] = { (ALubyte)(val&0xff), (ALubyte)((val>>8)&0xff), (ALubyte)((val>>16)&0xff),
(ALubyte)((val>>24)&0xff) };
fwrite(data, 1, 4, f);
}
typedef struct Recorder {
ALCdevice *mDevice;
FILE *mFile;
long mDataSizeOffset;
ALuint mDataSize;
float mRecTime;
ALuint mChannels;
ALuint mBits;
ALuint mSampleRate;
ALuint mFrameSize;
ALbyte *mBuffer;
ALsizei mBufferSize;
} Recorder;
int main(int argc, char **argv)
{
static const char optlist[] =
" --channels/-c <channels> Set channel count (1 or 2)\n"
" --bits/-b <bits> Set channel count (8, 16, or 32)\n"
" --rate/-r <rate> Set sample rate (8000 to 96000)\n"
" --time/-t <time> Time in seconds to record (1 to 10)\n"
" --outfile/-o <filename> Output filename (default: record.wav)";
const char *fname = "record.wav";
const char *devname = NULL;
const char *progname;
Recorder recorder;
long total_size;
ALenum format;
ALCenum err;
progname = argv[0];
if(argc < 2)
{
fprintf(stderr, "Record from a device to a wav file.\n\n"
"Usage: %s [-device <name>] [options...]\n\n"
"Available options:\n%s\n", progname, optlist);
return 0;
}
recorder.mDevice = NULL;
recorder.mFile = NULL;
recorder.mDataSizeOffset = 0;
recorder.mDataSize = 0;
recorder.mRecTime = 4.0f;
recorder.mChannels = 1;
recorder.mBits = 16;
recorder.mSampleRate = 44100;
recorder.mFrameSize = recorder.mChannels * recorder.mBits / 8;
recorder.mBuffer = NULL;
recorder.mBufferSize = 0;
argv++; argc--;
if(argc > 1 && strcmp(argv[0], "-device") == 0)
{
devname = argv[1];
argv += 2;
argc -= 2;
}
while(argc > 0)
{
char *end;
if(strcmp(argv[0], "--") == 0)
break;
else if(strcmp(argv[0], "--channels") == 0 || strcmp(argv[0], "-c") == 0)
{
if(argc < 2)
{
fprintf(stderr, "Missing argument for option: %s\n", argv[0]);
return 1;
}
recorder.mChannels = (ALuint)strtoul(argv[1], &end, 0);
if((recorder.mChannels != 1 && recorder.mChannels != 2) || (end && *end != '\0'))
{
fprintf(stderr, "Invalid channels: %s\n", argv[1]);
return 1;
}
argv += 2;
argc -= 2;
}
else if(strcmp(argv[0], "--bits") == 0 || strcmp(argv[0], "-b") == 0)
{
if(argc < 2)
{
fprintf(stderr, "Missing argument for option: %s\n", argv[0]);
return 1;
}
recorder.mBits = (ALuint)strtoul(argv[1], &end, 0);
if((recorder.mBits != 8 && recorder.mBits != 16 && recorder.mBits != 32) ||
(end && *end != '\0'))
{
fprintf(stderr, "Invalid bit count: %s\n", argv[1]);
return 1;
}
argv += 2;
argc -= 2;
}
else if(strcmp(argv[0], "--rate") == 0 || strcmp(argv[0], "-r") == 0)
{
if(argc < 2)
{
fprintf(stderr, "Missing argument for option: %s\n", argv[0]);
return 1;
}
recorder.mSampleRate = (ALuint)strtoul(argv[1], &end, 0);
if(!(recorder.mSampleRate >= 8000 && recorder.mSampleRate <= 96000) || (end && *end != '\0'))
{
fprintf(stderr, "Invalid sample rate: %s\n", argv[1]);
return 1;
}
argv += 2;
argc -= 2;
}
else if(strcmp(argv[0], "--time") == 0 || strcmp(argv[0], "-t") == 0)
{
if(argc < 2)
{
fprintf(stderr, "Missing argument for option: %s\n", argv[0]);
return 1;
}
recorder.mRecTime = strtof(argv[1], &end);
if(!(recorder.mRecTime >= 1.0f && recorder.mRecTime <= 10.0f) || (end && *end != '\0'))
{
fprintf(stderr, "Invalid record time: %s\n", argv[1]);
return 1;
}
argv += 2;
argc -= 2;
}
else if(strcmp(argv[0], "--outfile") == 0 || strcmp(argv[0], "-o") == 0)
{
if(argc < 2)
{
fprintf(stderr, "Missing argument for option: %s\n", argv[0]);
return 1;
}
fname = argv[1];
argv += 2;
argc -= 2;
}
else if(strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "-h") == 0)
{
fprintf(stderr, "Record from a device to a wav file.\n\n"
"Usage: %s [-device <name>] [options...]\n\n"
"Available options:\n%s\n", progname, optlist);
return 0;
}
else
{
fprintf(stderr, "Invalid option '%s'.\n\n"
"Usage: %s [-device <name>] [options...]\n\n"
"Available options:\n%s\n", argv[0], progname, optlist);
return 0;
}
}
recorder.mFrameSize = recorder.mChannels * recorder.mBits / 8;
format = AL_NONE;
if(recorder.mChannels == 1)
{
if(recorder.mBits == 8)
format = AL_FORMAT_MONO8;
else if(recorder.mBits == 16)
format = AL_FORMAT_MONO16;
else if(recorder.mBits == 32)
format = AL_FORMAT_MONO_FLOAT32;
}
else if(recorder.mChannels == 2)
{
if(recorder.mBits == 8)
format = AL_FORMAT_STEREO8;
else if(recorder.mBits == 16)
format = AL_FORMAT_STEREO16;
else if(recorder.mBits == 32)
format = AL_FORMAT_STEREO_FLOAT32;
}
recorder.mDevice = alcCaptureOpenDevice(devname, recorder.mSampleRate, format, 32768);
if(!recorder.mDevice)
{
fprintf(stderr, "Failed to open %s, %s %d-bit, %s, %dhz (%d samples)\n",
devname ? devname : "default device",
(recorder.mBits == 32) ? "Float" :
(recorder.mBits != 8) ? "Signed" : "Unsigned", recorder.mBits,
(recorder.mChannels == 1) ? "Mono" : "Stereo", recorder.mSampleRate,
32768
);
return 1;
}
fprintf(stderr, "Opened \"%s\"\n", alcGetString(
recorder.mDevice, ALC_CAPTURE_DEVICE_SPECIFIER
));
recorder.mFile = fopen(fname, "wb");
if(!recorder.mFile)
{
fprintf(stderr, "Failed to open '%s' for writing\n", fname);
alcCaptureCloseDevice(recorder.mDevice);
return 1;
}
fputs("RIFF", recorder.mFile);
fwrite32le(0xFFFFFFFF, recorder.mFile); // 'RIFF' header len; filled in at close
fputs("WAVE", recorder.mFile);
fputs("fmt ", recorder.mFile);
fwrite32le(18, recorder.mFile); // 'fmt ' header len
// 16-bit val, format type id (1 = integer PCM, 3 = float PCM)
fwrite16le((recorder.mBits == 32) ? 0x0003 : 0x0001, recorder.mFile);
// 16-bit val, channel count
fwrite16le((ALushort)recorder.mChannels, recorder.mFile);
// 32-bit val, frequency
fwrite32le(recorder.mSampleRate, recorder.mFile);
// 32-bit val, bytes per second
fwrite32le(recorder.mSampleRate * recorder.mFrameSize, recorder.mFile);
// 16-bit val, frame size
fwrite16le((ALushort)recorder.mFrameSize, recorder.mFile);
// 16-bit val, bits per sample
fwrite16le((ALushort)recorder.mBits, recorder.mFile);
// 16-bit val, extra byte count
fwrite16le(0, recorder.mFile);
fputs("data", recorder.mFile);
fwrite32le(0xFFFFFFFF, recorder.mFile); // 'data' header len; filled in at close
recorder.mDataSizeOffset = ftell(recorder.mFile) - 4;
if(ferror(recorder.mFile) || recorder.mDataSizeOffset < 0)
{
fprintf(stderr, "Error writing header: %s\n", strerror(errno));
fclose(recorder.mFile);
alcCaptureCloseDevice(recorder.mDevice);
return 1;
}
fprintf(stderr, "Recording '%s', %s %d-bit, %s, %dhz (%g second%s)\n", fname,
(recorder.mBits == 32) ? "Float" :
(recorder.mBits != 8) ? "Signed" : "Unsigned", recorder.mBits,
(recorder.mChannels == 1) ? "Mono" : "Stereo", recorder.mSampleRate,
recorder.mRecTime, (recorder.mRecTime != 1.0f) ? "s" : ""
);
err = ALC_NO_ERROR;
alcCaptureStart(recorder.mDevice);
while((double)recorder.mDataSize/(double)recorder.mSampleRate < recorder.mRecTime &&
(err=alcGetError(recorder.mDevice)) == ALC_NO_ERROR && !ferror(recorder.mFile))
{
ALCint count = 0;
fprintf(stderr, "\rCaptured %u samples", recorder.mDataSize);
alcGetIntegerv(recorder.mDevice, ALC_CAPTURE_SAMPLES, 1, &count);
if(count < 1)
{
al_nssleep(10000000);
continue;
}
if(count > recorder.mBufferSize)
{
ALbyte *data = calloc(recorder.mFrameSize, (ALuint)count);
free(recorder.mBuffer);
recorder.mBuffer = data;
recorder.mBufferSize = count;
}
alcCaptureSamples(recorder.mDevice, recorder.mBuffer, count);
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
/* Byteswap multibyte samples on big-endian systems (wav needs little-
* endian, and OpenAL gives the system's native-endian).
*/
if(recorder.mBits == 16)
{
ALCint i;
for(i = 0;i < count*recorder.mChannels;i++)
{
ALbyte b = recorder.mBuffer[i*2 + 0];
recorder.mBuffer[i*2 + 0] = recorder.mBuffer[i*2 + 1];
recorder.mBuffer[i*2 + 1] = b;
}
}
else if(recorder.mBits == 32)
{
ALCint i;
for(i = 0;i < count*recorder.mChannels;i++)
{
ALbyte b0 = recorder.mBuffer[i*4 + 0];
ALbyte b1 = recorder.mBuffer[i*4 + 1];
recorder.mBuffer[i*4 + 0] = recorder.mBuffer[i*4 + 3];
recorder.mBuffer[i*4 + 1] = recorder.mBuffer[i*4 + 2];
recorder.mBuffer[i*4 + 2] = b1;
recorder.mBuffer[i*4 + 3] = b0;
}
}
#endif
recorder.mDataSize += (ALuint)fwrite(recorder.mBuffer, recorder.mFrameSize, (ALuint)count,
recorder.mFile);
}
alcCaptureStop(recorder.mDevice);
fprintf(stderr, "\rCaptured %u samples\n", recorder.mDataSize);
if(err != ALC_NO_ERROR)
fprintf(stderr, "Got device error 0x%04x: %s\n", err, alcGetString(recorder.mDevice, err));
alcCaptureCloseDevice(recorder.mDevice);
recorder.mDevice = NULL;
free(recorder.mBuffer);
recorder.mBuffer = NULL;
recorder.mBufferSize = 0;
total_size = ftell(recorder.mFile);
if(fseek(recorder.mFile, recorder.mDataSizeOffset, SEEK_SET) == 0)
{
fwrite32le(recorder.mDataSize*recorder.mFrameSize, recorder.mFile);
if(fseek(recorder.mFile, 4, SEEK_SET) == 0)
fwrite32le((ALuint)total_size - 8, recorder.mFile);
}
fclose(recorder.mFile);
recorder.mFile = NULL;
return 0;
}
|