mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 10:05:23 +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.
752 lines
34 KiB
C++
752 lines
34 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/railsingingmodel.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
#include <complex>
|
|
#include <initializer_list>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <numbers>
|
|
#include <random>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace audio::rail_singing {
|
|
|
|
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<float, 232> constexpr measured_levels {
|
|
-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
|
|
|
|
using complex = std::complex<double>;
|
|
using signal = std::vector<double>;
|
|
using spectrum = std::vector<complex>;
|
|
|
|
// a range of frequencies
|
|
struct band {
|
|
double from; // Hz
|
|
double to; // Hz
|
|
bool contains( double const Frequency ) const {
|
|
return Frequency >= from && Frequency < to; }
|
|
};
|
|
|
|
band constexpr singing_band { 1000.0, 1700.0 }; // the close tones of the singing
|
|
band constexpr ring_band { 7300.0, 8400.0 }; // the ring of the rail near 7.84 kHz
|
|
band constexpr high_band { 8400.0, 17000.0 }; // the hiss over it
|
|
double constexpr rumble_edge { 250.0 }; // Hz, below this the rumble
|
|
double constexpr rumble_cut { -10.0 }; // dB, the rumble less than in the recording
|
|
double constexpr hiss_cut { -2.0 }; // dB, the noise between the tones and the ring less than in the recording
|
|
double constexpr tone_level { -9.0 }; // dB, the tones of the singing under the noise of their band, so they don't whistle
|
|
double constexpr singing_boost { 3.0 }; // dB, the singing band louder than in the recording
|
|
double constexpr rumble_balance { -8.0 }; // dB, the rumble under the balance of the recording
|
|
std::size_t constexpr control_step { 128 }; // samples per value of a wandering level, which changes slowly
|
|
|
|
// how a level wanders: at what rates, how steeply its power falls with the rate, and how far
|
|
struct wandering {
|
|
double from; // Hz
|
|
double to; // Hz
|
|
double slope; // the power falls as rate^-slope
|
|
double depth; // dB, the standard deviation of the level
|
|
};
|
|
|
|
wandering constexpr singing_wander { 3.0, 32.0, -0.3, 3.2 }; // the singing trembles fast, as in the recording
|
|
wandering constexpr ring_wander { 0.5, 16.0, 0.5, 1.0 }; // calm, or the ring chirps like a cricket
|
|
wandering constexpr high_wander { 0.3, 8.0, 1.0, 0.7 };
|
|
wandering constexpr tone_wander { 1.0, 8.0, 0.5, 12.0 }; // the tones taking the lead from one another
|
|
|
|
// the tones of the singing measured in the recording, and how strong each is
|
|
std::array<std::pair<double, double>, 7> constexpr measured_tones { {
|
|
{ 1225.0, 0.3 }, { 1300.0, 0.8 }, { 1318.0, 1.0 }, { 1345.0, 0.6 }, { 1368.0, 0.6 }, { 1437.0, 0.9 }, { 1455.0, 0.8 } } };
|
|
int constexpr extra_tones { 6 }; // more of them, weaker, between and around the measured ones
|
|
|
|
// the smacks: the whole singing band swelling at once, several of its tones together, now and then. nearly evenly spaced, the
|
|
// spacing of the swells wandering a little. a far train's swells are drawn out on their way along the rail
|
|
struct swelling {
|
|
double rise; // s, the time constant of the rise
|
|
double fall; // s, the time constant of the fall
|
|
};
|
|
swelling constexpr near_swells { 0.015, 0.035 };
|
|
swelling constexpr far_swells { 0.03, 0.09 };
|
|
double constexpr swell_rate { 3.5 }; // a second
|
|
double constexpr swell_spread { 0.4 }; // of the spacing, its standard deviation as a share of it
|
|
double constexpr swell_depth { 10.0 }; // dB, at the top of the strongest swell
|
|
|
|
// the plums: the knock of a wheel drawn out on its way along the rail into a short falling tone, as heard in a recording under
|
|
// a train. one now and then
|
|
band constexpr plum_band { 800.0, 1500.0 }; // what the plums are scaled against
|
|
double constexpr plum_from { 1320.0 }; // Hz
|
|
double constexpr plum_to { 920.0 }; // Hz
|
|
double constexpr plum_shortest { 0.035 }; // s
|
|
double constexpr plum_longest { 0.06 }; // s
|
|
double constexpr plum_level { 12.0 }; // dB over the band they're heard in
|
|
// how often: of a near train one every 1-2 s, of a far one every 3-5 s
|
|
struct spacing {
|
|
double least; // s
|
|
double most; // s
|
|
};
|
|
spacing constexpr near_plums { 1.0, 2.0 };
|
|
spacing constexpr far_plums { 3.0, 5.0 };
|
|
|
|
// the voice of a near train: steady high tones of the rail, as in a recording under a train, each against the level of the
|
|
// band around it in dB, slowly breathing
|
|
std::array<std::pair<double, double>, 3> constexpr voice_tones { { { 4955.0, 0.0 }, { 5360.0, -3.0 }, { 6215.0, 1.0 } } };
|
|
double constexpr voice_level { -9.0 }; // dB
|
|
wandering constexpr voice_wander { 0.3, 4.0, 0.5, 3.0 };
|
|
double constexpr peak_level { 0.7079 * 32767.0 }; // -3 dB of the full scale
|
|
|
|
// 0 below the edge, 1 above the other one, smooth in between
|
|
double smooth_step( double const From, double const To, double const Value ) {
|
|
|
|
if( To <= From ) {
|
|
return ( Value >= To ? 1.0 : 0.0 );
|
|
}
|
|
auto const x { std::clamp( ( Value - From ) / ( To - From ), 0.0, 1.0 ) };
|
|
return x * x * ( 3.0 - 2.0 * x );
|
|
}
|
|
|
|
// the amplitude of a level in dB: 10^(Level/20), as an exponential, which is cheaper and called millions of times
|
|
double decibels( double const Level ) {
|
|
return std::exp( Level * ( std::numbers::ln10 / 20.0 ) );
|
|
}
|
|
|
|
// the fast Fourier transform, in place. the count is a power of two. the inverse isn't divided by the count
|
|
void transform( spectrum &Data, bool const Inverse ) {
|
|
|
|
auto const count { Data.size() };
|
|
std::size_t reversed { 0 };
|
|
for( std::size_t idx = 1; idx < count; ++idx ) {
|
|
auto bit { count >> 1 };
|
|
while( ( reversed & bit ) != 0 ) {
|
|
reversed ^= bit;
|
|
bit >>= 1;
|
|
}
|
|
reversed ^= bit;
|
|
if( idx < reversed ) {
|
|
std::swap( Data[ idx ], Data[ reversed ] );
|
|
}
|
|
}
|
|
// the turns of the whole length, of which every stage takes every n-th
|
|
std::vector<complex> turns;
|
|
turns.reserve( count / 2 );
|
|
auto const angle { ( Inverse ? 2.0 : -2.0 ) * std::numbers::pi / static_cast<double>( count ) };
|
|
for( std::size_t idx = 0; idx < count / 2; ++idx ) {
|
|
turns.emplace_back( std::polar( 1.0, angle * static_cast<double>( idx ) ) );
|
|
}
|
|
for( std::size_t length = 2; length <= count; length <<= 1 ) {
|
|
auto const half { length / 2 };
|
|
auto const stride { count / length };
|
|
for( std::size_t start = 0; start < count; start += length ) {
|
|
for( std::size_t offset = 0; offset < half; ++offset ) {
|
|
auto const even { Data[ start + offset ] };
|
|
auto const odd { Data[ start + offset + half ] * turns[ offset * stride ] };
|
|
Data[ start + offset ] = even + odd;
|
|
Data[ start + offset + half ] = even - odd;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
spectrum to_spectrum( signal const &Signal ) {
|
|
|
|
spectrum data( Signal.begin(), Signal.end() );
|
|
transform( data, false );
|
|
return data;
|
|
}
|
|
|
|
signal to_signal( spectrum Data ) {
|
|
|
|
transform( Data, true );
|
|
auto const count { static_cast<double>( Data.size() ) };
|
|
signal output;
|
|
output.reserve( Data.size() );
|
|
for( auto const &value : Data ) {
|
|
output.emplace_back( value.real() / count );
|
|
}
|
|
return output;
|
|
}
|
|
|
|
// multiplies every bin of the spectrum of a real signal by what the given function gives for its frequency
|
|
template <typename Gain_>
|
|
void shape( spectrum &Data, double const Rate, Gain_ const &Gain ) {
|
|
|
|
auto const count { Data.size() };
|
|
for( std::size_t idx = 0; idx < count; ++idx ) {
|
|
auto const folded { idx <= count / 2 ? idx : count - idx };
|
|
Data[ idx ] *= Gain( static_cast<double>( folded ) * Rate / static_cast<double>( count ) );
|
|
}
|
|
}
|
|
|
|
double root_mean_square( signal const &Signal ) {
|
|
|
|
auto power { 0.0 };
|
|
for( auto const value : Signal ) {
|
|
power += value * value;
|
|
}
|
|
return ( Signal.empty() ? 0.0 : std::sqrt( power / static_cast<double>( Signal.size() ) ) );
|
|
}
|
|
|
|
void scale( signal &Signal, double const Factor ) {
|
|
for( auto &value : Signal ) {
|
|
value *= Factor;
|
|
}
|
|
}
|
|
|
|
// the level measured in the recording at the given frequency, in dB, straight between the measured points in octaves
|
|
double measured_level( double const Frequency ) {
|
|
|
|
if( Frequency <= measured_first ) {
|
|
return measured_levels.front();
|
|
}
|
|
auto const position { std::log2( Frequency / measured_first ) / measured_step };
|
|
auto const index { static_cast<std::size_t>( position ) };
|
|
if( index + 1 >= measured_levels.size() ) {
|
|
return measured_levels.back();
|
|
}
|
|
return std::lerp(
|
|
static_cast<double>( measured_levels[ index ] ),
|
|
static_cast<double>( measured_levels[ index + 1 ] ),
|
|
position - static_cast<double>( index ) );
|
|
}
|
|
|
|
// random numbers, the same for the same seed on every compiler: std::mt19937 is defined to the bit, the distributions of the
|
|
// standard library are not
|
|
class random_source {
|
|
|
|
public:
|
|
explicit random_source( std::uint32_t const Seed ) :
|
|
m_generator( Seed )
|
|
{}
|
|
|
|
// 0..1
|
|
double unit() {
|
|
return static_cast<double>( m_generator() >> 8 ) / 16777216.0; }
|
|
|
|
double uniform( double const From, double const To ) {
|
|
return From + ( To - From ) * unit(); }
|
|
|
|
// of the normal distribution
|
|
double gaussian() {
|
|
auto const first { ( static_cast<double>( m_generator() >> 8 ) + 1.0 ) / 16777217.0 };
|
|
auto const second { unit() };
|
|
return std::sqrt( -2.0 * std::log( first ) ) * std::cos( 2.0 * std::numbers::pi * second ); }
|
|
|
|
signal noise( std::size_t const Count ) {
|
|
signal output( Count );
|
|
for( auto &value : output ) {
|
|
value = gaussian();
|
|
}
|
|
return output; }
|
|
|
|
private:
|
|
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
|
|
class sound_maker {
|
|
|
|
public:
|
|
sound_maker( unsigned const Rate, std::uint32_t const Seed ) :
|
|
m_random( Seed ),
|
|
m_rate( static_cast<double>( Rate ) )
|
|
{}
|
|
|
|
loops make() {
|
|
|
|
auto singing { measured_noise() };
|
|
wander_band( singing, singing_band, singing_wander );
|
|
wander_band( singing, ring_band, ring_wander );
|
|
wander_band( singing, high_band, high_wander );
|
|
// the tones go into the noise of the singing band, under it, and the band is made a little louder
|
|
auto band_noise { take_singing_band( singing ) };
|
|
auto tones { singing_tones() };
|
|
scale( tones, decibels( tone_level ) * root_mean_square( band_noise ) / std::max( root_mean_square( tones ), 1e-12 ) );
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
band_noise[ idx ] = ( band_noise[ idx ] + tones[ idx ] ) * decibels( singing_boost );
|
|
singing[ idx ] += band_noise[ idx ];
|
|
}
|
|
// the swells take the band louder for a moment: what they add is the smacks
|
|
auto nearby { swells( band_noise, near_swells ) };
|
|
auto distant { swells( band_noise, far_swells ) };
|
|
// the balance of the bands as in the recording, by the singing with the near smacks
|
|
auto const gains { balance( singing, nearby ) };
|
|
equalise( singing, gains );
|
|
equalise( nearby, gains );
|
|
equalise( distant, gains );
|
|
// the levels the plums and the voice are set against, all from one spectrum of the singing
|
|
auto const singing_spectrum { to_spectrum( singing ) };
|
|
auto const plum_reference { level_in( singing_spectrum, plum_band ) };
|
|
auto const near_plum_signal { plums( plum_reference, near_plums ) };
|
|
auto const far_plum_signal { plums( plum_reference, far_plums ) };
|
|
auto const voice_signal { voice( singing_spectrum ) };
|
|
// the scale puts the peak of what a near train gives at peak_level
|
|
auto const factor { peak_scale( { &singing, &nearby, &near_plum_signal, &voice_signal } ) };
|
|
loops made;
|
|
made[ index( part::singing ) ] = to_samples( singing, factor );
|
|
made[ index( part::smacks_near ) ] = to_samples( nearby, factor );
|
|
made[ index( part::smacks_far ) ] = to_samples( distant, factor );
|
|
made[ index( part::plums_near ) ] = to_samples( near_plum_signal, factor );
|
|
made[ index( part::plums_far ) ] = to_samples( far_plum_signal, factor );
|
|
made[ index( part::voice ) ] = to_samples( voice_signal, factor );
|
|
return made;
|
|
}
|
|
|
|
private:
|
|
// noise of the spectrum of the recording, with less of its rumble
|
|
signal measured_noise() {
|
|
|
|
auto data { to_spectrum( m_random.noise( loop_length ) ) };
|
|
shape(
|
|
data, m_rate,
|
|
[]( double const Frequency ) {
|
|
if( Frequency <= 0.0 ) {
|
|
return 0.0;
|
|
}
|
|
auto const low { 1.0 / std::sqrt( 1.0 + std::pow( Frequency / rumble_edge, 8.0 ) ) };
|
|
return decibels( measured_level( Frequency ) ) * ( 1.0 - low * ( 1.0 - decibels( rumble_cut ) ) ); } );
|
|
return to_signal( std::move( data ) );
|
|
}
|
|
|
|
// a level of the standard deviation of 1, wandering as given, of a value for every sample. it's made at a rate low enough
|
|
// for its slow changes and drawn straight between its values, the last running into the first
|
|
signal wander( wandering const &Wander ) {
|
|
|
|
auto const points { loop_length / control_step };
|
|
auto data { to_spectrum( m_random.noise( points ) ) };
|
|
shape(
|
|
data, m_rate / static_cast<double>( control_step ),
|
|
[ &Wander ]( double const Rate ) {
|
|
return ( Rate >= Wander.from && Rate < Wander.to ) ? std::pow( Rate, -Wander.slope / 2.0 ) : 0.0; } );
|
|
auto coarse { to_signal( std::move( data ) ) };
|
|
scale( coarse, 1.0 / std::max( root_mean_square( coarse ), 1e-12 ) );
|
|
signal output( loop_length );
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
auto const point { idx / control_step };
|
|
auto const share { static_cast<double>( idx % control_step ) / static_cast<double>( control_step ) };
|
|
output[ idx ] = std::lerp( coarse[ point ], coarse[ ( point + 1 ) % points ], share );
|
|
}
|
|
return output;
|
|
}
|
|
|
|
// the part of the signal within the band, its level wandering as given, its mean power kept
|
|
void wander_band( signal &Signal, band const &Band, wandering const &Wander ) {
|
|
|
|
auto data { to_spectrum( Signal ) };
|
|
shape( data, m_rate, [ &Band ]( double const Frequency ) { return Band.contains( Frequency ) ? 1.0 : 0.0; } );
|
|
auto const part { to_signal( std::move( data ) ) };
|
|
auto gains { wander( Wander ) };
|
|
for( auto &value : gains ) {
|
|
value = decibels( Wander.depth * value );
|
|
}
|
|
scale( gains, 1.0 / root_mean_square( gains ) );
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
Signal[ idx ] += part[ idx ] * ( gains[ idx ] - 1.0 );
|
|
}
|
|
}
|
|
|
|
// takes the singing band out of the signal and gives it back. the noise of the rest is made a little quieter, the rumble
|
|
// and the ring as they are
|
|
signal take_singing_band( signal &Signal ) const {
|
|
|
|
auto const whole { to_spectrum( Signal ) };
|
|
auto rest { whole };
|
|
shape(
|
|
rest, m_rate,
|
|
[]( double const Frequency ) {
|
|
if( singing_band.contains( Frequency ) ) {
|
|
return 0.0;
|
|
}
|
|
auto const kept { Frequency < rumble_edge || ring_band.contains( Frequency ) };
|
|
return ( kept ? 1.0 : decibels( hiss_cut ) ); } );
|
|
Signal = to_signal( std::move( rest ) );
|
|
auto taken { whole };
|
|
shape( taken, m_rate, []( double const Frequency ) { return singing_band.contains( Frequency ) ? 1.0 : 0.0; } );
|
|
return to_signal( std::move( taken ) );
|
|
}
|
|
|
|
// the close tones of the singing, each of a whole number of periods in the loop, taking the lead from one another
|
|
signal singing_tones() {
|
|
|
|
std::vector<std::pair<double, double>> tones( measured_tones.begin(), measured_tones.end() );
|
|
for( int idx = 0; idx < extra_tones; ++idx ) {
|
|
auto const frequency { m_random.uniform( 1180.0, 1520.0 ) };
|
|
tones.emplace_back( frequency, m_random.uniform( 0.35, 0.7 ) );
|
|
}
|
|
auto const resolution { m_rate / static_cast<double>( loop_length ) };
|
|
signal output( loop_length, 0.0 );
|
|
for( auto const &[ frequency, strength ] : tones ) {
|
|
auto const periods { std::round( frequency / resolution ) };
|
|
auto const phase { m_random.uniform( 0.0, 2.0 * std::numbers::pi ) };
|
|
auto const level { wander( tone_wander ) };
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
auto const turn { 2.0 * std::numbers::pi * periods * static_cast<double>( idx ) / static_cast<double>( loop_length ) };
|
|
output[ idx ] += strength * decibels( tone_wander.depth * level[ idx ] ) * std::sin( turn + phase );
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
// the root mean square of the part of a signal within the band, from its spectrum: the power of the bins of the band, both
|
|
// halves of the spectrum, over the count squared
|
|
double level_in( spectrum const &Data, band const &Band ) const {
|
|
|
|
auto const count { Data.size() };
|
|
auto power { 0.0 };
|
|
for( std::size_t idx = 1; idx < count / 2; ++idx ) {
|
|
if( Band.contains( static_cast<double>( idx ) * m_rate / static_cast<double>( count ) ) ) {
|
|
power += 2.0 * std::norm( Data[ idx ] );
|
|
}
|
|
}
|
|
return std::sqrt( power ) / static_cast<double>( count );
|
|
}
|
|
|
|
// one plum: a tone falling from about plum_from to plum_to, up in the first tenth of it and dying away over the rest
|
|
void add_plum( signal &Signal, std::size_t const Start, double const Level ) {
|
|
|
|
auto const duration { m_random.uniform( plum_shortest, plum_longest ) };
|
|
auto const from { plum_from * std::pow( 2.0, 0.02 * m_random.gaussian() ) };
|
|
auto const to { plum_to * std::pow( 2.0, 0.02 * m_random.gaussian() ) };
|
|
auto phase { m_random.uniform( 0.0, 2.0 * std::numbers::pi ) };
|
|
auto const length { static_cast<std::size_t>( duration * m_rate ) };
|
|
for( std::size_t idx = 0; idx < length; ++idx ) {
|
|
auto const share { static_cast<double>( idx ) / static_cast<double>( length ) };
|
|
phase += 2.0 * std::numbers::pi * from * std::pow( to / from, share ) / m_rate;
|
|
auto const envelope {
|
|
share < 0.1 ?
|
|
std::pow( std::sin( 0.5 * std::numbers::pi * share / 0.1 ), 2.0 ) :
|
|
std::pow( std::cos( 0.5 * std::numbers::pi * ( share - 0.1 ) / 0.9 ), 2.0 ) };
|
|
Signal[ ( Start + idx ) % loop_length ] += Level * envelope * std::sin( phase );
|
|
}
|
|
}
|
|
|
|
// the plums, spaced as given, of plum_level over the given level of their band
|
|
signal plums( double const Reference, spacing const &Spacing ) {
|
|
|
|
signal output( loop_length, 0.0 );
|
|
auto const length { static_cast<double>( loop_length ) / m_rate };
|
|
auto const level { Reference * std::sqrt( 2.0 ) * decibels( plum_level ) };
|
|
auto time { m_random.uniform( 0.0, Spacing.least ) };
|
|
// none running over the end of the loop: a plum cut in two would start the loop with its tail
|
|
while( time < length - plum_longest ) {
|
|
add_plum( output, static_cast<std::size_t>( time * m_rate ), level * m_random.uniform( 0.6, 1.0 ) );
|
|
time += m_random.uniform( Spacing.least, Spacing.most );
|
|
}
|
|
return output;
|
|
}
|
|
|
|
// the voice of a near train: the steady high tones, each of a whole number of periods in the loop, against the level of the
|
|
// band of the singing around it
|
|
signal voice( spectrum const &Singing ) {
|
|
|
|
signal output( loop_length, 0.0 );
|
|
auto const resolution { m_rate / static_cast<double>( loop_length ) };
|
|
for( auto const &[ frequency, offset ] : voice_tones ) {
|
|
auto const around { level_in( Singing, { 0.9 * frequency, 1.1 * frequency } ) };
|
|
auto const amplitude { around * std::sqrt( 2.0 ) * decibels( voice_level + offset ) };
|
|
auto const periods { std::round( frequency / resolution ) };
|
|
auto const phase { m_random.uniform( 0.0, 2.0 * std::numbers::pi ) };
|
|
auto const level { wander( voice_wander ) };
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
auto const turn { 2.0 * std::numbers::pi * periods * static_cast<double>( idx ) / static_cast<double>( loop_length ) };
|
|
output[ idx ] += amplitude * decibels( voice_wander.depth * level[ idx ] ) * std::sin( turn + phase );
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
// a number of the normal distribution of the given mean and spread, never below a tenth of the mean
|
|
double around( double const Mean, double const Spread ) {
|
|
return std::max( 0.1 * Mean, Mean * ( 1.0 + Spread * m_random.gaussian() ) );
|
|
}
|
|
|
|
// what the swells add to the given band: the band times the swell over 1, the swell rising and falling as given, of the
|
|
// strongest at swell_depth. where two swells meet, the stronger counts
|
|
signal swells( signal const &Band, swelling const &Swell ) {
|
|
|
|
signal level( loop_length, 0.0 );
|
|
auto const span_before { static_cast<std::size_t>( 4.0 * Swell.rise * m_rate ) };
|
|
auto const span_after { static_cast<std::size_t>( 6.0 * Swell.fall * m_rate ) };
|
|
auto const length { static_cast<double>( loop_length ) / m_rate };
|
|
auto time { m_random.uniform( 0.0, 1.0 / swell_rate ) };
|
|
while( time < length ) {
|
|
auto const strength { m_random.uniform( 0.5, 1.0 ) };
|
|
auto const top { static_cast<std::size_t>( time * m_rate ) + loop_length };
|
|
for( std::size_t idx = top - span_before; idx < top + span_after; ++idx ) {
|
|
auto const offset { ( static_cast<double>( idx ) - static_cast<double>( top ) ) / m_rate };
|
|
auto const shape { offset < 0.0 ? std::exp( offset / Swell.rise ) : std::exp( -offset / Swell.fall ) };
|
|
auto &value { level[ idx % loop_length ] };
|
|
value = std::max( value, strength * shape );
|
|
}
|
|
time += around( 1.0 / swell_rate, swell_spread );
|
|
}
|
|
signal output( loop_length );
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
output[ idx ] = Band[ idx ] * ( decibels( swell_depth * level[ idx ] ) - 1.0 );
|
|
}
|
|
return output;
|
|
}
|
|
|
|
// the mean power of the spectrum over every 1/6 of an octave from measured_first, the Count of them, in one pass: a bin
|
|
// goes to the sixth of an octave its frequency is nearest the middle of
|
|
static std::vector<double> smoothed( std::vector<double> const &Frequencies, std::vector<double> const &Power, std::size_t const Count ) {
|
|
|
|
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<double>( Count ) ) {
|
|
continue;
|
|
}
|
|
auto const centre { static_cast<std::size_t>( position ) };
|
|
sums[ centre ] += Power[ idx ];
|
|
++counts[ centre ];
|
|
}
|
|
for( std::size_t idx = 0; idx < Count; ++idx ) {
|
|
sums[ idx ] = ( counts[ idx ] > 0 ? sums[ idx ] / counts[ idx ] : 0.0 );
|
|
}
|
|
return sums;
|
|
}
|
|
|
|
// the gain for every 1/6 of an octave from 25 Hz which gives the sound the balance of the bands of the recording: the
|
|
// ratio of the measured spectrum and of the spectrum of the sound, 1 over the singing band, the rumble lower
|
|
std::vector<double> balance( signal const &Singing, signal const &Smacks ) const {
|
|
|
|
signal both( loop_length );
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
both[ idx ] = Singing[ idx ] + Smacks[ idx ];
|
|
}
|
|
auto const data { to_spectrum( both ) };
|
|
std::vector<double> frequencies;
|
|
std::vector<double> made;
|
|
std::vector<double> measured;
|
|
for( std::size_t idx = 1; idx <= loop_length / 2; ++idx ) {
|
|
auto const frequency { static_cast<double>( idx ) * m_rate / static_cast<double>( loop_length ) };
|
|
frequencies.emplace_back( frequency );
|
|
made.emplace_back( std::norm( data[ idx ] ) );
|
|
measured.emplace_back( std::pow( 10.0, measured_level( frequency ) / 10.0 ) );
|
|
}
|
|
std::vector<double> centres;
|
|
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() ) };
|
|
std::vector<double> gains;
|
|
gains.reserve( centres.size() );
|
|
auto singing { 0.0 };
|
|
auto count { 0 };
|
|
for( std::size_t idx = 0; idx < centres.size(); ++idx ) {
|
|
gains.emplace_back( actual[ idx ] > 0.0 ? target[ idx ] / actual[ idx ] : 1.0 );
|
|
if( singing_band.contains( centres[ idx ] ) ) {
|
|
singing += gains.back();
|
|
++count;
|
|
}
|
|
}
|
|
for( std::size_t idx = 0; idx < centres.size(); ++idx ) {
|
|
gains[ idx ] /= ( count > 0 ? singing / count : 1.0 );
|
|
if( centres[ idx ] < rumble_edge ) {
|
|
gains[ idx ] *= std::pow( 10.0, rumble_balance / 10.0 );
|
|
}
|
|
gains[ idx ] = std::sqrt( gains[ idx ] );
|
|
}
|
|
return gains;
|
|
}
|
|
|
|
// the signal through the gains of balance(), straight between them in octaves
|
|
void equalise( signal &Signal, std::vector<double> const &Gains ) const {
|
|
|
|
auto data { to_spectrum( Signal ) };
|
|
shape(
|
|
data, m_rate,
|
|
[ &Gains ]( double const Frequency ) {
|
|
if( Frequency <= 0.0 ) {
|
|
return 0.0;
|
|
}
|
|
auto const position { std::log2( std::max( Frequency, measured_first ) / measured_first ) * 6.0 };
|
|
auto const index { static_cast<std::size_t>( position ) };
|
|
if( index + 1 >= Gains.size() ) {
|
|
return Gains.back();
|
|
}
|
|
return std::lerp( Gains[ index ], Gains[ index + 1 ], position - static_cast<double>( index ) ); } );
|
|
Signal = to_signal( std::move( data ) );
|
|
}
|
|
|
|
// the scale which puts the peak of the given signals together at peak_level
|
|
static double peak_scale( std::initializer_list<signal const *> const Signals ) {
|
|
|
|
auto peak { 0.0 };
|
|
for( std::size_t idx = 0; idx < loop_length; ++idx ) {
|
|
auto sum { 0.0 };
|
|
for( auto const *signal_ : Signals ) {
|
|
sum += ( *signal_ )[ idx ];
|
|
}
|
|
peak = std::max( peak, std::abs( sum ) );
|
|
}
|
|
return ( peak > 0.0 ? peak_level / peak : 0.0 );
|
|
}
|
|
|
|
static std::vector<std::int16_t> to_samples( signal const &Signal, double const Factor ) {
|
|
|
|
std::vector<std::int16_t> output;
|
|
output.reserve( Signal.size() );
|
|
for( auto const value : Signal ) {
|
|
output.emplace_back( static_cast<std::int16_t>( std::clamp( std::lround( value * Factor ), -32767L, 32767L ) ) );
|
|
}
|
|
return output;
|
|
}
|
|
|
|
random_source m_random;
|
|
double m_rate;
|
|
};
|
|
|
|
// how much the load of the axles gives the source, as a share of the reference load
|
|
double load_share( double const AxleLoad, double const Exponent, tuning const &Tuning ) {
|
|
|
|
auto const load { AxleLoad > 0.0 ? AxleLoad : Tuning.load_reference };
|
|
return std::pow( load / Tuning.load_reference, Exponent );
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace {
|
|
|
|
// the share of the sound of a train coming in from the search distance: a little at first, ever faster as it nears, in full
|
|
// from fade_far
|
|
double fade_in( double const Distance, tuning const &Tuning ) {
|
|
|
|
if( Distance >= Tuning.search_distance ) {
|
|
return 0.0;
|
|
}
|
|
if( Distance <= Tuning.fade_far ) {
|
|
return 1.0;
|
|
}
|
|
auto const covered { ( Tuning.search_distance - Distance ) / ( Tuning.search_distance - Tuning.fade_far ) };
|
|
return decibels( -Tuning.fade_depth * ( 1.0 - std::pow( covered, Tuning.fade_power ) ) );
|
|
}
|
|
|
|
// what a source of the train gives the listener at the far end of the way: the volume of the source, the given share of the
|
|
// loss along the rails, the fade by the train itself
|
|
double heard_level( path const &Way, train const &Train, double const LossShare, tuning const &Tuning ) {
|
|
|
|
auto const speed { std::abs( Train.speed ) };
|
|
if( speed <= Tuning.speed_silent || Way.distance > Tuning.search_distance ) {
|
|
return 0.0;
|
|
}
|
|
auto const source {
|
|
std::min( Tuning.source_cap, std::pow( speed / Tuning.speed_reference, Tuning.speed_exponent ) )
|
|
* smooth_step( Tuning.speed_silent, Tuning.speed_full, speed )
|
|
* std::min( Tuning.load_cap, load_share( Train.axle_load, Tuning.load_exponent, Tuning ) ) };
|
|
auto const loss {
|
|
Tuning.decay_welded * std::max( 0.0, Way.distance )
|
|
+ Tuning.loss_joint * std::max( 0.0, Way.joints )
|
|
+ Tuning.loss_switch * std::max( 0, Way.switches ) };
|
|
auto const result {
|
|
Tuning.volume
|
|
* source
|
|
* std::pow( 10.0, -LossShare * loss / 20.0 )
|
|
* fade_in( Way.distance, Tuning )
|
|
* ( 1.0 + ( decibels( Tuning.near_boost ) - 1.0 ) * closeness( Way.distance, Tuning ) ) };
|
|
|
|
return ( result < Tuning.audible ? 0.0 : result );
|
|
}
|
|
|
|
} // namespace
|
|
|
|
double gain( path const &Way, train const &Train, tuning const &Tuning ) {
|
|
|
|
return heard_level( Way, Train, 1.0, Tuning );
|
|
}
|
|
|
|
double smack_gain( path const &Way, train const &Train, tuning const &Tuning ) {
|
|
|
|
auto const result { heard_level( Way, Train, Tuning.smack_loss_share, Tuning ) * smack_share( Train, Tuning ) };
|
|
return ( result < Tuning.audible ? 0.0 : result );
|
|
}
|
|
|
|
double far_share( double const Distance, tuning const &Tuning ) {
|
|
|
|
return smooth_step( Tuning.smacks_near, Tuning.smacks_far, Distance );
|
|
}
|
|
|
|
double smack_share( train const &Train, tuning const &Tuning ) {
|
|
|
|
auto const speed { std::abs( Train.speed ) / Tuning.speed_reference };
|
|
return std::min(
|
|
Tuning.smack_cap,
|
|
load_share( Train.axle_load, Tuning.smack_load_exponent, Tuning ) * std::pow( speed, Tuning.smack_speed_exponent ) );
|
|
}
|
|
|
|
double closeness( double const Distance, tuning const &Tuning ) {
|
|
|
|
return 1.0 - smooth_step( Tuning.near_to, Tuning.near_from, Distance );
|
|
}
|
|
|
|
heard hear( path const &Way, train const &Train, tuning const &Tuning ) {
|
|
|
|
auto const singing { gain( Way, Train, Tuning ) };
|
|
auto const smacks { smack_gain( Way, Train, Tuning ) };
|
|
// the near smacks give way to the far ones, their power kept; so do the plums, which grow quieter on the way too
|
|
auto const smack_turn { 0.5 * std::numbers::pi * far_share( Way.distance, Tuning ) };
|
|
auto const plum_share { smooth_step( Tuning.plums_near, Tuning.plums_far, Way.distance ) };
|
|
auto const plums { smacks * decibels( Tuning.plums_far_level * plum_share ) };
|
|
auto const plum_turn { 0.5 * std::numbers::pi * plum_share };
|
|
heard result {};
|
|
result[ index( part::singing ) ] = singing;
|
|
result[ index( part::smacks_near ) ] = smacks * std::cos( smack_turn );
|
|
result[ index( part::smacks_far ) ] = smacks * std::sin( smack_turn );
|
|
result[ index( part::plums_near ) ] = plums * std::cos( plum_turn );
|
|
result[ index( part::plums_far ) ] = plums * std::sin( plum_turn );
|
|
result[ index( part::voice ) ] = singing * closeness( Way.distance, Tuning );
|
|
return result;
|
|
}
|
|
|
|
heard combine( heard const &First, heard const &Second ) {
|
|
|
|
heard result {};
|
|
for( std::size_t idx = 0; idx < part_count; ++idx ) {
|
|
result[ idx ] = std::sqrt( First[ idx ] * First[ idx ] + Second[ idx ] * Second[ idx ] );
|
|
}
|
|
return result;
|
|
}
|
|
|
|
loops make_sound( unsigned const Rate, std::uint32_t const Seed ) {
|
|
|
|
if( Rate == 0 ) {
|
|
return {};
|
|
}
|
|
return sound_maker( Rate, Seed ).make();
|
|
}
|
|
|
|
} // namespace audio::rail_singing
|