From 111eeef550fc7cef10e39c9eacf419d1138331d8 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 23 Apr 2003 19:11:59 +0000 Subject: [PATCH] use RageSoundReader_Resample (almost not tied to 44100 ...) --- stepmania/src/RageSound.cpp | 9 +++++ stepmania/src/RageSoundReader.h | 1 + stepmania/src/RageSoundReader_Preload.cpp | 3 +- stepmania/src/RageSoundReader_SDL_Sound.cpp | 44 +++++++++------------ stepmania/src/RageSoundReader_SDL_Sound.h | 3 +- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index 4fff90b94b..a1abb083fa 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -43,6 +43,7 @@ #include "RageSoundReader_SDL_Sound.h" #include "RageSoundReader_Preload.h" +#include "RageSoundReader_Resample.h" const int channels = 2; const int samplesize = 2 * channels; /* 16-bit */ @@ -176,6 +177,14 @@ bool RageSound::Load(CString sSoundFilePath, int precache) sSoundFilePath.GetString(), NewSample->GetError().c_str()); Sample = NewSample; + if(Sample->GetSampleRate() != samplerate) + { + RageSoundReader_Resample *Resample = new RageSoundReader_Resample; + Resample->Open(Sample); + Resample->SetSampleRate(44100); + Sample = Resample; + } + /* Try to precache. */ if(precache) { diff --git a/stepmania/src/RageSoundReader.h b/stepmania/src/RageSoundReader.h index c6cd116a1a..ad71ae1066 100644 --- a/stepmania/src/RageSoundReader.h +++ b/stepmania/src/RageSoundReader.h @@ -17,6 +17,7 @@ public: virtual int Read(char *buf, unsigned len) = 0; virtual ~SoundReader() { } virtual SoundReader *Copy() const = 0; + virtual int GetSampleRate() const = 0; bool Error() const { return !error.empty(); } string GetError() const { return error; } diff --git a/stepmania/src/RageSoundReader_Preload.cpp b/stepmania/src/RageSoundReader_Preload.cpp index e326e700cb..fd040c2fce 100644 --- a/stepmania/src/RageSoundReader_Preload.cpp +++ b/stepmania/src/RageSoundReader_Preload.cpp @@ -4,7 +4,6 @@ const int channels = 2; const int samplesize = 2 * channels; /* 16-bit */ -const int samplerate = 44100; /* If a sound is smaller than this, we'll load it entirely into memory. */ const int max_prebuf_size = 1024*256; @@ -17,7 +16,7 @@ int SoundReader_Preload::total_samples() const bool SoundReader_Preload::Open(SoundReader *source) { ASSERT(source); -// samplerate = source->GetSampleRate(); + samplerate = source->GetSampleRate(); /* Check the length, and see if we think it'll fit in the buffer. */ int len = source->GetLength_Fast(); diff --git a/stepmania/src/RageSoundReader_SDL_Sound.cpp b/stepmania/src/RageSoundReader_SDL_Sound.cpp index ccfec8e559..ab99190081 100644 --- a/stepmania/src/RageSoundReader_SDL_Sound.cpp +++ b/stepmania/src/RageSoundReader_SDL_Sound.cpp @@ -19,8 +19,6 @@ #include "RageSoundReader_SDL_Sound.h" const int channels = 2; -const int samplesize = 2 * channels; /* 16-bit */ -const int samplerate = 44100; /* The amount of data to read from SDL_sound at once. */ const int read_block_size = 1024; @@ -42,16 +40,10 @@ bool SoundReader_SDL_Sound::Open(CString filename_) Sound_AudioInfo sound_desired; sound_desired.channels = channels; sound_desired.format = AUDIO_S16SYS; - sound_desired.rate = 0; // samplerate; + sound_desired.rate = 0; Sample = Sound_NewSampleFromFile(filename.GetString(), &sound_desired, read_block_size); - - resamp.reset(); - resamp.SetInputChannels(2); - resamp.SetOutputChannels(2); - resamp.SetInputSampleRate(Sample->actual.rate); - resamp.SetOutputSampleRate(samplerate); if(Sample) return true; @@ -87,7 +79,6 @@ int SoundReader_SDL_Sound::GetLength_Fast() const int SoundReader_SDL_Sound::SetPosition(int ms, bool accurate) { - resamp.reset(); if(ms == 0) { Sound_Rewind(Sample); @@ -124,16 +115,7 @@ int SoundReader_SDL_Sound::Read(char *buf, unsigned len) int bytes_read = 0; while(len) { - int size = resamp.read(buf, len); - - if(size == -1) - break; - - buf += size; - len -= size; - bytes_read += size; - - if(size == 0) + if(!avail) { /* Don't read at all if we've already hit EOF (it'll whine). */ if(Sample->flags & SOUND_SAMPLEFLAG_EOF) @@ -142,10 +124,7 @@ int SoundReader_SDL_Sound::Read(char *buf, unsigned len) int cnt = Sound_Decode(Sample); if(Sample->flags & SOUND_SAMPLEFLAG_EOF) - { - resamp.eof(); - continue; - } + return bytes_read; /* EOF */ if(Sample->flags & SOUND_SAMPLEFLAG_ERROR) { @@ -153,13 +132,28 @@ int SoundReader_SDL_Sound::Read(char *buf, unsigned len) return -1; } - resamp.write(Sample->buffer, cnt); + avail = cnt; + inbuf = (const char *) Sample->buffer; } + + unsigned size = min(avail, len); + memcpy(buf, inbuf, size); + buf += size; + len -= size; + inbuf += size; + avail -= size; + bytes_read += size; } return bytes_read; } +int SoundReader_SDL_Sound::GetSampleRate() const +{ + ASSERT(Sample); + return Sample->actual.rate; +} + SoundReader_SDL_Sound::~SoundReader_SDL_Sound() { Sound_FreeSample(Sample); diff --git a/stepmania/src/RageSoundReader_SDL_Sound.h b/stepmania/src/RageSoundReader_SDL_Sound.h index 0f67c3a274..17a71874ae 100644 --- a/stepmania/src/RageSoundReader_SDL_Sound.h +++ b/stepmania/src/RageSoundReader_SDL_Sound.h @@ -3,7 +3,6 @@ #include "RageSoundReader.h" #include "SDL_sound-1.0.0/SDL_sound.h" -#include "RageSoundResampler.h" class SoundReader_SDL_Sound: public SoundReader { Sound_Sample *Sample; @@ -11,7 +10,6 @@ class SoundReader_SDL_Sound: public SoundReader { unsigned avail; int SetPosition(int ms, bool accurate); CString filename; - RageSoundResampler resamp; public: bool Open(CString filename); @@ -20,6 +18,7 @@ public: int SetPosition_Accurate(int ms) { return SetPosition(ms, true); } int SetPosition_Fast(int ms) { return SetPosition(ms, false); } int Read(char *buf, unsigned len); + int GetSampleRate() const; ~SoundReader_SDL_Sound(); SoundReader *Copy() const; };