Files
FluidSim/Core/SoundProcessor.cs

55 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using FluidSim.Core;
namespace FluidSim.Core
{
public class SoundProcessor
{
private readonly double dt;
private readonly double scaleFactor; // 1 / (4π r)
private double prevMassFlowOut;
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)
{
dt = 1.0 / sampleRate;
scaleFactor = 1.0 / (4.0 * Math.PI * listenerDistanceMeters);
// Smoothing time constant for the derivative: 10 ms (much smoother)
double tau = 0.005; // 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(OpenEndLink openEnd)
{
double flowOut = openEnd.LastMassFlowRate;
// Lowpass the mass flow signal
flowLP = lpAlpha * flowLP + (1.0 - lpAlpha) * flowOut;
// 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 (should rarely trigger now)
return (float)pressure;
}
}
}