| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2020 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <smooth.h> |
| 14 | #include <math.h> |
| 15 | |
| 16 | namespace CA |
| 17 | { |
| 18 | # include <CoreServices/CoreServices.h> |
| 19 | # include <CoreAudio/AudioHardware.h> |
| 20 | # include <AudioUnit/AudioUnit.h> |
| 21 | }; |
| 22 | |
| 23 | #include "coreaudio.h" |
| 24 | |
| 25 | #ifndef MAC_OS_X_VERSION_10_6 |
| 26 | # define MAC_OS_X_VERSION_10_6 1060 |
| 27 | #endif |
| 28 | |
| 29 | using namespace smooth::Threads; |
| 30 | |
| 31 | const String &BoCA::OutputCoreAudio::GetComponentSpecs() |
| 32 | { |
| 33 | static String componentSpecs; |
| 34 | |
| 35 | /* See if we can find an output audio unit. |
| 36 | */ |
| 37 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 38 | CA::ComponentDescription description; |
| 39 | #else |
| 40 | CA::AudioComponentDescription description; |
| 41 | #endif |
| 42 | |
| 43 | description.componentType = CA::kAudioUnitType_Output; |
| 44 | description.componentSubType = CA::kAudioUnitSubType_DefaultOutput; |
| 45 | description.componentManufacturer = CA::kAudioUnitManufacturer_Apple; |
| 46 | description.componentFlags = 0; |
| 47 | description.componentFlagsMask = 0; |
| 48 | |
| 49 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 50 | if (CA::FindNextComponent(NIL, &description) != NIL) |
| 51 | #else |
| 52 | if (CA::AudioComponentFindNext(NIL, &description) != NIL) |
| 53 | #endif |
| 54 | { |
| 55 | componentSpecs = " \ |
| 56 | \ |
| 57 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 58 | <component> \ |
| 59 | <name>CoreAudio Output Plugin</name> \ |
| 60 | <version>1.0</version> \ |
| 61 | <id>coreaudio-out</id> \ |
| 62 | <type>output</type> \ |
| 63 | </component> \ |
| 64 | \ |
| 65 | "; |
| 66 | } |
| 67 | |
| 68 | return componentSpecs; |
| 69 | } |
| 70 | |
| 71 | namespace BoCA |
| 72 | { |
| 73 | CA::OSStatus AudioCallback(void *, CA::AudioUnitRenderActionFlags *, const CA::AudioTimeStamp *, CA::UInt32, CA::UInt32, CA::AudioBufferList *); |
| 74 | }; |
| 75 | |
| 76 | BoCA::OutputCoreAudio::OutputCoreAudio() |
| 77 | { |
| 78 | audioUnit = NIL; |
| 79 | paused = False; |
| 80 | |
| 81 | samplesBufferMutex = new Mutex(); |
| 82 | } |
| 83 | |
| 84 | BoCA::OutputCoreAudio::~OutputCoreAudio() |
| 85 | { |
| 86 | delete samplesBufferMutex; |
| 87 | } |
| 88 | |
| 89 | Bool BoCA::OutputCoreAudio::Activate() |
| 90 | { |
| 91 | static Endianness endianness = CPU().GetEndianness(); |
| 92 | |
| 93 | const Format &format = track.GetFormat(); |
| 94 | |
| 95 | /* Find default output audio unit. |
| 96 | */ |
| 97 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 98 | CA::ComponentDescription description; |
| 99 | CA::Component component; |
| 100 | #else |
| 101 | CA::AudioComponentDescription description; |
| 102 | CA::AudioComponent component; |
| 103 | #endif |
| 104 | |
| 105 | description.componentType = CA::kAudioUnitType_Output; |
| 106 | description.componentSubType = CA::kAudioUnitSubType_DefaultOutput; |
| 107 | description.componentManufacturer = CA::kAudioUnitManufacturer_Apple; |
| 108 | description.componentFlags = 0; |
| 109 | description.componentFlagsMask = 0; |
| 110 | |
| 111 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 112 | component = CA::FindNextComponent(NIL, &description); |
| 113 | #else |
| 114 | component = CA::AudioComponentFindNext(NIL, &description); |
| 115 | #endif |
| 116 | |
| 117 | if (component == NIL) return False; |
| 118 | |
| 119 | /* Initialize audio unit. |
| 120 | */ |
| 121 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 122 | if (CA::OpenAComponent(component, &audioUnit) != 0) return False; |
| 123 | #else |
| 124 | if (CA::AudioComponentInstanceNew(component, &audioUnit) != 0) return False; |
| 125 | #endif |
| 126 | if (CA::AudioUnitInitialize(audioUnit) != 0) return False; |
| 127 | |
| 128 | /* Set stream format. |
| 129 | */ |
| 130 | CA::AudioStreamBasicDescription streamFormat; |
| 131 | |
| 132 | streamFormat.mFormatID = CA::kAudioFormatLinearPCM; |
| 133 | streamFormat.mFormatFlags = CA::kLinearPCMFormatFlagIsPacked; |
| 134 | streamFormat.mFormatFlags |= format.fp ? CA::kLinearPCMFormatFlagIsFloat : 0; |
| 135 | streamFormat.mFormatFlags |= format.sign && !format.fp ? CA::kLinearPCMFormatFlagIsSignedInteger : 0; |
| 136 | streamFormat.mFormatFlags |= endianness == EndianBig ? CA::kLinearPCMFormatFlagIsBigEndian : 0; |
| 137 | streamFormat.mSampleRate = format.rate; |
| 138 | streamFormat.mChannelsPerFrame = format.channels; |
| 139 | streamFormat.mBitsPerChannel = format.bits; |
| 140 | streamFormat.mFramesPerPacket = 1; |
| 141 | streamFormat.mBytesPerFrame = streamFormat.mChannelsPerFrame * streamFormat.mBitsPerChannel / 8; |
| 142 | streamFormat.mBytesPerPacket = streamFormat.mFramesPerPacket * streamFormat.mBytesPerFrame; |
| 143 | |
| 144 | if (CA::AudioUnitSetProperty(audioUnit, CA::kAudioUnitProperty_StreamFormat, CA::kAudioUnitScope_Input, 0, &streamFormat, sizeof(streamFormat)) != 0) return False; |
| 145 | |
| 146 | /* Set audio callback. |
| 147 | */ |
| 148 | CA::AURenderCallbackStruct audioCallback; |
| 149 | |
| 150 | audioCallback.inputProc = AudioCallback; |
| 151 | audioCallback.inputProcRefCon = this; |
| 152 | |
| 153 | if (CA::AudioUnitSetProperty(audioUnit, CA::kAudioUnitProperty_SetRenderCallback, CA::kAudioUnitScope_Input, 0, &audioCallback, sizeof(audioCallback)) != 0) return False; |
| 154 | |
| 155 | /* Preallocate sample buffer. |
| 156 | */ |
| 157 | samplesBuffer.Resize((format.rate / 4 + 2048) * format.channels * (format.bits / 8)); |
| 158 | samplesBuffer.Resize(0); |
| 159 | |
| 160 | /* Start audio unit. |
| 161 | */ |
| 162 | if (CA::AudioOutputUnitStart(audioUnit) != 0) return False; |
| 163 | |
| 164 | paused = False; |
| 165 | |
| 166 | return True; |
| 167 | } |
| 168 | |
| 169 | Bool BoCA::OutputCoreAudio::Deactivate() |
| 170 | { |
| 171 | /* Stop audio unit. |
| 172 | */ |
| 173 | if (CA::AudioOutputUnitStop(audioUnit) != 0) return False; |
| 174 | |
| 175 | /* Remove audio callback. |
| 176 | */ |
| 177 | struct CA::AURenderCallbackStruct audioCallback; |
| 178 | |
| 179 | audioCallback.inputProc = 0; |
| 180 | audioCallback.inputProcRefCon = 0; |
| 181 | |
| 182 | if (CA::AudioUnitSetProperty(audioUnit, CA::kAudioUnitProperty_SetRenderCallback, CA::kAudioUnitScope_Input, 0, &audioCallback, sizeof(audioCallback)) != 0) return False; |
| 183 | |
| 184 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
| 185 | if (CA::CloseComponent(audioUnit) != 0) return False; |
| 186 | #else |
| 187 | if (CA::AudioComponentInstanceDispose(audioUnit) != 0) return False; |
| 188 | #endif |
| 189 | |
| 190 | audioUnit = NIL; |
| 191 | |
| 192 | return True; |
| 193 | } |
| 194 | |
| 195 | Int BoCA::OutputCoreAudio::WriteData(Buffer<UnsignedByte> &data) |
| 196 | { |
| 197 | samplesBufferMutex->Lock(); |
| 198 | |
| 199 | Int bytes = samplesBuffer.Size(); |
| 200 | |
| 201 | samplesBuffer.Resize(bytes + data.Size()); |
| 202 | |
| 203 | memcpy(samplesBuffer + bytes, data, data.Size()); |
| 204 | |
| 205 | samplesBufferMutex->Release(); |
| 206 | |
| 207 | return data.Size(); |
| 208 | } |
| 209 | |
| 210 | Int BoCA::OutputCoreAudio::CanWrite() |
| 211 | { |
| 212 | if (audioUnit == NIL || paused) return 0; |
| 213 | |
| 214 | const Format &format = track.GetFormat(); |
| 215 | |
| 216 | samplesBufferMutex->Lock(); |
| 217 | |
| 218 | Int canWrite = (format.rate / 4 + 2048) * format.channels * (format.bits / 8) - samplesBuffer.Size(); |
| 219 | |
| 220 | samplesBufferMutex->Release(); |
| 221 | |
| 222 | return canWrite; |
| 223 | } |
| 224 | |
| 225 | Int BoCA::OutputCoreAudio::SetPause(Bool pause) |
| 226 | { |
| 227 | if (audioUnit == NIL) return Error(); |
| 228 | |
| 229 | CA::OSStatus status = 0; |
| 230 | |
| 231 | if (pause) status = CA::AudioOutputUnitStop(audioUnit); |
| 232 | else status = CA::AudioOutputUnitStart(audioUnit); |
| 233 | |
| 234 | if (status != 0) return Error(); |
| 235 | |
| 236 | if (pause) paused = True; |
| 237 | else paused = False; |
| 238 | |
| 239 | return Success(); |
| 240 | } |
| 241 | |
| 242 | Bool BoCA::OutputCoreAudio::IsPlaying() |
| 243 | { |
| 244 | if (audioUnit == NIL) return False; |
| 245 | |
| 246 | samplesBufferMutex->Lock(); |
| 247 | |
| 248 | Bool isPlaying = (samplesBuffer.Size() > 0); |
| 249 | |
| 250 | samplesBufferMutex->Release(); |
| 251 | |
| 252 | return isPlaying; |
| 253 | } |
| 254 | |
| 255 | CA::OSStatus BoCA::AudioCallback(void *inClientData, CA::AudioUnitRenderActionFlags *ioActionFlags, const CA::AudioTimeStamp *inTimeStamp, CA::UInt32 inBusNumber, CA::UInt32 inNumberFrames, CA::AudioBufferList *ioData) |
| 256 | { |
| 257 | OutputCoreAudio *component = (OutputCoreAudio *) inClientData; |
| 258 | |
| 259 | component->samplesBufferMutex->Lock(); |
| 260 | |
| 261 | Int numBytes = Math::Min(component->samplesBuffer.Size(), ioData->mBuffers[0].mDataByteSize); |
| 262 | |
| 263 | /* Copy samples to output buffer. |
| 264 | */ |
| 265 | memcpy(ioData->mBuffers[0].mData, component->samplesBuffer, numBytes); |
| 266 | |
| 267 | ioData->mBuffers[0].mDataByteSize = numBytes; |
| 268 | ioData->mNumberBuffers = 1; |
| 269 | |
| 270 | /* Move remaining samples to the start of the buffer. |
| 271 | */ |
| 272 | memmove(component->samplesBuffer, component->samplesBuffer + numBytes, component->samplesBuffer.Size() - numBytes); |
| 273 | |
| 274 | component->samplesBuffer.Resize(component->samplesBuffer.Size() - numBytes); |
| 275 | |
| 276 | component->samplesBufferMutex->Release(); |
| 277 | |
| 278 | return CA::kAudioHardwareNoError; |
| 279 | } |