2016-05-25 02:00:22 +00:00
|
|
|
#include "AudioUnitBackend.hpp"
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <Availability.h>
|
|
|
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
|
|
|
|
#import <AudioUnit/AudioUnit.h>
|
|
|
|
#import <CoreAudioKit/CoreAudioKit.h>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
#error ARC Required
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "logvisor/logvisor.hpp"
|
|
|
|
#include "audiodev/AudioVoiceEngine.hpp"
|
2016-06-07 03:42:51 +00:00
|
|
|
#import "AudioUnitViewController.hpp"
|
2016-05-25 02:00:22 +00:00
|
|
|
|
|
|
|
static logvisor::Module Log("amuse::AudioUnitBackend");
|
|
|
|
|
|
|
|
struct AudioUnitVoiceEngine : boo::BaseAudioVoiceEngine
|
|
|
|
{
|
2016-06-08 04:33:15 +00:00
|
|
|
AudioGroupToken* m_reqGroup = nullptr;
|
|
|
|
AudioGroupToken* m_curGroup = nullptr;
|
2016-06-07 03:42:51 +00:00
|
|
|
std::vector<float> m_interleavedBuf;
|
2016-05-25 02:00:22 +00:00
|
|
|
std::vector<std::unique_ptr<float[]>> m_renderBufs;
|
2016-06-07 03:42:51 +00:00
|
|
|
size_t m_renderFrames = 0;
|
2016-05-26 01:28:50 +00:00
|
|
|
AudioBufferList* m_outputData = nullptr;
|
2016-05-25 02:00:22 +00:00
|
|
|
|
|
|
|
boo::AudioChannelSet _getAvailableSet()
|
|
|
|
{
|
|
|
|
return boo::AudioChannelSet::Stereo;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> enumerateMIDIDevices() const
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2016-06-07 03:42:51 +00:00
|
|
|
boo::ReceiveFunctor* m_midiReceiver = nullptr;
|
2016-05-25 02:00:22 +00:00
|
|
|
|
2016-06-07 03:42:51 +00:00
|
|
|
struct MIDIIn : public boo::IMIDIIn
|
|
|
|
{
|
|
|
|
MIDIIn(bool virt, boo::ReceiveFunctor&& receiver)
|
|
|
|
: IMIDIIn(virt, std::move(receiver)) {}
|
|
|
|
|
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
return "AudioUnit MIDI";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
std::unique_ptr<boo::IMIDIIn> newVirtualMIDIIn(boo::ReceiveFunctor&& receiver)
|
|
|
|
{
|
2016-06-07 03:42:51 +00:00
|
|
|
std::unique_ptr<boo::IMIDIIn> ret = std::make_unique<MIDIIn>(true, std::move(receiver));
|
|
|
|
m_midiReceiver = &ret->m_receiver;
|
|
|
|
return ret;
|
2016-05-25 02:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<boo::IMIDIOut> newVirtualMIDIOut()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<boo::IMIDIInOut> newVirtualMIDIInOut(boo::ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<boo::IMIDIIn> newRealMIDIIn(const char* name, boo::ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<boo::IMIDIOut> newRealMIDIOut(const char* name)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<boo::IMIDIInOut> newRealMIDIInOut(const char* name, boo::ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2016-06-07 03:42:51 +00:00
|
|
|
|
|
|
|
bool useMIDILock() const {return false;}
|
2016-05-25 02:00:22 +00:00
|
|
|
|
|
|
|
AudioUnitVoiceEngine()
|
|
|
|
{
|
2016-06-07 03:42:51 +00:00
|
|
|
m_mixInfo.m_periodFrames = 512;
|
2016-05-25 02:00:22 +00:00
|
|
|
m_mixInfo.m_sampleRate = 96000.0;
|
|
|
|
m_mixInfo.m_sampleFormat = SOXR_FLOAT32_I;
|
|
|
|
m_mixInfo.m_bitsPerSample = 32;
|
2016-06-07 03:42:51 +00:00
|
|
|
_buildAudioRenderClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _buildAudioRenderClient()
|
|
|
|
{
|
|
|
|
m_mixInfo.m_channels = _getAvailableSet();
|
|
|
|
unsigned chCount = ChannelCount(m_mixInfo.m_channels);
|
|
|
|
|
|
|
|
m_5msFrames = m_mixInfo.m_sampleRate * 5 / 1000;
|
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
boo::ChannelMap& chMapOut = m_mixInfo.m_channelMap;
|
|
|
|
chMapOut.m_channelCount = 2;
|
|
|
|
chMapOut.m_channels[0] = boo::AudioChannel::FrontLeft;
|
|
|
|
chMapOut.m_channels[1] = boo::AudioChannel::FrontRight;
|
2016-06-07 03:42:51 +00:00
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
while (chMapOut.m_channelCount < chCount)
|
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = boo::AudioChannel::Unknown;
|
2016-06-07 03:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _rebuildAudioRenderClient(double sampleRate, size_t periodFrames)
|
|
|
|
{
|
|
|
|
m_mixInfo.m_periodFrames = periodFrames;
|
|
|
|
m_mixInfo.m_sampleRate = sampleRate;
|
|
|
|
_buildAudioRenderClient();
|
|
|
|
|
|
|
|
for (boo::AudioVoice* vox : m_activeVoices)
|
|
|
|
vox->_resetSampleRate(vox->m_sampleRateIn);
|
|
|
|
for (boo::AudioSubmix* smx : m_activeSubmixes)
|
|
|
|
smx->_resetOutputSampleRate();
|
2016-05-25 02:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pumpAndMixVoices()
|
|
|
|
{
|
2016-06-07 03:42:51 +00:00
|
|
|
_pumpAndMixVoices(m_renderFrames, m_interleavedBuf.data());
|
2016-05-26 01:28:50 +00:00
|
|
|
|
2016-06-07 03:42:51 +00:00
|
|
|
for (size_t i=0 ; i<m_renderBufs.size() ; ++i)
|
2016-05-26 01:28:50 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<float[]>& buf = m_renderBufs[i];
|
|
|
|
AudioBuffer& auBuf = m_outputData->mBuffers[i];
|
|
|
|
if (!auBuf.mData)
|
|
|
|
{
|
2016-06-07 03:42:51 +00:00
|
|
|
buf.reset(new float[auBuf.mDataByteSize / 4]);
|
2016-05-26 01:28:50 +00:00
|
|
|
auBuf.mData = buf.get();
|
|
|
|
}
|
2016-06-07 03:42:51 +00:00
|
|
|
for (size_t f=0 ; f<m_renderFrames ; ++f)
|
|
|
|
{
|
|
|
|
float* bufOut = reinterpret_cast<float*>(auBuf.mData);
|
|
|
|
bufOut[f] = m_interleavedBuf[f*2+i];
|
|
|
|
}
|
2016-05-26 01:28:50 +00:00
|
|
|
}
|
2016-05-25 02:00:22 +00:00
|
|
|
}
|
2016-06-08 04:33:15 +00:00
|
|
|
|
|
|
|
double getCurrentSampleRate() const {return m_mixInfo.m_sampleRate;}
|
2016-05-25 02:00:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@implementation AmuseAudioUnit
|
2016-06-07 03:42:51 +00:00
|
|
|
- (id)initWithComponentDescription:(AudioComponentDescription)componentDescription
|
|
|
|
error:(NSError * _Nullable *)outError
|
|
|
|
viewController:(AudioUnitViewController*)vc
|
|
|
|
{
|
|
|
|
m_viewController = vc;
|
|
|
|
vc->m_audioUnit = self;
|
|
|
|
self = [super initWithComponentDescription:componentDescription error:outError];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
- (id)initWithComponentDescription:(AudioComponentDescription)componentDescription
|
|
|
|
options:(AudioComponentInstantiationOptions)options
|
2016-06-07 03:42:51 +00:00
|
|
|
error:(NSError * _Nullable *)outError
|
2016-05-25 02:00:22 +00:00
|
|
|
{
|
|
|
|
self = [super initWithComponentDescription:componentDescription options:options error:outError];
|
|
|
|
if (!self)
|
|
|
|
return nil;
|
2016-06-07 03:42:51 +00:00
|
|
|
|
|
|
|
AVAudioFormat* format = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:96000.0 channels:2];
|
|
|
|
m_outBus = [[AUAudioUnitBus alloc] initWithFormat:format error:outError];
|
|
|
|
if (!m_outBus)
|
2016-05-25 02:00:22 +00:00
|
|
|
return nil;
|
2016-06-07 03:42:51 +00:00
|
|
|
//m_outBus.supportedChannelCounts = @[@1,@2];
|
|
|
|
m_outBus.maximumChannelCount = 2;
|
|
|
|
|
2016-06-08 04:33:15 +00:00
|
|
|
m_outs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self
|
|
|
|
busType:AUAudioUnitBusTypeOutput
|
|
|
|
busses:@[m_outBus]];
|
2016-05-25 02:00:22 +00:00
|
|
|
|
|
|
|
m_booBackend = std::make_unique<AudioUnitVoiceEngine>();
|
|
|
|
if (!m_booBackend)
|
|
|
|
{
|
|
|
|
*outError = [NSError errorWithDomain:@"amuse" code:-1
|
2016-06-07 03:42:51 +00:00
|
|
|
userInfo:@{NSLocalizedDescriptionKey:@"Unable to construct boo mixer"}];
|
2016-05-25 02:00:22 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-06-07 03:42:51 +00:00
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
m_voxAlloc.emplace(*m_booBackend);
|
|
|
|
m_engine.emplace(*m_voxAlloc);
|
2016-06-08 04:33:15 +00:00
|
|
|
dispatch_sync(dispatch_get_main_queue(),
|
|
|
|
^{
|
2016-06-07 03:42:51 +00:00
|
|
|
m_filePresenter = [[AudioGroupFilePresenter alloc] initWithAudioGroupClient:self];
|
|
|
|
});
|
|
|
|
|
|
|
|
self.maximumFramesToRender = 512;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:33:15 +00:00
|
|
|
- (void)requestAudioGroup:(AudioGroupToken*)group
|
|
|
|
{
|
|
|
|
AudioUnitVoiceEngine& voxEngine = static_cast<AudioUnitVoiceEngine&>(*m_booBackend);
|
|
|
|
voxEngine.m_reqGroup = group;
|
|
|
|
}
|
|
|
|
|
2016-06-07 03:42:51 +00:00
|
|
|
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError
|
|
|
|
{
|
|
|
|
if (![super allocateRenderResourcesAndReturnError:outError])
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
size_t chanCount = m_outBus.format.channelCount;
|
|
|
|
size_t renderFrames = self.maximumFramesToRender;
|
|
|
|
|
|
|
|
NSLog(@"Alloc Chans: %zu Frames: %zu SampRate: %f", chanCount, renderFrames, m_outBus.format.sampleRate);
|
|
|
|
|
|
|
|
AudioUnitVoiceEngine& voxEngine = static_cast<AudioUnitVoiceEngine&>(*m_booBackend);
|
|
|
|
voxEngine.m_renderFrames = renderFrames;
|
|
|
|
voxEngine.m_interleavedBuf.resize(renderFrames * std::max(2ul, chanCount));
|
|
|
|
voxEngine.m_renderBufs.resize(chanCount);
|
|
|
|
voxEngine._rebuildAudioRenderClient(m_outBus.format.sampleRate, renderFrames);
|
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
*outError = nil;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)deallocateRenderResources
|
|
|
|
{
|
|
|
|
[super deallocateRenderResources];
|
2016-06-07 03:42:51 +00:00
|
|
|
AudioUnitVoiceEngine& voxEngine = static_cast<AudioUnitVoiceEngine&>(*m_booBackend);
|
|
|
|
voxEngine.m_renderBufs.clear();
|
2016-05-25 02:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (AUAudioUnitBusArray*)outputBusses
|
|
|
|
{
|
|
|
|
return m_outs;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)musicDeviceOrEffect
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)virtualMIDICableCount
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (AUInternalRenderBlock)internalRenderBlock
|
|
|
|
{
|
2016-05-26 01:28:50 +00:00
|
|
|
__block AudioUnitVoiceEngine& voxEngine = static_cast<AudioUnitVoiceEngine&>(*m_booBackend);
|
|
|
|
__block amuse::Engine& amuseEngine = *m_engine;
|
2016-06-08 04:33:15 +00:00
|
|
|
__block std::shared_ptr<amuse::Sequencer> curSeq;
|
2016-05-25 02:00:22 +00:00
|
|
|
|
|
|
|
return ^AUAudioUnitStatus(AudioUnitRenderActionFlags* actionFlags, const AudioTimeStamp* timestamp,
|
|
|
|
AUAudioFrameCount frameCount, NSInteger outputBusNumber, AudioBufferList* outputData,
|
|
|
|
const AURenderEvent* realtimeEventListHead, AURenderPullInputBlock pullInputBlock)
|
|
|
|
{
|
2016-06-08 04:33:15 +00:00
|
|
|
/* Handle group load request */
|
|
|
|
AudioGroupToken* reqGroup = voxEngine.m_reqGroup;
|
|
|
|
if (voxEngine.m_curGroup != reqGroup)
|
|
|
|
{
|
|
|
|
voxEngine.m_curGroup = reqGroup;
|
|
|
|
if (reqGroup->m_song)
|
|
|
|
{
|
|
|
|
if (curSeq)
|
|
|
|
curSeq->kill();
|
|
|
|
curSeq = amuseEngine.seqPlay(reqGroup->m_id, -1, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 02:00:22 +00:00
|
|
|
/* Process MIDI events first */
|
|
|
|
if (voxEngine.m_midiReceiver)
|
|
|
|
{
|
|
|
|
for (const AUMIDIEvent* event = &realtimeEventListHead->MIDI ;
|
|
|
|
event != nullptr ; event = &event->next->MIDI)
|
|
|
|
{
|
|
|
|
if (event->eventType == AURenderEventMIDI)
|
|
|
|
{
|
2016-06-07 03:42:51 +00:00
|
|
|
(*voxEngine.m_midiReceiver)(std::vector<uint8_t>(std::cbegin(event->data),
|
2016-06-08 04:33:15 +00:00
|
|
|
std::cbegin(event->data) + event->length),
|
|
|
|
event->eventSampleTime / voxEngine.getCurrentSampleRate());
|
2016-05-25 02:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output buffers */
|
2016-06-07 03:42:51 +00:00
|
|
|
voxEngine.m_renderFrames = frameCount;
|
2016-05-26 01:28:50 +00:00
|
|
|
voxEngine.m_outputData = outputData;
|
|
|
|
amuseEngine.pumpEngine();
|
2016-05-25 02:00:22 +00:00
|
|
|
return noErr;
|
|
|
|
};
|
|
|
|
}
|
2016-06-07 03:42:51 +00:00
|
|
|
|
|
|
|
- (amuse::Engine&)getAmuseEngine
|
|
|
|
{
|
|
|
|
return *m_engine;
|
|
|
|
}
|
2016-05-25 02:00:22 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
|
|
|
|
|
|
|
void RegisterAudioUnit()
|
|
|
|
{
|
|
|
|
AudioComponentDescription desc = {};
|
|
|
|
desc.componentType = 'aumu';
|
|
|
|
desc.componentSubType = 'amus';
|
|
|
|
desc.componentManufacturer = 'AXDL';
|
|
|
|
[AUAudioUnit registerSubclass:[AmuseAudioUnit class] asComponentDescription:desc name:@"Amuse" version:0100];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|