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
+9 -9
View File
@@ -46,12 +46,8 @@ bool DSound::IsEmulated() const
}
DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
int channels_, int samplerate_, int samplebits_, int writeahead_,
float vol)
int channels_, int samplerate_, int samplebits_, int writeahead_)
{
ASSERT(vol >= 0);
ASSERT(vol <= 1);
channels = channels_;
samplerate = samplerate_;
samplebits = samplebits_;
@@ -79,15 +75,14 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
DSBUFFERDESC format;
memset(&format, 0, 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
* only use streaming buffers. */
if(hardware == HW_HARDWARE)
format.dwFlags |= DSBCAPS_LOCHARDWARE;
else
format.dwFlags |= DSBCAPS_LOCSOFTWARE;
if(vol != 1)
format.dwFlags |= DSBCAPS_CTRLVOLUME;
format.dwBufferBytes = buffersize;
format.dwReserved = 0;
format.lpwfxFormat = &waveformat;
@@ -113,6 +108,12 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
buffersize = bcaps.dwBufferBytes;
writeahead = min(writeahead, buffersize);
}
}
void DSoundBuf::SetVolume(float vol)
{
ASSERT(vol >= 0);
ASSERT(vol <= 1);
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));
}
DSoundBuf::~DSoundBuf()
{
buf->Release();
+2 -2
View File
@@ -39,8 +39,7 @@ class DSoundBuf
public:
enum hw { HW_HARDWARE, HW_SOFTWARE, HW_DONT_CARE };
DSoundBuf(DSound &ds, hw hardware,
int channels, int samplerate, int samplebits, int writeahead,
float vol=1);
int channels, int samplerate, int samplebits, int writeahead);
bool get_output_buf(char **buffer, unsigned *bufsiz, int *play_pos, int chunksize);
void release_output_buf(char *buffer, unsigned bufsiz);
@@ -48,6 +47,7 @@ public:
void Reset();
void Play();
void Stop();
void SetVolume(float vol);
~DSoundBuf();
int GetPosition() const;
@@ -33,6 +33,10 @@ protected:
* hearing it. (This isn't necessarily the same as the buffer latency.) */
virtual float GetPlayLatency() const { return 0.0f; }
/* This is called if the volume changed; call SOUNDMAN->GetMixVolume() to
* get it. */
virtual void VolumeChanged() { }
public:
virtual ~RageSoundDriver() { }
};
@@ -12,7 +12,6 @@
#include "../../RageUtil.h"
#include "../../RageSound.h"
#include "../../RageLog.h"
#include "../../PrefsManager.h"
#include "../../tls.h"
#include "SDL.h"
@@ -165,8 +164,7 @@ RageSound_DSound::RageSound_DSound()
try {
newbuf = new DSoundBuf(ds,
DSoundBuf::HW_HARDWARE,
channels, samplerate, 16, buffersize,
PREFSMAN->m_fSoundVolume);
channels, samplerate, 16, buffersize);
} catch(const RageException &e) {
/* If we didn't get at least 8, fail. */
if(i >= 8) break; /* OK */
@@ -188,8 +186,12 @@ RageSound_DSound::RageSound_DSound()
s->str_ds = newbuf;
stream_pool.push_back(s);
}
LOG->Trace("Got %i hardware buffers", stream_pool.size());
/* Set channel volumes. */
VolumeChanged();
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
}
@@ -197,12 +199,24 @@ RageSound_DSound::~RageSound_DSound()
{
/* Signal the mixing thread to quit. */
shutdown = true;
LOG->Trace("Shutting down mixer thread ...");
LOG->Flush();
SDL_WaitThread(MixerThreadPtr, NULL);
LOG->Trace("Mixer thread shut down.");
LOG->Flush();
for(unsigned i = 0; i < stream_pool.size(); ++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)
{
LockMutex L(SOUNDMAN->lock);
@@ -48,6 +48,7 @@ class RageSound_DSound: public RageSoundDriver
void StopMixing(RageSound *snd); /* used by RageSound */
int GetPosition(const RageSound *snd) const;
void Update(float delta);
void VolumeChanged();
public:
RageSound_DSound();
@@ -43,7 +43,6 @@ void RageSound_DSound_Software::MixerThread()
while(!shutdown) {
Sleep(10);
while(GetData())
;
}
@@ -179,9 +178,11 @@ RageSound_DSound_Software::~RageSound_DSound_Software()
{
/* Signal the mixing thread to quit. */
shutdown = true;
LOG->Trace("Shutting down mixer thread ...");
LOG->Trace("Shutting down mixer thread %p ...", MixerThreadPtr);
LOG->Flush();
SDL_WaitThread(MixerThreadPtr, NULL);
LOG->Trace("Mixer thread shut down.");
LOG->Flush();
delete str_ds;
}