From 036fd2beb928b2504b2c63908f6fdbfbe57532ae Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 2 Oct 2003 04:56:09 +0000 Subject: [PATCH] Fix: don't chop off the first 26ms of each MP3 (but don't change sync, either). --- stepmania/src/RageSound.cpp | 12 ++++++++++-- stepmania/src/RageSound.h | 1 + stepmania/src/RageSoundReader.h | 2 ++ stepmania/src/RageSoundReader_MP3.cpp | 16 ++++++++++++++-- stepmania/src/RageSoundReader_MP3.h | 3 +++ stepmania/src/RageSoundReader_Preload.cpp | 1 + stepmania/src/RageSoundReader_Preload.h | 2 ++ stepmania/src/RageSoundReader_Resample_Fast.h | 1 + stepmania/src/RageSoundReader_Resample_Good.h | 1 + 9 files changed, 35 insertions(+), 4 deletions(-) diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index d2c2dd0e8d..3b5b63cedc 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -520,7 +520,7 @@ void RageSound::StopPlaying() if(!playing) return; - stopped_position = GetPositionSeconds(); + stopped_position = GetPositionSecondsInternal(); /* Tell the sound manager to stop mixing this sound. */ SOUNDMAN->StopMixing(this); @@ -555,7 +555,8 @@ float RageSound::GetLengthSeconds() return len / 1000.f; /* ms -> secs */ } -float RageSound::GetPositionSeconds() const +/* Get the position, not counting GetOffsetFix. */ +float RageSound::GetPositionSecondsInternal() const { LockMut(SOUNDMAN->lock); @@ -624,6 +625,13 @@ float RageSound::GetPositionSeconds() const return GetPlaybackRate() * closest_position / float(samplerate()); } +float RageSound::GetPositionSeconds() const +{ + LOG->Trace("XXX fix %f", Sample->GetOffsetFix()); + return GetPositionSecondsInternal() + Sample->GetOffsetFix(); +} + + bool RageSound::SetPositionSeconds( float fSeconds ) { return SetPositionSamples( fSeconds == -1? -1: int(fSeconds * samplerate()) ); diff --git a/stepmania/src/RageSound.h b/stepmania/src/RageSound.h index 6c69cca6d0..c6f6036c61 100644 --- a/stepmania/src/RageSound.h +++ b/stepmania/src/RageSound.h @@ -132,6 +132,7 @@ private: CString error; + float GetPositionSecondsInternal() const; bool SetPositionSamples( int samples = -1 ); int GetData(char *buffer, int size); void Fail(CString reason); diff --git a/stepmania/src/RageSoundReader.h b/stepmania/src/RageSoundReader.h index ad71ae1066..34fab2adee 100644 --- a/stepmania/src/RageSoundReader.h +++ b/stepmania/src/RageSoundReader.h @@ -10,6 +10,8 @@ protected: void SetError(string e) const { error = e; } public: + virtual float GetOffsetFix() const { return 0; } + virtual int GetLength() const = 0; /* ms */ virtual int GetLength_Fast() const { return GetLength(); } /* ms */ virtual int SetPosition_Accurate(int ms) = 0; diff --git a/stepmania/src/RageSoundReader_MP3.cpp b/stepmania/src/RageSoundReader_MP3.cpp index 0006a8e0d4..9b6b807876 100644 --- a/stepmania/src/RageSoundReader_MP3.cpp +++ b/stepmania/src/RageSoundReader_MP3.cpp @@ -604,12 +604,24 @@ SoundReader_FileReader::OpenResult RageSoundReader_MP3::Open( CString filename_ SampleRate = mad->Frame.header.samplerate; mad->framelength = mad->Frame.header.duration; - this->Channels = 0; + this->Channels = MAD_NCHANNELS( &mad->Frame.header ); /* Since we've already decoded a frame, just synth it instead of rewinding * the stream. */ synth_output(); - this->Channels = MAD_NCHANNELS( &mad->Frame.header ); + + /* + * Hack: Old code was setting Channels = 0 before synth_output. This caused + * the first synthed frame to not be written to the output buffer, offsetting + * the whole song by a buffer. On one test song, it was 1152/44100, or 26ms. + * This affects all MP3s. We can't leave the bug in, since we need to handle + * seeks properly, too. So, let's make the offset to fix seeks available. + * + * This is negative: if we have 26ms of extra data, then song offsets are + * fixed by subtracting 26ms. + */ + + this->OffsetFix = - (float) mad->Synth.pcm.length / mad->Frame.header.samplerate; if(mad->length == -1) { diff --git a/stepmania/src/RageSoundReader_MP3.h b/stepmania/src/RageSoundReader_MP3.h index 7ca51d9597..9f5ce2054a 100644 --- a/stepmania/src/RageSoundReader_MP3.h +++ b/stepmania/src/RageSoundReader_MP3.h @@ -11,6 +11,7 @@ class RageSoundReader_MP3: public SoundReader_FileReader public: int SampleRate; int Channels; + float OffsetFix; CString filename; FILE *rw; @@ -40,6 +41,8 @@ public: int SetPosition_Fast(int ms); int Read(char *buf, unsigned len); int GetSampleRate() const { return SampleRate; } + float GetOffsetFix() const { return OffsetFix; } + RageSoundReader_MP3(); ~RageSoundReader_MP3(); RageSoundReader_MP3( const RageSoundReader_MP3 & ); /* not defined; don't use */ diff --git a/stepmania/src/RageSoundReader_Preload.cpp b/stepmania/src/RageSoundReader_Preload.cpp index e81a7efff7..57a49ace8b 100644 --- a/stepmania/src/RageSoundReader_Preload.cpp +++ b/stepmania/src/RageSoundReader_Preload.cpp @@ -17,6 +17,7 @@ bool SoundReader_Preload::Open(SoundReader *source) { ASSERT(source); samplerate = source->GetSampleRate(); + OffsetFix = source->GetOffsetFix(); // propagate upwards /* 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_Preload.h b/stepmania/src/RageSoundReader_Preload.h index 3b0c1d4cb6..7469f8ae07 100644 --- a/stepmania/src/RageSoundReader_Preload.h +++ b/stepmania/src/RageSoundReader_Preload.h @@ -30,6 +30,7 @@ class SoundReader_Preload: public SoundReader { int total_samples() const; int samplerate; + float OffsetFix; public: /* Return true if the sound has been preloaded, in which case source will @@ -41,6 +42,7 @@ public: int SetPosition_Fast(int ms); int Read(char *buf, unsigned len); int GetSampleRate() const { return samplerate; } + float GetOffsetFix() const { return OffsetFix; } SoundReader *Copy() const; ~SoundReader_Preload() { } diff --git a/stepmania/src/RageSoundReader_Resample_Fast.h b/stepmania/src/RageSoundReader_Resample_Fast.h index 29a201cb22..737d3203ef 100644 --- a/stepmania/src/RageSoundReader_Resample_Fast.h +++ b/stepmania/src/RageSoundReader_Resample_Fast.h @@ -27,6 +27,7 @@ public: RageSoundReader_Resample_Fast(); virtual ~RageSoundReader_Resample_Fast(); SoundReader *Copy() const; + float GetOffsetFix() const { return source->GetOffsetFix(); } /* Change the actual sample rate of a sound. */ void SetSampleRate(int hz); diff --git a/stepmania/src/RageSoundReader_Resample_Good.h b/stepmania/src/RageSoundReader_Resample_Good.h index 5915f2437a..8b9d4692fc 100644 --- a/stepmania/src/RageSoundReader_Resample_Good.h +++ b/stepmania/src/RageSoundReader_Resample_Good.h @@ -35,6 +35,7 @@ public: RageSoundReader_Resample_Good(); virtual ~RageSoundReader_Resample_Good(); SoundReader *Copy() const; + float GetOffsetFix() const { return source->GetOffsetFix(); } /* Change the actual sample rate of a sound. */ void SetSampleRate( int hz );