audio: allow looping of streams with unknown length

This commit is contained in:
rdb 2019-04-29 18:04:38 +02:00
parent 2f2354550d
commit 44f4ad94ba
2 changed files with 16 additions and 6 deletions

View File

@ -442,7 +442,7 @@ get_sound_data(MovieAudio *movie, int mode) {
int channels = stream->audio_channels(); int channels = stream->audio_channels();
int samples = (int)(stream->length() * stream->audio_rate()); int samples = (int)(stream->length() * stream->audio_rate());
int16_t *data = new int16_t[samples * channels]; int16_t *data = new int16_t[samples * channels];
stream->read_samples(samples, data); samples = stream->read_samples(samples, data);
alBufferData(sd->_sample, alBufferData(sd->_sample,
(channels>1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, (channels>1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16,
data, samples * channels * 2, stream->audio_rate()); data, samples * channels * 2, stream->audio_rate());

View File

@ -373,7 +373,6 @@ read_stream_data(int bytelen, unsigned char *buffer) {
nassertr(has_sound_data(), 0); nassertr(has_sound_data(), 0);
MovieAudioCursor *cursor = _sd->_stream; MovieAudioCursor *cursor = _sd->_stream;
double length = cursor->length();
int channels = cursor->audio_channels(); int channels = cursor->audio_channels();
int rate = cursor->audio_rate(); int rate = cursor->audio_rate();
int space = bytelen / (channels * 2); int space = bytelen / (channels * 2);
@ -381,7 +380,7 @@ read_stream_data(int bytelen, unsigned char *buffer) {
while (space && (_loops_completed < _playing_loops)) { while (space && (_loops_completed < _playing_loops)) {
double t = cursor->tell(); double t = cursor->tell();
double remain = length - t; double remain = cursor->length() - t;
if (remain > 60.0) { if (remain > 60.0) {
remain = 60.0; remain = 60.0;
} }
@ -403,9 +402,20 @@ read_stream_data(int bytelen, unsigned char *buffer) {
if (samples > _sd->_stream->ready()) { if (samples > _sd->_stream->ready()) {
samples = _sd->_stream->ready(); samples = _sd->_stream->ready();
} }
cursor->read_samples(samples, (int16_t *)buffer); samples = cursor->read_samples(samples, (int16_t *)buffer);
size_t hval = AddHash::add_hash(0, (uint8_t*)buffer, samples*channels*2); if (audio_cat.is_debug()) {
audio_debug("Streaming " << cursor->get_source()->get_name() << " at " << t << " hash " << hval); 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; fill += samples;
space -= samples; space -= samples;
buffer += (samples * channels * 2); buffer += (samples * channels * 2);