2 * OpenAL Recording Example
4 * Copyright (c) 2017 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains a relatively simple recorder. */
36 #include "common/alhelpers.h"
48 #if defined(_MSC_VER) && (_MSC_VER < 1900)
49 static float msvc_strtof(const char *str
, char **end
)
50 { return (float)strtod(str
, end
); }
51 #define strtof msvc_strtof
55 static void fwrite16le(ALushort val
, FILE *f
)
57 ALubyte data
[2] = { (ALubyte
)(val
&0xff), (ALubyte
)((val
>>8)&0xff) };
58 fwrite(data
, 1, 2, f
);
61 static void fwrite32le(ALuint val
, FILE *f
)
63 ALubyte data
[4] = { (ALubyte
)(val
&0xff), (ALubyte
)((val
>>8)&0xff), (ALubyte
)((val
>>16)&0xff),
64 (ALubyte
)((val
>>24)&0xff) };
65 fwrite(data
, 1, 4, f
);
69 typedef struct Recorder
{
85 int main(int argc
, char **argv
)
87 static const char optlist
[] =
88 " --channels/-c <channels> Set channel count (1 or 2)\n"
89 " --bits/-b <bits> Set channel count (8, 16, or 32)\n"
90 " --rate/-r <rate> Set sample rate (8000 to 96000)\n"
91 " --time/-t <time> Time in seconds to record (1 to 10)\n"
92 " --outfile/-o <filename> Output filename (default: record.wav)";
93 const char *fname
= "record.wav";
94 const char *devname
= NULL
;
104 fprintf(stderr
, "Record from a device to a wav file.\n\n"
105 "Usage: %s [-device <name>] [options...]\n\n"
106 "Available options:\n%s\n", progname
, optlist
);
110 recorder
.mDevice
= NULL
;
111 recorder
.mFile
= NULL
;
112 recorder
.mDataSizeOffset
= 0;
113 recorder
.mDataSize
= 0;
114 recorder
.mRecTime
= 4.0f
;
115 recorder
.mChannels
= 1;
117 recorder
.mSampleRate
= 44100;
118 recorder
.mFrameSize
= recorder
.mChannels
* recorder
.mBits
/ 8;
119 recorder
.mBuffer
= NULL
;
120 recorder
.mBufferSize
= 0;
123 if(argc
> 1 && strcmp(argv
[0], "-device") == 0)
133 if(strcmp(argv
[0], "--") == 0)
135 else if(strcmp(argv
[0], "--channels") == 0 || strcmp(argv
[0], "-c") == 0)
139 fprintf(stderr
, "Missing argument for option: %s\n", argv
[0]);
143 recorder
.mChannels
= (ALuint
)strtoul(argv
[1], &end
, 0);
144 if((recorder
.mChannels
!= 1 && recorder
.mChannels
!= 2) || (end
&& *end
!= '\0'))
146 fprintf(stderr
, "Invalid channels: %s\n", argv
[1]);
152 else if(strcmp(argv
[0], "--bits") == 0 || strcmp(argv
[0], "-b") == 0)
156 fprintf(stderr
, "Missing argument for option: %s\n", argv
[0]);
160 recorder
.mBits
= (ALuint
)strtoul(argv
[1], &end
, 0);
161 if((recorder
.mBits
!= 8 && recorder
.mBits
!= 16 && recorder
.mBits
!= 32) ||
162 (end
&& *end
!= '\0'))
164 fprintf(stderr
, "Invalid bit count: %s\n", argv
[1]);
170 else if(strcmp(argv
[0], "--rate") == 0 || strcmp(argv
[0], "-r") == 0)
174 fprintf(stderr
, "Missing argument for option: %s\n", argv
[0]);
178 recorder
.mSampleRate
= (ALuint
)strtoul(argv
[1], &end
, 0);
179 if(!(recorder
.mSampleRate
>= 8000 && recorder
.mSampleRate
<= 96000) || (end
&& *end
!= '\0'))
181 fprintf(stderr
, "Invalid sample rate: %s\n", argv
[1]);
187 else if(strcmp(argv
[0], "--time") == 0 || strcmp(argv
[0], "-t") == 0)
191 fprintf(stderr
, "Missing argument for option: %s\n", argv
[0]);
195 recorder
.mRecTime
= strtof(argv
[1], &end
);
196 if(!(recorder
.mRecTime
>= 1.0f
&& recorder
.mRecTime
<= 10.0f
) || (end
&& *end
!= '\0'))
198 fprintf(stderr
, "Invalid record time: %s\n", argv
[1]);
204 else if(strcmp(argv
[0], "--outfile") == 0 || strcmp(argv
[0], "-o") == 0)
208 fprintf(stderr
, "Missing argument for option: %s\n", argv
[0]);
216 else if(strcmp(argv
[0], "--help") == 0 || strcmp(argv
[0], "-h") == 0)
218 fprintf(stderr
, "Record from a device to a wav file.\n\n"
219 "Usage: %s [-device <name>] [options...]\n\n"
220 "Available options:\n%s\n", progname
, optlist
);
225 fprintf(stderr
, "Invalid option '%s'.\n\n"
226 "Usage: %s [-device <name>] [options...]\n\n"
227 "Available options:\n%s\n", argv
[0], progname
, optlist
);
232 recorder
.mFrameSize
= recorder
.mChannels
* recorder
.mBits
/ 8;
235 if(recorder
.mChannels
== 1)
237 if(recorder
.mBits
== 8)
238 format
= AL_FORMAT_MONO8
;
239 else if(recorder
.mBits
== 16)
240 format
= AL_FORMAT_MONO16
;
241 else if(recorder
.mBits
== 32)
242 format
= AL_FORMAT_MONO_FLOAT32
;
244 else if(recorder
.mChannels
== 2)
246 if(recorder
.mBits
== 8)
247 format
= AL_FORMAT_STEREO8
;
248 else if(recorder
.mBits
== 16)
249 format
= AL_FORMAT_STEREO16
;
250 else if(recorder
.mBits
== 32)
251 format
= AL_FORMAT_STEREO_FLOAT32
;
254 recorder
.mDevice
= alcCaptureOpenDevice(devname
, recorder
.mSampleRate
, format
, 32768);
255 if(!recorder
.mDevice
)
257 fprintf(stderr
, "Failed to open %s, %s %d-bit, %s, %dhz (%d samples)\n",
258 devname
? devname
: "default device",
259 (recorder
.mBits
== 32) ? "Float" :
260 (recorder
.mBits
!= 8) ? "Signed" : "Unsigned", recorder
.mBits
,
261 (recorder
.mChannels
== 1) ? "Mono" : "Stereo", recorder
.mSampleRate
,
266 fprintf(stderr
, "Opened \"%s\"\n", alcGetString(
267 recorder
.mDevice
, ALC_CAPTURE_DEVICE_SPECIFIER
270 recorder
.mFile
= fopen(fname
, "wb");
273 fprintf(stderr
, "Failed to open '%s' for writing\n", fname
);
274 alcCaptureCloseDevice(recorder
.mDevice
);
278 fputs("RIFF", recorder
.mFile
);
279 fwrite32le(0xFFFFFFFF, recorder
.mFile
); // 'RIFF' header len; filled in at close
281 fputs("WAVE", recorder
.mFile
);
283 fputs("fmt ", recorder
.mFile
);
284 fwrite32le(18, recorder
.mFile
); // 'fmt ' header len
286 // 16-bit val, format type id (1 = integer PCM, 3 = float PCM)
287 fwrite16le((recorder
.mBits
== 32) ? 0x0003 : 0x0001, recorder
.mFile
);
288 // 16-bit val, channel count
289 fwrite16le((ALushort
)recorder
.mChannels
, recorder
.mFile
);
290 // 32-bit val, frequency
291 fwrite32le(recorder
.mSampleRate
, recorder
.mFile
);
292 // 32-bit val, bytes per second
293 fwrite32le(recorder
.mSampleRate
* recorder
.mFrameSize
, recorder
.mFile
);
294 // 16-bit val, frame size
295 fwrite16le((ALushort
)recorder
.mFrameSize
, recorder
.mFile
);
296 // 16-bit val, bits per sample
297 fwrite16le((ALushort
)recorder
.mBits
, recorder
.mFile
);
298 // 16-bit val, extra byte count
299 fwrite16le(0, recorder
.mFile
);
301 fputs("data", recorder
.mFile
);
302 fwrite32le(0xFFFFFFFF, recorder
.mFile
); // 'data' header len; filled in at close
304 recorder
.mDataSizeOffset
= ftell(recorder
.mFile
) - 4;
305 if(ferror(recorder
.mFile
) || recorder
.mDataSizeOffset
< 0)
307 fprintf(stderr
, "Error writing header: %s\n", strerror(errno
));
308 fclose(recorder
.mFile
);
309 alcCaptureCloseDevice(recorder
.mDevice
);
313 fprintf(stderr
, "Recording '%s', %s %d-bit, %s, %dhz (%g second%s)\n", fname
,
314 (recorder
.mBits
== 32) ? "Float" :
315 (recorder
.mBits
!= 8) ? "Signed" : "Unsigned", recorder
.mBits
,
316 (recorder
.mChannels
== 1) ? "Mono" : "Stereo", recorder
.mSampleRate
,
317 recorder
.mRecTime
, (recorder
.mRecTime
!= 1.0f
) ? "s" : ""
321 alcCaptureStart(recorder
.mDevice
);
322 while((double)recorder
.mDataSize
/(double)recorder
.mSampleRate
< recorder
.mRecTime
&&
323 (err
=alcGetError(recorder
.mDevice
)) == ALC_NO_ERROR
&& !ferror(recorder
.mFile
))
326 fprintf(stderr
, "\rCaptured %u samples", recorder
.mDataSize
);
327 alcGetIntegerv(recorder
.mDevice
, ALC_CAPTURE_SAMPLES
, 1, &count
);
330 al_nssleep(10000000);
333 if(count
> recorder
.mBufferSize
)
335 ALbyte
*data
= calloc(recorder
.mFrameSize
, (ALuint
)count
);
336 free(recorder
.mBuffer
);
337 recorder
.mBuffer
= data
;
338 recorder
.mBufferSize
= count
;
340 alcCaptureSamples(recorder
.mDevice
, recorder
.mBuffer
, count
);
341 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
342 /* Byteswap multibyte samples on big-endian systems (wav needs little-
343 * endian, and OpenAL gives the system's native-endian).
345 if(recorder
.mBits
== 16)
348 for(i
= 0;i
< count
*recorder
.mChannels
;i
++)
350 ALbyte b
= recorder
.mBuffer
[i
*2 + 0];
351 recorder
.mBuffer
[i
*2 + 0] = recorder
.mBuffer
[i
*2 + 1];
352 recorder
.mBuffer
[i
*2 + 1] = b
;
355 else if(recorder
.mBits
== 32)
358 for(i
= 0;i
< count
*recorder
.mChannels
;i
++)
360 ALbyte b0
= recorder
.mBuffer
[i
*4 + 0];
361 ALbyte b1
= recorder
.mBuffer
[i
*4 + 1];
362 recorder
.mBuffer
[i
*4 + 0] = recorder
.mBuffer
[i
*4 + 3];
363 recorder
.mBuffer
[i
*4 + 1] = recorder
.mBuffer
[i
*4 + 2];
364 recorder
.mBuffer
[i
*4 + 2] = b1
;
365 recorder
.mBuffer
[i
*4 + 3] = b0
;
369 recorder
.mDataSize
+= (ALuint
)fwrite(recorder
.mBuffer
, recorder
.mFrameSize
, (ALuint
)count
,
372 alcCaptureStop(recorder
.mDevice
);
373 fprintf(stderr
, "\rCaptured %u samples\n", recorder
.mDataSize
);
374 if(err
!= ALC_NO_ERROR
)
375 fprintf(stderr
, "Got device error 0x%04x: %s\n", err
, alcGetString(recorder
.mDevice
, err
));
377 alcCaptureCloseDevice(recorder
.mDevice
);
378 recorder
.mDevice
= NULL
;
380 free(recorder
.mBuffer
);
381 recorder
.mBuffer
= NULL
;
382 recorder
.mBufferSize
= 0;
384 total_size
= ftell(recorder
.mFile
);
385 if(fseek(recorder
.mFile
, recorder
.mDataSizeOffset
, SEEK_SET
) == 0)
387 fwrite32le(recorder
.mDataSize
*recorder
.mFrameSize
, recorder
.mFile
);
388 if(fseek(recorder
.mFile
, 4, SEEK_SET
) == 0)
389 fwrite32le((ALuint
)total_size
- 8, recorder
.mFile
);
392 fclose(recorder
.mFile
);
393 recorder
.mFile
= NULL
;