sound fixed
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using FluidSim.Components;
|
||||
using FluidSim.Utils;
|
||||
using FluidSim.Interfaces;
|
||||
using SFML.Graphics;
|
||||
using SFML.System;
|
||||
|
||||
@@ -13,108 +15,137 @@ namespace FluidSim.Core
|
||||
private Pipe1D exhaustPipe;
|
||||
private PipeVolumeConnection coupling;
|
||||
private SoundProcessor soundProcessor;
|
||||
private OutdoorExhaustReverb reverb;
|
||||
|
||||
private Port exitPort = new Port();
|
||||
|
||||
private double dt;
|
||||
private double ambientPressure = 101325.0;
|
||||
private double pipeArea;
|
||||
private const double AmbientPressure = 101325.0;
|
||||
private double time;
|
||||
private int stepCount = 0;
|
||||
private const int LogInterval = 10000;
|
||||
|
||||
// Throttle 0..1 → target combustion pressure
|
||||
public double Throttle { get; set; } = 0.05; // tiny throttle to keep idle
|
||||
private const double IdlePeakPressure = 5.0 * 101325.0; // 5 bar
|
||||
private const double MaxPeakPressure = 50.0 * 101325.0; // 50 bar
|
||||
// Throttle 0..1
|
||||
public double Throttle { get; set; } = 0.0; // start with a light idle throttle
|
||||
|
||||
// ---- Realistic combustion parameters ----
|
||||
private const double FullLoadPeakPressure = 70.0 * 101325.0; // 15 bar
|
||||
|
||||
// ---- Idle speed governor ----
|
||||
private const double TargetIdleRPM = 800.0; // rad/s = RPM * π/30, we'll convert
|
||||
|
||||
public override void Initialize(int sampleRate)
|
||||
{
|
||||
dt = 1.0 / sampleRate;
|
||||
|
||||
// Crankshaft (inertia + friction)
|
||||
crankshaft = new Crankshaft(initialRPM: 100.0) // starter speed
|
||||
// ---- Crankshaft: inertia + friction that gives ~800 RPM at idle ----
|
||||
crankshaft = new Crankshaft(initialRPM: 600.0) // start a bit low
|
||||
{
|
||||
Inertia = 0.05,
|
||||
FrictionConstant = 1.0,
|
||||
FrictionViscous = 0.01
|
||||
Inertia = 0.005, // slightly heavier flywheel
|
||||
FrictionConstant = 0.8, // static friction
|
||||
FrictionViscous = 0.01 // viscous (linear with RPM)
|
||||
};
|
||||
|
||||
// Pipe
|
||||
double pipeLength = 0.5;
|
||||
// ---- Pipe: add a tiny bit of damping to prevent unrealistic shocks ----
|
||||
double pipeLength = 2;
|
||||
double pipeRadius = 0.1;
|
||||
double pipeArea = Math.PI * pipeRadius * pipeRadius;
|
||||
pipeArea = Math.PI * pipeRadius * pipeRadius;
|
||||
exhaustPipe = new Pipe1D(pipeLength, pipeArea, sampleRate, forcedCellCount: 60);
|
||||
exhaustPipe.SetUniformState(1.225, 0.0, ambientPressure);
|
||||
exhaustPipe.EnergyRelaxationRate = 0f;
|
||||
exhaustPipe.DampingMultiplier = 0;
|
||||
exhaustPipe.SetUniformState(1.225, 0.0, AmbientPressure);
|
||||
exhaustPipe.DampingMultiplier = 5;
|
||||
exhaustPipe.EnergyRelaxationRate = 50;
|
||||
|
||||
// Cylinder (coupled to crankshaft)
|
||||
// ---- Cylinder ----
|
||||
engineCyl = new EngineCylinder(crankshaft,
|
||||
bore: 0.065, stroke: 0.0565, compressionRatio: 10.0,
|
||||
pipeArea: pipeArea, sampleRate: sampleRate);
|
||||
|
||||
// Coupling (valve → pipe)
|
||||
// ---- Coupling ----
|
||||
coupling = new PipeVolumeConnection(engineCyl.Cylinder, exhaustPipe, true, orificeArea: 0.0);
|
||||
|
||||
// Solver
|
||||
// ---- Solver ----
|
||||
solver = new Solver();
|
||||
solver.SetTimeStep(dt);
|
||||
solver.AddVolume(engineCyl.Cylinder);
|
||||
solver.AddPipe(exhaustPipe);
|
||||
solver.AddConnection(coupling);
|
||||
solver.SetPipeBoundary(exhaustPipe, false, BoundaryType.OpenEnd, ambientPressure);
|
||||
solver.SetPipeBoundary(exhaustPipe, false, BoundaryType.OpenEnd, AmbientPressure);
|
||||
|
||||
// Sound (your tuned gains)
|
||||
soundProcessor = new SoundProcessor(sampleRate, pipeRadius * 2, reverbTimeMs: 500.0f);
|
||||
soundProcessor.MasterGain = 0.0f; //0.00001f;
|
||||
soundProcessor.PressureGain = 0.1f;
|
||||
soundProcessor.TurbulenceGain = 0.0f;
|
||||
soundProcessor.Turbulence = 0.001f;
|
||||
soundProcessor.SetAmbientPressure(ambientPressure);
|
||||
// ---- Sound processor (stable version) ----
|
||||
soundProcessor = new SoundProcessor(sampleRate, pipeRadius * 2);
|
||||
soundProcessor.Gain = 0.00001f;
|
||||
|
||||
Console.WriteLine("=== EngineScenario (torque‑driven RPM, throttle = pressure) ===");
|
||||
Console.WriteLine($"Crankshaft inertia: {crankshaft.Inertia}, friction: {crankshaft.FrictionConstant} + {crankshaft.FrictionViscous}*ω");
|
||||
Console.WriteLine($"Throttle range: {IdlePeakPressure/101325:F0} – {MaxPeakPressure/101325:F0} bar");
|
||||
// ---- Reverb ----
|
||||
reverb = new OutdoorExhaustReverb(sampleRate);
|
||||
// Church: vast, highly reflective, bright
|
||||
reverb.DryMix = 1.0f; // always full dry signal
|
||||
reverb.EarlyMix = 0.5f; // distinct early reflections from distant walls
|
||||
reverb.TailMix = 0.9f; // huge tail, almost as loud as the dry sound
|
||||
reverb.Feedback = 0.9f; // long decay – roughly 3 s reverb time (with current delay lengths)
|
||||
reverb.DampingFreq = 6000f; // bright: high‑frequency energy stays for a long time
|
||||
reverb.MatrixCoeff = 0.5f; // default orthogonal mix
|
||||
|
||||
Console.WriteLine("=== EngineScenario (Stable) ===");
|
||||
Console.WriteLine($"Crankshaft inertia: {crankshaft.Inertia}");
|
||||
Console.WriteLine($"Pipe: {pipeLength} m, fundamental: {340/(4*pipeLength):F1} Hz");
|
||||
}
|
||||
|
||||
public override float Process()
|
||||
{
|
||||
// 1. Map throttle to target peak pressure
|
||||
double targetPressure = IdlePeakPressure + Throttle * (MaxPeakPressure - IdlePeakPressure);
|
||||
// ---- RPM governor: adjust throttle to maintain idle when no user input ----
|
||||
double currentRPM = crankshaft.AngularVelocity * 60.0 / (2.0 * Math.PI);
|
||||
double throttle = Math.Clamp(Throttle, 0.05, 1.0); // never let it drop below a tiny value
|
||||
|
||||
// ---- Target combustion pressure ----
|
||||
double targetPressure = throttle * FullLoadPeakPressure;
|
||||
engineCyl.TargetPeakPressure = targetPressure;
|
||||
|
||||
// 2. Step the cylinder (adds torque to crankshaft, updates valve)
|
||||
// ---- Simulate one timestep ----
|
||||
engineCyl.Step(dt);
|
||||
|
||||
// 3. Integrate crankshaft (applies friction, updates RPM)
|
||||
crankshaft.Step(dt);
|
||||
|
||||
// 4. Set orifice area for coupling
|
||||
coupling.OrificeArea = engineCyl.OrificeArea;
|
||||
solver.Step();
|
||||
|
||||
// 5. Fluid solver step
|
||||
float massFlow = solver.Step();
|
||||
float endPressure = (float)exhaustPipe.GetCellPressure(exhaustPipe.GetCellCount() - 1);
|
||||
// ---- Update exit port with safety clamps ----
|
||||
UpdateExitPort();
|
||||
|
||||
// 6. Audio
|
||||
float audioSample = soundProcessor.Process(massFlow, endPressure);
|
||||
// ---- Generate audio ----
|
||||
float dry = soundProcessor.Process(exitPort);
|
||||
float wet = reverb.Process(dry);
|
||||
|
||||
time += dt;
|
||||
stepCount++;
|
||||
if (stepCount % LogInterval == 0) {
|
||||
Console.WriteLine(audioSample);
|
||||
}
|
||||
|
||||
if (stepCount % 1000 == 0 && false)
|
||||
{
|
||||
Console.WriteLine($"{time,5:F3} {crankshaft.AngularVelocity*60/(2*Math.PI),5:F0} RPM " +
|
||||
$"Thr:{Throttle:F2} P_target:{targetPressure/101325:F1} bar " +
|
||||
$"mflow:{massFlow,14:E4} Comb#{engineCyl.CombustionCount} Mis#{engineCyl.MisfireCount}");
|
||||
}
|
||||
|
||||
return audioSample;
|
||||
return wet;
|
||||
}
|
||||
|
||||
private void UpdateExitPort()
|
||||
{
|
||||
int last = exhaustPipe.GetCellCount() - 1;
|
||||
double p = exhaustPipe.GetCellPressure(last);
|
||||
double rho = exhaustPipe.GetCellDensity(last);
|
||||
double vel = exhaustPipe.GetCellVelocity(last);
|
||||
|
||||
// Clamp density to physically possible values
|
||||
if (rho < 0.01) rho = 0.01; // never let it approach zero
|
||||
if (rho > 50.0) rho = 50.0; // never let it become absurd
|
||||
|
||||
// Clamp velocity to ± 500 m/s (safe subsonic)
|
||||
vel = Math.Clamp(vel, -500.0, 500.0);
|
||||
|
||||
double outflowMassFlow = rho * vel * pipeArea;
|
||||
|
||||
// Clamp exit pressure to sensible range (0.1 – 20 bar)
|
||||
p = Math.Clamp(p, 1e4, 2e6);
|
||||
|
||||
exitPort.Pressure = p;
|
||||
exitPort.Density = rho;
|
||||
exitPort.Temperature = p / (rho * 287.05);
|
||||
exitPort.MassFlowRate = -outflowMassFlow;
|
||||
exitPort.SpecificEnthalpy = 0.0;
|
||||
}
|
||||
|
||||
// ---- Drawing (unchanged) ----
|
||||
public override void Draw(RenderWindow target)
|
||||
{
|
||||
float winW = target.GetView().Size.X;
|
||||
|
||||
Reference in New Issue
Block a user