@@ -6,10 +6,6 @@ - (instancetype)initWithSampleRate:(int)sampleRate
66{
77 if (self = [super init ]) {
88 self.sampleRate = sampleRate;
9- self.format = [[AVAudioFormat alloc ] initWithCommonFormat: AVAudioPCMFormatFloat32
10- sampleRate: self .sampleRate
11- channels: 2
12- interleaved: NO ];
139 }
1410 return self;
1511}
@@ -20,13 +16,15 @@ - (const AudioBufferList *)decode:(NSString *)pathOrURL
2016 NSURL *url = [NSURL URLWithString: pathOrURL];
2117
2218 if (url && url.scheme ) {
23- return [self decodeWithURL: url];
19+ self. buffer = [self decodeWithURL: url];
2420 } else {
25- return [self decodeWithFilePath: pathOrURL];
21+ self. buffer = [self decodeWithFilePath: pathOrURL];
2622 }
23+
24+ return self.buffer .audioBufferList ;
2725}
2826
29- - (const AudioBufferList *)decodeWithFilePath : (NSString *)path
27+ - (AVAudioPCMBuffer *)decodeWithFilePath : (NSString *)path
3028{
3129 NSError *error = nil ;
3230 NSURL *fileURL = [NSURL fileURLWithPath: path];
@@ -36,23 +34,28 @@ - (const AudioBufferList *)decodeWithFilePath:(NSString *)path
3634 NSLog (@" Error occurred while opening the audio file: %@ " , [error localizedDescription ]);
3735 return nil ;
3836 }
39- self.buffer = [[AVAudioPCMBuffer alloc ] initWithPCMFormat: [audioFile processingFormat ]
40- frameCapacity: [audioFile length ]];
37+ AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc ] initWithPCMFormat: [audioFile processingFormat ]
38+ frameCapacity: [audioFile length ]];
39+
40+ AVAudioFormat *format = [[AVAudioFormat alloc ] initWithCommonFormat: AVAudioPCMFormatFloat32
41+ sampleRate: self .sampleRate
42+ channels: buffer.audioBufferList->mNumberBuffers
43+ interleaved: NO ];
4144
42- [audioFile readIntoBuffer: self . buffer error: &error];
45+ [audioFile readIntoBuffer: buffer error: &error];
4346 if (error) {
4447 NSLog (@" Error occurred while reading the audio file: %@ " , [error localizedDescription ]);
4548 return nil ;
4649 }
4750
4851 if (self.sampleRate != audioFile.processingFormat .sampleRate ) {
49- [self convertFromFormat: self . buffer. format];
52+ return [self convertBuffer: buffer ToFormat: format];
5053 }
5154
52- return self. buffer . audioBufferList ;
55+ return buffer;
5356}
5457
55- - (const AudioBufferList *)decodeWithURL : (NSURL *)url
58+ - (AVAudioPCMBuffer *)decodeWithURL : (NSURL *)url
5659{
5760 __block NSURL *tempFileURL = nil ;
5861
@@ -127,20 +130,19 @@ - (void)downloadFileFromURL:(NSURL *)url completion:(void (^)(NSURL *tempFileURL
127130 [downloadTask resume ];
128131}
129132
130- - (void ) convertFromFormat : (AVAudioFormat *)format
133+ - (AVAudioPCMBuffer *) convertBuffer : (AVAudioPCMBuffer *) buffer ToFormat : (AVAudioFormat *)format
131134{
132135 NSError *error = nil ;
133- AVAudioConverter *converter = [[AVAudioConverter alloc ] initFromFormat: format toFormat: self . format];
136+ AVAudioConverter *converter = [[AVAudioConverter alloc ] initFromFormat: buffer. format toFormat: format];
134137 AVAudioPCMBuffer *convertedBuffer =
135- [[AVAudioPCMBuffer alloc ] initWithPCMFormat: self .format
136- frameCapacity: (AVAudioFrameCount)self .buffer.frameCapacity];
138+ [[AVAudioPCMBuffer alloc ] initWithPCMFormat: format frameCapacity: (AVAudioFrameCount)buffer.frameCapacity];
137139
138140 AVAudioConverterInputBlock inputBlock =
139141 ^AVAudioBuffer *(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus)
140142 {
141- if (self. buffer .frameLength > 0 ) {
143+ if (buffer.frameLength > 0 ) {
142144 *outStatus = AVAudioConverterInputStatus_HaveData;
143- return self. buffer ;
145+ return buffer;
144146 } else {
145147 *outStatus = AVAudioConverterInputStatus_NoDataNow;
146148 return nil ;
@@ -151,16 +153,15 @@ - (void)convertFromFormat:(AVAudioFormat *)format
151153
152154 if (error) {
153155 NSLog (@" Error occurred while converting the audio file: %@ " , [error localizedDescription ]);
154- return ;
156+ return nil ;
155157 }
156158
157- self. buffer = convertedBuffer;
159+ return convertedBuffer;
158160}
159161
160162- (void )cleanup
161163{
162164 self.buffer = nil ;
163- self.format = nil ;
164165}
165166
166167@end
0 commit comments