18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/audio/audiorenderer_extra.h
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

59 lines
1.9 KiB
C++

// separate file because of include dependecies mess
namespace audio {
template <class Iterator_>
openal_source &
openal_source::bind( sound_source *Controller, uint32_sequence Sounds, Iterator_ First, Iterator_ Last ) {
controller = Controller;
sounds = Sounds;
// look up and queue assigned buffers
std::vector<ALuint> buffers;
std::for_each(
First, Last,
[&]( audio::buffer_handle const &bufferhandle ) {
auto const &buffer { audio::renderer.buffer( bufferhandle ) };
if (buffer.id != null_resource) buffers.emplace_back( buffer.id ); } );
is_multipart = buffers.size() > 1;
if( id != audio::null_resource && !buffers.empty()) {
::alSourceQueueBuffers( id, static_cast<ALsizei>( 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
// for single sound we also make sure not to apply the offset to optional bookends
if( controller->start() == 0.f || is_multipart || controller->is_bookend( buffers.front() ) ) {
// regular case with no offset, reset bound source just in case
::alSourcei( id, AL_SAMPLE_OFFSET, 0 );
}
else {
// move playback start to specified point in 0-1 range
ALint buffersize { 0 };
::alGetBufferi( buffers.front(), AL_SIZE, &buffersize );
::alSourcei(
id,
AL_SAMPLE_OFFSET,
audio::start_sample( controller->start(), buffersize ) );
}
}
return *this;
}
inline
float
amplitude_to_db( float const Amplitude ) {
return 20.f * std::log10( Amplitude );
}
inline
float
db_to_amplitude( float const Decibels ) {
return std::pow( 10.f, Decibels / 20.f );
}
}