-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathSFBModuleDecoder.m
336 lines (257 loc) · 8.27 KB
/
SFBModuleDecoder.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
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
//
// Copyright (c) 2011-2025 Stephen F. Booth <[email protected]>
// Part of https://github.com/sbooth/SFBAudioEngine
// MIT license
//
@import os.log;
@import dumb;
#import "SFBModuleDecoder.h"
#import "NSError+SFBURLPresentation.h"
SFBAudioDecoderName const SFBAudioDecoderNameModule = @"org.sbooth.AudioEngine.Decoder.Module";
#define DUMB_SAMPLE_RATE 65536
#define DUMB_CHANNELS 2
#define DUMB_BIT_DEPTH 16
#define DUMB_BUF_FRAMES 512
static int skip_callback(void *f, dumb_off_t n)
{
NSCParameterAssert(f != NULL);
SFBModuleDecoder *decoder = (__bridge SFBModuleDecoder *)f;
NSInteger offset;
if(![decoder->_inputSource getOffset:&offset error:nil])
return 1;
return ![decoder->_inputSource seekToOffset:(offset + n) error:nil];
}
static int getc_callback(void *f)
{
NSCParameterAssert(f != NULL);
SFBModuleDecoder *decoder = (__bridge SFBModuleDecoder *)f;
uint8_t value;
if(![decoder->_inputSource readUInt8:&value error:nil])
return -1;
return (int)value;
}
static dumb_ssize_t getnc_callback(char *ptr, size_t n, void *f)
{
NSCParameterAssert(f != NULL);
SFBModuleDecoder *decoder = (__bridge SFBModuleDecoder *)f;
NSInteger bytesRead;
if(![decoder->_inputSource readBytes:ptr length:(NSInteger)n bytesRead:&bytesRead error:nil])
return -1;
return bytesRead;
}
static void close_callback(void *f)
{
#pragma unused(f)
}
static int seek_callback(void *f, dumb_off_t offset)
{
NSCParameterAssert(f != NULL);
SFBModuleDecoder *decoder = (__bridge SFBModuleDecoder *)f;
if(![decoder->_inputSource seekToOffset:offset error:nil])
return -1;
return 0;
}
static dumb_off_t get_size_callback(void *f)
{
NSCParameterAssert(f != NULL);
SFBModuleDecoder *decoder = (__bridge SFBModuleDecoder *)f;
NSInteger length;
if(![decoder->_inputSource getLength:&length error:nil])
return -1;
return length;
}
@interface SFBModuleDecoder ()
{
@private
DUMBFILE_SYSTEM _dfs;
DUMBFILE *_df;
DUH *_duh;
DUH_SIGRENDERER *_dsr;
sample_t **_samples;
AVAudioFramePosition _framePosition;
AVAudioFramePosition _frameLength;
}
- (BOOL)openDecoderReturningError:(NSError **)error;
- (void)closeDecoder;
@end
@implementation SFBModuleDecoder
+ (void)load
{
[SFBAudioDecoder registerSubclass:[self class]];
}
+ (NSSet *)supportedPathExtensions
{
return [NSSet setWithArray:@[@"it", @"xm", @"s3m", @"stm", @"mod", @"ptm", @"669", @"psm", @"mtm", /*@"riff",*/ @"asy", @"amf", @"okt"]];
}
+ (NSSet *)supportedMIMETypes
{
// FIXME: Add additional MIME types?
return [NSSet setWithArray:@[@"audio/it", @"audio/xm", @"audio/s3m", @"audio/mod", @"audio/x-mod"]];
}
+ (SFBAudioDecoderName)decoderName
{
return SFBAudioDecoderNameModule;
}
+ (BOOL)testInputSource:(SFBInputSource *)inputSource formatIsSupported:(SFBTernaryTruthValue *)formatIsSupported error:(NSError **)error
{
NSParameterAssert(inputSource != nil);
NSParameterAssert(formatIsSupported != NULL);
*formatIsSupported = SFBTernaryTruthValueUnknown;
return YES;
}
- (BOOL)decodingIsLossless
{
return NO;
}
- (BOOL)openReturningError:(NSError **)error
{
if(![super openReturningError:error])
return NO;
// Generate interleaved 2-channel 16-bit output
// For mono and stereo the channel layout is assumed
_processingFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:DUMB_SAMPLE_RATE channels:DUMB_CHANNELS interleaved:YES];
// Set up the source format
AudioStreamBasicDescription sourceStreamDescription = {0};
sourceStreamDescription.mFormatID = kSFBAudioFormatModule;
sourceStreamDescription.mSampleRate = DUMB_SAMPLE_RATE;
sourceStreamDescription.mChannelsPerFrame = DUMB_CHANNELS;
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
return [self openDecoderReturningError:error];
}
- (BOOL)closeReturningError:(NSError **)error
{
[self closeDecoder];
return [super closeReturningError:error];
}
- (BOOL)isOpen
{
return _df != NULL && _duh != NULL && _dsr != NULL;
}
- (AVAudioFramePosition)framePosition
{
return _framePosition;
}
- (AVAudioFramePosition)frameLength
{
return _frameLength;
}
- (BOOL)decodeIntoBuffer:(AVAudioPCMBuffer *)buffer frameLength:(AVAudioFrameCount)frameLength error:(NSError **)error
{
NSParameterAssert(buffer != nil);
NSParameterAssert([buffer.format isEqual:_processingFormat]);
// Reset output buffer data size
buffer.frameLength = 0;
if(frameLength > buffer.frameCapacity)
frameLength = buffer.frameCapacity;
if(frameLength == 0)
return YES;
AVAudioFrameCount framesProcessed = 0;
for(;;) {
AVAudioFrameCount framesRemaining = frameLength - framesProcessed;
AVAudioFrameCount framesToCopy = MIN(framesRemaining, DUMB_BUF_FRAMES);
long samplesSize = framesToCopy;
long framesCopied = duh_render_int(_dsr, &_samples, &samplesSize, DUMB_BIT_DEPTH, 0, 1, 65536.0f / DUMB_SAMPLE_RATE, framesToCopy, buffer.int16ChannelData[0] + (framesProcessed * DUMB_CHANNELS));
framesProcessed += framesCopied;
// All requested frames were read or EOS reached
if(framesProcessed == frameLength || framesCopied == 0)
break;
}
_framePosition += framesProcessed;
buffer.frameLength = (AVAudioFrameCount)framesProcessed;
return YES;
}
- (BOOL)seekToFrame:(AVAudioFramePosition)frame error:(NSError **)error
{
NSParameterAssert(frame >= 0);
// DUMB cannot seek backwards, so the decoder must be reset
if(frame < _framePosition) {
[self closeDecoder];
if(![_inputSource seekToOffset:0 error:error] || ![self openDecoderReturningError:error])
return NO;
_framePosition = 0;
}
AVAudioFramePosition framesToSkip = frame - _framePosition;
duh_sigrenderer_generate_samples(_dsr, 1, 65536.0f / DUMB_SAMPLE_RATE, framesToSkip, NULL);
_framePosition += framesToSkip;
return YES;
}
- (BOOL)openDecoderReturningError:(NSError **)error
{
_dfs.open = NULL;
_dfs.skip = skip_callback;
_dfs.getc = getc_callback;
_dfs.getnc = getnc_callback;
_dfs.close = close_callback;
_dfs.seek = seek_callback;
_dfs.get_size = get_size_callback;
_df = dumbfile_open_ex((__bridge void *)self, &_dfs);
if(!_df) {
os_log_error(gSFBAudioDecoderLog, "dumbfile_open_ex failed");
if(error)
*error = [NSError errorWithDomain:SFBAudioDecoderErrorDomain code:SFBAudioDecoderErrorCodeInternalError userInfo:@{ NSURLErrorKey: _inputSource.url }];
return NO;
}
_duh = dumb_read_any(_df, 0, 0);
if(!_duh) {
dumbfile_close(_df);
_df = NULL;
if(error)
*error = [NSError SFB_errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInvalidFormat
descriptionFormatStringForURL:NSLocalizedString(@"The file “%@” is not a valid Module file.", @"")
url:_inputSource.url
failureReason:NSLocalizedString(@"Not a Module file", @"")
recoverySuggestion:NSLocalizedString(@"The file's extension may not match the file's type.", @"")];
return NO;
}
// NB: This must change if the sample rate changes because it is based on 65536 Hz
_frameLength = duh_get_length(_duh);
// Generate 2-channel audio
_dsr = duh_start_sigrenderer(_duh, 0, DUMB_CHANNELS, 0);
if(!_dsr) {
os_log_error(gSFBAudioDecoderLog, "duh_start_sigrenderer failed");
unload_duh(_duh);
_duh = NULL;
dumbfile_close(_df);
_df = NULL;
if(error)
*error = [NSError SFB_errorWithDomain:SFBAudioDecoderErrorDomain
code:SFBAudioDecoderErrorCodeInvalidFormat
descriptionFormatStringForURL:NSLocalizedString(@"The file “%@” is not a valid Module file.", @"")
url:_inputSource.url
failureReason:NSLocalizedString(@"Not a Module file", @"")
recoverySuggestion:NSLocalizedString(@"The file's extension may not match the file's type.", @"")];
return NO;
}
// Stop producing samples on module end
DUMB_IT_SIGRENDERER *itsr = duh_get_it_sigrenderer(_dsr);
dumb_it_set_loop_callback(itsr, &dumb_it_callback_terminate, NULL);
dumb_it_set_xm_speed_zero_callback(itsr, &dumb_it_callback_terminate, NULL);
_samples = allocate_sample_buffer(DUMB_CHANNELS, DUMB_BUF_FRAMES);
if(!_samples) {
if(error)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:ENOMEM userInfo:nil];
return NO;
}
return YES;
}
- (void)closeDecoder
{
if(_dsr) {
duh_end_sigrenderer(_dsr);
_dsr = NULL;
}
if(_duh) {
unload_duh(_duh);
_duh = NULL;
}
if(_df) {
dumbfile_close(_df);
_df = NULL;
}
if(_samples) {
destroy_sample_buffer(_samples);
_samples = NULL;
}
}
@end