|
| 1 | +#include "AudioUnitBackend.hpp" |
| 2 | +#ifdef __APPLE__ |
| 3 | +#include <Availability.h> |
| 4 | +#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101100 |
| 5 | +#import <AudioUnit/AudioUnit.h> |
| 6 | +#import <CoreAudioKit/CoreAudioKit.h> |
| 7 | +#import <AVFoundation/AVFoundation.h> |
| 8 | + |
| 9 | +#if !__has_feature(objc_arc) |
| 10 | +#error ARC Required |
| 11 | +#endif |
| 12 | + |
| 13 | +#include "logvisor/logvisor.hpp" |
| 14 | +#include "audiodev/AudioVoiceEngine.hpp" |
| 15 | + |
| 16 | +static logvisor::Module Log("amuse::AudioUnitBackend"); |
| 17 | + |
| 18 | +struct AudioUnitVoiceEngine : boo::BaseAudioVoiceEngine |
| 19 | +{ |
| 20 | + std::vector<std::unique_ptr<float[]>> m_renderBufs; |
| 21 | + size_t m_frameBytes; |
| 22 | + |
| 23 | + void render(AudioBufferList* outputData) |
| 24 | + { |
| 25 | + if (m_renderBufs.size() < outputData->mNumberBuffers) |
| 26 | + m_renderBufs.resize(outputData->mNumberBuffers); |
| 27 | + |
| 28 | + for (int i=0 ; i<outputData->mNumberBuffers ; ++i) |
| 29 | + { |
| 30 | + std::unique_ptr<float[]>& buf = m_renderBufs[i]; |
| 31 | + AudioBuffer& auBuf = outputData->mBuffers[i]; |
| 32 | + if (!auBuf.mData) |
| 33 | + { |
| 34 | + buf.reset(new float[auBuf.mDataByteSize]); |
| 35 | + auBuf.mData = buf.get(); |
| 36 | + } |
| 37 | + |
| 38 | + _pumpAndMixVoices(auBuf.mDataByteSize / 2 / 4, reinterpret_cast<float*>(auBuf.mData)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + boo::AudioChannelSet _getAvailableSet() |
| 43 | + { |
| 44 | + return boo::AudioChannelSet::Stereo; |
| 45 | + } |
| 46 | + |
| 47 | + std::vector<std::pair<std::string, std::string>> enumerateMIDIDevices() const |
| 48 | + { |
| 49 | + return {}; |
| 50 | + } |
| 51 | + |
| 52 | + boo::ReceiveFunctor m_midiReceiver = nullptr; |
| 53 | + |
| 54 | + std::unique_ptr<boo::IMIDIIn> newVirtualMIDIIn(boo::ReceiveFunctor&& receiver) |
| 55 | + { |
| 56 | + m_midiReceiver = std::move(receiver); |
| 57 | + return {}; |
| 58 | + } |
| 59 | + |
| 60 | + std::unique_ptr<boo::IMIDIOut> newVirtualMIDIOut() |
| 61 | + { |
| 62 | + return {}; |
| 63 | + } |
| 64 | + |
| 65 | + std::unique_ptr<boo::IMIDIInOut> newVirtualMIDIInOut(boo::ReceiveFunctor&& receiver) |
| 66 | + { |
| 67 | + return {}; |
| 68 | + } |
| 69 | + |
| 70 | + std::unique_ptr<boo::IMIDIIn> newRealMIDIIn(const char* name, boo::ReceiveFunctor&& receiver) |
| 71 | + { |
| 72 | + return {}; |
| 73 | + } |
| 74 | + |
| 75 | + std::unique_ptr<boo::IMIDIOut> newRealMIDIOut(const char* name) |
| 76 | + { |
| 77 | + return {}; |
| 78 | + } |
| 79 | + |
| 80 | + std::unique_ptr<boo::IMIDIInOut> newRealMIDIInOut(const char* name, boo::ReceiveFunctor&& receiver) |
| 81 | + { |
| 82 | + return {}; |
| 83 | + } |
| 84 | + |
| 85 | + AudioUnitVoiceEngine() |
| 86 | + { |
| 87 | + m_mixInfo.m_channels = _getAvailableSet(); |
| 88 | + unsigned chCount = ChannelCount(m_mixInfo.m_channels); |
| 89 | + |
| 90 | + m_mixInfo.m_sampleRate = 96000.0; |
| 91 | + m_mixInfo.m_sampleFormat = SOXR_FLOAT32_I; |
| 92 | + m_mixInfo.m_bitsPerSample = 32; |
| 93 | + m_5msFrames = 96000 * 5 / 1000; |
| 94 | + |
| 95 | + boo::ChannelMap& chMapOut = m_mixInfo.m_channelMap; |
| 96 | + chMapOut.m_channelCount = 2; |
| 97 | + chMapOut.m_channels[0] = boo::AudioChannel::FrontLeft; |
| 98 | + chMapOut.m_channels[1] = boo::AudioChannel::FrontRight; |
| 99 | + |
| 100 | + while (chMapOut.m_channelCount < chCount) |
| 101 | + chMapOut.m_channels[chMapOut.m_channelCount++] = boo::AudioChannel::Unknown; |
| 102 | + |
| 103 | + m_mixInfo.m_periodFrames = 2400; |
| 104 | + |
| 105 | + m_frameBytes = m_mixInfo.m_periodFrames * m_mixInfo.m_channelMap.m_channelCount * 4; |
| 106 | + } |
| 107 | + |
| 108 | + void pumpAndMixVoices() |
| 109 | + { |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +@implementation AmuseAudioUnit |
| 114 | +- (id)initWithComponentDescription:(AudioComponentDescription)componentDescription |
| 115 | + options:(AudioComponentInstantiationOptions)options |
| 116 | + error:(NSError * _Nullable *)outError; |
| 117 | +{ |
| 118 | + self = [super initWithComponentDescription:componentDescription options:options error:outError]; |
| 119 | + if (!self) |
| 120 | + return nil; |
| 121 | + |
| 122 | + AUAudioUnitBus* outBus = [[AUAudioUnitBus alloc] initWithFormat: |
| 123 | + [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 |
| 124 | + sampleRate:96000.0 |
| 125 | + channels:2 |
| 126 | + interleaved:TRUE] |
| 127 | + error:outError]; |
| 128 | + if (!outBus) |
| 129 | + return nil; |
| 130 | + |
| 131 | + m_outs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeOutput busses:@[outBus]]; |
| 132 | + |
| 133 | + self.maximumFramesToRender = 2400; |
| 134 | + return self; |
| 135 | +} |
| 136 | + |
| 137 | +- (BOOL)allocateRenderResourcesAndReturnError:(NSError * _Nullable *)outError |
| 138 | +{ |
| 139 | + if (![super allocateRenderResourcesAndReturnError:outError]) |
| 140 | + return FALSE; |
| 141 | + |
| 142 | + m_booBackend = std::make_unique<AudioUnitVoiceEngine>(); |
| 143 | + if (!m_booBackend) |
| 144 | + { |
| 145 | + *outError = [NSError errorWithDomain:@"amuse" code:-1 |
| 146 | + userInfo:@{NSLocalizedDescriptionKey:@"Unable to construct boo mixer"}]; |
| 147 | + return FALSE; |
| 148 | + } |
| 149 | + |
| 150 | + m_voxAlloc.emplace(*m_booBackend); |
| 151 | + m_engine.emplace(*m_voxAlloc); |
| 152 | + *outError = nil; |
| 153 | + return TRUE; |
| 154 | +} |
| 155 | + |
| 156 | +- (void)deallocateRenderResources |
| 157 | +{ |
| 158 | + m_engine = std::experimental::nullopt; |
| 159 | + m_voxAlloc = std::experimental::nullopt; |
| 160 | + m_booBackend.reset(); |
| 161 | + [super deallocateRenderResources]; |
| 162 | +} |
| 163 | + |
| 164 | +- (BOOL)renderResourcesAllocated |
| 165 | +{ |
| 166 | + if (m_engine) |
| 167 | + return TRUE; |
| 168 | + return FALSE; |
| 169 | +} |
| 170 | + |
| 171 | +- (AUAudioUnitBusArray*)outputBusses |
| 172 | +{ |
| 173 | + return m_outs; |
| 174 | +} |
| 175 | + |
| 176 | +- (BOOL)musicDeviceOrEffect |
| 177 | +{ |
| 178 | + return TRUE; |
| 179 | +} |
| 180 | + |
| 181 | +- (NSInteger)virtualMIDICableCount |
| 182 | +{ |
| 183 | + return 1; |
| 184 | +} |
| 185 | + |
| 186 | +- (AUInternalRenderBlock)internalRenderBlock |
| 187 | +{ |
| 188 | + AudioUnitVoiceEngine& voxEngine = static_cast<AudioUnitVoiceEngine&>(*m_booBackend); |
| 189 | + |
| 190 | + return ^AUAudioUnitStatus(AudioUnitRenderActionFlags* actionFlags, const AudioTimeStamp* timestamp, |
| 191 | + AUAudioFrameCount frameCount, NSInteger outputBusNumber, AudioBufferList* outputData, |
| 192 | + const AURenderEvent* realtimeEventListHead, AURenderPullInputBlock pullInputBlock) |
| 193 | + { |
| 194 | + /* Process MIDI events first */ |
| 195 | + if (voxEngine.m_midiReceiver) |
| 196 | + { |
| 197 | + for (const AUMIDIEvent* event = &realtimeEventListHead->MIDI ; |
| 198 | + event != nullptr ; event = &event->next->MIDI) |
| 199 | + { |
| 200 | + if (event->eventType == AURenderEventMIDI) |
| 201 | + { |
| 202 | + voxEngine.m_midiReceiver(std::vector<uint8_t>(std::cbegin(event->data), |
| 203 | + std::cbegin(event->data) + event->length)); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /* Output buffers */ |
| 209 | + voxEngine.render(outputData); |
| 210 | + return noErr; |
| 211 | + }; |
| 212 | +} |
| 213 | +@end |
| 214 | + |
| 215 | +namespace amuse |
| 216 | +{ |
| 217 | + |
| 218 | +void RegisterAudioUnit() |
| 219 | +{ |
| 220 | + AudioComponentDescription desc = {}; |
| 221 | + desc.componentType = 'aumu'; |
| 222 | + desc.componentSubType = 'amus'; |
| 223 | + desc.componentManufacturer = 'AXDL'; |
| 224 | + [AUAudioUnit registerSubclass:[AmuseAudioUnit class] asComponentDescription:desc name:@"Amuse" version:0100]; |
| 225 | +} |
| 226 | + |
| 227 | +} |
| 228 | + |
| 229 | +#endif |
| 230 | +#endif |
0 commit comments