20 #include "../stdafx.h" 21 #include "../os/macosx/macos.h" 23 #include "../driver.h" 25 #include "../core/endian_type.hpp" 29 #define Point OTTDPoint 30 #include <AudioUnit/AudioUnit.h> 34 #include "../safeguards.h" 38 static AudioUnit _outputAudioUnit;
41 static OSStatus audioCallback(
void *inRefCon, AudioUnitRenderActionFlags *inActionFlags,
const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * ioData)
43 MxMixSamples(ioData->mBuffers[0].mData, ioData->mBuffers[0].mDataByteSize / 4);
51 struct AURenderCallbackStruct callback;
52 AudioStreamBasicDescription requestedDesc;
55 requestedDesc.mFormatID = kAudioFormatLinearPCM;
56 requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
57 requestedDesc.mChannelsPerFrame = 2;
60 requestedDesc.mBitsPerChannel = 16;
61 requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
63 #if TTD_ENDIAN == TTD_BIG_ENDIAN 64 requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
67 requestedDesc.mFramesPerPacket = 1;
68 requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
69 requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
71 MxInitialize((uint)requestedDesc.mSampleRate);
73 #if defined(__AUDIOCOMPONENT_H__) || defined(HAVE_OSX_107_SDK) 76 AudioComponentDescription desc;
77 desc.componentType = kAudioUnitType_Output;
78 desc.componentSubType = kAudioUnitSubType_HALOutput;
79 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
80 desc.componentFlags = 0;
81 desc.componentFlagsMask = 0;
83 AudioComponent comp = AudioComponentFindNext (
nullptr, &desc);
84 if (comp ==
nullptr) {
85 return "cocoa_s: Failed to start CoreAudio: AudioComponentFindNext returned nullptr";
89 if (AudioComponentInstanceNew(comp, &_outputAudioUnit) != noErr) {
90 return "cocoa_s: Failed to start CoreAudio: AudioComponentInstanceNew";
95 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6) 97 ComponentDescription desc;
98 desc.componentType = kAudioUnitType_Output;
99 desc.componentSubType = kAudioUnitSubType_HALOutput;
100 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
101 desc.componentFlags = 0;
102 desc.componentFlagsMask = 0;
104 Component comp = FindNextComponent (
nullptr, &desc);
105 if (comp ==
nullptr) {
106 return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned nullptr";
110 if (OpenAComponent(comp, &_outputAudioUnit) != noErr) {
111 return "cocoa_s: Failed to start CoreAudio: OpenAComponent";
114 return "cocoa_s: Not supported on this OS X version";
118 if (AudioUnitInitialize(_outputAudioUnit) != noErr) {
119 return "cocoa_s: Failed to start CoreAudio: AudioUnitInitialize";
123 if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &requestedDesc,
sizeof(requestedDesc)) != noErr) {
124 return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)";
128 callback.inputProc = audioCallback;
129 callback.inputProcRefCon =
nullptr;
130 if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback,
sizeof(callback)) != noErr) {
131 return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)";
135 if (AudioOutputUnitStart(_outputAudioUnit) != noErr) {
136 return "cocoa_s: Failed to start CoreAudio: AudioOutputUnitStart";
146 struct AURenderCallbackStruct callback;
149 if (AudioOutputUnitStop(_outputAudioUnit) != noErr) {
150 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: AudioOutputUnitStop failed");
155 callback.inputProc = 0;
156 callback.inputProcRefCon = 0;
157 if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback,
sizeof(callback)) != noErr) {
158 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback) failed");
162 #if defined(__AUDIOCOMPONENT_H__) || defined(HAVE_OSX_107_SDK) 164 if (AudioComponentInstanceDispose(_outputAudioUnit) != noErr) {
165 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: AudioComponentInstanceDispose failed");
171 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6) 172 if (CloseComponent(_outputAudioUnit) != noErr) {
173 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: CloseComponent failed");
static bool MacOSVersionIsAtLeast(long major, long minor, long bugfix)
Check if we are at least running on the specified version of Mac OS.
Base for Cocoa sound handling.
void Stop() override
Stop this driver.
#define DEBUG(name, level,...)
Output a line of debugging information.
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
const char * Start(const char *const *param) override
Start this driver.