From 97a6218094e9556c9b24cd2ee1b331a240842515 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Fri, 20 Nov 2026 16:33:34 +0100 Subject: [PATCH] fix(audio): measure the start offset on the buffer queued last, not on AL_BUFFER (AL_INVALID_NAME) (#350) * fix(audio): measure the start offset on the buffer queued last, not on AL_BUFFER (AL_INVALID_NAME) openal_source::update() looked up the buffer left playing after a multipart sound's opening bookend with alGetSourcei(AL_BUFFER). For a source with queued buffers that is 0 in OpenAL Soft 1.18 (the version the game ships), and in newer versions once the source has stopped, so alGetBufferi(0) raised AL_INVALID_NAME ("sound: al error: AL_INVALID_NAME" in errors.txt) and the sample offset came from an uninitialised size. It happened every time a sound with a start offset (the motor blowers of the EN76/EN96 family, with start/stop bookends) went from its start sample to the loop. The source now remembers the buffer it queued last and measures that one. The offset itself is computed by audio::start_sample(), in the core library with unit tests; it also keeps the offset inside the buffer. * refactor(audio): the start sample without Sonar issues The sample count is cast to float explicitly, as it was converted implicitly. No change in behaviour. --- CMakeLists.txt | 1 + audio/audiorenderer.cpp | 16 +++++++-------- audio/audiorenderer.h | 2 ++ audio/audiorenderer_extra.h | 5 +++-- audio/startsample.cpp | 28 ++++++++++++++++++++++++++ audio/startsample.h | 19 ++++++++++++++++++ tests/unit/test_start_sample.cpp | 34 ++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 audio/startsample.cpp create mode 100644 audio/startsample.h create mode 100644 tests/unit/test_start_sample.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a047f11ad..3e80acbf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -541,6 +541,7 @@ add_library(eu07_settings INTERFACE) # the parts of the simulation the game shares with the unit tests: the vehicle physics, the vehicle # definition parser, the helpers they use and the rules of the test mode. built once, and tested as the game uses them set(CORE_SOURCES +"audio/startsample.cpp" "McZapkie/friction.cpp" "McZapkie/hamulce.cpp" "McZapkie/Mover.cpp" diff --git a/audio/audiorenderer.cpp b/audio/audiorenderer.cpp index 7950fca56..7d5285def 100644 --- a/audio/audiorenderer.cpp +++ b/audio/audiorenderer.cpp @@ -182,18 +182,16 @@ openal_source::update( double const Deltatime, glm::vec3 const &Listenervelocity --sound_index; sound_change = true; // potentially adjust starting point of the last buffer (to reduce chance of reverb effect with multiple, looping copies playing) - if( controller->start() > 0.f && sounds.size() == 1 ) { - ALint bufferid; - ::alGetSourcei( - id, - AL_BUFFER, - &bufferid ); - ALint buffersize; - ::alGetBufferi( bufferid, AL_SIZE, &buffersize ); + if( controller->start() > 0.f && sounds.size() == 1 && last_buffer != audio::null_resource ) { + // NOTE: the buffer is the one queued last, not the one the source reports: for a source with queued + // buffers AL_BUFFER is 0 in OpenAL Soft 1.18 (the one the game comes with), and in the newer ones + // once the source has stopped. alGetBufferi() on buffer 0 fails with AL_INVALID_NAME + ALint buffersize { 0 }; + ::alGetBufferi( last_buffer, AL_SIZE, &buffersize ); ::alSourcei( id, AL_SAMPLE_OFFSET, - static_cast( controller->start() * ( buffersize / sizeof( std::int16_t ) ) ) ); + audio::start_sample( controller->start(), buffersize ) ); } } diff --git a/audio/audiorenderer.h b/audio/audiorenderer.h index 86ea89cd8..afa819c1b 100644 --- a/audio/audiorenderer.h +++ b/audio/audiorenderer.h @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #pragma once #include "audio/audio.h" +#include "audio/startsample.h" #include "model/ResourceManager.h" #include "application/uitranscripts.h" @@ -69,6 +70,7 @@ struct openal_source { using buffer_sequence = std::vector; ALuint id { audio::null_resource }; + ALuint last_buffer { audio::null_resource }; // the buffer queued last, the one left playing once those before it are done sound_source *controller { nullptr }; uint32_sequence sounds; int sound_index { 0 }; diff --git a/audio/audiorenderer_extra.h b/audio/audiorenderer_extra.h index 430f594b3..fb38816fc 100644 --- a/audio/audiorenderer_extra.h +++ b/audio/audiorenderer_extra.h @@ -19,6 +19,7 @@ openal_source::bind( sound_source *Controller, uint32_sequence Sounds, Iterator_ if( id != audio::null_resource && !buffers.empty()) { ::alSourceQueueBuffers( id, static_cast( buffers.size() ), buffers.data() ); + last_buffer = buffers.back(); ::alSourceRewind( id ); // sound controller can potentially request playback to start from certain buffer point // for multipart sounds the offset is applied only to last piece during playback @@ -29,12 +30,12 @@ openal_source::bind( sound_source *Controller, uint32_sequence Sounds, Iterator_ } else { // move playback start to specified point in 0-1 range - ALint buffersize; + ALint buffersize { 0 }; ::alGetBufferi( buffers.front(), AL_SIZE, &buffersize ); ::alSourcei( id, AL_SAMPLE_OFFSET, - static_cast( controller->start() * ( buffersize / sizeof( std::int16_t ) ) ) ); + audio::start_sample( controller->start(), buffersize ) ); } } diff --git a/audio/startsample.cpp b/audio/startsample.cpp new file mode 100644 index 000000000..673dda151 --- /dev/null +++ b/audio/startsample.cpp @@ -0,0 +1,28 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#include "stdafx.h" +#include "audio/startsample.h" + +#include +#include + +namespace audio { + +int start_sample( float const Start, int const Buffersize ) { + + auto const samples { Buffersize / static_cast( sizeof( std::int16_t ) ) }; + if( samples <= 0 + || false == ( Start > 0.f ) ) { // also a NaN + return 0; + } + return std::min( static_cast( std::min( Start, 1.f ) * static_cast( samples ) ), samples - 1 ); +} + +} // audio diff --git a/audio/startsample.h b/audio/startsample.h new file mode 100644 index 000000000..e9837a3a9 --- /dev/null +++ b/audio/startsample.h @@ -0,0 +1,19 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +namespace audio { + +// the sample a sound set to start at Start (a fraction of its length, 0-1) begins to play from, in a buffer of +// Buffersize bytes of 16 bit mono samples (the sound buffers are all made so, see upload_sound()). always a sample +// the buffer has: OpenAL rejects an offset at or past the end with AL_INVALID_VALUE. 0 for an empty buffer +int start_sample( float const Start, int const Buffersize ); + +} // audio diff --git a/tests/unit/test_start_sample.cpp b/tests/unit/test_start_sample.cpp new file mode 100644 index 000000000..00e7fc781 --- /dev/null +++ b/tests/unit/test_start_sample.cpp @@ -0,0 +1,34 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "audio/startsample.h" + +#include + +// the sample a sound with a start offset (startoffset:, or the random one of motors, motor blowers and +// the running noise) begins to play from, in the 16 bit mono buffers the sound files are loaded into + +TEST_CASE("start_sample: a fraction of the samples of the buffer, two bytes each") { + CHECK(audio::start_sample(0.5f, 1000) == 250); + CHECK(audio::start_sample(0.25f, 88200) == 11025); + CHECK(audio::start_sample(0.999f, 1000) == 499); +} + +TEST_CASE("start_sample: no offset starts at the first sample") { + CHECK(audio::start_sample(0.f, 1000) == 0); + CHECK(audio::start_sample(-0.5f, 1000) == 0); + CHECK(audio::start_sample(std::numeric_limits::quiet_NaN(), 1000) == 0); +} + +TEST_CASE("start_sample: the end of the buffer is its last sample, not past it") { + // OpenAL takes an offset at or past the end of the buffer for AL_INVALID_VALUE + CHECK(audio::start_sample(1.f, 1000) == 499); + CHECK(audio::start_sample(1.5f, 1000) == 499); + CHECK(audio::start_sample(1.f, 2) == 0); +} + +TEST_CASE("start_sample: a buffer of unknown or no size starts at 0") { + CHECK(audio::start_sample(0.5f, 0) == 0); + CHECK(audio::start_sample(0.5f, 1) == 0); + CHECK(audio::start_sample(0.5f, -4) == 0); +}