2003-02-16 04:02:21 +00:00
|
|
|
#include "global.h"
|
2002-12-13 08:33:25 +00:00
|
|
|
#include "RageSoundDriver_DSound_Software.h"
|
|
|
|
|
#include "DSoundHelpers.h"
|
|
|
|
|
|
2003-01-24 22:28:23 +00:00
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageUtil.h"
|
2003-07-26 23:12:38 +00:00
|
|
|
#include "RageSoundManager.h"
|
2004-03-01 05:05:43 +00:00
|
|
|
#include "PrefsManager.h"
|
2006-09-18 22:45:42 +00:00
|
|
|
#include "archutils/Win32/ErrorStrings.h"
|
2002-12-13 08:33:25 +00:00
|
|
|
|
2006-11-18 20:33:35 +00:00
|
|
|
REGISTER_SOUND_DRIVER_CLASS2( DirectSound-sw, DSound_Software );
|
|
|
|
|
|
2004-02-27 10:01:44 +00:00
|
|
|
static const int channels = 2;
|
|
|
|
|
static const int bytes_per_frame = channels*2; /* 16-bit */
|
2004-03-01 05:05:43 +00:00
|
|
|
static const int safe_writeahead = 1024*4; /* in frames */
|
2005-10-24 02:14:43 +00:00
|
|
|
static int g_iMaxWriteahead;
|
2002-12-13 08:33:25 +00:00
|
|
|
|
2004-03-01 05:05:43 +00:00
|
|
|
/* We'll fill the buffer in chunks this big. */
|
2004-02-27 10:01:44 +00:00
|
|
|
static const int num_chunks = 8;
|
2005-10-24 02:14:43 +00:00
|
|
|
static int chunksize() { return g_iMaxWriteahead / num_chunks; }
|
2002-12-13 08:33:25 +00:00
|
|
|
|
|
|
|
|
void RageSound_DSound_Software::MixerThread()
|
|
|
|
|
{
|
2004-03-18 04:27:22 +00:00
|
|
|
if( !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) )
|
|
|
|
|
if( !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) )
|
|
|
|
|
LOG->Warn(werr_ssprintf(GetLastError(), "Failed to set sound thread priority"));
|
2002-12-21 05:16:05 +00:00
|
|
|
|
2006-12-06 06:19:57 +00:00
|
|
|
/* Fill a buffer before we start playing, so we don't play whatever junk is
|
|
|
|
|
* in the buffer. */
|
|
|
|
|
char *locked_buf;
|
|
|
|
|
unsigned len;
|
|
|
|
|
while( m_pPCM->get_output_buf(&locked_buf, &len, chunksize()) )
|
|
|
|
|
{
|
|
|
|
|
memset( locked_buf, 0, len );
|
|
|
|
|
m_pPCM->release_output_buf(locked_buf, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Start playing. */
|
|
|
|
|
m_pPCM->Play();
|
|
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
while( !m_bShutdownMixerThread )
|
2004-03-18 04:27:22 +00:00
|
|
|
{
|
2005-10-24 02:14:43 +00:00
|
|
|
char *pLockedBuf;
|
|
|
|
|
unsigned iLen;
|
|
|
|
|
const int64_t iPlayPos = m_pPCM->GetOutputPosition(); /* must be called before get_output_buf */
|
2003-07-23 21:25:38 +00:00
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
if( !m_pPCM->get_output_buf(&pLockedBuf, &iLen, chunksize()) )
|
2004-03-18 04:27:22 +00:00
|
|
|
{
|
2005-10-24 02:37:04 +00:00
|
|
|
Sleep( chunksize()*1000 / m_iSampleRate );
|
2004-03-18 04:27:22 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2003-07-23 21:25:38 +00:00
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
this->Mix( (int16_t *) pLockedBuf, iLen/bytes_per_frame, iPlayPos, m_pPCM->GetPosition() );
|
2004-03-18 04:27:22 +00:00
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
m_pPCM->release_output_buf( pLockedBuf, iLen );
|
2002-12-13 08:33:25 +00:00
|
|
|
}
|
2003-11-01 06:41:14 +00:00
|
|
|
|
|
|
|
|
/* I'm not sure why, but if we don't stop the stream now, then the thread will take
|
|
|
|
|
* 90ms (our buffer size) longer to close. */
|
2005-10-24 02:14:43 +00:00
|
|
|
m_pPCM->Stop();
|
2002-12-13 08:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
int64_t RageSound_DSound_Software::GetPosition( const RageSoundBase *pSound ) const
|
2002-12-13 08:33:25 +00:00
|
|
|
{
|
2005-10-24 02:14:43 +00:00
|
|
|
return m_pPCM->GetPosition();
|
2002-12-13 08:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-18 04:27:22 +00:00
|
|
|
int RageSound_DSound_Software::MixerThread_start(void *p)
|
|
|
|
|
{
|
|
|
|
|
((RageSound_DSound_Software *) p)->MixerThread();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-13 08:33:25 +00:00
|
|
|
RageSound_DSound_Software::RageSound_DSound_Software()
|
|
|
|
|
{
|
2005-10-24 02:14:43 +00:00
|
|
|
m_bShutdownMixerThread = false;
|
|
|
|
|
m_pPCM = NULL;
|
2004-11-30 21:49:02 +00:00
|
|
|
}
|
2002-12-13 08:33:25 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
RString RageSound_DSound_Software::Init()
|
2004-11-30 21:49:02 +00:00
|
|
|
{
|
2006-01-22 01:00:06 +00:00
|
|
|
RString sError = ds.Init();
|
2004-12-01 00:06:41 +00:00
|
|
|
if( sError != "" )
|
|
|
|
|
return sError;
|
|
|
|
|
|
2002-12-21 07:40:19 +00:00
|
|
|
/* If we're emulated, we're better off with the WaveOut driver; DS
|
|
|
|
|
* emulation tends to be desynced. */
|
2004-03-18 04:27:22 +00:00
|
|
|
if( ds.IsEmulated() )
|
2004-11-30 21:49:02 +00:00
|
|
|
return "Driver unusable (emulated device)";
|
2002-12-21 07:40:19 +00:00
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
g_iMaxWriteahead = safe_writeahead;
|
2004-03-01 05:05:43 +00:00
|
|
|
if( PREFSMAN->m_iSoundWriteAhead )
|
2005-10-24 02:14:43 +00:00
|
|
|
g_iMaxWriteahead = PREFSMAN->m_iSoundWriteAhead;
|
2004-03-01 05:05:43 +00:00
|
|
|
|
2002-12-13 08:33:25 +00:00
|
|
|
/* Create a DirectSound stream, but don't force it into hardware. */
|
2005-10-24 02:14:43 +00:00
|
|
|
m_pPCM = new DSoundBuf;
|
2005-10-24 02:37:04 +00:00
|
|
|
m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
|
|
|
|
|
sError = m_pPCM->Init( ds, DSoundBuf::HW_DONT_CARE, channels, m_iSampleRate, 16, g_iMaxWriteahead );
|
2004-12-01 00:06:41 +00:00
|
|
|
if( sError != "" )
|
|
|
|
|
return sError;
|
2004-03-18 04:27:22 +00:00
|
|
|
|
2005-10-24 02:37:04 +00:00
|
|
|
LOG->Info( "Software mixing at %i hz", m_iSampleRate );
|
|
|
|
|
|
2004-04-02 21:32:25 +00:00
|
|
|
StartDecodeThread();
|
|
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
m_MixingThread.SetName("Mixer thread");
|
|
|
|
|
m_MixingThread.Create( MixerThread_start, this );
|
2004-11-30 21:49:02 +00:00
|
|
|
|
2006-01-22 01:00:06 +00:00
|
|
|
return RString();
|
2002-12-13 08:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageSound_DSound_Software::~RageSound_DSound_Software()
|
|
|
|
|
{
|
|
|
|
|
/* Signal the mixing thread to quit. */
|
2005-10-24 02:14:43 +00:00
|
|
|
if( m_MixingThread.IsCreated() )
|
2004-11-30 21:49:02 +00:00
|
|
|
{
|
2005-10-24 02:14:43 +00:00
|
|
|
m_bShutdownMixerThread = true;
|
2004-11-30 21:49:02 +00:00
|
|
|
LOG->Trace("Shutting down mixer thread ...");
|
|
|
|
|
LOG->Flush();
|
2005-10-24 02:14:43 +00:00
|
|
|
m_MixingThread.Wait();
|
2004-11-30 21:49:02 +00:00
|
|
|
LOG->Trace("Mixer thread shut down.");
|
|
|
|
|
LOG->Flush();
|
|
|
|
|
}
|
2002-12-13 08:33:25 +00:00
|
|
|
|
2005-10-24 02:14:43 +00:00
|
|
|
delete m_pPCM;
|
2002-12-13 08:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-18 04:27:22 +00:00
|
|
|
void RageSound_DSound_Software::SetupDecodingThread()
|
|
|
|
|
{
|
|
|
|
|
if( !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) )
|
|
|
|
|
LOG->Warn( werr_ssprintf(GetLastError(), "Failed to set decoding thread priority") );
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-13 08:33:25 +00:00
|
|
|
float RageSound_DSound_Software::GetPlayLatency() const
|
|
|
|
|
{
|
2005-10-24 02:37:04 +00:00
|
|
|
return (1.0f / m_iSampleRate) * g_iMaxWriteahead;
|
2002-12-13 08:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
2004-01-08 07:33:29 +00:00
|
|
|
int RageSound_DSound_Software::GetSampleRate( int rate ) const
|
|
|
|
|
{
|
2005-10-24 02:37:04 +00:00
|
|
|
return m_iSampleRate;
|
2004-01-08 07:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
2002-12-13 08:33:25 +00:00
|
|
|
/*
|
2004-05-15 20:28:17 +00:00
|
|
|
* (c) 2002-2004 Glenn Maynard
|
|
|
|
|
* 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.
|
2002-12-13 08:33:25 +00:00
|
|
|
*/
|