1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 06:53:53 +00:00

Use the decoder's sample offset for calculating the stream offset

This commit is contained in:
Chris Robinson 2012-12-13 05:04:53 -08:00
parent cab68df257
commit 0a5ab977b7
9 changed files with 35 additions and 10 deletions

View file

@ -398,6 +398,11 @@ public:
return total;
}
size_t getSampleOffset()
{
return (size_t)(is->audio_clock*is->audio_st->codec->sample_rate);
}
};

View file

@ -117,6 +117,11 @@ void Audiere_Decoder::rewind()
mSoundSource->reset();
}
size_t Audiere_Decoder::getSampleOffset()
{
return 0;
}
Audiere_Decoder::Audiere_Decoder()
{
}

View file

@ -25,6 +25,7 @@ namespace MWSound
virtual size_t read(char *buffer, size_t bytes);
virtual void rewind();
virtual size_t getSampleOffset();
Audiere_Decoder& operator=(const Audiere_Decoder &rhs);
Audiere_Decoder(const Audiere_Decoder &rhs);

View file

@ -383,7 +383,11 @@ size_t FFmpeg_Decoder::read(char *buffer, size_t bytes)
if(mStreams.empty())
fail("No audio streams");
return mStreams.front()->readAVAudioData(buffer, bytes);
MyStream *stream = mStreams.front();
size_t got = stream->readAVAudioData(buffer, bytes);
mSamplesRead += got / av_samples_get_buffer_size(NULL, stream->mCodecCtx->channels, 1,
stream->mCodecCtx->sample_fmt, 1);
return got;
}
void FFmpeg_Decoder::readAll(std::vector<char> &output)
@ -402,9 +406,15 @@ void FFmpeg_Decoder::rewind()
{
av_seek_frame(mFormatCtx, -1, 0, 0);
std::for_each(mStreams.begin(), mStreams.end(), std::mem_fun(&MyStream::clearPackets));
mSamplesRead = 0;
}
FFmpeg_Decoder::FFmpeg_Decoder() : mFormatCtx(NULL)
size_t FFmpeg_Decoder::getSampleOffset()
{
return mSamplesRead;
}
FFmpeg_Decoder::FFmpeg_Decoder() : mFormatCtx(NULL), mSamplesRead(0)
{
static bool done_init = false;

View file

@ -25,6 +25,7 @@ namespace MWSound
struct MyStream;
std::vector<MyStream*> mStreams;
size_t mSamplesRead;
bool getNextPacket(int streamidx);
@ -42,6 +43,7 @@ namespace MWSound
virtual size_t read(char *buffer, size_t bytes);
virtual void readAll(std::vector<char> &output);
virtual void rewind();
virtual size_t getSampleOffset();
FFmpeg_Decoder& operator=(const FFmpeg_Decoder &rhs);
FFmpeg_Decoder(const FFmpeg_Decoder &rhs);

View file

@ -218,6 +218,11 @@ void MpgSnd_Decoder::rewind()
}
}
size_t MpgSnd_Decoder::getSampleOffset()
{
return 0;
}
MpgSnd_Decoder::MpgSnd_Decoder()
: mSndInfo()
, mSndFile(NULL)

View file

@ -39,6 +39,7 @@ namespace MWSound
virtual size_t read(char *buffer, size_t bytes);
virtual void readAll(std::vector<char> &output);
virtual void rewind();
virtual size_t getSampleOffset();
MpgSnd_Decoder& operator=(const MpgSnd_Decoder &rhs);
MpgSnd_Decoder(const MpgSnd_Decoder &rhs);

View file

@ -122,7 +122,6 @@ class OpenAL_SoundStream : public Sound
ALsizei mSampleRate;
ALuint mBufferSize;
ALuint mSamplesTotal;
ALuint mSamplesQueued;
DecoderPtr mDecoder;
@ -215,8 +214,7 @@ private:
OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder)
: mOutput(output), mSource(src), mSamplesTotal(0), mSamplesQueued(0),
mDecoder(decoder), mIsFinished(true)
: mOutput(output), mSource(src), mSamplesQueued(0), mDecoder(decoder), mIsFinished(true)
{
throwALerror();
@ -286,7 +284,6 @@ void OpenAL_SoundStream::stop()
mSamplesQueued = 0;
mDecoder->rewind();
mSamplesTotal = 0;
}
bool OpenAL_SoundStream::isPlaying()
@ -311,9 +308,9 @@ double OpenAL_SoundStream::getTimeOffset()
alGetSourcef(mSource, AL_SEC_OFFSET, &offset);
alGetSourcei(mSource, AL_SOURCE_STATE, &state);
if(state == AL_PLAYING || state == AL_PAUSED)
t = (double)(mSamplesTotal - mSamplesQueued)/(double)mSampleRate + offset;
t = (double)(mDecoder->getSampleOffset() - mSamplesQueued)/(double)mSampleRate + offset;
else
t = (double)mSamplesTotal / (double)mSampleRate;
t = (double)mDecoder->getSampleOffset() / (double)mSampleRate;
mOutput.mStreamThread->mMutex.unlock();
throwALerror();
@ -388,13 +385,11 @@ bool OpenAL_SoundStream::process()
mSamplesQueued -= samples_unqueued;
mSamplesQueued += samples_queued;
mSamplesTotal += samples_queued;
mIsFinished = finished;
}
catch(std::exception &e) {
std::cout<< "Error updating stream \""<<mDecoder->getName()<<"\"" <<std::endl;
mSamplesQueued = 0;
mSamplesTotal = 0;
mIsFinished = true;
}
return !mIsFinished;

View file

@ -38,6 +38,7 @@ namespace MWSound
virtual size_t read(char *buffer, size_t bytes) = 0;
virtual void readAll(std::vector<char> &output);
virtual void rewind() = 0;
virtual size_t getSampleOffset() = 0;
Sound_Decoder() : mResourceMgr(Ogre::ResourceGroupManager::getSingleton())
{ }