Skip to content

Commit

Permalink
Satisfy CodeFactor and simplify mixing
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Sep 14, 2024
1 parent 28a59b0 commit 5fd89ba
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 444 deletions.
4 changes: 2 additions & 2 deletions osu.Framework.Tests/Audio/SDL3AudioTestComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace osu.Framework.Tests.Audio
/// </summary>
public class SDL3AudioTestComponents : AudioTestComponents
{
private SDL3BaseAudioManager baseManager = null!;
private SDL3AudioManager.SDL3BaseAudioManager baseManager = null!;

public SDL3AudioTestComponents(bool init = true)
: base(init)
Expand All @@ -31,7 +31,7 @@ protected override void Prepare()
base.Prepare();

SDL_SetHint(SDL_HINT_AUDIO_DRIVER, "dummy"u8);
baseManager = new SDL3BaseAudioManager(MixerComponents.Items.OfType<SDL3AudioMixer>);
baseManager = new SDL3AudioManager.SDL3BaseAudioManager(MixerComponents.Items.OfType<SDL3AudioMixer>);
}

public override void Init()
Expand Down
48 changes: 14 additions & 34 deletions osu.Framework/Audio/Mixing/SDL3/SDL3AudioMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ManagedBass.Fx;
using osu.Framework.Statistics;
using NAudio.Dsp;
using System;

namespace osu.Framework.Audio.Mixing.SDL3
{
Expand Down Expand Up @@ -56,27 +57,16 @@ protected override void UpdateState()
base.UpdateState();
}

private void mixAudio(float[] dst, float[] src, ref int filled, int samples, float left, float right)
private void mixAudio(float[] dst, float[] src, int samples, float left, float right)
{
if (left <= 0 && right <= 0)
return;

for (int e = 0; e < samples; e += 2)
{
if (e < filled)
{
dst[e] += src[e] * left;
dst[e + 1] += src[e + 1] * right;
}
else
{
dst[e] = src[e] * left;
dst[e + 1] = src[e + 1] * right;
}
dst[e] += src[e] * left;
dst[e + 1] += src[e + 1] * right;
}

if (samples > filled)
filled = samples;
}

private float[]? ret;
Expand All @@ -90,8 +80,7 @@ private void mixAudio(float[] dst, float[] src, ref int filled, int samples, flo
/// </summary>
/// <param name="data">A float array that audio will be mixed into.</param>
/// <param name="sampleCount">Size of data</param>
/// <param name="filledSamples">Count of usable audio samples in data</param>
public void MixChannelsInto(float[] data, int sampleCount, ref int filledSamples)
public void MixChannelsInto(float[] data, int sampleCount)
{
lock (syncRoot)
{
Expand All @@ -100,10 +89,13 @@ public void MixChannelsInto(float[] data, int sampleCount, ref int filledSamples

bool useFilters = activeEffects.Count > 0;

if (useFilters && (filterArray == null || filterArray.Length != sampleCount))
filterArray = new float[sampleCount];
if (useFilters)
{
if (filterArray == null || filterArray.Length != sampleCount)
filterArray = new float[sampleCount];

int filterArrayFilled = 0;
Array.Fill(filterArray, 0);
}

var node = activeChannels.First;

Expand All @@ -123,11 +115,7 @@ public void MixChannelsInto(float[] data, int sampleCount, ref int filledSamples
if (size > 0)
{
var (left, right) = channel.Volume;

if (!useFilters)
mixAudio(data, ret, ref filledSamples, size, left, right);
else
mixAudio(filterArray!, ret, ref filterArrayFilled, size, left, right);
mixAudio(useFilters ? filterArray! : data, ret, size, left, right);
}
}

Expand All @@ -140,21 +128,13 @@ public void MixChannelsInto(float[] data, int sampleCount, ref int filledSamples
{
foreach (var filter in activeEffects.Values)
{
for (int i = 0; i < filterArrayFilled; i++)
for (int i = 0; i < sampleCount; i++)
filterArray![i] = filter.Transform(filterArray[i]);
}

mixAudio(data, filterArray!, ref filledSamples, filterArrayFilled, 1, 1);
mixAudio(data, filterArray!, sampleCount, 1, 1);
}
}

for (int i = 0; i < filledSamples; i++)
{
if (data[i] > 1.0f)
data[i] = 1.0f;
else if (data[i] < -1.0f)
data[i] = -1.0f;
}
}

private static BiQuadFilter updateFilter(BiQuadFilter? filter, float freq, BQFParameters bqfp)
Expand Down
Loading

0 comments on commit 5fd89ba

Please sign in to comment.