2003-08-31 13:41:04 +00:00
|
|
|
#include "global.h"
|
2004-05-16 08:09:36 +00:00
|
|
|
#include "RageUtil.h"
|
2004-03-24 17:02:02 +00:00
|
|
|
#include "RageSoundDriver_CA.h"
|
|
|
|
|
#include "CAHelpers.h"
|
2004-04-16 05:04:23 +00:00
|
|
|
#include "RageLog.h"
|
2004-05-19 00:57:39 +00:00
|
|
|
#include "RageTimer.h"
|
2003-08-31 13:41:04 +00:00
|
|
|
|
2004-03-24 17:02:02 +00:00
|
|
|
#include "CAAudioHardwareSystem.h"
|
|
|
|
|
#include "CAAudioHardwareDevice.h"
|
2004-03-28 21:18:21 +00:00
|
|
|
#include "CAAudioHardwareStream.h"
|
2004-03-24 17:02:02 +00:00
|
|
|
#include "CAStreamBasicDescription.h"
|
|
|
|
|
#include "CAException.h"
|
|
|
|
|
#include "archutils/Unix/CrashHandler.h"
|
2004-03-28 21:18:21 +00:00
|
|
|
#include <mach/thread_act.h>
|
|
|
|
|
#include <mach/mach_init.h>
|
2004-04-25 23:25:38 +00:00
|
|
|
#include <mach/mach_error.h>
|
2003-08-31 13:41:04 +00:00
|
|
|
|
2004-03-28 22:37:12 +00:00
|
|
|
static AudioConverter *gConverter;
|
2004-03-28 21:18:21 +00:00
|
|
|
|
2004-05-19 00:57:39 +00:00
|
|
|
/* temporary hack: */
|
2004-05-23 19:45:19 +00:00
|
|
|
static float g_fLastIOProcTime = 0;
|
|
|
|
|
static int g_iNumIOProcCalls = 0;
|
2004-05-19 00:57:39 +00:00
|
|
|
|
2004-05-16 08:09:36 +00:00
|
|
|
static CString FormatToString( int fmt )
|
|
|
|
|
{
|
|
|
|
|
char c[4];
|
|
|
|
|
c[0] = (fmt >> 24) & 0xFF;
|
|
|
|
|
c[1] = (fmt >> 16) & 0xFF;
|
|
|
|
|
c[2] = (fmt >> 8) & 0xFF;
|
|
|
|
|
c[3] = (fmt) & 0xFF;
|
|
|
|
|
|
|
|
|
|
/* Sanitize: */
|
|
|
|
|
for( int i = 0; i < 4; ++i )
|
|
|
|
|
if( c[i] < 32 || c[i] >= 127 )
|
|
|
|
|
return ssprintf( "0x%X", fmt );
|
|
|
|
|
|
|
|
|
|
return ssprintf( "%c%c%c%c", c[0], c[1], c[2], c[3] );
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-28 21:18:21 +00:00
|
|
|
static Desc FindClosestFormat(const vector<Desc>& formats)
|
|
|
|
|
{
|
|
|
|
|
vector<Desc> v;
|
|
|
|
|
|
|
|
|
|
vector<Desc>::const_iterator i;
|
|
|
|
|
for (i = formats.begin(); i != formats.end(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const Desc& format = *i;
|
|
|
|
|
|
2004-05-10 00:43:57 +00:00
|
|
|
if( !format.IsPCM() )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if( format.mSampleRate != 0. && format.mSampleRate != 44100.0 )
|
2004-03-28 21:18:21 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (format.SampleWordSize() == 2 &&
|
2004-04-16 05:04:23 +00:00
|
|
|
(format.mFormatFlags & kAudioFormatFlagIsSignedInteger) ==
|
2004-03-28 21:18:21 +00:00
|
|
|
kAudioFormatFlagIsSignedInteger)
|
|
|
|
|
{ // exact match
|
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
v.push_back(format);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = v.begin(); i != v.end(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const Desc& format = *i;
|
|
|
|
|
if (format.SampleWordSize() == 2)
|
|
|
|
|
{
|
|
|
|
|
return format; // close
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (v.empty())
|
|
|
|
|
RageException::ThrowNonfatal("Couldn't find a close format.");
|
|
|
|
|
return v[0]; // something is better than nothing.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void GetIOProcFormats( CAAudioHardwareStream stream, vector<Desc> &procFormats )
|
|
|
|
|
{
|
|
|
|
|
UInt32 numFormats = stream.GetNumberAvailableIOProcFormats();
|
|
|
|
|
|
|
|
|
|
for( UInt32 i=0; i<numFormats; ++i )
|
|
|
|
|
{
|
|
|
|
|
Desc desc;
|
|
|
|
|
|
|
|
|
|
stream.GetAvailableIOProcFormatByIndex( i, desc );
|
|
|
|
|
procFormats.push_back( desc );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void GetPhysicalFormats( CAAudioHardwareStream stream, vector<Desc> &physicalFormats )
|
|
|
|
|
{
|
|
|
|
|
UInt32 numFormats = stream.GetNumberAvailablePhysicalFormats();
|
|
|
|
|
for( UInt32 i=0; i<numFormats; ++i )
|
|
|
|
|
{
|
|
|
|
|
Desc desc;
|
|
|
|
|
|
|
|
|
|
stream.GetAvailablePhysicalFormatByIndex( i, desc );
|
|
|
|
|
physicalFormats.push_back( desc );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-24 17:02:02 +00:00
|
|
|
RageSound_CA::RageSound_CA()
|
|
|
|
|
{
|
|
|
|
|
try
|
2004-02-20 22:52:20 +00:00
|
|
|
{
|
2004-03-24 17:02:02 +00:00
|
|
|
AudioDeviceID dID = CAAudioHardwareSystem::GetDefaultDevice(false, false);
|
|
|
|
|
|
|
|
|
|
mOutputDevice = new CAAudioHardwareDevice(dID);
|
|
|
|
|
}
|
|
|
|
|
catch (const CAException& e)
|
|
|
|
|
{
|
|
|
|
|
RageException::ThrowNonfatal("Couldn't create default output device.");
|
|
|
|
|
}
|
2004-04-27 06:44:20 +00:00
|
|
|
|
2004-05-16 08:09:36 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
mOutputDevice->SetNominalSampleRate(44100.0);
|
|
|
|
|
}
|
|
|
|
|
catch (const CAException& e)
|
|
|
|
|
{
|
|
|
|
|
RageException::ThrowNonfatal("Couldn't set the nominal sample rate.");
|
|
|
|
|
}
|
2004-04-27 05:07:19 +00:00
|
|
|
AudioStreamID sID = mOutputDevice->GetStreamByIndex( kAudioDeviceSectionOutput, 0 );
|
|
|
|
|
CAAudioHardwareStream stream( sID );
|
2004-04-27 06:44:20 +00:00
|
|
|
|
2004-04-27 05:07:19 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
mOutputDevice->AddPropertyListener(kAudioPropertyWildcardChannel,
|
|
|
|
|
kAudioPropertyWildcardSection,
|
|
|
|
|
kAudioDeviceProcessorOverload,
|
|
|
|
|
OverloadListener, this);
|
|
|
|
|
}
|
|
|
|
|
catch (const CAException& e)
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("Could not install the overload listener.");
|
|
|
|
|
}
|
2004-03-24 17:02:02 +00:00
|
|
|
|
2004-03-28 21:18:21 +00:00
|
|
|
|
|
|
|
|
vector<Desc> physicalFormats;
|
|
|
|
|
GetPhysicalFormats( sID, physicalFormats );
|
2004-04-16 05:04:23 +00:00
|
|
|
unsigned i;
|
|
|
|
|
LOG->Info("Available physical formats:");
|
|
|
|
|
for (i = 0; i < physicalFormats.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const Desc& f = physicalFormats[i];
|
|
|
|
|
|
2004-05-16 08:09:36 +00:00
|
|
|
LOG->Info("Format %u: Rate: %i ID: %s Flags 0x%lx bpp %lu fpp %lu bpf %lu channels %lu bits %lu",
|
|
|
|
|
i, (int) f.mSampleRate, FormatToString(f.mFormatID).c_str(), f.mFormatFlags,
|
2004-04-16 05:04:23 +00:00
|
|
|
f.mBytesPerPacket, f.mFramesPerPacket, f.mBytesPerFrame,
|
|
|
|
|
f.mChannelsPerFrame, f.mBitsPerChannel);
|
|
|
|
|
}
|
2004-03-28 21:18:21 +00:00
|
|
|
const Desc& physicalFormat = FindClosestFormat( physicalFormats );
|
|
|
|
|
stream.SetCurrentPhysicalFormat( physicalFormat );
|
|
|
|
|
|
2004-04-10 08:32:59 +00:00
|
|
|
vector<Desc> procFormats;
|
|
|
|
|
GetIOProcFormats( sID, procFormats );
|
2004-04-16 22:59:59 +00:00
|
|
|
LOG->Info("Available I/O procedure formats:");
|
2004-04-16 05:04:23 +00:00
|
|
|
for (i = 0; i < procFormats.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
const Desc& f = procFormats[i];
|
|
|
|
|
|
2004-05-16 08:09:36 +00:00
|
|
|
LOG->Info("Format %u: Rate: %i ID: %s Flags 0x%lx bpp %lu fpp %lu bpf %lu channels %lu bits %lu",
|
|
|
|
|
i, (int) f.mSampleRate, FormatToString(f.mFormatID).c_str(), f.mFormatFlags,
|
2004-04-16 05:04:23 +00:00
|
|
|
f.mBytesPerPacket, f.mFramesPerPacket, f.mBytesPerFrame,
|
|
|
|
|
f.mChannelsPerFrame, f.mBitsPerChannel);
|
|
|
|
|
}
|
2004-04-10 08:32:59 +00:00
|
|
|
const Desc& procFormat = FindClosestFormat( procFormats );
|
|
|
|
|
stream.SetCurrentIOProcFormat( procFormat );
|
2004-04-27 06:44:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
UInt32 bufferSize = mOutputDevice->GetIOBufferSize();
|
|
|
|
|
LOG->Info("I/O Buffer size: %lu", bufferSize);
|
|
|
|
|
}
|
|
|
|
|
catch (const CAException& e)
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("Could not determine buffer size.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
UInt32 frames = mOutputDevice->GetLatency(kAudioDeviceSectionOutput);
|
|
|
|
|
if (stream.HasProperty(0, kAudioDevicePropertyLatency))
|
|
|
|
|
{
|
|
|
|
|
UInt32 t, size = 4;
|
|
|
|
|
|
|
|
|
|
stream.GetPropertyData(0, kAudioDevicePropertyLatency, size, &t);
|
|
|
|
|
frames += t;
|
|
|
|
|
LOG->Info("Frames of stream latency: %lu", t);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
LOG->Warn("Stream reports no latency.");
|
|
|
|
|
mLatency = frames / 44100.0;
|
|
|
|
|
LOG->Info("Frames of latency: %lu\n"
|
|
|
|
|
"Seconds of latency: %f", frames, mLatency);
|
|
|
|
|
}
|
|
|
|
|
catch (const CAException& e)
|
|
|
|
|
{
|
|
|
|
|
delete mOutputDevice;
|
|
|
|
|
RageException::ThrowNonfatal("Couldn't get Latency.");
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-10 08:32:59 +00:00
|
|
|
|
2004-04-02 21:32:25 +00:00
|
|
|
StartDecodeThread();
|
|
|
|
|
|
2004-03-28 21:18:21 +00:00
|
|
|
gConverter = new AudioConverter( this, procFormat );
|
2004-03-24 17:02:02 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
mOutputDevice->AddIOProc(GetData, this);
|
|
|
|
|
mOutputDevice->StartIOProc(GetData);
|
|
|
|
|
}
|
|
|
|
|
catch(const CAException& e)
|
|
|
|
|
{
|
2004-03-28 22:37:12 +00:00
|
|
|
delete gConverter;
|
|
|
|
|
delete mOutputDevice;
|
2004-03-24 17:02:02 +00:00
|
|
|
RageException::Throw("Couldn't start the IOProc.");
|
2004-02-20 22:52:20 +00:00
|
|
|
}
|
2003-08-31 13:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageSound_CA::~RageSound_CA()
|
|
|
|
|
{
|
2004-03-24 17:02:02 +00:00
|
|
|
mOutputDevice->StopIOProc(GetData);
|
|
|
|
|
delete gConverter;
|
|
|
|
|
delete mOutputDevice;
|
2003-08-31 13:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-24 17:02:02 +00:00
|
|
|
int64_t RageSound_CA::GetPosition(const RageSoundBase *sound) const
|
2003-08-31 13:41:04 +00:00
|
|
|
{
|
|
|
|
|
AudioTimeStamp time;
|
2004-03-24 17:02:02 +00:00
|
|
|
|
|
|
|
|
mOutputDevice->GetCurrentTime(time);
|
|
|
|
|
return int64_t(time.mSampleTime);
|
2003-08-31 13:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-24 17:02:02 +00:00
|
|
|
void RageSound_CA::FillConverter(void *data, UInt32 dataByteSize)
|
2003-08-31 13:41:04 +00:00
|
|
|
{
|
2004-03-24 17:02:02 +00:00
|
|
|
int frames = dataByteSize / gConverter->GetInputFormat().mBytesPerPacket;
|
|
|
|
|
this->Mix((int16_t *)data, frames, mDecodePos, GetPosition(NULL));
|
2003-08-31 13:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-24 17:02:02 +00:00
|
|
|
OSStatus RageSound_CA::GetData(AudioDeviceID inDevice,
|
|
|
|
|
const AudioTimeStamp *inNow,
|
|
|
|
|
const AudioBufferList *inInputData,
|
|
|
|
|
const AudioTimeStamp *inInputTime,
|
|
|
|
|
AudioBufferList *outOutputData,
|
|
|
|
|
const AudioTimeStamp *inOutputTime,
|
|
|
|
|
void *inClientData)
|
2003-08-31 13:41:04 +00:00
|
|
|
{
|
2004-05-19 00:57:39 +00:00
|
|
|
RageTimer tm;
|
|
|
|
|
|
2004-03-24 17:02:02 +00:00
|
|
|
RageSound_CA *This = (RageSound_CA *)inClientData;
|
|
|
|
|
UInt32 dataPackets = outOutputData->mBuffers[0].mDataByteSize;
|
|
|
|
|
|
|
|
|
|
dataPackets /= gConverter->GetOutputFormat().mBytesPerPacket;
|
|
|
|
|
|
|
|
|
|
This->mDecodePos = int64_t(inOutputTime->mSampleTime);
|
|
|
|
|
gConverter->FillComplexBuffer(dataPackets, *outOutputData, NULL);
|
2004-05-19 00:57:39 +00:00
|
|
|
|
2004-05-23 19:45:19 +00:00
|
|
|
g_fLastIOProcTime = tm.GetDeltaTime();
|
|
|
|
|
++g_iNumIOProcCalls;
|
2004-05-19 00:57:39 +00:00
|
|
|
|
|
|
|
|
return noErr;
|
2004-02-20 16:37:07 +00:00
|
|
|
}
|
2004-03-28 21:18:21 +00:00
|
|
|
|
2004-04-27 05:07:19 +00:00
|
|
|
OSStatus RageSound_CA::OverloadListener(AudioDeviceID inDevice,
|
|
|
|
|
UInt32 inChannel,
|
|
|
|
|
Boolean isInput,
|
|
|
|
|
AudioDevicePropertyID inPropertyID,
|
|
|
|
|
void *inData)
|
|
|
|
|
{
|
2004-05-23 19:45:19 +00:00
|
|
|
LOG->Warn( "Audio overload. Last IOProc time: %f IOProc calls: %i", g_fLastIOProcTime, g_iNumIOProcCalls );
|
|
|
|
|
g_iNumIOProcCalls = 0;
|
2004-04-27 05:07:19 +00:00
|
|
|
return noErr;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-28 21:18:21 +00:00
|
|
|
void RageSound_CA::SetupDecodingThread()
|
|
|
|
|
{
|
|
|
|
|
/* Increase the scheduling precedence of the decoder thread. */
|
|
|
|
|
thread_precedence_policy po;
|
2004-04-25 23:25:38 +00:00
|
|
|
po.importance = 32;
|
|
|
|
|
kern_return_t ret = thread_policy_set( mach_thread_self(), THREAD_PRECEDENCE_POLICY,
|
2004-03-28 22:37:12 +00:00
|
|
|
(int *)&po, THREAD_PRECEDENCE_POLICY_COUNT );
|
2004-04-25 23:25:38 +00:00
|
|
|
if( ret != KERN_SUCCESS )
|
|
|
|
|
LOG->Warn( "thread_policy_set(THREAD_PRECEDENCE_POLICY) failed: %s", mach_error_string(ret) );
|
2004-03-28 21:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
2004-05-15 20:28:17 +00:00
|
|
|
/*
|
|
|
|
|
* (c) 2004 Steve Checkoway
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|