Files
itgmania212121/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp
T

136 lines
3.7 KiB
C++
Raw Normal View History

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 "RageTimer.h"
#include "RageLog.h"
#include "RageSound.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"
2002-12-13 08:33:25 +00:00
2004-02-27 10:01:44 +00:00
static const int channels = 2;
static const int bytes_per_frame = channels*2; /* 16-bit */
static const int samplerate = 44100;
2004-03-01 05:05:43 +00:00
static const int safe_writeahead = 1024*4; /* in frames */
static int max_writeahead;
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;
2004-03-01 05:05:43 +00:00
static int chunksize() { return max_writeahead / num_chunks; }
2002-12-13 08:33:25 +00:00
void RageSound_DSound_Software::MixerThread()
{
2002-12-21 02:10:17 +00:00
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
* assigns it; we might get here before that happens, though. */
while( !SOUNDMAN && !shutdown_mixer_thread )
2004-05-14 05:38:44 +00:00
usleep( 10000 );
2002-12-21 07:21:49 +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
while( !shutdown_mixer_thread )
{
char *locked_buf;
unsigned len;
2004-05-14 05:38:44 +00:00
const int64_t play_pos = pcm->GetOutputPosition(); /* must be called before get_output_buf */
2003-07-23 21:25:38 +00:00
if( !pcm->get_output_buf(&locked_buf, &len, chunksize()) )
{
Sleep( chunksize()*1000 / samplerate );
continue;
}
2003-07-23 21:25:38 +00:00
this->Mix( (int16_t *) locked_buf, len/bytes_per_frame, play_pos, pcm->GetPosition() );
pcm->release_output_buf(locked_buf, len);
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. */
2004-01-12 23:10:14 +00:00
pcm->Stop();
2002-12-13 08:33:25 +00:00
}
2004-01-19 22:21:57 +00:00
int64_t RageSound_DSound_Software::GetPosition( const RageSoundBase *snd ) const
2002-12-13 08:33:25 +00:00
{
2004-01-12 23:10:14 +00:00
return pcm->GetPosition();
2002-12-13 08:33:25 +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()
{
shutdown_mixer_thread = false;
pcm = NULL;
2002-12-13 08:33:25 +00:00
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. */
if( ds.IsEmulated() )
RageException::ThrowNonfatal( "Driver unusable (emulated device)" );
2002-12-21 07:40:19 +00:00
2004-03-01 05:05:43 +00:00
max_writeahead = safe_writeahead;
if( PREFSMAN->m_iSoundWriteAhead )
max_writeahead = PREFSMAN->m_iSoundWriteAhead;
2002-12-13 08:33:25 +00:00
/* Create a DirectSound stream, but don't force it into hardware. */
pcm = new DSoundBuf( ds, DSoundBuf::HW_DONT_CARE, channels, samplerate, 16, max_writeahead );
/* Fill a buffer before we start playing, so we don't play whatever junk is
* in the buffer. */
char *locked_buf;
unsigned len;
while( pcm->get_output_buf(&locked_buf, &len, chunksize()) )
{
memset( locked_buf, 0, len );
pcm->release_output_buf(locked_buf, len);
}
2004-04-02 21:32:25 +00:00
StartDecodeThread();
/* Start playing. */
pcm->Play();
2003-08-05 01:32:51 +00:00
MixingThread.SetName("Mixer thread");
MixingThread.Create( MixerThread_start, this );
2002-12-13 08:33:25 +00:00
}
RageSound_DSound_Software::~RageSound_DSound_Software()
{
/* Signal the mixing thread to quit. */
shutdown_mixer_thread = true;
2003-08-05 01:32:51 +00:00
LOG->Trace("Shutting down mixer thread ...");
LOG->Flush();
2003-08-05 01:32:51 +00:00
MixingThread.Wait();
2002-12-13 08:33:25 +00:00
LOG->Trace("Mixer thread shut down.");
LOG->Flush();
2002-12-13 08:33:25 +00:00
2004-01-12 23:10:14 +00:00
delete pcm;
2002-12-13 08:33:25 +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
{
2004-03-01 05:05:43 +00:00
return (1.0f / samplerate) * max_writeahead;
2002-12-13 08:33:25 +00:00
}
int RageSound_DSound_Software::GetSampleRate( int rate ) const
{
return samplerate;
}
2002-12-13 08:33:25 +00:00
/*
2004-01-12 23:20:52 +00:00
* Copyright (c) 2002-2004 by the person(s) listed below. All rights reserved.
2002-12-13 08:33:25 +00:00
*
* Glenn Maynard
*/