From 276bdd1dbf8e7bb651814d576e1aa1a02b67ad0c Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 23 Apr 2019 21:18:56 -0300 Subject: [PATCH] iOS: fix audio clicks immediately after playing a source. Resolves issue #1485. --- src/modules/audio/openal/Source.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/modules/audio/openal/Source.cpp b/src/modules/audio/openal/Source.cpp index 61aa3160f..934229746 100644 --- a/src/modules/audio/openal/Source.cpp +++ b/src/modules/audio/openal/Source.cpp @@ -919,27 +919,31 @@ bool Source::playAtomic(ALuint source) // Clear errors. alGetError(); - alSourcePlay(source); + // Seek to the current/pending offset before playing. If this call happens + // immediately after alSourcePlay it could introduce a click depending on + // the platform, because the source is asynchronously playing by then. + alSourcef(source, AL_SAMPLE_OFFSET, offsetSamples); bool success = alGetError() == AL_NO_ERROR; + alSourcePlay(source); + + success &= alGetError() == AL_NO_ERROR; + if (sourceType == TYPE_STREAM) { valid = true; //isPlaying() needs source to be valid if (!isPlaying()) success = false; } - else if (success) - { - alSourcef(source, AL_SAMPLE_OFFSET, offsetSamples); - success = alGetError() == AL_NO_ERROR; - } if (!success) { valid = true; //stop() needs source to be valid stop(); } + + // Static sources: reset the pending offset since it's not valid anymore. if (sourceType != TYPE_STREAM) offsetSamples = offsetSeconds = 0;