mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
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.
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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<ALint>( controller->start() * ( buffersize / sizeof( std::int16_t ) ) ) );
|
||||
audio::start_sample( controller->start(), buffersize ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<audio::buffer_handle>;
|
||||
|
||||
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 };
|
||||
|
||||
@@ -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<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
|
||||
@@ -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<ALint>( controller->start() * ( buffersize / sizeof( std::int16_t ) ) ) );
|
||||
audio::start_sample( controller->start(), buffersize ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
audio/startsample.cpp
Normal file
28
audio/startsample.cpp
Normal file
@@ -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 <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
|
||||
19
audio/startsample.h
Normal file
19
audio/startsample.h
Normal file
@@ -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
|
||||
34
tests/unit/test_start_sample.cpp
Normal file
34
tests/unit/test_start_sample.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "audio/startsample.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
// 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<float>::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);
|
||||
}
|
||||
Reference in New Issue
Block a user