sound/cocoa_s.c
author Darkvater
Thu, 02 Mar 2006 02:22:15 +0000
changeset 3121 2e50f731567a
parent 2977 0240dd4704a7
child 4077 d3022f976946
permissions -rw-r--r--
(svn r3726) - [6/6] Finalize conversion, finally save the patches struct.
- Remove the temporary synchronisation in during the map-transfer as this is no longer needed
- The saved patches work just like the saved gameoptions. You have a _patches and a _patches_newgame struct. The _patches_newgame struct contains the values from the configuration file and thus the defaults for new games. When a new game is started or an older game is loaded, the default values are copied over to _patches to be used. When you load a game that has PATS saved, the default values are also loaded, but immediately overwritten by the values from the savegame. This ensures that player-based values are always taken from your personal preferences.
- The current implementation also changes the default values if you change player-based settings in the game. For example changing window_snap_radius in a certain game will also change it for all next OpenTTD sessions.
- The savegame version has been increased to 22.
- The last 6 orso patches close the following reports:
[ 1366446 ] different names for patches: all patch settings have the same name as in the configuration file and are reachable from the console.
[ 1288024 ] Strange string on OTTD initial screen: configuration (and this includes patches) inputs are validated and clamped to their minimum/maximum values.
[ 1423198 ] Make "Signals on Drive side" player, not server, based: this is only visual so current setting is to save it with the savegame but not synchronise in multiplayer.
[ 1208070 ] Patches and New GRF options saved: apart from newgrf this is done
/* $Id$ */

/*****************************************************************************
 *                             Cocoa sound driver                            *
 * Known things left to do:                                                  *
 * - Might need to do endian checking for it to work on both ppc and x86     *
 *****************************************************************************/

#ifdef WITH_COCOA

#include <AudioUnit/AudioUnit.h>

/* Name conflict */
#define Rect        OTTDRect
#define Point       OTTDPoint
#define WindowClass OTTDWindowClass
/* 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 */
static OSStatus audioCallback(void *inRefCon, AudioUnitRenderActionFlags inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, AudioBuffer *ioData)
{
	MxMixSamples(ioData->mData, ioData->mDataByteSize / 4);

	return noErr;
}


static const char *CocoaSoundStart(const char * const *parm)
{
	Component comp;
	ComponentDescription desc;
	struct AudioUnitInputCallback callback;
	AudioStreamBasicDescription requestedDesc;

	DEBUG(driver, 1)("cocoa_s: CocoaSoundStart");

	/* 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;

#ifdef TTD_BIG_ENDIAN
	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);
	if (comp == NULL)
		return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned NULL";

	/* Open & initialize the default output audio unit */
	if (OpenAComponent(comp, &_outputAudioUnit) != noErr)
		return "cocoa_s: Failed to start CoreAudio: OpenAComponent";

	if (AudioUnitInitialize(_outputAudioUnit) != noErr)
		return "cocoa_s: Failed to start CoreAudio: AudioUnitInitialize";

	/* Set the input format of the audio unit. */
	if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &requestedDesc, sizeof(requestedDesc)) != noErr)
		return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)";

	/* Set the audio callback */
	callback.inputProc = audioCallback;
	callback.inputProcRefCon = NULL;
	if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetInputCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr)
		return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)";

	/* Finally, start processing of the audio unit */
	if (AudioOutputUnitStart(_outputAudioUnit) != noErr)
		return "cocoa_s: Failed to start CoreAudio: AudioOutputUnitStart";

	/* We're running! */
	return NULL;
}


static void CocoaSoundStop(void)
{
	struct AudioUnitInputCallback callback;

	DEBUG(driver, 1)("cocoa_s: CocoaSoundStop");

	/* stop processing the audio unit */
	if(AudioOutputUnitStop(_outputAudioUnit) != noErr) {
		DEBUG(driver, 1)("cocoa_s: Core_CloseAudio: AudioOutputUnitStop failed");
		return;
	}

	/* Remove the input callback */
	callback.inputProc = 0;
	callback.inputProcRefCon = 0;
	if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetInputCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr) {
		DEBUG(driver, 1)("cocoa_s: Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback) failed");
		return;
	}

	if (CloseComponent(_outputAudioUnit) != noErr) {
		DEBUG(driver, 1)("cocoa_s: Core_CloseAudio: CloseComponent failed");
		return;
	}
}


const HalSoundDriver _cocoa_sound_driver = {
	CocoaSoundStart,
	CocoaSoundStop,
};

#endif /* WITH_COCOA */