From 35cafe9de54463aba39b7238ef9748753666b9b3 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Wed, 2 Dec 2026 18:33:09 +0100 Subject: [PATCH] fix(audio): the rails stop singing when the editor opens, nor sing in the test mode; Sonar of #584 - driver_mode::push_editor() stops the singing before the editor is pushed, by F11 and by the edit session. push_mode() doesn't call exit() of the mode it covers, so the loops went on playing in the editor. Back in the driver mode the next update scans the rails again. - The sound of the singing isn't made in the test mode, which has no sound (enter() skips begin()); update() is under the test-mode guard of staging since the merge. - sound.railsinging.nearboost: a train within 40 m is louder by it, one at 40-100 m by a part of it growing as it comes, as closeness() has it; the comment said within 100 m. - Sonar, the values unchanged: the measured levels written as floats (each the same float as before, checked for all 232), the centres of the sixths of an octave counted in a while loop, CTAD, try_emplace, the flag of the sound made with the default ordering, an if without a variable, using enum in a test. The std::mt19937 of the noise is NOSONAR: the noise of a sound, the same for a seed on every compiler. --- application/drivermode.cpp | 13 ++++++++--- application/drivermode.h | 3 +++ audio/audio.cpp | 2 +- audio/railsinging.cpp | 10 ++++---- audio/railsingingmodel.cpp | 40 +++++++++++++++++--------------- tests/unit/test_rail_singing.cpp | 3 ++- utilities/Globals.h | 2 +- 7 files changed, 42 insertions(+), 31 deletions(-) diff --git a/application/drivermode.cpp b/application/drivermode.cpp index 6c401a6a7..7680823ce 100644 --- a/application/drivermode.cpp +++ b/application/drivermode.cpp @@ -521,7 +521,7 @@ bool driver_mode::update() // edit session starts in the editor. the switch is made after the initial update on purpose, so the // simulation state is set up the same way as when the editor is entered by hand right after the load m_editorpending = false; - Application.push_mode(eu07_application::mode::editor); + push_editor(); } return true; @@ -537,7 +537,8 @@ void driver_mode::enter() Global.pCamera = Camera; Global.pDebugCamera = DebugCamera; - if (Global.RailSinging && Global.bSoundEnabled) + // the test mode has no sound, so the sound of the singing isn't made either + if (Global.RailSinging && Global.bSoundEnabled && false == Global.test.enabled) { // the sound of the singing rails takes a while to make: on a thread of its own, from now on m_railsinging.begin(); @@ -597,6 +598,12 @@ void driver_mode::exit() m_railsinging.stop(); } +void driver_mode::push_editor() +{ + m_railsinging.stop(); + Application.push_mode(eu07_application::mode::editor); +} + void driver_mode::on_key(int const Key, int const Scancode, int const Action, int const Mods) { @@ -1173,7 +1180,7 @@ void driver_mode::OnKeyDown(int cKey) // editor mode if (false == Global.ctrlState && false == Global.shiftState) { - Application.push_mode(eu07_application::mode::editor); + push_editor(); } break; } diff --git a/application/drivermode.h b/application/drivermode.h index b42cd886c..2b704c649 100644 --- a/application/drivermode.h +++ b/application/drivermode.h @@ -97,6 +97,9 @@ private: }; // methods + // the editor opened over this mode. push_mode() doesn't call exit() of the mode it covers, so the singing of the rails is + // stopped here: its loops would go on playing in the editor, and the scenery may change there + void push_editor(); void update_camera( const double Deltatime ); double shake_fraction() const; // handles vehicle change flag diff --git a/audio/audio.cpp b/audio/audio.cpp index 81fbdd011..0347a1e3d 100644 --- a/audio/audio.cpp +++ b/audio/audio.cpp @@ -305,7 +305,7 @@ buffer_manager::create( std::string const &Name, std::vector const // no caption: there's no file it could be named after buffer.id = upload_sound( sound ); m_buffers.emplace_back( std::move( buffer ) ); - m_buffermappings.emplace( Name, handle ); + m_buffermappings.try_emplace( Name, handle ); return handle; } diff --git a/audio/railsinging.cpp b/audio/railsinging.cpp index 5fb6effe6..216aa197b 100644 --- a/audio/railsinging.cpp +++ b/audio/railsinging.cpp @@ -247,12 +247,10 @@ std::vector hear_lines( rail_singing::tuning const &Tuning, std::size if( lines.size() >= Count ) { break; } - auto const known { - std::ranges::any_of( + if( std::ranges::any_of( lines, [ &rail ]( rail_line const &Line ) { - return std::ranges::find( Line.covered, rail.track ) != std::end( Line.covered ); } ) }; - if( known ) { + return std::ranges::find( Line.covered, rail.track ) != std::end( Line.covered ); } ) ) { // another stretch of a line already heard continue; } @@ -277,7 +275,7 @@ rail_singing_effect::begin() { m_maker = std::jthread( [ this ]() { m_loops = rail_singing::make_sound( sound_rate, sound_seed ); - m_made.store( true, std::memory_order_release ); } ); + m_made.store( true ); } ); } void @@ -406,7 +404,7 @@ rail_singing_effect::prepare() { return; } begin(); - if( false == m_made.load( std::memory_order_acquire ) ) { + if( false == m_made.load() ) { return; } m_maker.join(); diff --git a/audio/railsingingmodel.cpp b/audio/railsingingmodel.cpp index 4cffe14a6..656e2863a 100644 --- a/audio/railsingingmodel.cpp +++ b/audio/railsingingmodel.cpp @@ -29,21 +29,21 @@ namespace { // the spectrum of a rail singing before a train, measured from a recording of one: the level in dB of every 1/24 of an octave // from 25 Hz up, 0 at the loudest std::array constexpr measured_levels { - -13.4, -13.2, -12.9, -12.6, -12.3, -12.1, -11.5, -10.0, -8.8, -8.0, -8.1, -8.2, -8.5, -8.9, -9.5, -9.4, - -9.1, -8.8, -8.5, -8.2, -8.0, -7.8, -7.9, -8.0, -8.2, -8.5, -8.9, -9.2, -9.1, -7.7, -6.7, -7.1, - -7.5, -7.8, -8.0, -8.1, -8.1, -8.1, -7.1, -7.3, -8.1, -8.5, -8.7, -9.6, -8.0, -5.8, -7.3, -4.2, - 0.0, -2.3, -7.6, -6.6, -9.1, -7.9, -8.0, -10.0, -8.2, -6.6, -0.9, -5.5, -12.4, -13.7, -14.3, -16.4, - -17.3, -15.6, -14.4, -17.8, -17.7, -17.5, -17.7, -17.3, -18.6, -18.2, -17.4, -20.3, -19.4, -20.7, -21.0, -20.2, - -18.8, -21.2, -22.1, -22.2, -21.7, -21.7, -22.4, -23.4, -22.7, -23.9, -25.1, -24.1, -25.1, -25.3, -26.1, -25.5, - -28.0, -27.8, -27.8, -27.4, -28.3, -27.2, -27.1, -28.1, -28.9, -29.1, -29.5, -30.1, -31.2, -32.3, -32.6, -30.9, - -32.0, -32.4, -31.6, -32.4, -31.0, -31.6, -31.4, -33.0, -33.7, -32.8, -34.9, -33.8, -34.2, -33.3, -33.0, -31.1, - -31.8, -33.1, -33.3, -33.2, -31.0, -32.6, -25.3, -25.4, -24.6, -18.4, -19.3, -23.9, -21.8, -20.8, -33.3, -40.0, - -40.0, -41.5, -42.5, -43.2, -43.8, -43.3, -43.3, -45.2, -44.9, -45.7, -46.5, -47.2, -47.0, -48.0, -48.0, -48.1, - -48.5, -48.6, -48.9, -49.6, -49.7, -50.6, -50.4, -50.5, -50.6, -50.8, -50.7, -51.0, -51.2, -51.9, -52.0, -51.6, - -51.6, -51.7, -51.3, -51.1, -51.2, -51.7, -51.8, -52.2, -52.2, -51.9, -52.0, -51.7, -51.7, -51.4, -51.5, -51.7, - -51.1, -50.9, -50.4, -49.4, -48.9, -46.5, -43.0, -40.4, -41.4, -43.4, -44.8, -47.9, -50.9, -52.0, -52.2, -53.1, - -53.3, -53.7, -53.6, -53.4, -53.3, -52.7, -50.1, -50.4, -51.1, -51.0, -50.7, -48.8, -46.9, -48.4, -49.6, -48.4, - -46.2, -46.2, -47.1, -46.4, -46.5, -46.5, -46.5, -46.1 + -13.4f, -13.2f, -12.9f, -12.6f, -12.3f, -12.1f, -11.5f, -10.0f, -8.8f, -8.0f, -8.1f, -8.2f, -8.5f, -8.9f, -9.5f, -9.4f, + -9.1f, -8.8f, -8.5f, -8.2f, -8.0f, -7.8f, -7.9f, -8.0f, -8.2f, -8.5f, -8.9f, -9.2f, -9.1f, -7.7f, -6.7f, -7.1f, + -7.5f, -7.8f, -8.0f, -8.1f, -8.1f, -8.1f, -7.1f, -7.3f, -8.1f, -8.5f, -8.7f, -9.6f, -8.0f, -5.8f, -7.3f, -4.2f, + 0.0f, -2.3f, -7.6f, -6.6f, -9.1f, -7.9f, -8.0f, -10.0f, -8.2f, -6.6f, -0.9f, -5.5f, -12.4f, -13.7f, -14.3f, -16.4f, + -17.3f, -15.6f, -14.4f, -17.8f, -17.7f, -17.5f, -17.7f, -17.3f, -18.6f, -18.2f, -17.4f, -20.3f, -19.4f, -20.7f, -21.0f, -20.2f, + -18.8f, -21.2f, -22.1f, -22.2f, -21.7f, -21.7f, -22.4f, -23.4f, -22.7f, -23.9f, -25.1f, -24.1f, -25.1f, -25.3f, -26.1f, -25.5f, + -28.0f, -27.8f, -27.8f, -27.4f, -28.3f, -27.2f, -27.1f, -28.1f, -28.9f, -29.1f, -29.5f, -30.1f, -31.2f, -32.3f, -32.6f, -30.9f, + -32.0f, -32.4f, -31.6f, -32.4f, -31.0f, -31.6f, -31.4f, -33.0f, -33.7f, -32.8f, -34.9f, -33.8f, -34.2f, -33.3f, -33.0f, -31.1f, + -31.8f, -33.1f, -33.3f, -33.2f, -31.0f, -32.6f, -25.3f, -25.4f, -24.6f, -18.4f, -19.3f, -23.9f, -21.8f, -20.8f, -33.3f, -40.0f, + -40.0f, -41.5f, -42.5f, -43.2f, -43.8f, -43.3f, -43.3f, -45.2f, -44.9f, -45.7f, -46.5f, -47.2f, -47.0f, -48.0f, -48.0f, -48.1f, + -48.5f, -48.6f, -48.9f, -49.6f, -49.7f, -50.6f, -50.4f, -50.5f, -50.6f, -50.8f, -50.7f, -51.0f, -51.2f, -51.9f, -52.0f, -51.6f, + -51.6f, -51.7f, -51.3f, -51.1f, -51.2f, -51.7f, -51.8f, -52.2f, -52.2f, -51.9f, -52.0f, -51.7f, -51.7f, -51.4f, -51.5f, -51.7f, + -51.1f, -50.9f, -50.4f, -49.4f, -48.9f, -46.5f, -43.0f, -40.4f, -41.4f, -43.4f, -44.8f, -47.9f, -50.9f, -52.0f, -52.2f, -53.1f, + -53.3f, -53.7f, -53.6f, -53.4f, -53.3f, -52.7f, -50.1f, -50.4f, -51.1f, -51.0f, -50.7f, -48.8f, -46.9f, -48.4f, -49.6f, -48.4f, + -46.2f, -46.2f, -47.1f, -46.4f, -46.5f, -46.5f, -46.5f, -46.1f }; double constexpr measured_first { 25.0 }; // Hz, the frequency of the first level double constexpr measured_step { 1.0 / 24.0 }; // octave, between the levels @@ -268,7 +268,7 @@ public: return output; } private: - std::mt19937 m_generator; + std::mt19937 m_generator; // NOSONAR: the noise of a sound, made the same for a seed on every compiler, not for security }; // makes the loops, step by step as in the recording @@ -519,8 +519,8 @@ private: // goes to the sixth of an octave its frequency is nearest the middle of static std::vector smoothed( std::vector const &Frequencies, std::vector const &Power, std::size_t const Count ) { - std::vector sums( Count, 0.0 ); - std::vector counts( Count, 0 ); + std::vector sums( Count, 0.0 ); + std::vector counts( Count, 0 ); for( std::size_t idx = 0; idx < Frequencies.size(); ++idx ) { auto const position { std::floor( std::log2( Frequencies[ idx ] / measured_first ) * 6.0 + 0.5 ) }; if( position < 0.0 || position >= static_cast( Count ) ) { @@ -555,8 +555,10 @@ private: measured.emplace_back( std::pow( 10.0, measured_level( frequency ) / 10.0 ) ); } std::vector centres; - for( auto centre { measured_first }; centre < std::min( 20000.0, 0.5 * m_rate ); centre *= std::pow( 2.0, 1.0 / 6.0 ) ) { + auto centre { measured_first }; + while( centre < std::min( 20000.0, 0.5 * m_rate ) ) { centres.emplace_back( centre ); + centre *= std::pow( 2.0, 1.0 / 6.0 ); } auto const target { smoothed( frequencies, measured, centres.size() ) }; auto const actual { smoothed( frequencies, made, centres.size() ) }; diff --git a/tests/unit/test_rail_singing.cpp b/tests/unit/test_rail_singing.cpp index 4fb461f16..c29bfcd84 100644 --- a/tests/unit/test_rail_singing.cpp +++ b/tests/unit/test_rail_singing.cpp @@ -216,10 +216,11 @@ TEST_CASE("rail singing: the sound is the same for the same seed, its loops all TEST_CASE("rail singing: what a near train gives peaks at -3 dB of the full scale") { auto const &loops { sound() }; + using enum rs::part; auto peak { 0 }; for( std::size_t idx = 0; idx < rs::loop_length; ++idx ) { auto sum { 0 }; - for( auto const part : { rs::part::singing, rs::part::smacks_near, rs::part::plums_near, rs::part::voice } ) { + for( auto const part : { singing, smacks_near, plums_near, voice } ) { sum += loops[ rs::index( part ) ][ idx ]; } peak = std::max( peak, std::abs( sum ) ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 01bebbcbd..5e450efd1 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -292,7 +292,7 @@ struct global_settings { double RailSingingRange{ 1500.0 }; // sound.railsinging.range: m, the farthest along the rails a train is heard from double RailSingingFull{ 100.0 }; // sound.railsinging.full: m, from here in a train is heard in full double RailSingingDecay{ 0.02 }; // sound.railsinging.decay: dB/m, what the rail itself takes on the way - double RailSingingNearBoost{ 6.0 }; // sound.railsinging.nearboost: dB, a train within 100 m sounds louder by this + double RailSingingNearBoost{ 6.0 }; // sound.railsinging.nearboost: dB, a train within 40 m sounds louder by this, one at 40-100 m by a part of it growing as it comes int audio_max_sources = 30; std::string AudioRenderer; bool AudioAsyncLoad{ false }; // sound files are decoded by worker threads