Open end working

This commit is contained in:
2026-05-07 12:55:57 +02:00
parent bc0df51ddb
commit 685b48b577
7 changed files with 355 additions and 330 deletions

View File

@@ -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 lowpass for derivative smoothing (≈ 23 ms)
private double smoothDMdt;
private readonly double alpha;
// New: lowpass 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 singlesample 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);
// Lowpass time constant for the mass flow: 5 ms (kneecap highfreq 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;
// Lowpass 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;
// Farfield 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);
}
}
}