-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayFile.cpp
More file actions
402 lines (329 loc) · 15.8 KB
/
PlayFile.cpp
File metadata and controls
402 lines (329 loc) · 15.8 KB
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
397
398
399
400
401
402
#include <AudioToolbox/AudioToolbox.h>
#include "CAXException.h"
#include "CAAudioUnit.h"
void UsageString(int exitCode)
{
switch (exitCode) {
case 1:
printf ("Usage: record <name of file> <duration :sec>\n");
break;
case 2:
printf ("Usage: play <name of file>\n");
break;
case 3:
printf ("Incorrect command\n");
printf ("Usage: record <name of file> <duration :sec>\n");
printf ("Usage: play <name of file>\n");
break;
}
exit(exitCode);
}
typedef struct MyRecorder {
AudioQueueRef queue;
CFAbsoluteTime queueStartStopTime;
AudioFileID recordFile;
SInt64 recordPacket; // current packet number in record file
Boolean running;
Boolean verbose;
} MyRecorder;
// helper functions
double PrepareFileAU (CAAudioUnit &au, AudioStreamBasicDescription &fileFormat, AudioFileID audioFile);
void MakeSimpleGraph (AUGraph &theGraph, CAAudioUnit &fileAU, AudioFileID audioFile);
static void MyInputBufferHandler( void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp * inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription *inPacketDesc)
{
MyRecorder *aqr = (MyRecorder *)inUserData;
if (aqr->verbose) {
printf("buf data %p, 0x%x bytes, 0x%x packets\n", inBuffer->mAudioData,
(int)inBuffer->mAudioDataByteSize, (int)inNumPackets);
}
if (inNumPackets > 0) {
AudioFileWritePackets(aqr->recordFile, FALSE, inBuffer->mAudioDataByteSize,
inPacketDesc, aqr->recordPacket, &inNumPackets, inBuffer->mAudioData);
aqr->recordPacket += inNumPackets;
}
// if we're not stopping, re-enqueue the buffe so that it gets filled again
if (aqr->running)
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}
static void MyPropertyListener(void *userData, AudioQueueRef queue, AudioQueuePropertyID propertyID)
{
MyRecorder *aqr = (MyRecorder *)userData;
if (propertyID == kAudioQueueProperty_IsRunning)
aqr->queueStartStopTime = CFAbsoluteTimeGetCurrent();
}
static Boolean InferAudioFileFormatFromFilename(CFStringRef filename, AudioFileTypeID *outFiletype)
{
OSStatus err;
// find the extension in the filename.
CFRange range = CFStringFind(filename, CFSTR("."), kCFCompareBackwards);
if (range.location == kCFNotFound)
return FALSE;
range.location += 1;
range.length = CFStringGetLength(filename) - range.location;
CFStringRef extension = CFStringCreateWithSubstring(NULL, filename, range);
UInt32 propertySize = sizeof(AudioFileTypeID);
err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_TypesForExtension, sizeof(extension), &extension, &propertySize, outFiletype);
CFRelease(extension);
return (err == noErr && propertySize > 0);
}
static void MyCopyEncoderCookieToFile(AudioQueueRef theQueue, AudioFileID theFile)
{
OSStatus err;
UInt32 propertySize;
// get the magic cookie, if any, from the converter
err = AudioQueueGetPropertySize(theQueue, kAudioConverterCompressionMagicCookie, &propertySize);
if (err == noErr && propertySize > 0) {
// there is valid cookie data to be fetched; get it
Byte *magicCookie = (Byte *)malloc(propertySize);
try {
AudioQueueGetProperty(theQueue, kAudioConverterCompressionMagicCookie, magicCookie,
&propertySize);
AudioFileSetProperty(theFile, kAudioFilePropertyMagicCookieData, propertySize, magicCookie);
}
catch (CAXException e) {
char buf[256];
fprintf(stderr, "MyCopyEncoderCookieToFile: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
free(magicCookie);
}
}
static int MyComputeRecordBufferSize(const AudioStreamBasicDescription *format, AudioQueueRef queue, float seconds)
{
int packets, frames, bytes;
frames = (int)ceil(seconds * format->mSampleRate);
if (format->mBytesPerFrame > 0)
bytes = frames * format->mBytesPerFrame;
else {
UInt32 maxPacketSize;
if (format->mBytesPerPacket > 0)
maxPacketSize = format->mBytesPerPacket; // constant packet size
else {
UInt32 propertySize = sizeof(maxPacketSize);
AudioQueueGetProperty(queue, kAudioConverterPropertyMaximumOutputPacketSize, &maxPacketSize,
&propertySize);
}
if (format->mFramesPerPacket > 0)
packets = frames / format->mFramesPerPacket;
else
packets = frames; // worst-case scenario: 1 frame in a packet
if (packets == 0) // sanity check
packets = 1;
bytes = packets * maxPacketSize;
}
return bytes;
}
int main (int argc, char * const argv[])
{
if ( argc > 2 ) {
const char *arg = argv[1];
const char* suffix = ".wav";
if (strcmp(arg, "record") == 0 && argc == 4) {
MyRecorder aqr;
UInt32 size;
CFURLRef url;
const char *recordFileName = NULL;
OSStatus err = noErr;
float seconds = 0;
char *argfileName = argv[2];
if (argfileName == NULL) {
UsageString(1);
}
strcat(argfileName, suffix);
const char *argTime = argv[3];
if ( sscanf(argTime, "%f", &seconds ) != 1 )
UsageString(1);
aqr.verbose = TRUE;
recordFileName = strcat( argv[0], argfileName );
AudioFileTypeID audioFileType = kAudioFileWAVEType;
CFStringRef cfRecordFileName = CFStringCreateWithCString(NULL, recordFileName, kCFStringEncodingUTF8);
InferAudioFileFormatFromFilename(cfRecordFileName, &audioFileType);
CFRelease(cfRecordFileName);
try {
AudioStreamBasicDescription df;
df.mFormatID = kAudioFormatLinearPCM;
df.mSampleRate = 48000.0;
df.mChannelsPerFrame = 1; // Mono
df.mBitsPerChannel = 32;
df.mBytesPerPacket =
df.mBytesPerFrame =
df.mChannelsPerFrame * sizeof(AudioSampleType);
df.mFramesPerPacket = 1;
df.mFormatFlags = kAudioFormatFlagsCanonical;
// create the queue
AudioQueueNewInput(
&df,
MyInputBufferHandler,
&aqr /* userData */,
NULL /* run loop */, NULL /* run loop mode */,
0 /* flags */, &aqr.queue);
// get the record format back from the queue's audio converter --
// the file may require a more specific stream description than was necessary to create the encoder.
size = sizeof(df);
AudioQueueGetProperty(aqr.queue, kAudioConverterCurrentOutputStreamDescription,
&df, &size);
// convert recordFileName from C string to CFURL
url = CFURLCreateFromFileSystemRepresentation(NULL, (Byte *)recordFileName, strlen(recordFileName), FALSE);
// create the audio file
err = AudioFileCreateWithURL(url, audioFileType, &df, kAudioFileFlags_EraseFile, &aqr.recordFile);
CFRelease(url);
// copy the cookie first to give the file object as much info as we can about the data going in
MyCopyEncoderCookieToFile(aqr.queue, aqr.recordFile);
// allocate and enqueue buffers
int bufferByteSize = MyComputeRecordBufferSize(&df, aqr.queue, 0.5); // enough bytes for half a second
for (int i = 0; i < 3; ++i) {
AudioQueueBufferRef buffer;
AudioQueueAllocateBuffer(aqr.queue, bufferByteSize, &buffer);
AudioQueueEnqueueBuffer(aqr.queue, buffer, 0, NULL);
}
// record
if (seconds > 0) {
// user requested a fixed-length recording (specified a duration with -s)
// to time the recording more accurately, watch the queue's IsRunning property
AudioQueueAddPropertyListener(aqr.queue, kAudioQueueProperty_IsRunning,
MyPropertyListener, &aqr);
// start the queue
aqr.running = TRUE;
AudioQueueStart(aqr.queue, NULL);
CFAbsoluteTime waitUntil = CFAbsoluteTimeGetCurrent() + 10;
// wait for the started notification
while (aqr.queueStartStopTime == 0.) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.010, FALSE);
if (CFAbsoluteTimeGetCurrent() >= waitUntil) {
fprintf(stderr, "Timeout waiting for the queue's IsRunning notification\n");
goto cleanup;
}
}
printf("Recording...\n");
CFAbsoluteTime stopTime = aqr.queueStartStopTime + seconds;
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
CFRunLoopRunInMode(kCFRunLoopDefaultMode, stopTime - now, FALSE);
} else {
// start the queue
aqr.running = TRUE;
AudioQueueStart(aqr.queue, NULL);
// and wait
printf("Recording, press <return> to stop:\n");
getchar();
}
// end recording
printf("* recording done *\n");
aqr.running = FALSE;
AudioQueueStop(aqr.queue, TRUE);
MyCopyEncoderCookieToFile(aqr.queue, aqr.recordFile);
cleanup:
AudioQueueDispose(aqr.queue, TRUE);
AudioFileClose(aqr.recordFile);
}
catch (CAXException e) {
char buf[256];
fprintf(stderr, "MyInputBufferHandler: %s (%s)\n", e.mOperation, e.FormatError(buf));
return e.mError;
}
}
if (strcmp(arg, "play") == 0 && argc == 3 ) {
AudioFileID audioFile;
char* inputFileName = argv[2];
strcat(inputFileName, suffix);
const char* inputFile = NULL;
inputFile = strcat(argv[0], inputFileName);
CFURLRef theURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8*)inputFile, strlen(inputFile), false);
AudioFileOpenURL (theURL, kAudioFileReadPermission, 0, &audioFile);
CFRelease(theURL);
AudioStreamBasicDescription fileFormat = { 0.0, 0, 0, 0, 0, 0, 0, 0, 0 };
UInt32 propsize = sizeof(fileFormat);
OSStatus status = AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &propsize, &fileFormat);
if (status) {
UsageString(2);
}
// lets set up our playing state now
AUGraph theGraph;
CAAudioUnit fileAU;
// this makes the graph, the file AU and sets it all up for playing
MakeSimpleGraph (theGraph, fileAU, audioFile);
Float64 fileDuration = PrepareFileAU (fileAU, fileFormat, audioFile);
printf ("file duration: %f secs\n", fileDuration);
printf ("Playing....\n");
// start playing
AUGraphStart (theGraph);
// sleep until the file is finished
usleep ((int)(fileDuration * 1000. * 1000.));
}
else{
UsageString(3);
}
}
else
UsageString(3);
return 0;
}
double PrepareFileAU (CAAudioUnit &au, AudioStreamBasicDescription &fileFormat, AudioFileID audioFile)
{
//
// calculate the duration
UInt64 nPackets;
UInt32 propsize = sizeof(nPackets);
AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataPacketCount, &propsize, &nPackets);
Float64 fileDuration = (nPackets * fileFormat.mFramesPerPacket) / fileFormat.mSampleRate;
ScheduledAudioFileRegion rgn;
memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
rgn.mTimeStamp.mSampleTime = 0;
rgn.mCompletionProc = NULL;
rgn.mCompletionProcUserData = NULL;
rgn.mAudioFile = audioFile;
rgn.mLoopCount = 1;
rgn.mStartFrame = 0;
rgn.mFramesToPlay = UInt32(nPackets * fileFormat.mFramesPerPacket);
// tell the file player AU to play all of the file
au.SetProperty (kAudioUnitProperty_ScheduledFileRegion,
kAudioUnitScope_Global, 0,&rgn, sizeof(rgn));
// prime the fp AU with default values
UInt32 defaultVal = 0;
au.SetProperty (kAudioUnitProperty_ScheduledFilePrime,
kAudioUnitScope_Global, 0, &defaultVal, sizeof(defaultVal));
// tell the fp AU when to start playing (this ts is in the AU's render time stamps; -1 means next render cycle)
AudioTimeStamp startTime;
memset (&startTime, 0, sizeof(startTime));
startTime.mFlags = kAudioTimeStampSampleTimeValid;
startTime.mSampleTime = -1;
au.SetProperty(kAudioUnitProperty_ScheduleStartTimeStamp,
kAudioUnitScope_Global, 0, &startTime, sizeof(startTime));
return fileDuration;
}
void MakeSimpleGraph (AUGraph &theGraph, CAAudioUnit &fileAU, AudioFileID audioFile)
{
NewAUGraph (&theGraph);
CAComponentDescription cd;
// output node
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_DefaultOutput;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode outputNode;
AUGraphAddNode (theGraph, &cd, &outputNode);
// file AU node
AUNode fileNode;
cd.componentType = kAudioUnitType_Generator;
cd.componentSubType = kAudioUnitSubType_AudioFilePlayer;
AUGraphAddNode (theGraph, &cd, &fileNode);
// connect & setup
AUGraphOpen (theGraph);
// install overload listener to detect when something is wrong
AudioUnit anAU;
AUGraphNodeInfo(theGraph, fileNode, NULL, &anAU);
// fileAU = CAAudioUnit (fileNode, anAU);
fileAU = CAAudioUnit (fileNode, anAU);
// load in the file
fileAU.SetProperty(kAudioUnitProperty_ScheduledFileIDs,
kAudioUnitScope_Global, 0, &audioFile, sizeof(audioFile));
AUGraphConnectNodeInput (theGraph, fileNode, 0, outputNode, 0);
// AT this point we make sure we have the file player AU initialized
// this also propogates the output format of the AU to the output unit
AUGraphInitialize (theGraph);
// workaround a race condition in the file player AU
usleep (10 * 1000);
}