16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-23 00:29:19 +02:00

minor sound refactoring

This commit is contained in:
tmj-fstate
2017-12-19 00:26:46 +01:00
parent c22390b4ff
commit bc13e5b0b5
5 changed files with 107 additions and 72 deletions

View File

@@ -3623,34 +3623,22 @@ void TDynamicObject::RenderSounds() {
// frequency calculation // frequency calculation
auto normalizer { 1.f }; auto normalizer { 1.f };
// for combined sounds normalize frequency to 0-1 range // for combined sound engine we calculate sound point in rpm, to make .mmd files setup easier
switch( MoverParameters->EngineType ) { switch( MoverParameters->EngineType ) {
case DieselElectric: { case DieselElectric: {
if( true == sConverter.is_combined() ) { if( true == sConverter.is_combined() ) {
normalizer = MoverParameters->DElist[ MoverParameters->MainCtrlPosNo ].RPM / 60; normalizer = 60.f * 0.01f;
}
break;
}
case DieselEngine: {
if( true == rsSilnik.is_combined() ) {
normalizer = MoverParameters->dizel_nmax;
}
break;
}
case ElectricInductionMotor: {
if( true == rsSilnik.is_combined() ) {
// TODO: implement normalization/a way to calculate max expected engine rpm
} }
break; break;
} }
default: { default: {
if( true == rsSilnik.is_combined() ) { if( true == rsSilnik.is_combined() ) {
normalizer = MoverParameters->nmax; normalizer = 60.f * 0.01f;
} }
break; break;
} }
} }
frequency = rsSilnik.m_frequencyfactor * std::abs( MoverParameters->enrot ) / std::max( 1.f, normalizer ) + rsSilnik.m_frequencyoffset; frequency = rsSilnik.m_frequencyfactor * std::abs( MoverParameters->enrot ) * normalizer + rsSilnik.m_frequencyoffset;
if( MoverParameters->EngineType == Dumb ) { if( MoverParameters->EngineType == Dumb ) {
frequency -= 0.2 * MoverParameters->EnginePower / ( 1 + MoverParameters->Power * 1000 ); frequency -= 0.2 * MoverParameters->EnginePower / ( 1 + MoverParameters->Power * 1000 );
} }
@@ -4147,10 +4135,11 @@ void TDynamicObject::RenderSounds() {
// szum w czasie jazdy // szum w czasie jazdy
if( ( GetVelocity() > 0.5 ) if( ( GetVelocity() > 0.5 )
&& ( // compound test whether the vehicle belongs to user-driven consist (as these don't emit outer noise in cab view) && ( // compound test whether the vehicle belongs to user-driven consist (as these don't emit outer noise in cab view)
true == FreeFlyModeFlag ? true : // in external view all vehicles emit outer noise FreeFlyModeFlag ? true : // in external view all vehicles emit outer noise
// Global::pWorld->train() == nullptr ? true : // (can skip this check, with no player train the external view is a given) // Global::pWorld->train() == nullptr ? true : // (can skip this check, with no player train the external view is a given)
ctOwner == nullptr ? true : // standalone vehicle, can't be part of user-driven train ctOwner == nullptr ? true : // standalone vehicle, can't be part of user-driven train
ctOwner != Global::pWorld->train()->Dynamic()->ctOwner ? true : // confirmed isn't a part of the user-driven train ctOwner != Global::pWorld->train()->Dynamic()->ctOwner ? true : // confirmed isn't a part of the user-driven train
Global::CabWindowOpen ? true : // sticking head out we get to hear outer noise
false ) ) { false ) ) {
volume = rsOuterNoise.m_amplitudefactor * MoverParameters->Vel + rsOuterNoise.m_amplitudeoffset; volume = rsOuterNoise.m_amplitudefactor * MoverParameters->Vel + rsOuterNoise.m_amplitudeoffset;
@@ -5076,15 +5065,16 @@ void TDynamicObject::LoadMMediaFile( std::string BaseDir, std::string TypeName,
rsSilnik.m_amplitudefactor /= amplitudedivisor; rsSilnik.m_amplitudefactor /= amplitudedivisor;
} }
else if( ( token == "ventilator:" ) else if( token == "ventilator:" ) {
&& ( ( MoverParameters->EngineType == ElectricSeriesMotor )
|| ( MoverParameters->EngineType == ElectricInductionMotor ) ) ) {
// plik z dzwiekiem wentylatora, mnozniki i ofsety amp. i czest. // plik z dzwiekiem wentylatora, mnozniki i ofsety amp. i czest.
rsWentylator.deserialize( parser, sound_type::single, sound_parameters::range | sound_parameters::amplitude | sound_parameters::frequency ); rsWentylator.deserialize( parser, sound_type::single, sound_parameters::range | sound_parameters::amplitude | sound_parameters::frequency );
rsWentylator.owner( this ); rsWentylator.owner( this );
if( ( MoverParameters->EngineType == ElectricSeriesMotor )
|| ( MoverParameters->EngineType == ElectricInductionMotor ) ) {
rsWentylator.m_amplitudefactor /= MoverParameters->RVentnmax; rsWentylator.m_amplitudefactor /= MoverParameters->RVentnmax;
rsWentylator.m_frequencyfactor /= MoverParameters->RVentnmax; rsWentylator.m_frequencyfactor /= MoverParameters->RVentnmax;
}
} }
else if( ( token == "transmission:" ) else if( ( token == "transmission:" )

View File

@@ -4197,7 +4197,7 @@ double TMoverParameters::CouplerForce(int CouplerN, double dt)
// ************************************************************************************************* // *************************************************************************************************
double TMoverParameters::TractionForce(double dt) double TMoverParameters::TractionForce(double dt)
{ {
double PosRatio, dmoment, dtrans, tmp, tmpV; double PosRatio, dmoment, dtrans, tmp;// , tmpV;
int i; int i;
Ft = 0; Ft = 0;
@@ -4244,38 +4244,82 @@ double TMoverParameters::TractionForce(double dt)
// eAngle = Pirazy2 - eAngle; <- ABu: a nie czasem tak, jak nizej? // eAngle = Pirazy2 - eAngle; <- ABu: a nie czasem tak, jak nizej?
eAngle -= M_PI * 2.0; eAngle -= M_PI * 2.0;
*/ */
// hunter-091012: przeniesione z if ActiveDir<>0 (zeby po zejsciu z kierunku dalej spadala // hunter-091012: przeniesione z if ActiveDir<>0 (zeby po zejsciu z kierunku dalej spadala predkosc wentylatorow)
// predkosc wentylatorow) // wentylatory rozruchowe
if (EngineType == ElectricSeriesMotor) // TODO: move this to update, it doesn't exactly have much to do with traction
{ if( true == Mains ) {
switch (RVentType) // wentylatory rozruchowe}
{ switch( EngineType ) {
case 1: case ElectricInductionMotor: {
{ // TBD, TODO: currently ignores RVentType, fix this?
if ((ActiveDir != 0) && (RList[MainCtrlActualPos].R > RVentCutOff)) auto const tmpV { std::abs( eimv[ eimv_fp ] ) };
RventRot += (RVentnmax - RventRot) * RVentSpeed * dt;
else if( ( RlistSize > 0 )
RventRot *= (1.0 - RVentSpeed * dt); && ( ( std::abs( eimv[ eimv_If ] ) > 1.0 )
break; || ( tmpV > 0.1 ) ) ) {
i = 0;
while( ( i < RlistSize - 1 )
&& ( DElist[ i + 1 ].RPM < tmpV ) ) {
++i;
} }
case 2: RventRot =
{ ( tmpV - DElist[ i ].RPM )
if ((abs(Itot) > RVentMinI) && (RList[MainCtrlActualPos].R > RVentCutOff)) / std::max( 1.0, ( DElist[ i + 1 ].RPM - DElist[ i ].RPM ) )
RventRot += * ( DElist[ i + 1 ].GenPower - DElist[ i ].GenPower )
(RVentnmax * abs(Itot) / (ImaxLo * RList[MainCtrlActualPos].Bn) - RventRot) * + DElist[ i ].GenPower;
RVentSpeed * dt; }
else if ((DynamicBrakeType == dbrake_automatic) && (DynamicBrakeFlag)) else {
RventRot += (RVentnmax * Im / ImaxLo - RventRot) * RVentSpeed * dt; RventRot *= std::max( 0.0, 1.0 - RVentSpeed * dt );
else
{
RventRot *= (1.0 - RVentSpeed * dt);
if (RventRot < 0.1)
RventRot = 0;
} }
break; break;
} }
case ElectricSeriesMotor: {
switch( RVentType ) {
case 1: { // manual
if( ( ActiveDir != 0 )
&& ( RList[ MainCtrlActualPos ].R > RVentCutOff ) ) {
RventRot += ( RVentnmax - RventRot ) * RVentSpeed * dt;
} }
else {
RventRot *= std::max( 0.0, 1.0 - RVentSpeed * dt );
} }
break;
}
case 2: { // automatic
if( ( std::abs( Itot ) > RVentMinI )
&& ( RList[ MainCtrlActualPos ].R > RVentCutOff ) ) {
RventRot += ( RVentnmax * abs( Itot ) / ( ImaxLo * RList[ MainCtrlActualPos ].Bn ) - RventRot ) * RVentSpeed * dt;
}
else if( ( DynamicBrakeType == dbrake_automatic )
&& ( true == DynamicBrakeFlag ) ) {
RventRot += ( RVentnmax * Im / ImaxLo - RventRot ) * RVentSpeed * dt;
}
else {
RventRot *= std::max( 0.0, 1.0 - RVentSpeed * dt );
}
break;
}
default: {
break;
}
} // rventtype
}
case DieselElectric: {
// TBD, TODO: currently ignores RVentType, fix this?
RventRot += clamp( DElist[ MainCtrlPos ].RPM - RventRot, -100.0, 50.0 ) * dt;
break;
}
case DieselEngine:
default: {
break;
}
} // enginetype
}
else {
RventRot *= std::max( 0.0, 1.0 - RVentSpeed * dt );
}
RventRot = std::max( 0.0, RventRot );
if (ActiveDir != 0) if (ActiveDir != 0)
switch (EngineType) switch (EngineType)
@@ -4397,7 +4441,7 @@ double TMoverParameters::TractionForce(double dt)
case DieselElectric: // youBy case DieselElectric: // youBy
{ {
// tmpV:=V*CabNo*ActiveDir; // tmpV:=V*CabNo*ActiveDir;
tmpV = nrot * Pirazy2 * 0.5 * WheelDiameter * DirAbsolute; //*CabNo*ActiveDir; auto const tmpV { nrot * Pirazy2 * 0.5 * WheelDiameter * DirAbsolute }; //*CabNo*ActiveDir;
// jazda manewrowa // jazda manewrowa
if (ShuntMode) if (ShuntMode)
{ {
@@ -4665,7 +4709,6 @@ double TMoverParameters::TractionForce(double dt)
MainSwitch( false, ( TrainType == dt_EZT ? range::unit : range::local ) ); // TODO: check whether we need to send this EMU-wide MainSwitch( false, ( TrainType == dt_EZT ? range::unit : range::local ) ); // TODO: check whether we need to send this EMU-wide
} }
} }
tmpV = abs(nrot) * (PI * WheelDiameter) * 3.6; //*DirAbsolute*eimc[eimc_s_p]; - do przemyslenia dzialanie pp
if ((Mains)) if ((Mains))
{ {
@@ -4835,20 +4878,6 @@ double TMoverParameters::TractionForce(double dt)
Itot = eimv[eimv_Ipoj] * (0.01 + Min0R(0.99, 0.99 - Vadd)); Itot = eimv[eimv_Ipoj] * (0.01 + Min0R(0.99, 0.99 - Vadd));
EnginePower = abs(eimv[eimv_Ic] * eimv[eimv_U] * NPoweredAxles) / 1000; EnginePower = abs(eimv[eimv_Ic] * eimv[eimv_U] * NPoweredAxles) / 1000;
tmpV = eimv[eimv_fp];
if (((abs(eimv[eimv_If]) > 1) || (abs(tmpV) > 0.1)) && (RlistSize > 0))
{
i = 0;
while ((i < RlistSize - 1) && (DElist[i + 1].RPM < abs(tmpV)))
i++;
RventRot =
( std::abs( tmpV ) - DElist[ i ].RPM )
/ std::max( 1.0, ( DElist[ i + 1 ].RPM - DElist[ i ].RPM ) )
* ( DElist[ i + 1 ].GenPower - DElist[ i ].GenPower )
+ DElist[ i ].GenPower;
}
else
RventRot = 0;
Mm = eimv[eimv_M] * DirAbsolute; Mm = eimv[eimv_M] * DirAbsolute;
Mw = Mm * Transmision.Ratio; Mw = Mm * Transmision.Ratio;

View File

@@ -5285,6 +5285,7 @@ TTrain::update_sounds( double const Deltatime ) {
// szum w czasie jazdy // szum w czasie jazdy
if( ( false == FreeFlyModeFlag ) if( ( false == FreeFlyModeFlag )
&& ( false == Global::CabWindowOpen )
&& ( DynamicObject->GetVelocity() > 0.5 ) ) { && ( DynamicObject->GetVelocity() > 0.5 ) ) {
volume = rsRunningNoise.m_amplitudefactor * mvOccupied->Vel + rsRunningNoise.m_amplitudeoffset; volume = rsRunningNoise.m_amplitudefactor * mvOccupied->Vel + rsRunningNoise.m_amplitudeoffset;

View File

@@ -331,7 +331,7 @@ void
sound_source::play_combined() { sound_source::play_combined() {
// combined sound consists of table od samples, each sample associated with certain range of values of controlling variable // combined sound consists of table od samples, each sample associated with certain range of values of controlling variable
// current value of the controlling variable is passed to the source with pitch() call // current value of the controlling variable is passed to the source with pitch() call
auto const soundpoint { clamp( m_properties.pitch * 100.f, 0.f, 99.f ) }; auto const soundpoint { compute_combined_point() };
for( std::uint32_t idx = 0; idx < m_soundchunks.size(); ++idx ) { for( std::uint32_t idx = 0; idx < m_soundchunks.size(); ++idx ) {
auto const &soundchunk { m_soundchunks[ idx ] }; auto const &soundchunk { m_soundchunks[ idx ] };
@@ -376,6 +376,18 @@ sound_source::play_combined() {
} }
} }
// calculates requested sound point, used to select specific sample from the sample table
float
sound_source::compute_combined_point() const {
return (
m_properties.pitch < 1.1f ?
// most sounds use 0-1 value range, we clamp these to 0-99 to allow more intuitive sound definition in .mmd files
clamp( m_properties.pitch, 0.f, 0.99f ) :
std::max( 0.f, m_properties.pitch )
) * 100.f;
}
// stops currently active play commands controlled by this emitter // stops currently active play commands controlled by this emitter
void void
sound_source::stop( bool const Skipend ) { sound_source::stop( bool const Skipend ) {
@@ -520,7 +532,7 @@ sound_source::update_combined( audio::openal_source &Source ) {
if( ( soundhandle & sound_id::chunk ) != 0 ) { if( ( soundhandle & sound_id::chunk ) != 0 ) {
// for sound chunks, test whether the chunk should still be active given current value of the controlling variable // for sound chunks, test whether the chunk should still be active given current value of the controlling variable
auto const soundpoint { clamp( m_properties.pitch * 100.f, 0.f, 99.f ) }; auto const soundpoint { compute_combined_point() };
auto const &soundchunk { m_soundchunks[ soundhandle ^ sound_id::chunk ] }; auto const &soundchunk { m_soundchunks[ soundhandle ^ sound_id::chunk ] };
if( ( soundpoint < soundchunk.second.fadein ) if( ( soundpoint < soundchunk.second.fadein )
|| ( soundpoint > soundchunk.second.fadeout ) ) { || ( soundpoint > soundchunk.second.fadeout ) ) {
@@ -598,7 +610,7 @@ sound_source::update_crossfade( sound_handle const Chunk ) {
return; return;
} }
auto const soundpoint { clamp( m_properties.pitch * 100.f, 0.f, 99.f ) }; auto const soundpoint { compute_combined_point() };
// NOTE: direct access to implementation details ahead, kinda fugly // NOTE: direct access to implementation details ahead, kinda fugly
auto const chunkindex { Chunk ^ sound_id::chunk }; auto const chunkindex { Chunk ^ sound_id::chunk };

View File

@@ -150,6 +150,9 @@ private:
play_basic(); play_basic();
void void
play_combined(); play_combined();
// calculates requested sound point, used to select specific sample from the sample table
float
compute_combined_point() const;
void void
update_basic( audio::openal_source &Source ); update_basic( audio::openal_source &Source );
void void