2005-12-11 10:25:27 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2006-02-01 09:08:25 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Cocoa sound driver *
|
|
|
|
* Known things left to do: *
|
|
|
|
* - Might need to do endian checking for it to work on both ppc and x86 *
|
|
|
|
*****************************************************************************/
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
#ifdef WITH_COCOA
|
|
|
|
|
|
|
|
#include <AudioUnit/AudioUnit.h>
|
|
|
|
|
|
|
|
/* Name conflict */
|
2006-02-01 09:08:25 +00:00
|
|
|
#define Rect OTTDRect
|
|
|
|
#define Point OTTDPoint
|
|
|
|
#define WindowClass OTTDWindowClass
|
2005-12-10 11:16:45 +00:00
|
|
|
/* Defined in stdbool.h */
|
|
|
|
#ifndef __cplusplus
|
|
|
|
# ifndef __BEOS__
|
|
|
|
# undef bool
|
|
|
|
# undef false
|
|
|
|
# undef true
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../openttd.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../driver.h"
|
|
|
|
#include "../mixer.h"
|
|
|
|
#include "../sdl.h"
|
|
|
|
|
|
|
|
#include "cocoa_s.h"
|
|
|
|
|
|
|
|
#undef WindowClass
|
|
|
|
#undef Point
|
|
|
|
#undef Rect
|
|
|
|
|
|
|
|
|
|
|
|
static AudioUnit _outputAudioUnit;
|
|
|
|
|
|
|
|
/* The CoreAudio callback */
|
2006-02-01 09:08:25 +00:00
|
|
|
static OSStatus audioCallback(void *inRefCon, AudioUnitRenderActionFlags inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, AudioBuffer *ioData)
|
2005-12-10 11:16:45 +00:00
|
|
|
{
|
2006-02-05 09:56:04 +00:00
|
|
|
MxMixSamples(ioData->mData, ioData->mDataByteSize / 4);
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *CocoaSoundStart(const char * const *parm)
|
|
|
|
{
|
|
|
|
Component comp;
|
|
|
|
ComponentDescription desc;
|
|
|
|
struct AudioUnitInputCallback callback;
|
|
|
|
AudioStreamBasicDescription requestedDesc;
|
|
|
|
|
|
|
|
/* Setup a AudioStreamBasicDescription with the requested format */
|
|
|
|
requestedDesc.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
|
|
|
|
requestedDesc.mChannelsPerFrame = 2;
|
|
|
|
requestedDesc.mSampleRate = GetDriverParamInt(parm, "hz", 11025);
|
|
|
|
|
|
|
|
requestedDesc.mBitsPerChannel = 16;
|
|
|
|
requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
|
2006-01-07 13:21:04 +00:00
|
|
|
|
|
|
|
#ifdef TTD_BIG_ENDIAN
|
2005-12-10 11:16:45 +00:00
|
|
|
requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
requestedDesc.mFramesPerPacket = 1;
|
|
|
|
requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
|
|
|
|
requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
|
|
|
|
|
|
|
|
|
|
|
|
/* Locate the default output audio unit */
|
|
|
|
desc.componentType = kAudioUnitComponentType;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_Output;
|
|
|
|
desc.componentManufacturer = kAudioUnitID_DefaultOutput;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
|
|
|
comp = FindNextComponent (NULL, &desc);
|
2006-06-27 21:25:53 +00:00
|
|
|
if (comp == NULL) {
|
2005-12-10 11:16:45 +00:00
|
|
|
return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned NULL";
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
/* Open & initialize the default output audio unit */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (OpenAComponent(comp, &_outputAudioUnit) != noErr) {
|
2005-12-10 11:16:45 +00:00
|
|
|
return "cocoa_s: Failed to start CoreAudio: OpenAComponent";
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (AudioUnitInitialize(_outputAudioUnit) != noErr) {
|
2005-12-10 11:16:45 +00:00
|
|
|
return "cocoa_s: Failed to start CoreAudio: AudioUnitInitialize";
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
/* Set the input format of the audio unit. */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &requestedDesc, sizeof(requestedDesc)) != noErr) {
|
2006-02-01 09:08:25 +00:00
|
|
|
return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)";
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
/* Set the audio callback */
|
|
|
|
callback.inputProc = audioCallback;
|
|
|
|
callback.inputProcRefCon = NULL;
|
2006-06-27 21:25:53 +00:00
|
|
|
if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetInputCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr) {
|
2005-12-10 11:16:45 +00:00
|
|
|
return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)";
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
/* Finally, start processing of the audio unit */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (AudioOutputUnitStart(_outputAudioUnit) != noErr) {
|
2005-12-10 11:16:45 +00:00
|
|
|
return "cocoa_s: Failed to start CoreAudio: AudioOutputUnitStart";
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
|
|
|
/* We're running! */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CocoaSoundStop(void)
|
|
|
|
{
|
|
|
|
struct AudioUnitInputCallback callback;
|
|
|
|
|
|
|
|
/* stop processing the audio unit */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (AudioOutputUnitStop(_outputAudioUnit) != noErr) {
|
2006-12-26 22:04:53 +00:00
|
|
|
DEBUG(driver, 0, "cocoa_s: Core_CloseAudio: AudioOutputUnitStop failed");
|
2005-12-10 11:16:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the input callback */
|
|
|
|
callback.inputProc = 0;
|
|
|
|
callback.inputProcRefCon = 0;
|
2006-02-01 09:08:25 +00:00
|
|
|
if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetInputCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr) {
|
2006-12-26 22:04:53 +00:00
|
|
|
DEBUG(driver, 0, "cocoa_s: Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback) failed");
|
2005-12-10 11:16:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CloseComponent(_outputAudioUnit) != noErr) {
|
2006-12-26 22:04:53 +00:00
|
|
|
DEBUG(driver, 0, "cocoa_s: Core_CloseAudio: CloseComponent failed");
|
2005-12-10 11:16:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const HalSoundDriver _cocoa_sound_driver = {
|
|
|
|
CocoaSoundStart,
|
|
|
|
CocoaSoundStop,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* WITH_COCOA */
|