Skip to content

Commit

Permalink
Add bandpass filter, update butterworth and others
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytra committed Nov 25, 2024
1 parent ae4e659 commit b9a01ec
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
64 changes: 64 additions & 0 deletions ProjectObsidian/Components/Audio/BandPassFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using FrooxEngine;
using Elements.Assets;
using System.Net;
using ProtoFlux.Runtimes.Execution;

namespace Obsidian.Components.Audio
{
[Category(new string[] { "Obsidian/Audio" })]
public class BandPassFilter : Component, IAudioSource, IWorldElement
{
[Range(0.1f, 1.41f, "0.00")]
public readonly Sync<float> Resonance;

[Range(20f, 20000f, "0.00")]
public readonly Sync<float> LowFrequency;

[Range(20f, 20000f, "0.00")]
public readonly Sync<float> HighFrequency;

public readonly SyncRef<IAudioSource> Source;

private double lastTime;

public bool IsActive
{
get => Source.Target != null && Source.Target.IsActive;
}

public int ChannelCount => Source.Target?.ChannelCount ?? 0;

public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S>
{
if (!IsActive)
{
return;
}

Span<S> tempBuffer = stackalloc S[buffer.Length];
tempBuffer = buffer;
Source.Target.Read(tempBuffer);

var LowPass = new ButterworthFilter.FilterButterworth<S>(HighFrequency, (int)(tempBuffer.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), ButterworthFilter.FilterButterworth<S>.PassType.Lowpass, Resonance);
var HighPass = new ButterworthFilter.FilterButterworth<S>(LowFrequency, (int)(tempBuffer.Length / (Engine.Current.AudioSystem.DSPTime - lastTime)), ButterworthFilter.FilterButterworth<S>.PassType.Highpass, Resonance);

for (int i = 0; i < tempBuffer.Length; i++)
{
LowPass.Update(ref tempBuffer[i]);
HighPass.Update(ref tempBuffer[i]);
}

lastTime = Engine.Current.AudioSystem.DSPTime;
}

protected override void OnAwake()
{
base.OnAwake();
Resonance.Value = 1.41f;
LowFrequency.Value = 20f;
HighFrequency.Value = 20000f;
lastTime = Engine.Current.AudioSystem.DSPTime;
}
}
}
6 changes: 4 additions & 2 deletions ProjectObsidian/Components/Audio/ButterworthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Obsidian.Components.Audio;
[Category(new string[] { "Obsidian/Audio" })]
public class ButterworthFilter : Component, IAudioSource, IWorldElement
{
[Range(0f, 10000f, "0.00")]
[Range(20f, 20000f, "0.00")]
public readonly Sync<float> Frequency;

[Range(0.1f, 1.41f, "0.00")]
Expand All @@ -30,7 +30,9 @@ public bool IsActive
protected override void OnAwake()
{
base.OnAwake();
lastTime = -1;
lastTime = Engine.Current.AudioSystem.DSPTime;
Frequency.Value = 20f;
Resonance.Value = 1.41f;
}

public int ChannelCount => Source.Target?.ChannelCount ?? 0;
Expand Down

0 comments on commit b9a01ec

Please sign in to comment.