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

215 lines
4.9 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"
2002-12-13 08:33:25 +00:00
#include "SDL.h"
/* samples */
const int channels = 2;
2004-01-12 02:19:56 +00:00
const int bytes_per_frame = channels*2; /* 16-bit */
2002-12-13 08:33:25 +00:00
const int samplerate = 44100;
2002-12-21 07:21:49 +00:00
const int buffersize_frames = 1024*4; /* in frames */
2004-01-12 02:19:56 +00:00
const int buffersize = buffersize_frames * bytes_per_frame; /* in bytes */
2002-12-13 08:33:25 +00:00
2002-12-21 05:16:05 +00:00
/* We'll fill the buffer in chunks this big. This should evenly divide the
* buffer size. */
const int num_chunks = 8;
const int chunksize_frames = buffersize_frames / num_chunks;
const int chunksize = buffersize / num_chunks;
2002-12-13 08:33:25 +00:00
int RageSound_DSound_Software::MixerThread_start(void *p)
{
((RageSound_DSound_Software *) p)->MixerThread();
return 0;
}
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) Sleep(10);
2002-12-21 07:21:49 +00:00
2002-12-21 05:16:05 +00:00
if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL))
2002-12-21 08:44:51 +00:00
LOG->Warn(werr_ssprintf(GetLastError(), "Failed to set sound thread priority"));
2002-12-21 05:16:05 +00:00
2003-07-23 21:25:38 +00:00
/* Fill a buffer before we start playing, so we don't play whatever junk is
* in the buffer. */
while(GetData())
;
/* Start playing. */
str_ds->Play();
2002-12-13 08:33:25 +00:00
while(!shutdown) {
Sleep(10);
while(GetData())
2002-12-21 05:16:05 +00:00
;
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. */
str_ds->Stop();
2002-12-13 08:33:25 +00:00
}
bool RageSound_DSound_Software::GetData()
2002-12-13 08:33:25 +00:00
{
2002-12-21 05:16:05 +00:00
LockMut(SOUNDMAN->lock);
2002-12-21 07:21:49 +00:00
char *locked_buf;
unsigned len;
2003-11-01 18:10:15 +00:00
const int play_pos = str_ds->GetOutputPosition();
2002-12-13 08:33:25 +00:00
2003-11-01 18:10:15 +00:00
if(!str_ds->get_output_buf(&locked_buf, &len, chunksize))
2002-12-21 05:16:05 +00:00
return false;
2002-12-13 08:33:25 +00:00
/* Silence the buffer. */
memset(locked_buf, 0, len);
static Sint16 *buf = NULL;
int bufsize = buffersize_frames * channels;
if(!buf)
{
buf = new Sint16[bufsize];
}
memset(buf, 0, bufsize*sizeof(Uint16));
2002-12-31 01:13:58 +00:00
static SoundMixBuffer mix;
2003-10-16 18:49:12 +00:00
mix.SetVolume( SOUNDMAN->GetMixVolume() );
2002-12-13 08:33:25 +00:00
for(unsigned i = 0; i < sounds.size(); ++i)
{
if(sounds[i]->stopping)
2002-12-21 23:14:46 +00:00
continue;
/* Call the callback. */
unsigned got = sounds[i]->snd->GetPCM((char *) buf, len, play_pos);
2003-01-11 04:04:15 +00:00
mix.write((Sint16 *) buf, got/2);
2002-12-21 23:14:46 +00:00
if(got < len)
2002-12-13 08:33:25 +00:00
{
2002-12-21 23:14:46 +00:00
/* This sound is finishing. */
sounds[i]->stopping = true;
2003-11-01 18:10:15 +00:00
sounds[i]->flush_pos = str_ds->GetOutputPosition();
2002-12-13 08:33:25 +00:00
}
}
2002-12-31 01:13:58 +00:00
mix.read((Sint16 *) locked_buf);
2002-12-21 07:21:49 +00:00
str_ds->release_output_buf(locked_buf, len);
2002-12-21 05:16:05 +00:00
return true;
2002-12-13 08:33:25 +00:00
}
void RageSound_DSound_Software::StartMixing(RageSound *snd)
{
sound *s = new sound;
s->snd = snd;
2003-04-12 18:10:50 +00:00
LockMut(SOUNDMAN->lock);
2002-12-13 08:33:25 +00:00
sounds.push_back(s);
}
void RageSound_DSound_Software::Update(float delta)
{
ASSERT(SOUNDMAN);
2002-12-21 05:16:05 +00:00
LockMut(SOUNDMAN->lock);
2002-12-13 08:33:25 +00:00
/* SoundStopped might erase sounds out from under us, so make a copy
* of the sound list. */
vector<sound *> snds = sounds;
for(unsigned i = 0; i < snds.size(); ++i)
{
2002-12-21 23:14:46 +00:00
if(!snds[i]->stopping) continue;
if(GetPosition(snds[i]->snd) < snds[i]->flush_pos)
continue; /* stopping but still flushing */
/* This sound is done. */
2002-12-22 08:13:33 +00:00
snds[i]->snd->StopPlaying();
2002-12-13 08:33:25 +00:00
}
}
void RageSound_DSound_Software::StopMixing(RageSound *snd)
{
2002-12-21 05:16:05 +00:00
LockMut(SOUNDMAN->lock);
2002-12-13 08:33:25 +00:00
/* Find the sound. */
unsigned i;
for(i = 0; i < sounds.size(); ++i)
if(sounds[i]->snd == snd) break;
if(i == sounds.size())
{
LOG->Trace("not stopping a sound because it's not playing");
return;
}
delete sounds[i];
sounds.erase(sounds.begin()+i, sounds.begin()+i+1);
2004-01-12 02:19:56 +00:00
/* If nothing is playing, reset the frame count; this is just to
2002-12-21 07:21:49 +00:00
* prevent eventual overflow. */
2002-12-13 08:33:25 +00:00
if(sounds.empty())
2002-12-21 07:21:49 +00:00
str_ds->Reset();
2002-12-13 08:33:25 +00:00
}
int RageSound_DSound_Software::GetPosition(const RageSound *snd) const
{
2002-12-21 05:16:05 +00:00
LockMut(SOUNDMAN->lock);
2002-12-21 07:21:49 +00:00
return str_ds->GetPosition();
2002-12-13 08:33:25 +00:00
}
RageSound_DSound_Software::RageSound_DSound_Software()
{
shutdown = false;
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())
2002-12-21 08:15:35 +00:00
RageException::ThrowNonfatal("Driver unusable (emulated device)");
2002-12-21 07:40:19 +00:00
2002-12-13 08:33:25 +00:00
/* Create a DirectSound stream, but don't force it into hardware. */
2002-12-21 07:21:49 +00:00
str_ds = new DSoundBuf(ds,
DSoundBuf::HW_DONT_CARE,
channels, samplerate, 16, buffersize);
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 = 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
2002-12-21 07:21:49 +00:00
delete str_ds;
2002-12-13 08:33:25 +00:00
}
float RageSound_DSound_Software::GetPlayLatency() const
{
return (1.0f / samplerate) * buffersize_frames;
}
int RageSound_DSound_Software::GetSampleRate( int rate ) const
{
return samplerate;
}
2002-12-13 08:33:25 +00:00
/*
* Copyright (c) 2002 by the person(s) listed below. All rights reserved.
*
* Glenn Maynard
*/