Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / sound / cocoa_s.cpp
blob1dc2a25d4acc757c1d37ca6afb912de350893901
1 /* $Id$ */
3 /*
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/>.
8 */
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 *****************************************************************************/
18 #ifdef WITH_COCOA
20 #include "../stdafx.h"
21 #include "../debug.h"
22 #include "../driver.h"
23 #include "../mixer.h"
24 #include "../core/endian_type.hpp"
25 #include "cocoa_s.h"
27 #define Rect OTTDRect
28 #define Point OTTDPoint
29 #include <AudioUnit/AudioUnit.h>
30 #undef Rect
31 #undef Point
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);
42 return noErr;
46 const char *SoundDriver_Cocoa::Start(const char * const *parm)
48 Component comp;
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);
80 if (comp == NULL) {
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";
110 /* We're running! */
111 return NULL;
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");
122 return;
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");
130 return;
133 if (CloseComponent(_outputAudioUnit) != noErr) {
134 DEBUG(driver, 0, "cocoa_s: Core_CloseAudio: CloseComponent failed");
135 return;
139 #endif /* WITH_COCOA */