4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file cocoa_s.cpp Sound driver for cocoa. */
12 /*****************************************************************************
13 * Cocoa sound driver *
14 * Known things left to do: *
15 * - Might need to do endian checking for it to work on both ppc and x86 *
16 *****************************************************************************/
20 #include "../stdafx.h"
22 #include "../driver.h"
24 #include "../core/endian_type.hpp"
28 #define Point OTTDPoint
29 #include <AudioUnit/AudioUnit.h>
33 static FSoundDriver_Cocoa iFSoundDriver_Cocoa
;
35 static AudioUnit _outputAudioUnit
;
37 /* The CoreAudio callback */
38 static OSStatus
audioCallback(void *inRefCon
, AudioUnitRenderActionFlags
*inActionFlags
, const AudioTimeStamp
*inTimeStamp
, UInt32 inBusNumber
, UInt32 inNumberFrames
, AudioBufferList
* ioData
)
40 MxMixSamples(ioData
->mBuffers
[0].mData
, ioData
->mBuffers
[0].mDataByteSize
/ 4);
46 const char *SoundDriver_Cocoa::Start(const char * const *parm
)
49 ComponentDescription desc
;
50 struct AURenderCallbackStruct callback
;
51 AudioStreamBasicDescription requestedDesc
;
53 /* Setup a AudioStreamBasicDescription with the requested format */
54 requestedDesc
.mFormatID
= kAudioFormatLinearPCM
;
55 requestedDesc
.mFormatFlags
= kLinearPCMFormatFlagIsPacked
;
56 requestedDesc
.mChannelsPerFrame
= 2;
57 requestedDesc
.mSampleRate
= GetDriverParamInt(parm
, "hz", 44100);
59 requestedDesc
.mBitsPerChannel
= 16;
60 requestedDesc
.mFormatFlags
|= kLinearPCMFormatFlagIsSignedInteger
;
62 #if TTD_ENDIAN == TTD_BIG_ENDIAN
63 requestedDesc
.mFormatFlags
|= kLinearPCMFormatFlagIsBigEndian
;
64 #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
66 requestedDesc
.mFramesPerPacket
= 1;
67 requestedDesc
.mBytesPerFrame
= requestedDesc
.mBitsPerChannel
* requestedDesc
.mChannelsPerFrame
/ 8;
68 requestedDesc
.mBytesPerPacket
= requestedDesc
.mBytesPerFrame
* requestedDesc
.mFramesPerPacket
;
70 MxInitialize((uint
)requestedDesc
.mSampleRate
);
72 /* Locate the default output audio unit */
73 desc
.componentType
= kAudioUnitType_Output
;
74 desc
.componentSubType
= kAudioUnitSubType_HALOutput
;
75 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
76 desc
.componentFlags
= 0;
77 desc
.componentFlagsMask
= 0;
79 comp
= FindNextComponent (NULL
, &desc
);
81 return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned NULL";
84 /* Open & initialize the default output audio unit */
85 if (OpenAComponent(comp
, &_outputAudioUnit
) != noErr
) {
86 return "cocoa_s: Failed to start CoreAudio: OpenAComponent";
89 if (AudioUnitInitialize(_outputAudioUnit
) != noErr
) {
90 return "cocoa_s: Failed to start CoreAudio: AudioUnitInitialize";
93 /* Set the input format of the audio unit. */
94 if (AudioUnitSetProperty(_outputAudioUnit
, kAudioUnitProperty_StreamFormat
, kAudioUnitScope_Input
, 0, &requestedDesc
, sizeof(requestedDesc
)) != noErr
) {
95 return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)";
98 /* Set the audio callback */
99 callback
.inputProc
= audioCallback
;
100 callback
.inputProcRefCon
= NULL
;
101 if (AudioUnitSetProperty(_outputAudioUnit
, kAudioUnitProperty_SetRenderCallback
, kAudioUnitScope_Input
, 0, &callback
, sizeof(callback
)) != noErr
) {
102 return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)";
105 /* Finally, start processing of the audio unit */
106 if (AudioOutputUnitStart(_outputAudioUnit
) != noErr
) {
107 return "cocoa_s: Failed to start CoreAudio: AudioOutputUnitStart";
115 void SoundDriver_Cocoa::Stop()
117 struct AURenderCallbackStruct callback
;
119 /* stop processing the audio unit */
120 if (AudioOutputUnitStop(_outputAudioUnit
) != noErr
) {
121 DEBUG(driver
, 0, "cocoa_s: Core_CloseAudio: AudioOutputUnitStop failed");
125 /* Remove the input callback */
126 callback
.inputProc
= 0;
127 callback
.inputProcRefCon
= 0;
128 if (AudioUnitSetProperty(_outputAudioUnit
, kAudioUnitProperty_SetRenderCallback
, kAudioUnitScope_Input
, 0, &callback
, sizeof(callback
)) != noErr
) {
129 DEBUG(driver
, 0, "cocoa_s: Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback) failed");
133 if (CloseComponent(_outputAudioUnit
) != noErr
) {
134 DEBUG(driver
, 0, "cocoa_s: Core_CloseAudio: CloseComponent failed");
139 #endif /* WITH_COCOA */