Files @ r23882:d683a0787bc9
Branch filter:

Location: cpp/openttd-patchpack/source/src/sound/cocoa_s.cpp - annotation

Nikolas Nyby
Codechange: Don't use SDL_CreateRGBSurfaceWithFormat()

This function requires libSDL 2.0.5 or higher. It looks like we don't
need to use it, and can just use the original SDL_CreateRGBSurface(),
with the masks set to 0, to trigger the default 8-bit format, which is
SDL_PIXELFORMAT_INDEX8.

Closes #7785

Note: this code path is activated by using an 8-bit blitter, like:

./bin/openttd -b 8bpp-simple
r5584:545d748cc681
r5584:545d748cc681
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r9111:983de9c5a848
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r22776:70f8e2860d3d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9543:54a9c0376831
r5584:545d748cc681
r5584:545d748cc681
r12386:d4d6ee913899
r12386:d4d6ee913899
r12386:d4d6ee913899
r12386:d4d6ee913899
r5584:545d748cc681
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r7170:38b143754b40
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7839:e456d3331541
r5584:545d748cc681
r7839:e456d3331541
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7170:38b143754b40
r5584:545d748cc681
r7839:e456d3331541
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12675:08178fc9d621
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9543:54a9c0376831
r5584:545d748cc681
r9543:54a9c0376831
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r20676:1eee2eea40ab
r5584:545d748cc681
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r23607:36c15679007d
r23607:36c15679007d
r23607:36c15679007d
r22776:70f8e2860d3d
r5584:545d748cc681
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r5584:545d748cc681
r23607:36c15679007d
r23607:36c15679007d
r23607:36c15679007d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23607:36c15679007d
r7839:e456d3331541
r7839:e456d3331541
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23607:36c15679007d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7170:38b143754b40
r5584:545d748cc681
r7839:e456d3331541
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7839:e456d3331541
r7839:e456d3331541
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r22776:70f8e2860d3d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file cocoa_s.cpp Sound driver for cocoa. */

/*****************************************************************************
 *                             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 "../stdafx.h"
#include "../os/macosx/macos.h"
#include "../debug.h"
#include "../driver.h"
#include "../mixer.h"
#include "../core/endian_type.hpp"
#include "cocoa_s.h"

#define Rect        OTTDRect
#define Point       OTTDPoint
#include <AudioUnit/AudioUnit.h>
#undef Rect
#undef Point

#include "../safeguards.h"

static FSoundDriver_Cocoa iFSoundDriver_Cocoa;

static AudioUnit _outputAudioUnit;

/* The CoreAudio callback */
static OSStatus audioCallback(void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * ioData)
{
	MxMixSamples(ioData->mBuffers[0].mData, ioData->mBuffers[0].mDataByteSize / 4);

	return noErr;
}


const char *SoundDriver_Cocoa::Start(const char * const *parm)
{
	struct AURenderCallbackStruct callback;
	AudioStreamBasicDescription requestedDesc;

	/* Setup a AudioStreamBasicDescription with the requested format */
	requestedDesc.mFormatID = kAudioFormatLinearPCM;
	requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
	requestedDesc.mChannelsPerFrame = 2;
	requestedDesc.mSampleRate = GetDriverParamInt(parm, "hz", 44100);

	requestedDesc.mBitsPerChannel = 16;
	requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;

#if TTD_ENDIAN == TTD_BIG_ENDIAN
	requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */

	requestedDesc.mFramesPerPacket = 1;
	requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
	requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;

	MxInitialize((uint)requestedDesc.mSampleRate);

#if defined(__AUDIOCOMPONENT_H__) || defined(HAVE_OSX_107_SDK)
	if (MacOSVersionIsAtLeast(10, 6, 0)) {
		/* Locate the default output audio unit */
		AudioComponentDescription desc;
		desc.componentType = kAudioUnitType_Output;
		desc.componentSubType = kAudioUnitSubType_HALOutput;
		desc.componentManufacturer = kAudioUnitManufacturer_Apple;
		desc.componentFlags = 0;
		desc.componentFlagsMask = 0;

		AudioComponent comp = AudioComponentFindNext (nullptr, &desc);
		if (comp == nullptr) {
			return "cocoa_s: Failed to start CoreAudio: AudioComponentFindNext returned nullptr";
		}

		/* Open & initialize the default output audio unit */
		if (AudioComponentInstanceNew(comp, &_outputAudioUnit) != noErr) {
			return "cocoa_s: Failed to start CoreAudio: AudioComponentInstanceNew";
		}
	} else
#endif
	{
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
		/* Locate the default output audio unit */
		ComponentDescription desc;
		desc.componentType = kAudioUnitType_Output;
		desc.componentSubType = kAudioUnitSubType_HALOutput;
		desc.componentManufacturer = kAudioUnitManufacturer_Apple;
		desc.componentFlags = 0;
		desc.componentFlagsMask = 0;

		Component comp = FindNextComponent (nullptr, &desc);
		if (comp == nullptr) {
			return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned nullptr";
		}

		/* Open & initialize the default output audio unit */
		if (OpenAComponent(comp, &_outputAudioUnit) != noErr) {
			return "cocoa_s: Failed to start CoreAudio: OpenAComponent";
		}
#else
		return "cocoa_s: Not supported on this OS X version";
#endif
	}

	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 = nullptr;
	if (AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr) {
		return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)";
	}

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

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


void SoundDriver_Cocoa::Stop()
{
	struct AURenderCallbackStruct callback;

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

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

#if defined(__AUDIOCOMPONENT_H__) || defined(HAVE_OSX_107_SDK)
	if (MacOSVersionIsAtLeast(10, 6, 0)) {
		if (AudioComponentInstanceDispose(_outputAudioUnit) != noErr) {
			DEBUG(driver, 0, "cocoa_s: Core_CloseAudio: AudioComponentInstanceDispose failed");
			return;
		}
	} else
#endif
	{
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
		if (CloseComponent(_outputAudioUnit) != noErr) {
			DEBUG(driver, 0, "cocoa_s: Core_CloseAudio: CloseComponent failed");
			return;
		}
#endif
	}
}

#endif /* WITH_COCOA */