forked from leejunkit/MiniMusicalStar-CustomAudioLibraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoomzAUOutputCapturer.m
executable file
·100 lines (83 loc) · 2.92 KB
/
BoomzAUOutputCapturer.m
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
//
// BoomzAUOutputCapturer.m
// ThirdAttempt
//
// Created by Jun Kit on 7/6/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "BoomzAUOutputCapturer.h"
@implementation BoomzAUOutputCapturer
static OSStatus RenderCallback( void * inRefCon,
AudioUnitRenderActionFlags * ioActionFlags,
const AudioTimeStamp * inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData)
{
if (*ioActionFlags & kAudioUnitRenderAction_PostRender) {
BoomzAUOutputCapturer *This = (BoomzAUOutputCapturer *)inRefCon;
static int TEMP_kAudioUnitRenderAction_PostRenderError = (1 << 8);
if (This->mBusNumber == inBusNumber && !(*ioActionFlags & TEMP_kAudioUnitRenderAction_PostRenderError)) {
OSStatus result = ExtAudioFileWriteAsync(This->mExtAudioFile, inNumberFrames, ioData);
CheckError(result, "Error writing frames!");
}
}
return noErr;
}
-(BoomzAUOutputCapturer *) initWithAudioUnit: (AudioUnit)au OutputURL: (CFURLRef)outputFileURL AudioFileTypeID: (AudioFileTypeID)fileType forBusNumber: (int)busNumber
{
self = [super init];
if (self)
{
mFileOpen = false;
mClientFormatSet = false;
mAudioUnit = au;
mExtAudioFile = NULL;
//should be changeable (set to 0 to record the entire mix (VERY LAGGY THOUGH), set to 1 to record just mic)
mBusNumber = busNumber;
//ASBDSetCanonical(&format, 2, YES);
ASBDSetIMA4(&format, 2);
}
{
CFShow(outputFileURL);
error = ExtAudioFileCreateWithURL(outputFileURL, fileType, &format, NULL, kAudioFileFlags_EraseFile, &mExtAudioFile);
CheckError(error, "Cannot execute ExtAudioFileCreateWithURL");
if (!error)
{
mFileOpen = true;
}
}
return self;
}
-(void) start
{
if (mFileOpen) {
if (!mClientFormatSet) {
AudioStreamBasicDescription clientFormat;
UInt32 size = sizeof(clientFormat);
AudioUnitGetProperty(mAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, mBusNumber, &clientFormat, &size);
ExtAudioFileSetProperty(mExtAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat);
mClientFormatSet = true;
}
ExtAudioFileWriteAsync(mExtAudioFile, 0, NULL); // initialize async writes
AudioUnitAddRenderNotify(mAudioUnit, RenderCallback, self);
}
}
-(void) stop
{
if (mFileOpen)
AudioUnitRemoveRenderNotify(mAudioUnit, RenderCallback, self);
}
-(void) close
{
if (mExtAudioFile) {
ExtAudioFileDispose(mExtAudioFile);
mExtAudioFile = NULL;
}
}
-(void) dealloc
{
[self close];
[super dealloc];
}
@end