From 922e374aaa8d504fac553f4f3bfc58312c448923 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 20 Feb 2003 22:54:12 +0000 Subject: [PATCH] Make this compile in Windows. Don't use nanosleep; it's nonportable. Use SDL_Delay() instead. --- .../src/arch/Sound/RageSoundDriver_Null.cpp | 56 +++++++++---------- .../src/arch/Sound/RageSoundDriver_Null.h | 29 +++++----- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp b/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp index 7e81190cb1..aec76a5ad1 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp @@ -6,7 +6,7 @@ #include "RageSound.h" #include "RageUtil.h" -#include "SDL.h" +#include "SDL_utils.h" const int channels = 2; const int samplesize = channels*2; /* 16-bit */ @@ -14,10 +14,6 @@ const int samplerate = 44100; const int buffersize_frames = 1024*8; /* in frames */ const int buffersize = buffersize_frames * samplesize; /* in bytes */ -const int num_chunks = 8; -const int chunksize_frames = buffersize_frames / num_chunks; -const int chunksize = buffersize / num_chunks; - int RageSound_Null::MixerThread_start(void *p) { @@ -31,15 +27,24 @@ void RageSound_Null::MixerThread() * assigns it; we might get here before that happens, though. */ while(!SOUNDMAN && !shutdown) SDL_Delay(10); - /* not sure if a nansleep is needed, but certainly helps - * when there is LOG->Trace in GetData() */ - struct timespec delay, remaining; - delay.tv_sec = 0; - delay.tv_nsec = 1000; + RageTimer Speaker; while(!shutdown) { + float delay_ms = float(buffersize_frames) / samplerate; + SDL_Delay(int(delay_ms / 2)); + + LockMutex L(SOUNDMAN->lock); + + /* "Play" frames. */ + const float ms_passed = Speaker.GetDeltaTime(); + const int samples_played = int(samplerate * ms_passed); + for(unsigned i = 0; i < sounds.size(); ++i) + { + sounds[i]->samples_buffered -= samples_played; + sounds[i]->samples_buffered = max(sounds[i]->samples_buffered, 0); + } + GetData(); - nanosleep(&delay, &remaining); } } @@ -49,7 +54,7 @@ bool RageSound_Null::GetData() /* Create a 32-bit buffer to mix sounds. */ static Sint16 *buf = NULL; - int bufsize = chunksize_frames * channels; + int bufsize = buffersize_frames * channels; if(!buf) buf = new Sint16[bufsize]; @@ -58,25 +63,24 @@ bool RageSound_Null::GetData() if(sounds[i]->stopping) continue; - /* Call the callback. */ - unsigned got = sounds[i]->snd->GetPCM((char *) buf, chunksize, last_cursor_pos); + unsigned samples_needed = buffersize_frames - sounds[i]->samples_buffered; + unsigned bytes_needed = samples_needed*samplesize; + int Play_Time = int(RageTimer::GetTimeSinceStart() * samplerate); + Play_Time += sounds[i]->samples_buffered; - if(got < chunksize) + /* Call the callback. */ + unsigned got = sounds[i]->snd->GetPCM((char *) buf, bytes_needed, Play_Time); + sounds[i]->samples_buffered += got/samplesize; + + if(got < bytes_needed) { /* This sound is finishing. */ sounds[i]->stopping = true; - sounds[i]->flush_pos = last_cursor_pos + (got / samplesize); } } // and, do nothing! it's silence - //XXX to make timing more accurate, nanosleep here - // as if playing the chunk - - /* Increment last_cursor_pos. */ - last_cursor_pos += chunksize_frames; - return true; } @@ -101,7 +105,7 @@ void RageSound_Null::Update(float delta) { if(!sounds[i]->stopping) continue; - if(GetPosition(snds[i]->snd) < sounds[i]->flush_pos) + if(sounds[i]->samples_buffered) continue; /* stopping but still flushing */ /* This sound is done. */ @@ -129,16 +133,12 @@ void RageSound_Null::StopMixing(RageSound *snd) int RageSound_Null::GetPosition(const RageSound *snd) const { - LockMutex L(SOUNDMAN->lock); - - return time(NULL)-startup_time; + return int(RageTimer::GetTimeSinceStart() * samplerate); } RageSound_Null::RageSound_Null() { shutdown = false; - last_cursor_pos = 0; - startup_time = time(NULL); MixerThreadPtr = SDL_CreateThread(MixerThread_start, this); } diff --git a/stepmania/src/arch/Sound/RageSoundDriver_Null.h b/stepmania/src/arch/Sound/RageSoundDriver_Null.h index 77d055a2a0..0aefb84657 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_Null.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_Null.h @@ -20,24 +20,21 @@ class RageSound_Null: public RageSoundDriver public: struct sound { RageSound *snd; - bool stopping; - int flush_pos; /* state == STOPPING only */ - sound() { snd = NULL; stopping=false; } - }; - - /* List of currently playing sounds: */ - vector sounds; + bool stopping; + int samples_buffered; /* fake */ + sound() { snd = NULL; stopping=false; samples_buffered = 0; } + }; + /* List of currently playing sounds: */ + vector sounds; bool shutdown; - int last_cursor_pos; - time_t startup_time; - static int MixerThread_start(void *p); - void MixerThread(); - SDL_Thread *MixerThreadPtr; + static int MixerThread_start(void *p); + void MixerThread(); + SDL_Thread *MixerThreadPtr; - bool GetData(); + bool GetData(); /* virtuals: */ void StartMixing(RageSound *snd); @@ -45,10 +42,10 @@ public: int GetPosition(const RageSound *snd) const; float GetPlayLatency() const; - void RageSound_Null::Update(float delta); + void Update(float delta); - RageSound_Null(); - ~RageSound_Null(); + RageSound_Null(); + ~RageSound_Null(); }; #endif