From 01dfee7daa868646a7f1667e8ac82990fb0bfe9a Mon Sep 17 00:00:00 2001 From: Nytra <14206961+Nytra@users.noreply.github.com> Date: Tue, 31 Dec 2024 10:14:28 +0000 Subject: [PATCH] Clamp audio buffer values to prevent audio breaking --- .../Components/Audio/BandPassFilter.cs | 1 + .../Components/Audio/ButterworthFilter.cs | 6 ++++++ .../Components/Audio/FrequencyModulator.cs | 17 ++++++++++------- ProjectObsidian/ProtoFlux/Audio/AudioAdder.cs | 6 ++++++ .../ProtoFlux/Audio/AudioMultiply.cs | 6 ++++++ .../ProtoFlux/Audio/AudioSubtractor.cs | 6 ++++++ 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/ProjectObsidian/Components/Audio/BandPassFilter.cs b/ProjectObsidian/Components/Audio/BandPassFilter.cs index a7ccc93..582652a 100644 --- a/ProjectObsidian/Components/Audio/BandPassFilter.cs +++ b/ProjectObsidian/Components/Audio/BandPassFilter.cs @@ -42,6 +42,7 @@ public void Read(Span buffer) where S : unmanaged, IAudioSample Span tempBuffer = stackalloc S[buffer.Length]; tempBuffer = buffer; + Source.Target.Read(tempBuffer); if (lowFilter == null) diff --git a/ProjectObsidian/Components/Audio/ButterworthFilter.cs b/ProjectObsidian/Components/Audio/ButterworthFilter.cs index ca59140..bdc0df5 100644 --- a/ProjectObsidian/Components/Audio/ButterworthFilter.cs +++ b/ProjectObsidian/Components/Audio/ButterworthFilter.cs @@ -138,6 +138,12 @@ public void Update(ref S newInput) S fifth = this.outputHistory[1].Multiply(b2); S final = first.Add(second).Add(third).Subtract(fourth).Subtract(fifth); + for (int i = 0; i < final.ChannelCount; i++) + { + if (final[i] > 1) final = final.SetChannel(i, 1f); + if (final[i] < -1) final = final.SetChannel(i, -1f); + } + this.inputHistory[1] = this.inputHistory[0]; this.inputHistory[0] = newInput; diff --git a/ProjectObsidian/Components/Audio/FrequencyModulator.cs b/ProjectObsidian/Components/Audio/FrequencyModulator.cs index 48cdbe0..8336c8f 100644 --- a/ProjectObsidian/Components/Audio/FrequencyModulator.cs +++ b/ProjectObsidian/Components/Audio/FrequencyModulator.cs @@ -66,15 +66,18 @@ public void Read(Span buffer) where S : unmanaged, IAudioSample // Apply FM synthesis for (int i = 0; i < buffer.Length; i++) { - // Get carrier and modulator values - float carrierValue = carrierBuffer[i].AbsoluteAmplitude; - float modulatorValue = modulatorBuffer[i].AbsoluteAmplitude; + for (int j = 0; j < buffer[i].ChannelCount; j++) + { + float carrierValue = carrierBuffer[i][j]; + float modulatorValue = modulatorBuffer[i][j]; - // Compute frequency modulation - float modulatedValue = (float)(carrierValue * Math.Sin(2 * Math.PI * modulationIndex * modulatorValue)); + float modulatedValue = (float)(carrierValue * Math.Sin(2 * Math.PI * modulationIndex * modulatorValue)); - // Write modulated value to the buffer - buffer[i] = buffer[i].Bias(buffer[i].AbsoluteAmplitude - modulatedValue); + buffer[i] = buffer[i].SetChannel(j, modulatedValue); + + if (buffer[i][j] > 1f) buffer[i] = buffer[i].SetChannel(j, 1f); + if (buffer[i][j] < -1f) buffer[i] = buffer[i].SetChannel(j, -1f); + } } } } diff --git a/ProjectObsidian/ProtoFlux/Audio/AudioAdder.cs b/ProjectObsidian/ProtoFlux/Audio/AudioAdder.cs index fb6479d..58be3ec 100644 --- a/ProjectObsidian/ProtoFlux/Audio/AudioAdder.cs +++ b/ProjectObsidian/ProtoFlux/Audio/AudioAdder.cs @@ -43,6 +43,12 @@ public void Read(Span buffer) where S : unmanaged, IAudioSample for (int i = 0; i < buffer.Length; i++) { newBuffer[i] = newBuffer[i].Add(newBuffer2[i]); + + for (int j = 0; j < newBuffer[i].ChannelCount; j++) + { + if (newBuffer[i][j] > 1f) newBuffer[i] = newBuffer[i].SetChannel(j, 1f); + if (newBuffer[i][j] < -1f) newBuffer[i] = newBuffer[i].SetChannel(j, -1f); + } } } } diff --git a/ProjectObsidian/ProtoFlux/Audio/AudioMultiply.cs b/ProjectObsidian/ProtoFlux/Audio/AudioMultiply.cs index 8688c5b..cda5382 100644 --- a/ProjectObsidian/ProtoFlux/Audio/AudioMultiply.cs +++ b/ProjectObsidian/ProtoFlux/Audio/AudioMultiply.cs @@ -35,6 +35,12 @@ public void Read(Span buffer) where S : unmanaged, IAudioSample for (int i = 0; i < buffer.Length; i++) { newBuffer[i] = newBuffer[i].Multiply(Value); + + for (int j = 0; j < newBuffer[i].ChannelCount; j++) + { + if (newBuffer[i][j] > 1f) newBuffer[i] = newBuffer[i].SetChannel(j, 1f); + if (newBuffer[i][j] < -1f) newBuffer[i] = newBuffer[i].SetChannel(j, -1f); + } } } } diff --git a/ProjectObsidian/ProtoFlux/Audio/AudioSubtractor.cs b/ProjectObsidian/ProtoFlux/Audio/AudioSubtractor.cs index 25aed94..e0bf207 100644 --- a/ProjectObsidian/ProtoFlux/Audio/AudioSubtractor.cs +++ b/ProjectObsidian/ProtoFlux/Audio/AudioSubtractor.cs @@ -43,6 +43,12 @@ public void Read(Span buffer) where S : unmanaged, IAudioSample for (int i = 0; i < buffer.Length; i++) { newBuffer[i] = newBuffer[i].Subtract(newBuffer2[i]); + + for (int j = 0; j < newBuffer[i].ChannelCount; j++) + { + if (newBuffer[i][j] > 1f) newBuffer[i] = newBuffer[i].SetChannel(j, 1f); + if (newBuffer[i][j] < -1f) newBuffer[i] = newBuffer[i].SetChannel(j, -1f); + } } } }