From 44f4ad94baf027231dac7b0a0d0eea25ca6de772 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 29 Apr 2019 18:04:38 +0200 Subject: [PATCH] audio: allow looping of streams with unknown length --- panda/src/audiotraits/openalAudioManager.cxx | 2 +- panda/src/audiotraits/openalAudioSound.cxx | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/panda/src/audiotraits/openalAudioManager.cxx b/panda/src/audiotraits/openalAudioManager.cxx index edf7dfe222..76e7b0dd81 100644 --- a/panda/src/audiotraits/openalAudioManager.cxx +++ b/panda/src/audiotraits/openalAudioManager.cxx @@ -442,7 +442,7 @@ get_sound_data(MovieAudio *movie, int mode) { int channels = stream->audio_channels(); int samples = (int)(stream->length() * stream->audio_rate()); int16_t *data = new int16_t[samples * channels]; - stream->read_samples(samples, data); + samples = stream->read_samples(samples, data); alBufferData(sd->_sample, (channels>1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, data, samples * channels * 2, stream->audio_rate()); diff --git a/panda/src/audiotraits/openalAudioSound.cxx b/panda/src/audiotraits/openalAudioSound.cxx index 9244b7509f..5676e63f80 100644 --- a/panda/src/audiotraits/openalAudioSound.cxx +++ b/panda/src/audiotraits/openalAudioSound.cxx @@ -373,7 +373,6 @@ read_stream_data(int bytelen, unsigned char *buffer) { nassertr(has_sound_data(), 0); MovieAudioCursor *cursor = _sd->_stream; - double length = cursor->length(); int channels = cursor->audio_channels(); int rate = cursor->audio_rate(); int space = bytelen / (channels * 2); @@ -381,7 +380,7 @@ read_stream_data(int bytelen, unsigned char *buffer) { while (space && (_loops_completed < _playing_loops)) { double t = cursor->tell(); - double remain = length - t; + double remain = cursor->length() - t; if (remain > 60.0) { remain = 60.0; } @@ -403,9 +402,20 @@ read_stream_data(int bytelen, unsigned char *buffer) { if (samples > _sd->_stream->ready()) { samples = _sd->_stream->ready(); } - cursor->read_samples(samples, (int16_t *)buffer); - size_t hval = AddHash::add_hash(0, (uint8_t*)buffer, samples*channels*2); - audio_debug("Streaming " << cursor->get_source()->get_name() << " at " << t << " hash " << hval); + samples = cursor->read_samples(samples, (int16_t *)buffer); + if (audio_cat.is_debug()) { + size_t hval = AddHash::add_hash(0, (uint8_t*)buffer, samples*channels*2); + audio_debug("Streaming " << cursor->get_source()->get_name() << " at " << t << " hash " << hval); + } + if (samples == 0) { + _loops_completed += 1; + cursor->seek(0.0); + if (_playing_loops >= 1000000000) { + // Prevent infinite loop if endlessly looping empty sound + return fill; + } + continue; + } fill += samples; space -= samples; buffer += (samples * channels * 2);