From 2c0b7865d41f544c315a55138d88c2a7d290ab03 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 21 Dec 2002 02:19:37 +0000 Subject: [PATCH] add fake sound support --- stepmania/src/RageSoundManager.cpp | 53 ++++++++++++++++++++++++++++++ stepmania/src/RageSoundManager.h | 11 +++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/stepmania/src/RageSoundManager.cpp b/stepmania/src/RageSoundManager.cpp index 38b9a52bfd..360ff198d9 100644 --- a/stepmania/src/RageSoundManager.cpp +++ b/stepmania/src/RageSoundManager.cpp @@ -47,10 +47,24 @@ void RageSoundManager::StopMixing(RageSound *snd) sounds_to_delete.insert(snd); owned_sounds.erase(snd); } + + map::iterator fake = + fake_sounds.find(const_cast(snd)); + if(fake != fake_sounds.end()) + fake_sounds.erase(fake); } int RageSoundManager::GetPosition(const RageSound *snd) const { + map::const_iterator fake = + fake_sounds.find(const_cast(snd)); + if(fake != fake_sounds.end()) + { + float time_since = RageTimer::GetTimeSinceStart() - fake->second.begin; + + return int(time_since * 44100); // XXX + } + return driver->GetPosition(snd); } @@ -67,6 +81,45 @@ void RageSoundManager::Update(float delta) (*i)->Update(delta); driver->Update(delta); + + /* We keep a list of "fake" sound effects. These are sound effects that + * have been requested to play, but the driver couldn't play them; usually + * because it's run out of sound channels. Pretend we're playing them, + * since this should be uncommon. */ + { + char buf[1024*16]; + map::iterator j = fake_sounds.begin(), + next = j; + while(j != fake_sounds.end()) + { + next++; + + RageSound *s = j->first; + FakeSound &fake = j->second; + + int bytes = min(unsigned(delta * 44100*4), sizeof(buf)); + + int now = int(RageTimer::GetTimeSinceStart() - fake.begin) * 44100; + + int got = s->GetPCM(buf, bytes, now); + if(got < bytes) + s->SoundStopped(); + + j = next; + } + } +} + +void RageSoundManager::AddFakeSound(RageSound *snd) +{ + map::const_iterator fake = + fake_sounds.find(const_cast(snd)); + + ASSERT(fake == fake_sounds.end()); + FakeSound newfake; + newfake.begin = RageTimer::GetTimeSinceStart(); + newfake.samples_read = 0; + fake_sounds[snd] = newfake; } float RageSoundManager::GetPlayLatency() const diff --git a/stepmania/src/RageSoundManager.h b/stepmania/src/RageSoundManager.h index 2f273418ea..33245e1ab4 100644 --- a/stepmania/src/RageSoundManager.h +++ b/stepmania/src/RageSoundManager.h @@ -2,6 +2,7 @@ #define RAGE_SOUND_MANAGER_H #include +#include #include "arch/Sound/RageSoundDriver.h" #include "RageThreads.h" @@ -20,6 +21,12 @@ class RageSoundManager /* Set of sounds that are finished and should be deleted. */ set sounds_to_delete; + struct FakeSound { + float begin; + int samples_read; + }; + map fake_sounds; + RageSoundDriver *driver; public: @@ -30,9 +37,9 @@ public: void Update(float delta); void StartMixing(RageSound *snd); /* used by RageSound */ - void StopMixing(RageSound *snd); /* used by RageSound */ + void StopMixing(RageSound *snd); /* used by RageSound */ int GetPosition(const RageSound *snd) const; /* used by RageSound */ - + void AddFakeSound(RageSound *snd); /* used by drivers */ float GetPlayLatency() const; void PlayOnce( CString sPath );