From c6bb52c866dce8248e6a136f3f59f21708530bac Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 25 Jun 2014 21:14:35 -0700 Subject: [PATCH] Get the length from sounds when possible --- src/sound/i_soundinternal.h | 1 + src/sound/mpg123_decoder.cpp | 6 ++++++ src/sound/mpg123_decoder.h | 1 + src/sound/oalsound.cpp | 10 ++++++++-- src/sound/sndfile_decoder.cpp | 5 +++++ src/sound/sndfile_decoder.h | 1 + 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/sound/i_soundinternal.h b/src/sound/i_soundinternal.h index 0a7e8b0e0b..499fe80a6e 100644 --- a/src/sound/i_soundinternal.h +++ b/src/sound/i_soundinternal.h @@ -126,6 +126,7 @@ struct SoundDecoder virtual std::vector readAll(); virtual bool seek(size_t ms_offset) = 0; virtual size_t getSampleOffset() = 0; + virtual size_t getSampleLength() { return 0; } SoundDecoder() { } virtual ~SoundDecoder() { } diff --git a/src/sound/mpg123_decoder.cpp b/src/sound/mpg123_decoder.cpp index 63ccb6922c..969706a98e 100644 --- a/src/sound/mpg123_decoder.cpp +++ b/src/sound/mpg123_decoder.cpp @@ -182,4 +182,10 @@ size_t MPG123Decoder::getSampleOffset() return mpg123_tell(MPG123); } +size_t MPG123Decoder::getSampleLength() +{ + off_t len = mpg123_length(MPG123); + return (len > 0) ? len : 0; +} + #endif diff --git a/src/sound/mpg123_decoder.h b/src/sound/mpg123_decoder.h index 0d8d2e7ab8..8b2b64fa79 100644 --- a/src/sound/mpg123_decoder.h +++ b/src/sound/mpg123_decoder.h @@ -14,6 +14,7 @@ struct MPG123Decoder : public SoundDecoder virtual size_t read(char *buffer, size_t bytes); virtual bool seek(size_t ms_offset); virtual size_t getSampleOffset(); + virtual size_t getSampleLength(); MPG123Decoder() : MPG123(0) { } virtual ~MPG123Decoder(); diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index f04990fa7e..27f7d47553 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -388,12 +388,12 @@ public: FString GetStats() { FString stats; + size_t pos, len; ALfloat volume; ALint offset; ALint processed; ALint queued; ALint state; - size_t pos; ALenum err; alGetSourcef(Source, AL_GAIN, &volume); @@ -412,16 +412,22 @@ public: (state == AL_PLAYING || state == AL_PAUSED) ? "Ready" : "Unknown state"; pos = Decoder->getSampleOffset(); + len = Decoder->getSampleLength(); if(state == AL_STOPPED) offset = BufferCount * (Data.size()/FrameSize); else { size_t rem = queued*(Data.size()/FrameSize) - offset; if(pos > rem) pos -= rem; + else if(len > 0) pos += len - rem; else pos = 0; } + pos = (size_t)(pos * 1000.0 / SampleRate); + len = (size_t)(len * 1000.0 / SampleRate); stats.AppendFormat(",%3lu%% buffered", 100 - 100*offset/(BufferCount*(Data.size()/FrameSize))); - stats.AppendFormat(", %zu ms", pos); + stats.AppendFormat(", %zu.%03zu", pos/1000, pos%1000); + if(len > 0) + stats.AppendFormat(" / %zu.%03zu", len/1000, len%1000); if(state == AL_PAUSED) stats += ", paused"; if(state == AL_PLAYING) diff --git a/src/sound/sndfile_decoder.cpp b/src/sound/sndfile_decoder.cpp index 922fc8b1ce..efcc6cf40d 100644 --- a/src/sound/sndfile_decoder.cpp +++ b/src/sound/sndfile_decoder.cpp @@ -106,4 +106,9 @@ size_t SndFileDecoder::getSampleOffset() return sf_seek(SndFile, 0, SEEK_CUR); } +size_t SndFileDecoder::getSampleLength() +{ + return (SndInfo.frames > 0) ? SndInfo.frames : 0; +} + #endif diff --git a/src/sound/sndfile_decoder.h b/src/sound/sndfile_decoder.h index f329ab8765..6c4dd3edac 100644 --- a/src/sound/sndfile_decoder.h +++ b/src/sound/sndfile_decoder.h @@ -15,6 +15,7 @@ struct SndFileDecoder : public SoundDecoder virtual std::vector readAll(); virtual bool seek(size_t ms_offset); virtual size_t getSampleOffset(); + virtual size_t getSampleLength(); SndFileDecoder() : SndFile(0) { } virtual ~SndFileDecoder();