forked from sbooth/SFBAudioEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFBLoopableRegionDecoder.m
241 lines (186 loc) · 6.89 KB
/
SFBLoopableRegionDecoder.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
237
238
239
240
241
//
// Copyright (c) 2006-2024 Stephen F. Booth <[email protected]>
// Part of https://github.com/sbooth/SFBAudioEngine
// MIT license
//
@import os.log;
@import AVFAudioExtensions;
#import "SFBLoopableRegionDecoder.h"
#import "SFBAudioDecoder+Internal.h"
@interface SFBLoopableRegionDecoder ()
{
@private
id <SFBPCMDecoding> _decoder;
AVAudioPCMBuffer *_buffer;
AVAudioFramePosition _framePosition;
AVAudioFramePosition _frameLength;
NSInteger _repeatCount;
AVAudioFramePosition _framesDecoded;
}
- (BOOL)resetReturningError:(NSError **)error;
- (BOOL)setupDecoderForcingReset:(BOOL)forceReset error:(NSError **)error;
@end
@implementation SFBLoopableRegionDecoder
- (instancetype)initWithURL:(NSURL *)url framePosition:(AVAudioFramePosition)framePosition frameLength:(AVAudioFramePosition)frameLength error:(NSError **)error
{
return [self initWithURL:url framePosition:framePosition frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithURL:(NSURL *)url framePosition:(AVAudioFramePosition)framePosition frameLength:(AVAudioFramePosition)frameLength repeatCount:(NSInteger)repeatCount error:(NSError **)error
{
NSParameterAssert(url != nil);
SFBInputSource *inputSource = [SFBInputSource inputSourceForURL:url flags:0 error:error];
if(!inputSource || !inputSource.supportsSeeking)
return nil;
return [self initWithInputSource:inputSource framePosition:framePosition frameLength:frameLength repeatCount:repeatCount error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource framePosition:(AVAudioFramePosition)framePosition frameLength:(AVAudioFramePosition)frameLength error:(NSError **)error
{
return [self initWithInputSource:inputSource framePosition:framePosition frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithInputSource:(SFBInputSource *)inputSource framePosition:(AVAudioFramePosition)framePosition frameLength:(AVAudioFramePosition)frameLength repeatCount:(NSInteger)repeatCount error:(NSError **)error
{
NSParameterAssert(inputSource != nil);
SFBAudioDecoder *decoder = [[SFBAudioDecoder alloc] initWithInputSource:inputSource error:error];
if(!decoder || !decoder.supportsSeeking)
return nil;
return [self initWithDecoder:decoder framePosition:framePosition frameLength:frameLength repeatCount:repeatCount error:error];
}
- (instancetype)initWithDecoder:(id <SFBPCMDecoding>)decoder framePosition:(AVAudioFramePosition)framePosition frameLength:(AVAudioFramePosition)frameLength error:(NSError **)error
{
return [self initWithDecoder:decoder framePosition:framePosition frameLength:frameLength repeatCount:0 error:error];
}
- (instancetype)initWithDecoder:(id <SFBPCMDecoding>)decoder framePosition:(AVAudioFramePosition)framePosition frameLength:(AVAudioFramePosition)frameLength repeatCount:(NSInteger)repeatCount error:(NSError **)error
{
NSParameterAssert(decoder != nil);
NSParameterAssert(framePosition >= 0);
NSParameterAssert(frameLength >= 0);
NSParameterAssert(repeatCount >= 0);
if((self = [super init])) {
_decoder = decoder;
_framePosition = framePosition;
_frameLength = frameLength;
_repeatCount = repeatCount;
}
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 || ![self setupDecoderForcingReset:NO error:error]) {
[_decoder closeReturningError:error];
return NO;
}
_buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:_decoder.processingFormat frameCapacity:512];
return YES;
}
- (BOOL)closeReturningError:(NSError **)error
{
_buffer = nil;
return [_decoder closeReturningError:error];
}
- (BOOL)isOpen
{
return _buffer != nil;
}
- (AVAudioFramePosition)framePosition
{
return _framesDecoded;
}
- (AVAudioFramePosition)frameLength
{
return _frameLength * (_repeatCount + 1);
}
- (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((_repeatCount && (_framesDecoded / _frameLength) == (_repeatCount + 1)) || frameLength == 0)
return YES;
if(frameLength > buffer.frameCapacity)
frameLength = buffer.frameCapacity;
AVAudioFrameCount framesRemaining = frameLength;
_buffer.frameLength = 0;
while(framesRemaining > 0) {
AVAudioFrameCount framesRemainingInCurrentPass = (AVAudioFrameCount)(_framePosition + _frameLength - _decoder.framePosition);
AVAudioFrameCount framesToDecode = MIN(MIN(framesRemaining, framesRemainingInCurrentPass), _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];
// Housekeeping
_framesDecoded += _buffer.frameLength;
framesRemaining -= _buffer.frameLength;
// If this pass is finished, seek to the beginning of the region in preparation for the next read
if(_repeatCount && (_framesDecoded % _frameLength) == 0) {
// Only seek to the beginning of the region if more passes remain
if((_framesDecoded / _frameLength) < (_repeatCount + 1)) {
if(![_decoder seekToFrame:_framePosition error:error])
return NO;
}
}
}
return YES;
}
- (BOOL)supportsSeeking
{
return _decoder.supportsSeeking;
}
- (BOOL)seekToFrame:(AVAudioFramePosition)frame error:(NSError **)error
{
NSParameterAssert(frame >= 0);
if(frame >= self.frameLength)
return NO;
_framesDecoded = frame;
return [_decoder seekToFrame:(_framePosition + (frame % _frameLength)) error:error];
}
- (BOOL)resetReturningError:(NSError **)error
{
_framesDecoded = 0;
if(_framePosition == _decoder.framePosition)
return YES;
if(![_decoder seekToFrame:_framePosition error:error])
return NO;
return _framePosition == _decoder.framePosition;
}
- (BOOL)setupDecoderForcingReset:(BOOL)forceReset error:(NSError **)error
{
if(_frameLength == 0)
_frameLength = _decoder.frameLength - _framePosition;
if(forceReset || _framePosition != 0)
return [self resetReturningError:error];
return YES;
}
@end