-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathSFBMonkeysAudioEncoder.mm
374 lines (302 loc) · 11.2 KB
/
SFBMonkeysAudioEncoder.mm
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
//
// Copyright (c) 2020-2025 Stephen F. Booth <me@sbooth.org>
// Part of https://github.com/sbooth/SFBAudioEngine
// MIT license
//
#import <exception>
#import <memory>
#import <os/log.h>
#import <AudioToolbox/AudioToolbox.h>
#define PLATFORM_APPLE
#include <MAC/All.h>
#include <MAC/IO.h>
#include <MAC/MACLib.h>
#undef PLATFORM_APPLE
#import "SFBMonkeysAudioEncoder.h"
#import "SFBCStringForOSType.h"
SFBAudioEncoderName const SFBAudioEncoderNameMonkeysAudio = @"org.sbooth.AudioEngine.Encoder.MonkeysAudio";
SFBAudioEncodingSettingsKey const SFBAudioEncodingSettingsKeyAPECompressionLevel = @"Compression Level";
SFBAudioEncodingSettingsValueAPECompressionLevel const SFBAudioEncodingSettingsValueAPECompressionLevelFast = @"Fast";
SFBAudioEncodingSettingsValueAPECompressionLevel const SFBAudioEncodingSettingsValueAPECompressionLevelNormal = @"Normal";
SFBAudioEncodingSettingsValueAPECompressionLevel const SFBAudioEncodingSettingsValueAPECompressionLevelHigh = @"High";
SFBAudioEncodingSettingsValueAPECompressionLevel const SFBAudioEncodingSettingsValueAPECompressionLevelExtraHigh = @"Extra High";
SFBAudioEncodingSettingsValueAPECompressionLevel const SFBAudioEncodingSettingsValueAPECompressionLevelInsane = @"Insane";
namespace {
// The I/O interface for MAC
class APEIOInterface final : public APE::CIO
{
public:
explicit APEIOInterface(SFBOutputSource *outputSource)
: mOutputSource(outputSource)
{}
int Open(const wchar_t * pName, bool bOpenReadOnly) override
{
#pragma unused(pName)
#pragma unused(bOpenReadOnly)
return ERROR_INVALID_INPUT_FILE;
}
int Close() override
{
return ERROR_SUCCESS;
}
int Read(void * pBuffer, unsigned int nBytesToRead, unsigned int * pBytesRead) override
{
NSInteger bytesRead;
if(![mOutputSource readBytes:pBuffer length:nBytesToRead bytesRead:&bytesRead error:nil])
return ERROR_IO_READ;
*pBytesRead = static_cast<unsigned int>(bytesRead);
return ERROR_SUCCESS;
}
int Write(const void * pBuffer, unsigned int nBytesToWrite, unsigned int * pBytesWritten) override
{
NSInteger bytesWritten;
if(![mOutputSource writeBytes:pBuffer length:(NSInteger)nBytesToWrite bytesWritten:&bytesWritten error:nil] || bytesWritten != nBytesToWrite)
return ERROR_IO_WRITE;
*pBytesWritten = static_cast<unsigned int>(bytesWritten);
return ERROR_SUCCESS;
}
int Seek(APE::int64 nPosition, APE::SeekMethod nMethod) override
{
if(!mOutputSource.supportsSeeking)
return ERROR_IO_READ;
NSInteger offset = nPosition;
switch(nMethod) {
case APE::SeekFileBegin:
// offset remains unchanged
break;
case APE::SeekFileCurrent: {
NSInteger inputSourceOffset;
if([mOutputSource getOffset:&inputSourceOffset error:nil])
offset += inputSourceOffset;
break;
}
case APE::SeekFileEnd: {
NSInteger inputSourceLength;
if([mOutputSource getLength:&inputSourceLength error:nil])
offset += inputSourceLength;
break;
}
}
return ![mOutputSource seekToOffset:offset error:nil];
}
int Create(const wchar_t * pName) override
{
#pragma unused(pName)
return ERROR_IO_WRITE;
}
int Delete() override
{
return ERROR_IO_WRITE;
}
int SetEOF() override
{
return ERROR_IO_WRITE;
}
unsigned char * GetBuffer(int * pnBufferBytes) override
{
#pragma unused(pnBufferBytes)
return nullptr;
}
APE::int64 GetPosition() override
{
NSInteger offset;
if(![mOutputSource getOffset:&offset error:nil])
return -1;
return offset;
}
APE::int64 GetSize() override
{
NSInteger length;
if(![mOutputSource getLength:&length error:nil])
return -1;
return length;
}
int GetName(wchar_t * pBuffer) override
{
#pragma unused(pBuffer)
return ERROR_SUCCESS;
}
private:
SFBOutputSource *mOutputSource;
};
} /* namespace */
@interface SFBMonkeysAudioEncoder ()
{
@private
std::unique_ptr<APEIOInterface> _ioInterface;
std::unique_ptr<APE::IAPECompress> _compressor;
AVAudioFramePosition _framePosition;
}
@end
@implementation SFBMonkeysAudioEncoder
+ (void)load
{
[SFBAudioEncoder registerSubclass:[self class]];
}
+ (NSSet *)supportedPathExtensions
{
return [NSSet setWithObject:@"ape"];
}
+ (NSSet *)supportedMIMETypes
{
return [NSSet setWithArray:@[@"audio/monkeys-audio", @"audio/x-monkeys-audio"]];
}
+ (SFBAudioEncoderName)encoderName
{
return SFBAudioEncoderNameMonkeysAudio;
}
- (BOOL)encodingIsLossless
{
return YES;
}
- (AVAudioFormat *)processingFormatForSourceFormat:(AVAudioFormat *)sourceFormat
{
NSParameterAssert(sourceFormat != nil);
// Validate format
if(sourceFormat.streamDescription->mFormatFlags & kAudioFormatFlagIsFloat || sourceFormat.channelCount < 1 || sourceFormat.channelCount > 32)
return nil;
APE::WAVEFORMATEX wve;
auto result = FillWaveFormatEx(&wve, WAVE_FORMAT_PCM, static_cast<int>(sourceFormat.sampleRate), static_cast<int>(sourceFormat.streamDescription->mBitsPerChannel), static_cast<int>(sourceFormat.channelCount));
if(result != ERROR_SUCCESS) {
os_log_error(gSFBAudioEncoderLog, "FillWaveFormatEx() failed: %d", result);
return nil;
}
// Set up the processing format
AudioStreamBasicDescription streamDescription{};
streamDescription.mFormatID = kAudioFormatLinearPCM;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-anon-enum-enum-conversion"
streamDescription.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
#pragma clang diagnostic pop
streamDescription.mSampleRate = wve.nSamplesPerSec;
streamDescription.mChannelsPerFrame = wve.nChannels;
streamDescription.mBitsPerChannel = wve.wBitsPerSample;
streamDescription.mBytesPerPacket = ((wve.wBitsPerSample + 7) / 8) * streamDescription.mChannelsPerFrame;
streamDescription.mFramesPerPacket = 1;
streamDescription.mBytesPerFrame = streamDescription.mBytesPerPacket / streamDescription.mFramesPerPacket;
// Use WAVFORMATEX channel order
AVAudioChannelLayout *channelLayout = nil;
if(sourceFormat.channelLayout != nil) {
AudioChannelBitmap channelBitmap = 0;
UInt32 propertySize = sizeof(channelBitmap);
AudioChannelLayoutTag layoutTag = sourceFormat.channelLayout.layoutTag;
OSStatus result = AudioFormatGetProperty(kAudioFormatProperty_BitmapForLayoutTag, sizeof(layoutTag), &layoutTag, &propertySize, &channelBitmap);
if(result == noErr) {
AudioChannelLayout acl = {
.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelBitmap,
.mChannelBitmap = channelBitmap,
.mNumberChannelDescriptions = 0
};
channelLayout = [[AVAudioChannelLayout alloc] initWithLayout:&acl];
}
else
os_log_info(gSFBAudioEncoderLog, "AudioFormatGetProperty(kAudioFormatProperty_BitmapForLayoutTag), layoutTag = %d failed: %d '%{public}.4s'", layoutTag, result, SFBCStringForOSType(result));
}
return [[AVAudioFormat alloc] initWithStreamDescription:&streamDescription channelLayout:channelLayout];
}
- (BOOL)openReturningError:(NSError **)error
{
if(![super openReturningError:error])
return NO;
try {
int result;
auto compressor = CreateIAPECompress(&result);
if(!compressor) {
os_log_error(gSFBAudioEncoderLog, "CreateIAPECompress() failed: %d", result);
if(error)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:ENOMEM userInfo:nil];
return NO;
}
_compressor = std::unique_ptr<APE::IAPECompress>(compressor);
_ioInterface = std::make_unique<APEIOInterface>(_outputSource);
}
catch(const std::exception& e) {
os_log_error(gSFBAudioEncoderLog, "Error creating Monkey's Audio encoder: %{public}s", e.what());
if(error)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:ENOMEM userInfo:nil];
return NO;
}
int compressionLevel = APE_COMPRESSION_LEVEL_NORMAL;
SFBAudioEncodingSettingsValue level = [_settings objectForKey:SFBAudioEncodingSettingsKeyAPECompressionLevel];
if(level != nil) {
if(level == SFBAudioEncodingSettingsValueAPECompressionLevelFast)
compressionLevel = APE_COMPRESSION_LEVEL_FAST;
else if(level == SFBAudioEncodingSettingsValueAPECompressionLevelNormal)
compressionLevel = APE_COMPRESSION_LEVEL_NORMAL;
else if(level == SFBAudioEncodingSettingsValueAPECompressionLevelHigh)
compressionLevel = APE_COMPRESSION_LEVEL_HIGH;
else if(level == SFBAudioEncodingSettingsValueAPECompressionLevelExtraHigh)
compressionLevel = APE_COMPRESSION_LEVEL_EXTRA_HIGH;
else if(level == SFBAudioEncodingSettingsValueAPECompressionLevelInsane)
compressionLevel = APE_COMPRESSION_LEVEL_INSANE;
else
os_log_info(gSFBAudioEncoderLog, "Ignoring unknown APE compression level: %{public}@", level);
}
APE::WAVEFORMATEX wve;
auto result = FillWaveFormatEx(&wve, WAVE_FORMAT_PCM, static_cast<int>(_sourceFormat.sampleRate), static_cast<int>(_sourceFormat.streamDescription->mBitsPerChannel), static_cast<int>(_sourceFormat.channelCount));
if(result != ERROR_SUCCESS) {
os_log_error(gSFBAudioEncoderLog, "FillWaveFormatEx() failed: %d", result);
if(error)
*error = [NSError errorWithDomain:SFBAudioEncoderErrorDomain code:SFBAudioEncoderErrorCodeInvalidFormat userInfo:nil];
return NO;
}
result = _compressor->StartEx(_ioInterface.get(), &wve, MAX_AUDIO_BYTES_UNKNOWN, compressionLevel);
if(result != ERROR_SUCCESS) {
os_log_error(gSFBAudioEncoderLog, "_compressor->StartEx() failed: %d", result);
if(error)
*error = [NSError errorWithDomain:SFBAudioEncoderErrorDomain code:SFBAudioEncoderErrorCodeInvalidFormat userInfo:nil];
return NO;
}
AudioStreamBasicDescription outputStreamDescription{};
outputStreamDescription.mFormatID = kSFBAudioFormatMonkeysAudio;
outputStreamDescription.mBitsPerChannel = wve.wBitsPerSample;
outputStreamDescription.mSampleRate = wve.nSamplesPerSec;
outputStreamDescription.mChannelsPerFrame = wve.nChannels;
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
return YES;
}
- (BOOL)closeReturningError:(NSError **)error
{
_ioInterface.reset();
_compressor.reset();
return [super closeReturningError:error];
}
- (BOOL)isOpen
{
return _compressor != nullptr;
}
- (AVAudioFramePosition)framePosition
{
return _framePosition;
}
- (BOOL)encodeFromBuffer:(AVAudioPCMBuffer *)buffer frameLength:(AVAudioFrameCount)frameLength error:(NSError **)error
{
NSParameterAssert(buffer != nil);
NSParameterAssert([buffer.format isEqual:_processingFormat]);
if(frameLength > buffer.frameLength)
frameLength = buffer.frameLength;
if(frameLength == 0)
return YES;
auto bytesToWrite = frameLength * _processingFormat.streamDescription->mBytesPerFrame;
auto result = _compressor->AddData((unsigned char *)buffer.audioBufferList->mBuffers[0].mData, bytesToWrite);
if(result != ERROR_SUCCESS) {
os_log_error(gSFBAudioEncoderLog, "_compressor->AddData() failed: %lld", result);
if(error)
*error = [NSError errorWithDomain:SFBAudioEncoderErrorDomain code:SFBAudioEncoderErrorCodeInternalError userInfo:nil];
return NO;
}
_framePosition += frameLength;
return YES;
}
- (BOOL)finishEncodingReturningError:(NSError **)error
{
auto result = _compressor->Finish(nullptr, 0, 0);
if(result != ERROR_SUCCESS) {
os_log_error(gSFBAudioEncoderLog, "_compressor->Finish() failed: %d", result);
if(error)
*error = [NSError errorWithDomain:SFBAudioEncoderErrorDomain code:SFBAudioEncoderErrorCodeInternalError userInfo:nil];
return NO;
}
return YES;
}
@end