mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
35cafe9de5
- 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.
433 lines
15 KiB
C++
433 lines
15 KiB
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/railsinging.h"
|
|
|
|
#include "audio/audiorenderer.h"
|
|
#include "scene/scene.h"
|
|
#include "simulation/simulation.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/utilities.h"
|
|
#include "physics/vehicle.h"
|
|
#include "vehicle/DynObj.h"
|
|
#include "world/Segment.h"
|
|
#include "world/Track.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <initializer_list>
|
|
#include <iterator>
|
|
#include <numbers>
|
|
#include <optional>
|
|
#include <tuple>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace audio {
|
|
|
|
namespace {
|
|
|
|
double constexpr scan_interval { 0.1 }; // s, how often the rails and the trains are looked for anew
|
|
double constexpr rail_reach { 25.0 }; // m, how far from the listener a rail is heard from
|
|
double constexpr line_span { 60.0 }; // m, the tracks this near along the rails from a rail are the same line as it
|
|
int constexpr max_steps { 1000 }; // the most tracks a walk along the rails goes through
|
|
double constexpr easing_rate { 4.0 }; // 1/s, how fast the played volume follows what the scan found
|
|
float constexpr emitter_range { 200.f }; // m, the range of the sound of the rail
|
|
unsigned int constexpr sound_rate { 44100 };
|
|
std::uint32_t constexpr sound_seed { 0x5EED5EEDu };
|
|
|
|
using vehicle_list = std::vector<TDynamicObject const *>;
|
|
using track_list = std::vector<TTrack const *>;
|
|
|
|
// the point of a rail nearest the listener
|
|
struct rail_point {
|
|
TTrack const *track { nullptr };
|
|
double position { 0.0 }; // m from Point1 of the track
|
|
double distance { 0.0 }; // m from the listener
|
|
glm::dvec3 point { 0.0 };
|
|
};
|
|
|
|
// a train found along the rails
|
|
struct train_ahead {
|
|
double ahead; // m along the track from where the walk stands on it
|
|
TDynamicObject *vehicle; // the vehicle of it nearest the listener
|
|
};
|
|
|
|
// one line of rails by the listener and what its trains give
|
|
struct rail_line {
|
|
rail_point rail;
|
|
rail_singing::heard sound;
|
|
track_list covered; // the tracks of the line near the rail, by which the emitter playing it is found again
|
|
};
|
|
|
|
bool is_rail( TTrack const &Track ) {
|
|
|
|
return ( ( Track.iCategoryFlag & 1 ) != 0 )
|
|
&& ( Track.eType == tt_Normal || Track.eType == tt_Switch )
|
|
&& ( Track.CurrentSegment() != nullptr );
|
|
}
|
|
|
|
// the point of the track, as it is set, nearest the listener
|
|
rail_point nearest_on( TTrack const &Track, glm::dvec3 const &Listener ) {
|
|
|
|
auto const segment { Track.CurrentSegment() };
|
|
auto const t { segment->find_nearest_point( Listener ) };
|
|
auto const point { segment->FastGetPoint( t ) };
|
|
// the parameter of the curve stands in for the share of the length; it's close enough for where the sound comes from
|
|
return { &Track, t * segment->GetLength(), glm::distance( point, Listener ), point };
|
|
}
|
|
|
|
void collect_rails( scene::basic_cell const &Cell, glm::dvec3 const &Listener, std::vector<rail_point> &Rails ) {
|
|
|
|
if( glm::distance( Cell.area().center, Listener ) > Cell.area().radius + rail_reach ) {
|
|
return;
|
|
}
|
|
for( auto const *path : Cell.m_paths ) {
|
|
if( path == nullptr || false == is_rail( *path ) ) {
|
|
continue;
|
|
}
|
|
if( auto const rail { nearest_on( *path, Listener ) }; rail.distance <= rail_reach ) {
|
|
Rails.emplace_back( rail );
|
|
}
|
|
}
|
|
}
|
|
|
|
// the rails within reach of the listener, the nearest first
|
|
std::vector<rail_point> nearest_rails( glm::dvec3 const &Listener ) {
|
|
|
|
std::vector<scene::basic_section *> sections;
|
|
simulation::Region->sections( Listener, static_cast<float>( rail_reach ), sections );
|
|
std::vector<rail_point> rails;
|
|
for( auto const *section : sections ) {
|
|
for( auto const &cell : section->m_cells ) {
|
|
collect_rails( cell, Listener, rails );
|
|
}
|
|
}
|
|
std::ranges::sort( rails, {}, &rail_point::distance );
|
|
return rails;
|
|
}
|
|
|
|
// the vehicles of the consist the listener sits in, which the listener doesn't hear from the rails. none outside a cab
|
|
vehicle_list listener_consist() {
|
|
|
|
vehicle_list consist;
|
|
auto *vehicle { Global.pCamera.m_owner };
|
|
if( vehicle == nullptr || FreeFlyModeFlag ) {
|
|
return consist;
|
|
}
|
|
vehicle->for_each(
|
|
CouplingSet::All(),
|
|
[ &consist ]( TDynamicObject const *Vehicle ) { consist.emplace_back( Vehicle ); } );
|
|
return consist;
|
|
}
|
|
|
|
// the mean load of an axle of the consist of the vehicle, t. 0 when the consist has no axles to tell
|
|
double axle_load( TDynamicObject &Vehicle ) {
|
|
|
|
auto mass { 0.0 };
|
|
auto axles { 0 };
|
|
Vehicle.for_each(
|
|
CouplingSet::All(),
|
|
[ &mass, &axles ]( TDynamicObject const *Coupled ) {
|
|
mass += Coupled->physics->TotalMass();
|
|
axles += Coupled->physics->NAxles(); } );
|
|
return ( axles > 0 ? mass * 0.001 / axles : 0.0 );
|
|
}
|
|
|
|
// adds a stretch of the track to the way along the rails
|
|
void advance( rail_singing::path &Way, TTrack const &Track, double const Length ) {
|
|
|
|
auto const length { std::max( 0.0, Length ) };
|
|
Way.distance += length;
|
|
if( Track.fSoundDistance > 0.f ) {
|
|
Way.joints += length / Track.fSoundDistance;
|
|
}
|
|
}
|
|
|
|
// the volume of the rail is that of the vehicles: louder in a tunnel and on a bridge
|
|
double environment_factor( TTrack const &Track ) {
|
|
|
|
switch( Track.eEnvironment ) {
|
|
case e_tunnel: { return 1.3; }
|
|
case e_bridge: { return 1.5; }
|
|
default: { return 1.0; }
|
|
}
|
|
}
|
|
|
|
// goes along the rails from a point, by the switches as they are set, up to the first train that sounds
|
|
class line_walk {
|
|
|
|
public:
|
|
line_walk( rail_singing::tuning const &Tuning, vehicle_list const &Consist ) :
|
|
m_tuning( Tuning ),
|
|
m_consist( Consist )
|
|
{}
|
|
|
|
rail_singing::heard walk( TTrack const *Track, double From, int Direction ) {
|
|
|
|
rail_singing::path way;
|
|
auto steps { 0 };
|
|
while( Track != nullptr
|
|
&& way.distance <= m_tuning.search_distance
|
|
&& steps < max_steps ) {
|
|
++steps;
|
|
if( way.distance < line_span ) {
|
|
m_covered.emplace_back( Track );
|
|
}
|
|
if( auto const found { nearest_train( *Track, From, Direction ) }; found ) {
|
|
auto to { way };
|
|
advance( to, *Track, found->ahead );
|
|
return hear( to, *found->vehicle );
|
|
}
|
|
advance( way, *Track, ( Direction > 0 ? Track->Length() - From : From ) );
|
|
auto direction { static_cast<double>( Direction ) };
|
|
auto const *next { Track->Connected( Direction, direction ) };
|
|
if( next != nullptr ) {
|
|
Direction = ( direction > 0.0 ? 1 : -1 );
|
|
From = ( Direction > 0 ? 0.0 : next->Length() );
|
|
if( next->eType == tt_Switch || next->eType == tt_Cross ) {
|
|
++way.switches;
|
|
}
|
|
}
|
|
Track = next;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
track_list const &covered() const {
|
|
return m_covered; }
|
|
|
|
private:
|
|
// what the rail by the listener gives for the train at the far end of the way
|
|
rail_singing::heard hear( rail_singing::path const &Way, TDynamicObject &Vehicle ) const {
|
|
|
|
return rail_singing::hear( Way, { Vehicle.GetVelocity(), axle_load( Vehicle ) }, m_tuning );
|
|
}
|
|
|
|
// the nearest vehicle ahead on the track which runs fast enough to sound, and isn't of the listener's consist
|
|
std::optional<train_ahead> nearest_train( TTrack const &Track, double const From, int const Direction ) const {
|
|
|
|
std::optional<train_ahead> nearest;
|
|
for( auto *vehicle : Track.Dynamics ) {
|
|
if( vehicle == nullptr
|
|
|| std::ranges::find( m_consist, vehicle ) != std::end( m_consist ) ) {
|
|
continue;
|
|
}
|
|
auto const speed { vehicle->GetVelocity() };
|
|
auto const ahead { Direction > 0 ? vehicle->RaTranslationGet() - From : From - vehicle->RaTranslationGet() };
|
|
if( std::abs( speed ) <= m_tuning.speed_silent || ahead < 0.0 ) {
|
|
continue;
|
|
}
|
|
if( false == nearest.has_value() || ahead < nearest->ahead ) {
|
|
nearest = train_ahead { ahead, vehicle };
|
|
}
|
|
}
|
|
return nearest;
|
|
}
|
|
|
|
rail_singing::tuning const &m_tuning;
|
|
vehicle_list const &m_consist;
|
|
track_list m_covered;
|
|
};
|
|
|
|
// the lines of rails by the listener, at most as many as there are emitters, and what their trains give
|
|
std::vector<rail_line> hear_lines( rail_singing::tuning const &Tuning, std::size_t const Count ) {
|
|
|
|
auto const consist { listener_consist() };
|
|
std::vector<rail_line> lines;
|
|
for( auto const &rail : nearest_rails( Global.pCamera.Pos ) ) {
|
|
if( lines.size() >= Count ) {
|
|
break;
|
|
}
|
|
if( std::ranges::any_of(
|
|
lines,
|
|
[ &rail ]( rail_line const &Line ) {
|
|
return std::ranges::find( Line.covered, rail.track ) != std::end( Line.covered ); } ) ) {
|
|
// another stretch of a line already heard
|
|
continue;
|
|
}
|
|
line_walk walk { Tuning, consist };
|
|
// one way along the rails, then the other: in two statements, so the tracks are walked in this order on every compiler
|
|
auto const ahead { walk.walk( rail.track, rail.position, 1 ) };
|
|
auto const behind { walk.walk( rail.track, rail.position, -1 ) };
|
|
auto const sound { rail_singing::combine( ahead, behind ) };
|
|
lines.emplace_back( rail, sound, walk.covered() );
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void
|
|
rail_singing_effect::begin() {
|
|
|
|
if( m_prepared || m_maker.joinable() ) {
|
|
return;
|
|
}
|
|
m_maker = std::jthread(
|
|
[ this ]() {
|
|
m_loops = rail_singing::make_sound( sound_rate, sound_seed );
|
|
m_made.store( true ); } );
|
|
}
|
|
|
|
void
|
|
rail_singing_effect::update( double const Deltatime ) {
|
|
|
|
if( false == Global.bSoundEnabled ) {
|
|
return;
|
|
}
|
|
prepare();
|
|
if( false == m_prepared ) {
|
|
// the sound is still being made
|
|
return;
|
|
}
|
|
m_sincescan += Deltatime;
|
|
if( m_sincescan >= scan_interval ) {
|
|
m_sincescan = 0.0;
|
|
scan();
|
|
}
|
|
for( auto &slot : m_emitters ) {
|
|
play( slot, Deltatime );
|
|
}
|
|
}
|
|
|
|
void
|
|
rail_singing_effect::stop() {
|
|
|
|
for( auto &slot : m_emitters ) {
|
|
for( auto const &source : slot.sources ) {
|
|
if( source != nullptr ) {
|
|
source->stop();
|
|
}
|
|
}
|
|
slot.track = nullptr;
|
|
slot.target = {};
|
|
slot.played = {};
|
|
}
|
|
m_sincescan = scan_interval;
|
|
}
|
|
|
|
void
|
|
rail_singing_effect::scan() {
|
|
|
|
auto const lines { hear_lines( m_tuning, m_emitters.size() ) };
|
|
std::array<bool, std::tuple_size_v<decltype( m_emitters )>> taken {};
|
|
std::vector<rail_line const *> unmatched;
|
|
auto const assign {
|
|
[ this, &taken ]( std::size_t const Index, rail_line const &Line ) {
|
|
auto &slot { m_emitters[ Index ] };
|
|
slot.track = Line.rail.track;
|
|
auto const environment { environment_factor( *Line.rail.track ) };
|
|
std::ranges::transform( Line.sound, std::begin( slot.target ), [ environment ]( double const Gain ) { return Gain * environment; } );
|
|
taken[ Index ] = true; } };
|
|
// an emitter already playing one of the lines keeps it, so the sound doesn't jump from rail to rail
|
|
for( auto const &line : lines ) {
|
|
auto const lookup {
|
|
std::ranges::find_if(
|
|
m_emitters,
|
|
[ &line ]( emitter const &Emitter ) {
|
|
return Emitter.track != nullptr
|
|
&& std::ranges::find( line.covered, Emitter.track ) != std::end( line.covered ); } ) };
|
|
auto const index { static_cast<std::size_t>( std::distance( std::begin( m_emitters ), lookup ) ) };
|
|
if( lookup != std::end( m_emitters ) && false == taken[ index ] ) {
|
|
assign( index, line );
|
|
}
|
|
else {
|
|
unmatched.emplace_back( &line );
|
|
}
|
|
}
|
|
for( auto const *line : unmatched ) {
|
|
auto const vacant { std::ranges::find( taken, false ) };
|
|
if( vacant != std::end( taken ) ) {
|
|
assign( static_cast<std::size_t>( std::distance( std::begin( taken ), vacant ) ), *line );
|
|
}
|
|
}
|
|
// the emitters left without a line fade out where they are
|
|
for( std::size_t idx = 0; idx < m_emitters.size(); ++idx ) {
|
|
if( false == taken[ idx ] ) {
|
|
m_emitters[ idx ].track = nullptr;
|
|
m_emitters[ idx ].target = {};
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
rail_singing_effect::play( emitter &Emitter, double const Deltatime ) const {
|
|
|
|
if( Emitter.sources.front() == nullptr ) {
|
|
return;
|
|
}
|
|
auto const ease { std::clamp( Deltatime * easing_rate, 0.0, 1.0 ) };
|
|
for( std::size_t idx = 0; idx < rail_singing::part_count; ++idx ) {
|
|
auto &played { Emitter.played[ idx ] };
|
|
played += ( Emitter.target[ idx ] - played ) * ease;
|
|
if( played < m_tuning.audible && Emitter.target[ idx ] <= 0.0 ) {
|
|
played = 0.0;
|
|
}
|
|
}
|
|
if( Emitter.track != nullptr ) {
|
|
// the sound comes from the rail where it's nearest the listener, moving with them
|
|
glm::vec3 const point { nearest_on( *Emitter.track, Global.pCamera.Pos ).point };
|
|
for( auto const &source : Emitter.sources ) {
|
|
source->offset( point );
|
|
}
|
|
}
|
|
for( std::size_t idx = 0; idx < rail_singing::part_count; ++idx ) {
|
|
sound( *Emitter.sources[ idx ], Emitter.played[ idx ] );
|
|
}
|
|
}
|
|
|
|
void
|
|
rail_singing_effect::sound( sound_source &Source, double const Gain ) const {
|
|
|
|
if( Gain < m_tuning.audible ) {
|
|
Source.stop();
|
|
return;
|
|
}
|
|
Source
|
|
.gain( static_cast<float>( Gain ) )
|
|
.play( sound_flags::exclusive | sound_flags::looping );
|
|
}
|
|
|
|
void
|
|
rail_singing_effect::prepare() {
|
|
|
|
if( m_prepared ) {
|
|
return;
|
|
}
|
|
begin();
|
|
if( false == m_made.load() ) {
|
|
return;
|
|
}
|
|
m_maker.join();
|
|
m_prepared = true;
|
|
// what eu07.ini sets; the distance of the full sound kept under the range, so the coming in has a way to cover
|
|
m_tuning.volume = Global.RailSingingVolume;
|
|
m_tuning.search_distance = Global.RailSingingRange;
|
|
m_tuning.fade_far = std::min( Global.RailSingingFull, 0.9 * Global.RailSingingRange );
|
|
m_tuning.decay_welded = Global.RailSingingDecay;
|
|
m_tuning.near_boost = Global.RailSingingNearBoost;
|
|
auto const sound { std::move( m_loops ) };
|
|
m_loops = {};
|
|
std::array<char const *, rail_singing::part_count> constexpr names {
|
|
"#railsinging", "#railsinging.smacks.near", "#railsinging.smacks.far", "#railsinging.plums.near", "#railsinging.plums.far",
|
|
"#railsinging.voice" };
|
|
for( std::size_t idx = 0; idx < rail_singing::part_count; ++idx ) {
|
|
auto const buffer { audio::renderer.make_buffer( names[ idx ], sound[ idx ], sound_rate ) };
|
|
for( auto &slot : m_emitters ) {
|
|
slot.sources[ idx ] = std::make_unique<sound_source>( sound_placement::external, emitter_range );
|
|
slot.sources[ idx ]->main_buffer( buffer );
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace audio
|