18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 22:55:16 +01:00
Files
maszyna/audio/startsample.cpp
jerrrrycho 97a6218094 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.
2026-11-20 16:33:34 +01:00

29 lines
681 B
C++

/*
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 <algorithm>
#include <cstdint>
namespace audio {
int start_sample( float const Start, int const Buffersize ) {
auto const samples { Buffersize / static_cast<int>( sizeof( std::int16_t ) ) };
if( samples <= 0
|| false == ( Start > 0.f ) ) { // also a NaN
return 0;
}
return std::min( static_cast<int>( std::min( Start, 1.f ) * static_cast<float>( samples ) ), samples - 1 );
}
} // audio