40 lines
1.8 KiB
C#
40 lines
1.8 KiB
C#
using System.Numerics;
|
|
namespace GamePort.Game;
|
|
|
|
/// <summary>412ED0 engine command followed by bounded47D9D0 positional pitch.
|
|
/// Owns source695F84 channel history; scene/global audio scheduling stays separate.</summary>
|
|
public sealed class ScooterAudio
|
|
{
|
|
private int appliedFrequency;
|
|
public float History { get; private set; }
|
|
public void Reset(){History=0;appliedFrequency=0;}
|
|
|
|
public static int EngineFrequency(int baseFrequency,float speed)
|
|
=>Math.Max(7000,(int)(baseFrequency+(double)speed*0.5263157894736842*51));
|
|
|
|
public int Frequency(int baseFrequency,float speed,Vector3 source,Vector3 listener)
|
|
{
|
|
float distance=OriginalBodySupport.QuickMagnitude(listener-source);
|
|
//A zero-distance source exits before set_frequency, retaining device Hz.
|
|
if(distance==0)return appliedFrequency==0?baseFrequency:appliedFrequency;
|
|
var history=History;
|
|
int nominal=EngineFrequency(baseFrequency,speed);
|
|
int adjusted=PositionalFrequency(nominal,distance,ref history);
|
|
History=history;
|
|
//DX accepts unsigned Hz, clamps nonzero to100..100000;0 means source rate.
|
|
uint hz=unchecked((uint)adjusted);
|
|
appliedFrequency=hz==0?baseFrequency:(int)Math.Clamp(hz,100u,100000u);
|
|
return appliedFrequency;
|
|
}
|
|
|
|
public static int PositionalFrequency(int nominal,float distance,ref float history)
|
|
{
|
|
if(distance==0)return nominal; //47DA7E returns without the channel write/history update.
|
|
int correction=(int)(((double)distance-history)*.75);
|
|
int result=distance>history?unchecked(nominal-correction):unchecked(nominal+correction);
|
|
//47DBFC/47DC11 fild the integer correction, not the current distance.
|
|
history=correction;
|
|
return result;
|
|
}
|
|
}
|