Get the length from sounds when possible

This commit is contained in:
Chris Robinson 2014-06-25 21:14:35 -07:00
parent fd25ec2a62
commit c6bb52c866
6 changed files with 22 additions and 2 deletions

View file

@ -126,6 +126,7 @@ struct SoundDecoder
virtual std::vector<char> readAll(); virtual std::vector<char> readAll();
virtual bool seek(size_t ms_offset) = 0; virtual bool seek(size_t ms_offset) = 0;
virtual size_t getSampleOffset() = 0; virtual size_t getSampleOffset() = 0;
virtual size_t getSampleLength() { return 0; }
SoundDecoder() { } SoundDecoder() { }
virtual ~SoundDecoder() { } virtual ~SoundDecoder() { }

View file

@ -182,4 +182,10 @@ size_t MPG123Decoder::getSampleOffset()
return mpg123_tell(MPG123); return mpg123_tell(MPG123);
} }
size_t MPG123Decoder::getSampleLength()
{
off_t len = mpg123_length(MPG123);
return (len > 0) ? len : 0;
}
#endif #endif

View file

@ -14,6 +14,7 @@ struct MPG123Decoder : public SoundDecoder
virtual size_t read(char *buffer, size_t bytes); virtual size_t read(char *buffer, size_t bytes);
virtual bool seek(size_t ms_offset); virtual bool seek(size_t ms_offset);
virtual size_t getSampleOffset(); virtual size_t getSampleOffset();
virtual size_t getSampleLength();
MPG123Decoder() : MPG123(0) { } MPG123Decoder() : MPG123(0) { }
virtual ~MPG123Decoder(); virtual ~MPG123Decoder();

View file

@ -388,12 +388,12 @@ public:
FString GetStats() FString GetStats()
{ {
FString stats; FString stats;
size_t pos, len;
ALfloat volume; ALfloat volume;
ALint offset; ALint offset;
ALint processed; ALint processed;
ALint queued; ALint queued;
ALint state; ALint state;
size_t pos;
ALenum err; ALenum err;
alGetSourcef(Source, AL_GAIN, &volume); alGetSourcef(Source, AL_GAIN, &volume);
@ -412,16 +412,22 @@ public:
(state == AL_PLAYING || state == AL_PAUSED) ? "Ready" : "Unknown state"; (state == AL_PLAYING || state == AL_PAUSED) ? "Ready" : "Unknown state";
pos = Decoder->getSampleOffset(); pos = Decoder->getSampleOffset();
len = Decoder->getSampleLength();
if(state == AL_STOPPED) if(state == AL_STOPPED)
offset = BufferCount * (Data.size()/FrameSize); offset = BufferCount * (Data.size()/FrameSize);
else else
{ {
size_t rem = queued*(Data.size()/FrameSize) - offset; size_t rem = queued*(Data.size()/FrameSize) - offset;
if(pos > rem) pos -= rem; if(pos > rem) pos -= rem;
else if(len > 0) pos += len - rem;
else pos = 0; 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(",%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) if(state == AL_PAUSED)
stats += ", paused"; stats += ", paused";
if(state == AL_PLAYING) if(state == AL_PLAYING)

View file

@ -106,4 +106,9 @@ size_t SndFileDecoder::getSampleOffset()
return sf_seek(SndFile, 0, SEEK_CUR); return sf_seek(SndFile, 0, SEEK_CUR);
} }
size_t SndFileDecoder::getSampleLength()
{
return (SndInfo.frames > 0) ? SndInfo.frames : 0;
}
#endif #endif

View file

@ -15,6 +15,7 @@ struct SndFileDecoder : public SoundDecoder
virtual std::vector<char> readAll(); virtual std::vector<char> readAll();
virtual bool seek(size_t ms_offset); virtual bool seek(size_t ms_offset);
virtual size_t getSampleOffset(); virtual size_t getSampleOffset();
virtual size_t getSampleLength();
SndFileDecoder() : SndFile(0) { } SndFileDecoder() : SndFile(0) { }
virtual ~SndFileDecoder(); virtual ~SndFileDecoder();