sound fixed
This commit is contained in:
@@ -1,132 +1,48 @@
|
||||
using System;
|
||||
using FluidSim.Interfaces;
|
||||
|
||||
namespace FluidSim.Core
|
||||
{
|
||||
public class SoundProcessor
|
||||
{
|
||||
private double dt;
|
||||
private double pipeArea;
|
||||
private double ambientPressure = 101325.0;
|
||||
private readonly double dt;
|
||||
private readonly double scaleFactor; // 1 / (4π r) and a user gain
|
||||
private double prevMassFlowOut;
|
||||
|
||||
// Monopole source state
|
||||
private double lastMassFlow = 0.0;
|
||||
// Simple low‑pass for derivative smoothing (≈ 2‑3 ms)
|
||||
private double smoothDMdt;
|
||||
private readonly double alpha;
|
||||
|
||||
// Gains
|
||||
private float masterGain = 0.0005f;
|
||||
private float pressureGain = 0.12f;
|
||||
private float turbulenceGain = 0.05f;
|
||||
private float turbulence = 0.05f;
|
||||
public float Gain { get; set; } = 1.0f;
|
||||
|
||||
private PinkNoiseGenerator pinkNoise;
|
||||
|
||||
// Reverb (outdoor)
|
||||
private float[] delayLine;
|
||||
private int writeIndex;
|
||||
private float feedback = 0.50f;
|
||||
private float lowpassCoeff = 0.70f;
|
||||
private float lastFeedbackSample = 0f;
|
||||
|
||||
public SoundProcessor(int sampleRate, double pipeDiameterMeters, float reverbTimeMs = 200.0f)
|
||||
public SoundProcessor(int sampleRate, double listenerDistanceMeters = 1.0)
|
||||
{
|
||||
dt = 1.0 / sampleRate;
|
||||
pipeArea = Math.PI * Math.Pow(pipeDiameterMeters / 2.0, 2.0);
|
||||
scaleFactor = 1.0 / (4.0 * Math.PI * listenerDistanceMeters);
|
||||
|
||||
int delaySamples = (int)(sampleRate * reverbTimeMs / 1000.0);
|
||||
delayLine = new float[delaySamples];
|
||||
writeIndex = 0;
|
||||
|
||||
pinkNoise = new PinkNoiseGenerator();
|
||||
// Smoothing time constant ~ 2 ms, blocks single‑sample spikes
|
||||
double tau = 0.002;
|
||||
alpha = Math.Exp(-dt / tau);
|
||||
}
|
||||
|
||||
public float MasterGain
|
||||
public float Process(Port port)
|
||||
{
|
||||
get => masterGain;
|
||||
set => masterGain = value;
|
||||
}
|
||||
public float PressureGain
|
||||
{
|
||||
get => pressureGain;
|
||||
set => pressureGain = value;
|
||||
}
|
||||
public float TurbulenceGain
|
||||
{
|
||||
get => turbulenceGain;
|
||||
set => turbulenceGain = value;
|
||||
}
|
||||
public float Turbulence
|
||||
{
|
||||
get => turbulence;
|
||||
set => turbulence = value;
|
||||
}
|
||||
// Outflow mass flow (positive = leaving pipe)
|
||||
double flowOut = -port.MassFlowRate;
|
||||
|
||||
public void SetAmbientPressure(double p) => ambientPressure = p;
|
||||
public void SetPipeDiameter(double diameterMeters) =>
|
||||
pipeArea = Math.PI * Math.Pow(diameterMeters / 2.0, 2.0);
|
||||
// Derivative
|
||||
double rawDerivative = (flowOut - prevMassFlowOut) / dt;
|
||||
prevMassFlowOut = flowOut;
|
||||
|
||||
public float Process(float massFlow, float pipeEndPressure)
|
||||
{
|
||||
// 1. Monopole: d(mdot)/dt
|
||||
double derivative = (massFlow - lastMassFlow) / dt;
|
||||
lastMassFlow = massFlow;
|
||||
float monopole = (float)(derivative * masterGain);
|
||||
// Smooth the derivative to kill isolated spikes
|
||||
smoothDMdt = alpha * smoothDMdt + (1.0 - alpha) * rawDerivative;
|
||||
|
||||
// 2. Pressure component
|
||||
float pressureDiff = (float)((pipeEndPressure - ambientPressure) / ambientPressure) * pressureGain;
|
||||
// Far‑field monopole pressure
|
||||
double pressure = smoothDMdt * scaleFactor * Gain;
|
||||
|
||||
float mixed = monopole + pressureDiff;
|
||||
|
||||
// 3. Turbulence: amplitude ∝ U^8
|
||||
double velocity = massFlow / (pipeArea * 1.225);
|
||||
double turbulenceAmp = Math.Pow(Math.Abs(velocity) * turbulence, 3.0);
|
||||
float pink = pinkNoise.Next() * turbulenceGain * (float)turbulenceAmp;
|
||||
|
||||
float combined = mixed + pink;
|
||||
|
||||
// 4. Outdoor reverb
|
||||
float delayed = delayLine[writeIndex];
|
||||
float filteredDelay = delayed * lowpassCoeff + lastFeedbackSample * (1f - lowpassCoeff);
|
||||
lastFeedbackSample = filteredDelay;
|
||||
float wet = delayed + filteredDelay * feedback;
|
||||
delayLine[writeIndex] = combined + filteredDelay * feedback;
|
||||
writeIndex = (writeIndex + 1) % delayLine.Length;
|
||||
|
||||
// 5. Dry/wet mix
|
||||
float output = combined * 0.7f + wet * 0.3f;
|
||||
return MathF.Tanh(output);
|
||||
}
|
||||
}
|
||||
|
||||
// PinkNoiseGenerator unchanged, same as before
|
||||
internal class PinkNoiseGenerator
|
||||
{
|
||||
private readonly Random random = new Random();
|
||||
private readonly float[] whiteNoise = new float[7];
|
||||
private int currentIndex = 0;
|
||||
|
||||
public PinkNoiseGenerator()
|
||||
{
|
||||
for (int i = 0; i < 7; i++)
|
||||
whiteNoise[i] = (float)(random.NextDouble() * 2.0 - 1.0);
|
||||
}
|
||||
|
||||
public float Next()
|
||||
{
|
||||
whiteNoise[0] = (float)(random.NextDouble() * 2.0 - 1.0);
|
||||
currentIndex = (currentIndex + 1) & 0x7F;
|
||||
int updateMask = 0;
|
||||
int temp = currentIndex;
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if ((temp & 1) == 0)
|
||||
updateMask |= (1 << i);
|
||||
temp >>= 1;
|
||||
}
|
||||
for (int i = 1; i < 7; i++)
|
||||
if ((updateMask & (1 << i)) != 0)
|
||||
whiteNoise[i] = (float)(random.NextDouble() * 2.0 - 1.0);
|
||||
float sum = 0f;
|
||||
for (int i = 0; i < 7; i++) sum += whiteNoise[i];
|
||||
return sum / 3.5f;
|
||||
// Soft clip to ±1 for audio output (safe limit)
|
||||
float sample = (float)Math.Tanh(pressure);
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user