Open end working
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
using System;
|
||||
using FluidSim.Interfaces;
|
||||
using FluidSim.Core;
|
||||
|
||||
namespace FluidSim.Core
|
||||
{
|
||||
public class SoundProcessor
|
||||
{
|
||||
private readonly double dt;
|
||||
private readonly double scaleFactor; // 1 / (4π r) and a user gain
|
||||
private readonly double scaleFactor; // 1 / (4π r)
|
||||
private double prevMassFlowOut;
|
||||
|
||||
// Simple low‑pass for derivative smoothing (≈ 2‑3 ms)
|
||||
private double smoothDMdt;
|
||||
private readonly double alpha;
|
||||
|
||||
// New: low‑pass the mass flow signal before derivative
|
||||
private double flowLP;
|
||||
private readonly double lpAlpha;
|
||||
|
||||
public float Gain { get; set; } = 1.0f;
|
||||
|
||||
public SoundProcessor(int sampleRate, double listenerDistanceMeters = 1.0)
|
||||
@@ -20,29 +22,34 @@ namespace FluidSim.Core
|
||||
dt = 1.0 / sampleRate;
|
||||
scaleFactor = 1.0 / (4.0 * Math.PI * listenerDistanceMeters);
|
||||
|
||||
// Smoothing time constant ~ 2 ms, blocks single‑sample spikes
|
||||
double tau = 0.002;
|
||||
// Smoothing time constant for the derivative: 10 ms (much smoother)
|
||||
double tau = 0.010; // 10 ms
|
||||
alpha = Math.Exp(-dt / tau);
|
||||
|
||||
// Low‑pass time constant for the mass flow: 5 ms (kneecap high‑freq directly)
|
||||
double tauLP = 0.005;
|
||||
lpAlpha = Math.Exp(-dt / tauLP);
|
||||
}
|
||||
|
||||
public float Process(Port port)
|
||||
public float Process(OpenEndLink openEnd)
|
||||
{
|
||||
// Outflow mass flow (positive = leaving pipe)
|
||||
double flowOut = -port.MassFlowRate;
|
||||
double flowOut = openEnd.LastMassFlowRate;
|
||||
|
||||
// Derivative
|
||||
double rawDerivative = (flowOut - prevMassFlowOut) / dt;
|
||||
prevMassFlowOut = flowOut;
|
||||
// Low‑pass the mass flow signal
|
||||
flowLP = lpAlpha * flowLP + (1.0 - lpAlpha) * flowOut;
|
||||
|
||||
// Smooth the derivative to kill isolated spikes
|
||||
// Derivative of the smoothed mass flow
|
||||
double rawDerivative = (flowLP - prevMassFlowOut) / dt;
|
||||
prevMassFlowOut = flowLP;
|
||||
|
||||
// Smooth the derivative
|
||||
smoothDMdt = alpha * smoothDMdt + (1.0 - alpha) * rawDerivative;
|
||||
|
||||
// Far‑field monopole pressure
|
||||
double pressure = smoothDMdt * scaleFactor * Gain;
|
||||
|
||||
// Soft clip to ±1 for audio output (safe limit)
|
||||
float sample = (float)Math.Tanh(pressure);
|
||||
return sample;
|
||||
// Soft clip to ±1 (should rarely trigger now)
|
||||
return (float)Math.Tanh(pressure);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user