use RageSoundReader_Resample

(almost not tied to 44100 ...)
This commit is contained in:
Glenn Maynard
2003-04-23 19:11:59 +00:00
parent 8d66bb367c
commit 111eeef550
5 changed files with 31 additions and 29 deletions
+9
View File
@@ -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)
{
+1
View File
@@ -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; }
+1 -2
View File
@@ -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();
+19 -25
View File
@@ -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);
+1 -2
View File
@@ -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;
};