Skip to content

Commit

Permalink
Clamp audio buffer values to prevent audio breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytra committed Dec 31, 2024
1 parent 6e7b125 commit 01dfee7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions ProjectObsidian/Components/Audio/BandPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>

Span<S> tempBuffer = stackalloc S[buffer.Length];
tempBuffer = buffer;

Source.Target.Read(tempBuffer);

if (lowFilter == null)
Expand Down
6 changes: 6 additions & 0 deletions ProjectObsidian/Components/Audio/ButterworthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 10 additions & 7 deletions ProjectObsidian/Components/Audio/FrequencyModulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
// 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);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions ProjectObsidian/ProtoFlux/Audio/AudioAdder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
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);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions ProjectObsidian/ProtoFlux/Audio/AudioMultiply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
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);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions ProjectObsidian/ProtoFlux/Audio/AudioSubtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
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);
}
}
}
}
Expand Down

0 comments on commit 01dfee7

Please sign in to comment.