From 9c23f3504725340b03aeb1ef6ec8e1eea3309ad6 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 21 Aug 2004 06:09:19 +0000 Subject: [PATCH] allocate buffer on demand --- .../RageSoundDriver_Generic_Software.cpp | 23 +++++++++++-------- .../Sound/RageSoundDriver_Generic_Software.h | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.cpp b/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.cpp index 866af9cde0..0d25df10a1 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.cpp @@ -10,8 +10,8 @@ static const int channels = 2; static const int bytes_per_frame = channels*2; /* 16-bit */ static int frames_to_buffer; -static const int num_chunks = 8; -static int chunksize() { return frames_to_buffer / num_chunks; } +/* 512 is about 10ms, which is big enough for the tolerance of most schedulers. */ +static int chunksize() { return 512; } static int underruns = 0, logged_underruns = 0; @@ -22,15 +22,20 @@ RageSound_Generic_Software::sound::sound() available = true; } -void RageSound_Generic_Software::sound::Init() +void RageSound_Generic_Software::sound::Allocate( int frames ) { /* Reserve enough blocks in the buffer to hold the buffer. Add one, to account for * the fact that we may have a partial block due to a previous Mix() call. */ const int frames_per_block = samples_per_block / channels; - const int blocks_to_prebuffer = frames_to_buffer / frames_per_block; + const int blocks_to_prebuffer = frames / frames_per_block; buffer.reserve( blocks_to_prebuffer + 1 ); } +void RageSound_Generic_Software::sound::Deallocate() +{ + buffer.reserve( 0 ); +} + int RageSound_Generic_Software::DecodeThread_start( void *p ) { ((RageSound_Generic_Software *) p)->DecodeThread(); @@ -59,8 +64,9 @@ void RageSound_Generic_Software::Mix( int16_t *buf, int frames, int64_t frameno, if( s.state == sound::HALTING ) { /* This indicates that this stream can be reused. */ + s.Deallocate(); s.state = sound::STOPPED; - s.available = true; + s.available = true; /* do this last */ // LOG->Trace("set %p from HALTING to STOPPED", sounds[i].snd); continue; @@ -295,6 +301,9 @@ void RageSound_Generic_Software::StartMixing( RageSoundBase *snd ) s.volume = snd->GetVolume(); s.buffer.clear(); + /* Initialize the sound buffer. */ + s.Allocate( frames_to_buffer ); + // LOG->Trace("StartMixing(%s) (%p)", s.snd->GetLoadedFilePath().c_str(), s.snd ); /* Prebuffer some frames before changing the sound to PLAYING. */ @@ -355,10 +364,6 @@ void RageSound_Generic_Software::StartDecodeThread() { ASSERT( !m_DecodeThread.IsCreated() ); - /* Initialize the sound buffers, now that frames_to_buffer is finalized. */ - for( unsigned i = 0; i < ARRAYSIZE(sounds); ++i ) - sounds[i].Init(); - m_DecodeThread.Create( DecodeThread_start, this ); } diff --git a/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.h b/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.h index 7e9a1548c1..a7e5332998 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_Generic_Software.h @@ -121,7 +121,8 @@ private: } state; sound(); - void Init(); + void Allocate( int frames ); + void Deallocate(); }; /* List of currently playing sounds: XXX no vector */