18 #include "../stdafx.h" 19 #include "../os/macosx/macos.h" 21 #include "../driver.h" 23 #include "../core/endian_type.hpp" 27 #define Point OTTDPoint 28 #include <AudioUnit/AudioUnit.h> 32 #include "../safeguards.h" 36 static AudioUnit _outputAudioUnit;
39 static OSStatus audioCallback(
void *inRefCon, AudioUnitRenderActionFlags *inActionFlags,
const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * ioData)
41 MxMixSamples(ioData->mBuffers[0].mData, ioData->mBuffers[0].mDataByteSize / 4);
49 struct AURenderCallbackStruct callback;
50 AudioStreamBasicDescription requestedDesc;
53 requestedDesc.mFormatID = kAudioFormatLinearPCM;
54 requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
55 requestedDesc.mChannelsPerFrame = 2;
58 requestedDesc.mBitsPerChannel = 16;
59 requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
61 #if TTD_ENDIAN == TTD_BIG_ENDIAN 62 requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
65 requestedDesc.mFramesPerPacket = 1;
66 requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
67 requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
69 MxInitialize((uint)requestedDesc.mSampleRate);
71 #if defined(__AUDIOCOMPONENT_H__) || defined(HAVE_OSX_107_SDK) 74 AudioComponentDescription desc;
75 desc.componentType = kAudioUnitType_Output;
76 desc.componentSubType = kAudioUnitSubType_HALOutput;
77 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
78 desc.componentFlags = 0;
79 desc.componentFlagsMask = 0;
81 AudioComponent comp = AudioComponentFindNext (
nullptr, &desc);
82 if (comp ==
nullptr) {
83 return "cocoa_s: Failed to start CoreAudio: AudioComponentFindNext returned nullptr";
87 if (AudioComponentInstanceNew(comp, &_outputAudioUnit) != noErr) {
88 return "cocoa_s: Failed to start CoreAudio: AudioComponentInstanceNew";
93 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6) 95 ComponentDescription desc;
96 desc.componentType = kAudioUnitType_Output;
97 desc.componentSubType = kAudioUnitSubType_HALOutput;
98 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
99 desc.componentFlags = 0;
100 desc.componentFlagsMask = 0;
102 Component comp = FindNextComponent (
nullptr, &desc);
103 if (comp ==
nullptr) {
104 return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned nullptr";
108 if (OpenAComponent(comp, &_outputAudioUnit) != noErr) {
109 return "cocoa_s: Failed to start CoreAudio: OpenAComponent";
112 return "cocoa_s: Not supported on this OS X version";
116 if (AudioUnitInitialize(_outputAudioUnit) != noErr) {
117 return "cocoa_s: Failed to start CoreAudio: AudioUnitInitialize";
121 if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &requestedDesc,
sizeof(requestedDesc)) != noErr) {
122 return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)";
126 callback.inputProc = audioCallback;
127 callback.inputProcRefCon =
nullptr;
128 if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback,
sizeof(callback)) != noErr) {
129 return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)";
133 if (AudioOutputUnitStart(_outputAudioUnit) != noErr) {
134 return "cocoa_s: Failed to start CoreAudio: AudioOutputUnitStart";
144 struct AURenderCallbackStruct callback;
147 if (AudioOutputUnitStop(_outputAudioUnit) != noErr) {
148 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: AudioOutputUnitStop failed");
153 callback.inputProc = 0;
154 callback.inputProcRefCon = 0;
155 if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback,
sizeof(callback)) != noErr) {
156 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback) failed");
160 #if defined(__AUDIOCOMPONENT_H__) || defined(HAVE_OSX_107_SDK) 162 if (AudioComponentInstanceDispose(_outputAudioUnit) != noErr) {
163 DEBUG(driver, 0,
"cocoa_s: Core_CloseAudio: AudioComponentInstanceDispose failed");
169 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6) 170 if (CloseComponent(_outputAudioUnit) != noErr) {
171 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.