18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-05 16:55:32 +01:00
Files
maszyna/tests/unit/test_rail_singing.cpp
T
maj00r 98ce78a659 feat(audio): the rails sing before a train comes, as heard by the track
A listener by the track hears the rail next to them hiss before a train
arrives: the wheels set the rail vibrating and the bending waves run along
the steel far ahead of the train. The rail nearest the listener now gives
this sound for the trains along it, as far as the way along the rails lets
it through.

The sound is made in code when first needed, so no asset is needed. It's
built from a recording of rail singing: noise of its measured spectrum (a
table of levels every 1/24 octave), close tones near 1.2-1.5 kHz that
swell together now and then, a calm ring near 7.84 kHz, short tones
falling from about 1.3 to 0.9 kHz now and then, and, for a train within
100 m, steady tones near 5 and 6 kHz measured under a passing train. The
parts are six looped buffers, played by sources placed on the rail where
it's nearest the listener.

The volume follows the way along the rails, walked through the switches as
they are set: the rail takes 0.02 dB/m, each joint 0.5 dB (from the
existing sound distance of the track, so jointed track carries it a much
shorter way), each switch 3 dB. The sound comes in from 1500 m, faster as
the train nears, in full from 100 m, and 6 dB louder within 100 m. It
grows with the speed and with the mean axle load of the consist, so a
draisine hardly sings. The listener's own consist isn't heard from the
rails.

eu07.ini: sound.railsinging (yes) turns it off, and sound.railsinging.volume
(0.45), .range (1500 m), .full (100 m), .decay (0.02 dB/m) and .nearboost
(6 dB) tune it. With sound.railsinging no the simulator behaves as before.
No format changes, no SVN changes.
2026-12-01 23:24:09 +01:00

276 lines
13 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "audio/railsingingmodel.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iterator>
#include <numbers>
#include <vector>
// the singing of the rails (sound.railsinging): the volume the rail by the listener sings with for a train along
// it, and the looped sound it's played with
namespace {
namespace rs = audio::rail_singing;
rs::tuning const tuning {};
double constexpr rate { 44100.0 };
rs::path welded( double const Distance ) {
return { Distance, 0.0, 0 };
}
rs::train running( double const Speed, double const AxleLoad = 20.0 ) {
return { Speed, AxleLoad };
}
rs::loops const &sound() {
static auto const made { rs::make_sound( 44100, 3 ) };
return made;
}
std::vector<std::int16_t> const &loop( rs::part const Part ) {
return sound()[ rs::index( Part ) ];
}
// the power of the samples at one frequency (the Goertzel filter), for the whole of the loop
double power_at( std::vector<std::int16_t> const &Samples, double const Frequency ) {
auto const coefficient { 2.0 * std::cos( 2.0 * std::numbers::pi * Frequency / rate ) };
auto previous { 0.0 };
auto beforeprevious { 0.0 };
for( auto const sample : Samples ) {
auto const current { sample + coefficient * previous - beforeprevious };
beforeprevious = previous;
previous = current;
}
auto const count { static_cast<double>( Samples.size() ) };
return ( previous * previous + beforeprevious * beforeprevious - coefficient * previous * beforeprevious ) / ( count * count );
}
// the mean power of the samples over a range of frequencies, every few hertz
double band_power( std::vector<std::int16_t> const &Samples, double const From, double const To ) {
auto sum { 0.0 };
auto count { 0 };
auto frequency { From };
while( frequency < To ) {
sum += power_at( Samples, frequency );
++count;
frequency += 23.7;
}
return sum / count;
}
double root_mean_square( std::vector<std::int16_t> const &Samples ) {
auto power { 0.0 };
for( auto const sample : Samples ) {
power += static_cast<double>( sample ) * sample;
}
return std::sqrt( power / static_cast<double>( Samples.size() ) );
}
} // namespace
TEST_CASE("rail singing: the farther the train along the rails, the quieter the rail") {
auto const close { rs::gain( welded( 200.0 ), running( 100.0 ), tuning ) };
auto const middle { rs::gain( welded( 400.0 ), running( 100.0 ), tuning ) };
auto const distant { rs::gain( welded( 700.0 ), running( 100.0 ), tuning ) };
CHECK(close > middle);
CHECK(middle > distant);
CHECK(distant > 0.0);
}
TEST_CASE("rail singing: the joints and the switches on the way take a part of the sound") {
auto const plain { rs::gain( welded( 500.0 ), running( 100.0 ), tuning ) };
auto const jointed { rs::gain( { 500.0, 5.0, 0 }, running( 100.0 ), tuning ) };
auto const switched { rs::gain( { 500.0, 0.0, 2 }, running( 100.0 ), tuning ) };
CHECK(jointed < plain);
CHECK(switched < plain);
CHECK(jointed == doctest::Approx( plain * std::pow( 10.0, -5.0 * tuning.loss_joint / 20.0 ) ));
// a jointed line carries the sound a much shorter way than a welded one
CHECK(rs::gain( welded( 700.0 ), running( 100.0 ), tuning ) > 0.0);
CHECK(rs::gain( { 700.0, 40.0, 0 }, running( 100.0 ), tuning ) == 0.0);
}
TEST_CASE("rail singing: a faster train sings louder, a standing or crawling one not at all") {
CHECK(rs::gain( welded( 500.0 ), running( 0.0 ), tuning ) == 0.0);
CHECK(rs::gain( welded( 500.0 ), running( tuning.speed_silent ), tuning ) == 0.0);
CHECK(rs::gain( welded( 500.0 ), running( 60.0 ), tuning ) < rs::gain( welded( 500.0 ), running( 120.0 ), tuning ));
// the direction of the run doesn't matter
CHECK(rs::gain( welded( 500.0 ), running( -80.0 ), tuning ) == doctest::Approx( rs::gain( welded( 500.0 ), running( 80.0 ), tuning ) ));
}
TEST_CASE("rail singing: a heavy axle sets the rail singing louder, a draisine hardly at all") {
auto const reference { rs::gain( welded( 500.0 ), running( 100.0, tuning.load_reference ), tuning ) };
auto const draisine { rs::gain( welded( 500.0 ), running( 100.0, 3.5 ), tuning ) };
auto const coach { rs::gain( welded( 500.0 ), running( 100.0, 12.0 ), tuning ) };
CHECK(draisine == doctest::Approx( reference * 3.5 / tuning.load_reference ));
CHECK(draisine < coach);
CHECK(coach < reference);
// no heavier than the cap, and a load not known is taken as the reference one
CHECK(rs::gain( welded( 500.0 ), running( 100.0, 200.0 ), tuning ) == doctest::Approx( reference * tuning.load_cap ));
CHECK(rs::gain( welded( 500.0 ), running( 100.0, 0.0 ), tuning ) == doctest::Approx( reference ));
}
TEST_CASE("rail singing: the smacks grow with the load and the speed faster than the singing") {
CHECK(rs::smack_share( running( 100.0, tuning.load_reference ), tuning ) == doctest::Approx( 1.0 ));
CHECK(rs::smack_share( running( 100.0, 3.5 ), tuning ) < rs::smack_share( running( 100.0, 12.0 ), tuning ));
CHECK(rs::smack_share( running( 60.0 ), tuning ) < rs::smack_share( running( 120.0 ), tuning ));
CHECK(rs::smack_share( running( 400.0, 100.0 ), tuning ) == doctest::Approx( tuning.smack_cap ));
}
TEST_CASE("rail singing: the smacks of a far train are drawn out, of a near one short") {
CHECK(rs::far_share( tuning.smacks_near - 100.0, tuning ) == 0.0);
CHECK(rs::far_share( tuning.smacks_far + 100.0, tuning ) == 1.0);
auto const between { rs::far_share( 0.5 * ( tuning.smacks_near + tuning.smacks_far ), tuning ) };
CHECK(between > 0.0);
CHECK(between < 1.0);
}
TEST_CASE("rail singing: the rail is heard in full right up to the train") {
CHECK(rs::gain( welded( 0.0 ), running( 100.0 ), tuning ) > 0.0);
CHECK(rs::gain( welded( 5.0 ), running( 100.0 ), tuning ) > rs::gain( welded( tuning.near_to ), running( 100.0 ), tuning ));
}
TEST_CASE("rail singing: a near train sounds louder, and the voice of the rail comes in") {
auto const far_away { rs::hear( welded( tuning.near_from ), running( 120.0 ), tuning ) };
auto const near_by { rs::hear( welded( tuning.near_to ), running( 120.0 ), tuning ) };
auto const voice { rs::index( rs::part::voice ) };
auto const singing { rs::index( rs::part::singing ) };
CHECK(far_away[ voice ] == 0.0);
CHECK(near_by[ voice ] == doctest::Approx( near_by[ singing ] ));
// over the way in between the rail itself takes a little, the boost gives 6 dB
auto const between { tuning.near_from - tuning.near_to };
CHECK(near_by[ singing ] / far_away[ singing ] == doctest::Approx( std::pow( 10.0, ( tuning.near_boost + tuning.decay_welded * between ) / 20.0 ) ));
}
TEST_CASE("rail singing: the plums of a far train are rarer and quieter") {
auto const near_by { rs::hear( welded( 60.0 ), running( 120.0 ), tuning ) };
auto const far_away { rs::hear( welded( 1000.0 ), running( 120.0 ), tuning ) };
auto const plums_near { rs::index( rs::part::plums_near ) };
auto const plums_far { rs::index( rs::part::plums_far ) };
auto const smacks { []( rs::heard const &Heard ) {
return std::hypot( Heard[ rs::index( rs::part::smacks_near ) ], Heard[ rs::index( rs::part::smacks_far ) ] ); } };
CHECK(near_by[ plums_far ] == doctest::Approx( 0.0 ));
CHECK(far_away[ plums_near ] == doctest::Approx( 0.0 ));
CHECK(far_away[ plums_far ] == doctest::Approx( smacks( far_away ) * std::pow( 10.0, tuning.plums_far_level / 20.0 ) ));
// the far loop has fewer of them: less of the sound in it
CHECK(root_mean_square( loop( rs::part::plums_far ) ) < 0.8 * root_mean_square( loop( rs::part::plums_near ) ));
}
TEST_CASE("rail singing: the voice is of steady tones near 5 and 6 kHz") {
auto const &voice { loop( rs::part::voice ) };
for( auto const frequency : { 4955.0, 5360.0, 6215.0 } ) {
CHECK(band_power( voice, frequency - 30.0, frequency + 30.0 ) > 1000.0 * band_power( voice, 3000.0, 4500.0 ));
}
}
TEST_CASE("rail singing: nothing is heard from beyond the search, and the sound comes in ever faster on the way from it") {
CHECK(rs::gain( welded( tuning.search_distance + 1.0 ), running( 200.0 ), tuning ) == 0.0);
CHECK(rs::smack_gain( welded( tuning.search_distance + 1.0 ), running( 200.0 ), tuning ) == 0.0);
auto const coming { rs::smack_gain( welded( 0.5 * ( tuning.fade_far + tuning.search_distance ) ), running( 200.0 ), tuning ) };
CHECK(coming > 0.0);
CHECK(coming < rs::smack_gain( welded( tuning.fade_far ), running( 200.0 ), tuning ));
// of the same 200 m of the way, the last ones before full bring more than the first ones: the dB grow ever faster. the
// rail itself takes the same over both, so it doesn't count
auto const level { []( double const Distance ) {
return 20.0 * std::log10( rs::smack_gain( welded( Distance ), running( 200.0, 30.0 ), tuning ) ); } };
auto const first { level( tuning.search_distance - 250.0 ) - level( tuning.search_distance - 50.0 ) };
auto const last { level( tuning.fade_far ) - level( tuning.fade_far + 200.0 ) };
CHECK(last > 2.0 * first);
// in full at fade_far, without a step
CHECK(level( tuning.fade_far ) - level( tuning.fade_far + 1.0 ) < 0.2);
}
TEST_CASE("rail singing: the smacks come out of the singing from farther away") {
auto const ratio { []( double const Distance ) {
return rs::smack_gain( welded( Distance ), running( 130.0 ), tuning ) / rs::gain( welded( Distance ), running( 130.0 ), tuning ); } };
CHECK(ratio( 35.0 ) > ratio( 5.0 ));
CHECK(ratio( 35.0 ) / ratio( 5.0 ) == doctest::Approx( std::pow( 10.0, ( 1.0 - tuning.smack_loss_share ) * tuning.decay_welded * 30.0 / 20.0 ) ));
// still heard where the singing has gone below hearing
CHECK(rs::gain( { 450.0, 0.0, 12 }, running( 130.0, 12.0 ), tuning ) == 0.0);
CHECK(rs::smack_gain( { 450.0, 0.0, 12 }, running( 130.0, 12.0 ), tuning ) > 0.0);
}
TEST_CASE("rail singing: the two sides add up as sound powers") {
auto const both { rs::combine( { 0.3, 0.6, 0.0, 0.0, 0.0, 0.0 }, { 0.4, 0.8, 0.0, 0.0, 0.0, 0.0 } ) };
CHECK(both[ rs::index( rs::part::singing ) ] == doctest::Approx( 0.5 ));
CHECK(both[ rs::index( rs::part::smacks_near ) ] == doctest::Approx( 1.0 ));
CHECK(rs::combine( {}, {} )[ rs::index( rs::part::singing ) ] == 0.0);
}
TEST_CASE("rail singing: the sound is the same for the same seed, its loops all of one length") {
auto const &first { sound() };
auto const second { rs::make_sound( 44100, 3 ) };
auto const other { rs::make_sound( 44100, 4 ) };
for( auto const &samples : first ) {
CHECK(samples.size() == rs::loop_length);
}
CHECK(first == second);
CHECK(first[ rs::index( rs::part::singing ) ] != other[ rs::index( rs::part::singing ) ]);
CHECK(rs::make_sound( 0, 3 )[ rs::index( rs::part::singing ) ].empty());
}
TEST_CASE("rail singing: what a near train gives peaks at -3 dB of the full scale") {
auto const &loops { sound() };
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 } ) {
sum += loops[ rs::index( part ) ][ idx ];
}
peak = std::max( peak, std::abs( sum ) );
}
CHECK(peak == doctest::Approx( 0.7079 * 32767.0 ).epsilon( 0.001 ));
}
TEST_CASE("rail singing: the loops run into their starts without a jump") {
for( auto const &samples : sound() ) {
auto steepest { 0 };
for( std::size_t idx = 1; idx < samples.size(); ++idx ) {
steepest = std::max( steepest, std::abs( samples[ idx ] - samples[ idx - 1 ] ) );
}
CHECK(std::abs( samples.front() - samples.back() ) <= steepest);
}
}
TEST_CASE("rail singing: the singing is of the close tones near 1.3 kHz and of the ring near 7.84 kHz") {
auto const &singing { loop( rs::part::singing ) };
auto const tones { band_power( singing, 1250.0, 1470.0 ) };
auto const middle { band_power( singing, 3000.0, 5000.0 ) };
auto const ring { band_power( singing, 7700.0, 7950.0 ) };
auto const beside { band_power( singing, 6000.0, 6500.0 ) };
CHECK(tones > 30.0 * middle);
CHECK(ring > 5.0 * beside);
}
TEST_CASE("rail singing: the smacks are swells of the singing band now and then, the far ones drawn out") {
auto const &nearby { loop( rs::part::smacks_near ) };
auto const quiet { std::ranges::count_if( nearby, []( std::int16_t const Sample ) { return std::abs( Sample ) < 30; } ) };
CHECK(static_cast<double>( quiet ) > 0.5 * static_cast<double>( nearby.size() ));
CHECK(band_power( nearby, 1250.0, 1470.0 ) > 100.0 * band_power( nearby, 3000.0, 6000.0 ));
// of the same peak but longer, the far ones carry more of the sound
CHECK(root_mean_square( loop( rs::part::smacks_far ) ) > 1.3 * root_mean_square( nearby ));
}
TEST_CASE("rail singing: the plums are short falling tones near 1 kHz, one now and then") {
auto const &plums { loop( rs::part::plums_near ) };
auto const quiet { std::ranges::count_if( plums, []( std::int16_t const Sample ) { return Sample == 0; } ) };
// one every 1-2 s, some 50 ms long: silence most of the time
CHECK(static_cast<double>( quiet ) > 0.9 * static_cast<double>( plums.size() ));
CHECK(band_power( plums, 900.0, 1350.0 ) > 100.0 * band_power( plums, 2500.0, 5000.0 ));
// the tone falls: in the first part of a plum it's higher than in the last
auto const start { static_cast<std::size_t>( std::distance( plums.begin(), std::ranges::find_if( plums, []( std::int16_t const Sample ) { return Sample != 0; } ) ) ) };
REQUIRE(start + 2000 < plums.size());
auto crossings { []( std::vector<std::int16_t> const &Samples, std::size_t const From, std::size_t const To ) {
auto count { 0 };
for( auto idx = From + 1; idx < To; ++idx ) {
if( ( Samples[ idx - 1 ] < 0 ) != ( Samples[ idx ] < 0 ) ) { ++count; }
}
return count; } };
CHECK(crossings( plums, start, start + 500 ) > crossings( plums, start + 1000, start + 1500 ));
}