properly abstract the volume so we don't touch prefsman in RSM

fixes crash
This commit is contained in:
Glenn Maynard
2003-01-19 19:55:24 +00:00
parent ee211f3b74
commit bfd2d10a15
9 changed files with 54 additions and 21 deletions
+11 -5
View File
@@ -9,7 +9,6 @@
#include "RageSound.h" #include "RageSound.h"
#include "RageLog.h" #include "RageLog.h"
#include "RageTimer.h" #include "RageTimer.h"
#include "PrefsManager.h"
#include "arch/arch.h" #include "arch/arch.h"
#include "arch/Sound/RageSoundDriver.h" #include "arch/Sound/RageSoundDriver.h"
@@ -17,13 +16,14 @@
RageSoundManager::RageSoundManager(CString drivers) RageSoundManager::RageSoundManager(CString drivers)
{ {
/* needs to be done first */
SOUNDMAN = this;
MixVolume = 1.0f;
driver = MakeRageSoundDriver(drivers); driver = MakeRageSoundDriver(drivers);
if(!driver) if(!driver)
RageException::Throw("Couldn't find a sound driver that works"); RageException::Throw("Couldn't find a sound driver that works");
/* needs to be done before RageSound::RageSound */
SOUNDMAN = this;
music = new RageSound; music = new RageSound;
} }
@@ -271,9 +271,15 @@ void RageSoundManager::PlayMusic(CString file, bool loop, float start_sec, float
music->StartPlaying(); music->StartPlaying();
} }
void RageSoundManager::SetPrefs(float MixVol)
{
MixVolume = MixVol;
driver->VolumeChanged();
}
SoundMixBuffer::SoundMixBuffer() SoundMixBuffer::SoundMixBuffer()
{ {
vol = PREFSMAN->m_fSoundVolume; vol = SOUNDMAN->GetMixVolume();
} }
void SoundMixBuffer::write(const Sint16 *buf, unsigned size) void SoundMixBuffer::write(const Sint16 *buf, unsigned size)
+6
View File
@@ -28,12 +28,18 @@ class RageSoundManager
RageSoundDriver *driver; RageSoundDriver *driver;
/* Prefs: */
float MixVolume;
public: public:
RageMutex lock; RageMutex lock;
RageSoundManager(CString drivers); RageSoundManager(CString drivers);
~RageSoundManager(); ~RageSoundManager();
float GetMixVolume() const { return MixVolume; }
void SetPrefs(float MixVol);
void Update(float delta); void Update(float delta);
void StartMixing(RageSound *snd); /* used by RageSound */ void StartMixing(RageSound *snd); /* used by RageSound */
void StopMixing(RageSound *snd); /* used by RageSound */ void StopMixing(RageSound *snd); /* used by RageSound */
+1
View File
@@ -268,6 +268,7 @@ int main(int argc, char* argv[])
GAMEMAN = new GameManager; GAMEMAN = new GameManager;
THEME = new ThemeManager; THEME = new ThemeManager;
SOUNDMAN = new RageSoundManager(PREFSMAN->m_bSoundDrivers); SOUNDMAN = new RageSoundManager(PREFSMAN->m_bSoundDrivers);
SOUNDMAN->SetPrefs(PREFSMAN->m_fSoundVolume);
ANNOUNCER = new AnnouncerManager; ANNOUNCER = new AnnouncerManager;
INPUTFILTER = new InputFilter; INPUTFILTER = new InputFilter;
INPUTMAPPER = new InputMapper; INPUTMAPPER = new InputMapper;
+9 -9
View File
@@ -46,12 +46,8 @@ bool DSound::IsEmulated() const
} }
DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
int channels_, int samplerate_, int samplebits_, int writeahead_, int channels_, int samplerate_, int samplebits_, int writeahead_)
float vol)
{ {
ASSERT(vol >= 0);
ASSERT(vol <= 1);
channels = channels_; channels = channels_;
samplerate = samplerate_; samplerate = samplerate_;
samplebits = samplebits_; samplebits = samplebits_;
@@ -79,15 +75,14 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
DSBUFFERDESC format; DSBUFFERDESC format;
memset(&format, 0, sizeof(format)); memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format); format.dwSize = sizeof(format);
format.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; format.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLVOLUME;
/* Don't use DSBCAPS_STATIC. It's meant for static buffers, and we /* Don't use DSBCAPS_STATIC. It's meant for static buffers, and we
* only use streaming buffers. */ * only use streaming buffers. */
if(hardware == HW_HARDWARE) if(hardware == HW_HARDWARE)
format.dwFlags |= DSBCAPS_LOCHARDWARE; format.dwFlags |= DSBCAPS_LOCHARDWARE;
else else
format.dwFlags |= DSBCAPS_LOCSOFTWARE; format.dwFlags |= DSBCAPS_LOCSOFTWARE;
if(vol != 1)
format.dwFlags |= DSBCAPS_CTRLVOLUME;
format.dwBufferBytes = buffersize; format.dwBufferBytes = buffersize;
format.dwReserved = 0; format.dwReserved = 0;
format.lpwfxFormat = &waveformat; format.lpwfxFormat = &waveformat;
@@ -113,6 +108,12 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
buffersize = bcaps.dwBufferBytes; buffersize = bcaps.dwBufferBytes;
writeahead = min(writeahead, buffersize); writeahead = min(writeahead, buffersize);
} }
}
void DSoundBuf::SetVolume(float vol)
{
ASSERT(vol >= 0);
ASSERT(vol <= 1);
float vl2 = log10f(vol) / log10f(2); /* vol log 2 */ float vl2 = log10f(vol) / log10f(2); /* vol log 2 */
@@ -122,7 +123,6 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
buf->SetVolume(max(int(1000 * vl2), DSBVOLUME_MIN)); buf->SetVolume(max(int(1000 * vl2), DSBVOLUME_MIN));
} }
DSoundBuf::~DSoundBuf() DSoundBuf::~DSoundBuf()
{ {
buf->Release(); buf->Release();
+2 -2
View File
@@ -39,8 +39,7 @@ class DSoundBuf
public: public:
enum hw { HW_HARDWARE, HW_SOFTWARE, HW_DONT_CARE }; enum hw { HW_HARDWARE, HW_SOFTWARE, HW_DONT_CARE };
DSoundBuf(DSound &ds, hw hardware, DSoundBuf(DSound &ds, hw hardware,
int channels, int samplerate, int samplebits, int writeahead, int channels, int samplerate, int samplebits, int writeahead);
float vol=1);
bool get_output_buf(char **buffer, unsigned *bufsiz, int *play_pos, int chunksize); bool get_output_buf(char **buffer, unsigned *bufsiz, int *play_pos, int chunksize);
void release_output_buf(char *buffer, unsigned bufsiz); void release_output_buf(char *buffer, unsigned bufsiz);
@@ -48,6 +47,7 @@ public:
void Reset(); void Reset();
void Play(); void Play();
void Stop(); void Stop();
void SetVolume(float vol);
~DSoundBuf(); ~DSoundBuf();
int GetPosition() const; int GetPosition() const;
@@ -33,6 +33,10 @@ protected:
* hearing it. (This isn't necessarily the same as the buffer latency.) */ * hearing it. (This isn't necessarily the same as the buffer latency.) */
virtual float GetPlayLatency() const { return 0.0f; } virtual float GetPlayLatency() const { return 0.0f; }
/* This is called if the volume changed; call SOUNDMAN->GetMixVolume() to
* get it. */
virtual void VolumeChanged() { }
public: public:
virtual ~RageSoundDriver() { } virtual ~RageSoundDriver() { }
}; };
@@ -12,7 +12,6 @@
#include "../../RageUtil.h" #include "../../RageUtil.h"
#include "../../RageSound.h" #include "../../RageSound.h"
#include "../../RageLog.h" #include "../../RageLog.h"
#include "../../PrefsManager.h"
#include "../../tls.h" #include "../../tls.h"
#include "SDL.h" #include "SDL.h"
@@ -165,8 +164,7 @@ RageSound_DSound::RageSound_DSound()
try { try {
newbuf = new DSoundBuf(ds, newbuf = new DSoundBuf(ds,
DSoundBuf::HW_HARDWARE, DSoundBuf::HW_HARDWARE,
channels, samplerate, 16, buffersize, channels, samplerate, 16, buffersize);
PREFSMAN->m_fSoundVolume);
} catch(const RageException &e) { } catch(const RageException &e) {
/* If we didn't get at least 8, fail. */ /* If we didn't get at least 8, fail. */
if(i >= 8) break; /* OK */ if(i >= 8) break; /* OK */
@@ -188,8 +186,12 @@ RageSound_DSound::RageSound_DSound()
s->str_ds = newbuf; s->str_ds = newbuf;
stream_pool.push_back(s); stream_pool.push_back(s);
} }
LOG->Trace("Got %i hardware buffers", stream_pool.size()); LOG->Trace("Got %i hardware buffers", stream_pool.size());
/* Set channel volumes. */
VolumeChanged();
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this); MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
} }
@@ -197,12 +199,24 @@ RageSound_DSound::~RageSound_DSound()
{ {
/* Signal the mixing thread to quit. */ /* Signal the mixing thread to quit. */
shutdown = true; shutdown = true;
LOG->Trace("Shutting down mixer thread ...");
LOG->Flush();
SDL_WaitThread(MixerThreadPtr, NULL); SDL_WaitThread(MixerThreadPtr, NULL);
LOG->Trace("Mixer thread shut down.");
LOG->Flush();
for(unsigned i = 0; i < stream_pool.size(); ++i) for(unsigned i = 0; i < stream_pool.size(); ++i)
delete stream_pool[i]; delete stream_pool[i];
} }
void RageSound_DSound::VolumeChanged()
{
for(unsigned i = 0; i < stream_pool.size(); ++i)
{
stream_pool[i]->str_ds->SetVolume(SOUNDMAN->GetMixVolume());
}
}
void RageSound_DSound::StartMixing(RageSound *snd) void RageSound_DSound::StartMixing(RageSound *snd)
{ {
LockMutex L(SOUNDMAN->lock); LockMutex L(SOUNDMAN->lock);
@@ -48,6 +48,7 @@ class RageSound_DSound: public RageSoundDriver
void StopMixing(RageSound *snd); /* used by RageSound */ void StopMixing(RageSound *snd); /* used by RageSound */
int GetPosition(const RageSound *snd) const; int GetPosition(const RageSound *snd) const;
void Update(float delta); void Update(float delta);
void VolumeChanged();
public: public:
RageSound_DSound(); RageSound_DSound();
@@ -43,7 +43,6 @@ void RageSound_DSound_Software::MixerThread()
while(!shutdown) { while(!shutdown) {
Sleep(10); Sleep(10);
while(GetData()) while(GetData())
; ;
} }
@@ -179,9 +178,11 @@ RageSound_DSound_Software::~RageSound_DSound_Software()
{ {
/* Signal the mixing thread to quit. */ /* Signal the mixing thread to quit. */
shutdown = true; shutdown = true;
LOG->Trace("Shutting down mixer thread ..."); LOG->Trace("Shutting down mixer thread %p ...", MixerThreadPtr);
LOG->Flush();
SDL_WaitThread(MixerThreadPtr, NULL); SDL_WaitThread(MixerThreadPtr, NULL);
LOG->Trace("Mixer thread shut down."); LOG->Trace("Mixer thread shut down.");
LOG->Flush();
delete str_ds; delete str_ds;
} }