Fix audio stopping on buffer underruns

This commit is contained in:
Eduardo Bart 2012-07-31 00:12:04 -03:00
parent 6c159333dd
commit fbb3c9933a
9 changed files with 51 additions and 11 deletions

View File

@ -163,6 +163,14 @@ uint FileStream::tell()
return m_pos; return m_pos;
} }
bool FileStream::eof()
{
if(!m_caching)
return PHYSFS_eof(m_fileHandle);
else
return m_pos >= m_data.size();
}
uint8 FileStream::getU8() uint8 FileStream::getU8()
{ {
uint8 v = 0; uint8 v = 0;

View File

@ -45,6 +45,7 @@ public:
void skip(uint len); void skip(uint len);
uint size(); uint size();
uint tell(); uint tell();
bool eof();
std::string name() { return m_name; } std::string name() { return m_name; }
uint8 getU8(); uint8 getU8();

View File

@ -43,6 +43,15 @@ void CombinedSoundSource::stop()
source->stop(); source->stop();
} }
bool CombinedSoundSource::isBuffering()
{
for(const SoundSourcePtr& source : m_sources) {
if(source->isBuffering())
return true;
}
return false;
}
bool CombinedSoundSource::isPlaying() bool CombinedSoundSource::isPlaying()
{ {
for(const SoundSourcePtr& source : m_sources) { for(const SoundSourcePtr& source : m_sources) {

View File

@ -35,6 +35,8 @@ public:
void play(); void play();
void stop(); void stop();
bool isBuffering();
bool isPlaying(); bool isPlaying();
void setLooping(bool looping); void setLooping(bool looping);

View File

@ -35,6 +35,7 @@ public:
virtual int read(void *buffer, int bufferSize) = 0; virtual int read(void *buffer, int bufferSize) = 0;
virtual void reset() = 0; virtual void reset() = 0;
bool eof() { return m_file->eof(); }
ALenum getSampleFormat(); ALenum getSampleFormat();

View File

@ -63,7 +63,7 @@ void SoundSource::stop()
} }
} }
bool SoundSource::isPlaying() bool SoundSource::isBuffering()
{ {
int state = AL_PLAYING; int state = AL_PLAYING;
alGetSourcei(m_sourceId, AL_SOURCE_STATE, &state); alGetSourcei(m_sourceId, AL_SOURCE_STATE, &state);

View File

@ -39,7 +39,9 @@ public:
virtual void play(); virtual void play();
virtual void stop(); virtual void stop();
virtual bool isPlaying();
virtual bool isBuffering();
virtual bool isPlaying() { return isBuffering(); }
virtual void setLooping(bool looping); virtual void setLooping(bool looping);
virtual void setRelative(bool relative); virtual void setRelative(bool relative);

View File

@ -45,6 +45,13 @@ void StreamSoundSource::setSoundFile(const SoundFilePtr& soundFile)
void StreamSoundSource::play() void StreamSoundSource::play()
{ {
m_playing = true;
if(m_eof) {
m_soundFile->reset();
m_eof = false;
}
if(!m_soundFile) { if(!m_soundFile) {
g_logger.error("there is not sound file to play the stream"); g_logger.error("there is not sound file to play the stream");
return; return;
@ -59,6 +66,7 @@ void StreamSoundSource::stop()
{ {
SoundSource::stop(); SoundSource::stop();
unqueueBuffers(); unqueueBuffers();
m_playing = false;
} }
void StreamSoundSource::queueBuffers() void StreamSoundSource::queueBuffers()
@ -96,12 +104,15 @@ void StreamSoundSource::update()
break; break;
} }
if(!isPlaying()) { if(!isBuffering() && m_playing) {
if(processed == 0 || !m_looping) if(!m_looping && m_eof) {
return; stop();
} else if(processed == 0) {
g_logger.traceError("restarting audio source because of buffer underrun"); g_logger.traceError("audio buffer underrun");
play(); play();
} else if(m_looping) {
play();
}
} }
} }
@ -123,8 +134,10 @@ bool StreamSoundSource::fillBufferAndQueue(uint buffer)
if(bytesRead < maxRead) { if(bytesRead < maxRead) {
if(m_looping) if(m_looping)
m_soundFile->reset(); m_soundFile->reset();
else else {
m_eof = true;
break; break;
}
} }
} while(bytesRead < maxRead); } while(bytesRead < maxRead);
@ -152,7 +165,7 @@ bool StreamSoundSource::fillBufferAndQueue(uint buffer)
} }
// return false if there aren't more buffers to fill // return false if there aren't more buffers to fill
return bytesRead >= STREAM_FRAGMENT_SIZE; return (bytesRead >= STREAM_FRAGMENT_SIZE && !m_eof);
} }
void StreamSoundSource::downMix(StreamSoundSource::DownMix downMix) void StreamSoundSource::downMix(StreamSoundSource::DownMix downMix)

View File

@ -28,7 +28,7 @@
class StreamSoundSource : public SoundSource class StreamSoundSource : public SoundSource
{ {
enum { enum {
STREAM_BUFFER_SIZE = 1024 * 100, STREAM_BUFFER_SIZE = 1024 * 200,
STREAM_FRAGMENTS = 5, STREAM_FRAGMENTS = 5,
STREAM_FRAGMENT_SIZE = STREAM_BUFFER_SIZE / STREAM_FRAGMENTS STREAM_FRAGMENT_SIZE = STREAM_BUFFER_SIZE / STREAM_FRAGMENTS
}; };
@ -42,6 +42,8 @@ public:
void play(); void play();
void stop(); void stop();
bool isPlaying() { return m_playing; }
void setSoundFile(const SoundFilePtr& soundFile); void setSoundFile(const SoundFilePtr& soundFile);
void downMix(DownMix downMix); void downMix(DownMix downMix);
@ -57,6 +59,8 @@ private:
std::array<SoundBufferPtr,STREAM_FRAGMENTS> m_buffers; std::array<SoundBufferPtr,STREAM_FRAGMENTS> m_buffers;
DownMix m_downMix; DownMix m_downMix;
stdext::boolean<false> m_looping; stdext::boolean<false> m_looping;
stdext::boolean<false> m_playing;
stdext::boolean<false> m_eof;
}; };
#endif #endif