/
AEAudioFileWriter.m
236 lines (197 loc) · 11 KB
/
AEAudioFileWriter.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
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
//
// AEAudioFileWriter.m
// The Amazing Audio Engine
//
// Created by Michael Tyson on 20/03/2012.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import "AEAudioFileWriter.h"
#import "TheAmazingAudioEngine.h"
NSString * AEAudioFileWriterErrorDomain = @"com.theamazingaudioengine.AEAudioFileWriterErrorDomain";
#define checkResult(result,operation) (_checkResult((result),(operation),strrchr(__FILE__, '/')+1,__LINE__))
static inline BOOL _checkResult(OSStatus result, const char *operation, const char* file, int line) {
if ( result != noErr ) {
NSLog(@"%s:%d: %s result %d %08X %4.4s\n", file, line, operation, (int)result, (int)result, (char*)&result);
return NO;
}
return YES;
}
@interface AEAudioFileWriter () {
BOOL _writing;
ExtAudioFileRef _audioFile;
UInt32 _priorMixOverrideValue;
AudioStreamBasicDescription _audioDescription;
}
@property (nonatomic, retain, readwrite) NSString *path;
@end
@implementation AEAudioFileWriter
@synthesize path = _path;
+ (BOOL)AACEncodingAvailable {
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
static BOOL available;
static BOOL available_set = NO;
if ( available_set ) return available;
// get an array of AudioClassDescriptions for all installed encoders for the given format
// the specifier is the format that we are interested in - this is 'aac ' in our case
UInt32 encoderSpecifier = kAudioFormatMPEG4AAC;
UInt32 size;
if ( !checkResult(AudioFormatGetPropertyInfo(kAudioFormatProperty_Encoders, sizeof(encoderSpecifier), &encoderSpecifier, &size),
"AudioFormatGetPropertyInfo(kAudioFormatProperty_Encoders") ) return NO;
UInt32 numEncoders = size / sizeof(AudioClassDescription);
AudioClassDescription encoderDescriptions[numEncoders];
if ( !checkResult(AudioFormatGetProperty(kAudioFormatProperty_Encoders, sizeof(encoderSpecifier), &encoderSpecifier, &size, encoderDescriptions),
"AudioFormatGetProperty(kAudioFormatProperty_Encoders") ) {
available_set = YES;
available = NO;
return NO;
}
for (UInt32 i=0; i < numEncoders; ++i) {
if ( encoderDescriptions[i].mSubType == kAudioFormatMPEG4AAC && encoderDescriptions[i].mManufacturer == kAppleHardwareAudioCodecManufacturer ) {
available_set = YES;
available = YES;
return YES;
}
}
available_set = YES;
available = NO;
return NO;
#endif
}
- (id)initWithAudioDescription:(AudioStreamBasicDescription)audioDescription {
if ( !(self = [super init]) ) return nil;
_audioDescription = audioDescription;
return self;
}
- (void)dealloc {
if ( _writing ) {
[self finishWriting];
}
self.path = nil;
[super dealloc];
}
- (BOOL)beginWritingToFileAtPath:(NSString*)path fileType:(AudioFileTypeID)fileType error:(NSError**)error {
OSStatus status;
if ( fileType == kAudioFileM4AType ) {
if ( ![AEAudioFileWriter AACEncodingAvailable] ) {
if ( error ) *error = [NSError errorWithDomain:AEAudioFileWriterErrorDomain
code:kAEAudioFileWriterFormatError
userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"AAC Encoding not available", @"")
forKey:NSLocalizedDescriptionKey]];
return NO;
}
// AAC won't work if the 'mix with others' session property is enabled. Disable it if it's on.
UInt32 size = sizeof(_priorMixOverrideValue);
checkResult(AudioSessionGetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, &size, &_priorMixOverrideValue),
"AudioSessionGetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers)");
if ( _priorMixOverrideValue != NO ) {
UInt32 allowMixing = NO;
checkResult(AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing),
"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers)");
}
// Get the output audio description
AudioStreamBasicDescription destinationFormat;
memset(&destinationFormat, 0, sizeof(destinationFormat));
destinationFormat.mChannelsPerFrame = _audioDescription.mChannelsPerFrame;
destinationFormat.mSampleRate = _audioDescription.mSampleRate;
destinationFormat.mFormatID = kAudioFormatMPEG4AAC;
size = sizeof(destinationFormat);
status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &destinationFormat);
if ( !checkResult(status, "AudioFormatGetProperty(kAudioFormatProperty_FormatInfo") ) {
int fourCC = CFSwapInt32HostToBig(status);
if ( error ) *error = [NSError errorWithDomain:NSOSStatusErrorDomain
code:status
userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:NSLocalizedString(@"Couldn't prepare the output format (error %d/%4.4s)", @""), status, (char*)&fourCC]
forKey:NSLocalizedDescriptionKey]];
return NO;
}
// Create the file
status = ExtAudioFileCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
kAudioFileM4AType,
&destinationFormat,
NULL,
kAudioFileFlags_EraseFile,
&_audioFile);
if ( !checkResult(status, "ExtAudioFileCreateWithURL") ) {
int fourCC = CFSwapInt32HostToBig(status);
if ( error ) *error = [NSError errorWithDomain:NSOSStatusErrorDomain
code:status
userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:NSLocalizedString(@"Couldn't open the output file (error %d/%4.4s)", @""), status, (char*)&fourCC]
forKey:NSLocalizedDescriptionKey]];
return NO;
}
} else {
// Derive the output audio description from the client format, but with interleaved, big endian (if AIFF) signed integers.
AudioStreamBasicDescription audioDescription = _audioDescription;
audioDescription.mFormatFlags = (fileType == kAudioFileAIFFType ? kLinearPCMFormatFlagIsBigEndian : 0) | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
audioDescription.mFormatID = kAudioFormatLinearPCM;
audioDescription.mBytesPerPacket =
audioDescription.mBytesPerFrame = audioDescription.mChannelsPerFrame * (audioDescription.mBitsPerChannel/8);
audioDescription.mFramesPerPacket = 1;
// Create the file
status = ExtAudioFileCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],
fileType,
&audioDescription,
NULL,
kAudioFileFlags_EraseFile,
&_audioFile);
if ( !checkResult(status, "ExtAudioFileCreateWithURL") ) {
int fourCC = CFSwapInt32HostToBig(status);
if ( error ) *error = [NSError errorWithDomain:NSOSStatusErrorDomain
code:status
userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:NSLocalizedString(@"Couldn't open the output file (error %d/%4.4s)", @""), status, (char*)&fourCC]
forKey:NSLocalizedDescriptionKey]];
return NO;
}
}
// Set up the converter
status = ExtAudioFileSetProperty(_audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &_audioDescription);
if ( !checkResult(status, "ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat") ) {
int fourCC = CFSwapInt32HostToBig(status);
if ( error ) *error = [NSError errorWithDomain:NSOSStatusErrorDomain
code:status
userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:NSLocalizedString(@"Couldn't configure the converter (error %d/%4.4s)", @""), status, (char*)&fourCC]
forKey:NSLocalizedDescriptionKey]];
ExtAudioFileDispose(_audioFile);
return NO;
}
// Init the async file writing mechanism
checkResult(ExtAudioFileWriteAsync(_audioFile, 0, NULL), "ExtAudioFileWriteAsync");
self.path = path;
_writing = YES;
return YES;
}
- (void)finishWriting {
if ( !_writing ) return;
_writing = NO;
checkResult(ExtAudioFileDispose(_audioFile), "AudioFileClose");
if ( _priorMixOverrideValue ) {
checkResult(AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(_priorMixOverrideValue), &_priorMixOverrideValue),
"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers)");
}
}
OSStatus AEAudioFileWriterAddAudio(AEAudioFileWriter* THIS, AudioBufferList *bufferList, UInt32 lengthInFrames) {
return ExtAudioFileWriteAsync(THIS->_audioFile, lengthInFrames, bufferList);
}
OSStatus AEAudioFileWriterAddAudioSynchronously(AEAudioFileWriter* THIS, AudioBufferList *bufferList, UInt32 lengthInFrames) {
return ExtAudioFileWrite(THIS->_audioFile, lengthInFrames, bufferList);
}
@end