-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathSFBAudioRegionDecoder.m
More file actions
389 lines (324 loc) · 13.8 KB
/
Copy pathSFBAudioRegionDecoder.m
File metadata and controls
389 lines (324 loc) · 13.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
//
// SPDX-FileCopyrightText: 2006 Stephen F. Booth <contact@sbooth.dev>
// SPDX-License-Identifier: MIT
//
// Part of https://github.com/sbooth/SFBAudioEngine
//
#import "SFBAudioRegionDecoder.h"
#import "SFBAudioDecoder+Internal.h"
#import <AVFAudioExtensions/AVFAudioExtensions.h>
#import <os/log.h>
#import <stdlib.h>
@interface SFBAudioRegionDecoder () {
@private
AVAudioPCMBuffer *_buffer;
}
@end
@implementation SFBAudioRegionDecoder
@synthesize actualStartFrame = _startFrame;
@synthesize actualFrameLength = _frameLength;
- (instancetype)initWithURL:(NSURL *)url initialFrames:(AVAudioFramePosition)frameLength error:(NSError **)error {
return [self initWithURL:url startFrame:0 frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithURL:(NSURL *)url finalFrames:(AVAudioFramePosition)frameLength error:(NSError **)error {
return [self initWithURL:url startFrame:-1 frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithURL:(NSURL *)url
startFrame:(AVAudioFramePosition)startFrame
frameLength:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithURL:url startFrame:startFrame frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithURL:(NSURL *)url repeatCount:(NSInteger)repeatCount error:(NSError **)error {
return [self initWithURL:url startFrame:0 frameLength:-1 repeatCount:repeatCount error:error];
}
- (instancetype)initWithURL:(NSURL *)url
startFrame:(AVAudioFramePosition)startFrame
frameLength:(AVAudioFramePosition)frameLength
repeatCount:(NSInteger)repeatCount
error:(NSError **)error {
NSParameterAssert(url != nil);
SFBInputSource *inputSource = [SFBInputSource inputSourceForURL:url flags:0 error:error];
if (!inputSource) {
return nil;
}
return [self initWithInputSource:inputSource
startFrame:startFrame
frameLength:frameLength
repeatCount:repeatCount
error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource
initialFrames:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithInputSource:inputSource startFrame:0 frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource
finalFrames:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithInputSource:inputSource startFrame:-1 frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource
startFrame:(AVAudioFramePosition)startFrame
frameLength:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithInputSource:inputSource
startFrame:startFrame
frameLength:frameLength
repeatCount:0
error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource
repeatCount:(NSInteger)repeatCount
error:(NSError **)error {
return [self initWithInputSource:inputSource startFrame:0 frameLength:-1 repeatCount:repeatCount error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource
startFrame:(AVAudioFramePosition)startFrame
frameLength:(AVAudioFramePosition)frameLength
repeatCount:(NSInteger)repeatCount
error:(NSError **)error {
NSParameterAssert(inputSource != nil);
SFBAudioDecoder *decoder = [[SFBAudioDecoder alloc] initWithInputSource:inputSource error:error];
if (!decoder) {
return nil;
}
return [self initWithDecoder:decoder
startFrame:startFrame
frameLength:frameLength
repeatCount:repeatCount
error:error];
}
- (instancetype)initWithDecoder:(id<SFBPCMDecoding>)decoder
initialFrames:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithDecoder:decoder startFrame:0 frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithDecoder:(id<SFBPCMDecoding>)decoder
finalFrames:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithDecoder:decoder startFrame:-1 frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithDecoder:(id<SFBPCMDecoding>)decoder
startFrame:(AVAudioFramePosition)startFrame
frameLength:(AVAudioFramePosition)frameLength
error:(NSError **)error {
return [self initWithDecoder:decoder startFrame:startFrame frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithDecoder:(id<SFBPCMDecoding>)decoder repeatCount:(NSInteger)repeatCount error:(NSError **)error {
return [self initWithDecoder:decoder startFrame:0 frameLength:-1 repeatCount:repeatCount error:error];
}
- (instancetype)initWithDecoder:(id<SFBPCMDecoding>)decoder
startFrame:(AVAudioFramePosition)startFrame
frameLength:(AVAudioFramePosition)frameLength
repeatCount:(NSInteger)repeatCount
error:(NSError **)error {
NSParameterAssert(decoder != nil);
NSParameterAssert(startFrame >= -1);
NSParameterAssert(frameLength > 0 || frameLength == -1);
NSParameterAssert(!(startFrame == -1 && frameLength == -1));
NSParameterAssert(repeatCount >= -1);
if ((self = [super init])) {
_decoder = decoder;
_requestedStartFrame = startFrame;
_requestedFrameLength = frameLength;
_repeatCount = repeatCount;
_completedLoops = 0;
_startFrame = SFBUnknownFramePosition;
_frameLength = SFBUnknownFramePosition;
}
return self;
}
- (SFBInputSource *)inputSource {
return _decoder.inputSource;
}
- (AVAudioFormat *)processingFormat {
return _decoder.processingFormat;
}
- (AVAudioFormat *)sourceFormat {
return _decoder.sourceFormat;
}
- (BOOL)decodingIsLossless {
return _decoder.decodingIsLossless;
}
- (NSDictionary *)properties {
return _decoder.properties;
}
- (BOOL)openReturningError:(NSError **)error {
if (!_decoder.isOpen && ![_decoder openReturningError:error]) {
return NO;
}
if (!_decoder.supportsSeeking) {
os_log_error(gSFBAudioDecoderLog, "Cannot open AudioRegionDecoder with non-seekable decoder %{public}@",
_decoder);
[_decoder closeReturningError:nil];
if (error) {
*error = [NSError errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInternalError
userInfo:nil];
}
return NO;
}
AVAudioFramePosition decoderFrameLength = _decoder.frameLength;
if (decoderFrameLength == SFBUnknownFrameLength) {
os_log_error(gSFBAudioDecoderLog, "Invalid frame length from %{public}@", _decoder);
[_decoder closeReturningError:nil];
if (error) {
*error = [NSError errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInternalError
userInfo:nil];
}
return NO;
}
if (_requestedStartFrame == -1) {
if (_requestedFrameLength > decoderFrameLength) {
os_log_error(gSFBAudioDecoderLog, "Invalid requested audio region frame length %lld",
_requestedFrameLength);
[_decoder closeReturningError:nil];
if (error) {
*error = [NSError errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInternalError
userInfo:nil];
}
return NO;
}
_startFrame = decoderFrameLength - _requestedFrameLength;
} else {
if (_requestedStartFrame >= decoderFrameLength) {
os_log_error(gSFBAudioDecoderLog, "Invalid requested audio region start frame %lld", _requestedStartFrame);
[_decoder closeReturningError:nil];
if (error) {
*error = [NSError errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInternalError
userInfo:nil];
}
return NO;
}
_startFrame = _requestedStartFrame;
}
if (_decoder.framePosition != _startFrame && ![_decoder seekToFrame:_startFrame error:error]) {
[_decoder closeReturningError:nil];
return NO;
}
if (_requestedFrameLength == -1) {
_frameLength = decoderFrameLength - _startFrame;
} else {
if (_requestedFrameLength > decoderFrameLength - _startFrame) {
os_log_error(gSFBAudioDecoderLog, "Invalid requested audio region frame length %lld",
_requestedFrameLength);
[_decoder closeReturningError:nil];
if (error) {
*error = [NSError errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInternalError
userInfo:nil];
}
return NO;
}
_frameLength = _requestedFrameLength;
}
_buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:_decoder.processingFormat frameCapacity:512];
_completedLoops = 0;
return YES;
}
- (BOOL)closeReturningError:(NSError **)error {
_buffer = nil;
return [_decoder closeReturningError:error];
}
- (BOOL)isOpen {
return _buffer != nil;
}
- (AVAudioFramePosition)framePosition {
AVAudioFramePosition framePosition = _decoder.framePosition;
if (framePosition == SFBUnknownFramePosition) {
return SFBUnknownFramePosition;
}
return (framePosition - _startFrame) + (_frameLength * _completedLoops);
}
- (AVAudioFramePosition)frameLength {
if (_repeatCount == -1) {
return INT64_MAX;
}
return _frameLength + (_frameLength * _repeatCount);
}
- (AVAudioFramePosition)frameOffset {
AVAudioFramePosition framePosition = _decoder.framePosition;
if (framePosition == SFBUnknownFramePosition) {
return SFBUnknownFramePosition;
}
return framePosition - _startFrame;
}
- (BOOL)decodeIntoBuffer:(AVAudioBuffer *)buffer error:(NSError **)error {
NSParameterAssert(buffer != nil);
NSParameterAssert([buffer isKindOfClass:[AVAudioPCMBuffer class]]);
return [self decodeIntoBuffer:(AVAudioPCMBuffer *)buffer
frameLength:((AVAudioPCMBuffer *)buffer).frameCapacity
error:error];
}
- (BOOL)decodeIntoBuffer:(AVAudioPCMBuffer *)buffer frameLength:(AVAudioFrameCount)frameLength error:(NSError **)error {
NSParameterAssert(buffer != nil);
NSParameterAssert([buffer.format isEqual:_decoder.processingFormat]);
// Reset output buffer data size
buffer.frameLength = 0;
if (frameLength == 0 || (_repeatCount != -1 && _completedLoops > _repeatCount)) {
return YES;
}
frameLength = MIN(frameLength, buffer.frameCapacity);
if (frameLength == 0) {
return YES;
}
AVAudioFrameCount framesRemaining = frameLength;
while (framesRemaining > 0) {
AVAudioFrameCount framesRemainingInRegion =
(AVAudioFrameCount)(_startFrame + _frameLength - _decoder.framePosition);
AVAudioFrameCount framesToDecode = MIN(MIN(framesRemaining, framesRemainingInRegion), _buffer.frameCapacity);
// Nothing left to read
if (framesToDecode == 0) {
break;
}
// Zero the internal buffer in preparation for decoding
_buffer.frameLength = 0;
// Decode audio into our internal buffer and append it to output
if (![_decoder decodeIntoBuffer:_buffer frameLength:framesToDecode error:error]) {
return NO;
}
[buffer appendContentsOfBuffer:_buffer];
// Reached end of region, loop back to beginning
if (framesToDecode == framesRemainingInRegion) {
_completedLoops++;
if (_repeatCount == -1 || _completedLoops <= _repeatCount) {
if (![_decoder seekToFrame:_startFrame error:error]) {
return NO;
}
}
}
// Housekeeping
framesRemaining -= _buffer.frameLength;
}
return YES;
}
- (BOOL)supportsSeeking {
return _decoder.supportsSeeking;
}
- (BOOL)seekToFrame:(AVAudioFramePosition)frame error:(NSError **)error {
NSParameterAssert(frame >= 0);
if (frame >= self.frameLength) {
if (error) {
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:EINVAL userInfo:nil];
}
return NO;
}
static_assert(sizeof(long long) == sizeof _frameLength, "AVAudioFramePosition not long long");
lldiv_t qr = lldiv(frame, _frameLength);
if (![_decoder seekToFrame:(_startFrame + qr.rem) error:error]) {
return NO;
}
_completedLoops = qr.quot;
return YES;
}
- (NSString *)description {
return [NSString
stringWithFormat:@"<%@ %p: _decoder = %@, _startFrame = %lld, _frameLength = %lld, _repeatCount = %ld>",
[self class], (__bridge void *)self, _decoder, _startFrame, _frameLength,
(long)_repeatCount];
}
@end