Skip to content

Commit

Permalink
Added lock for lazy evaluators.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Jun 5, 2024
1 parent d48bec3 commit 4accf2d
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class Analyser : Node
private AudioNode? audioNode;
public override Func<AudioContext, Task<AudioNode>> AudioNode => async (context) =>
{
_ = await audioNodeSlim.WaitAsync(200);
if (audioNode is null)
{
AnalyserNode analyser = await AnalyserNode.CreateAsync(context.JSRuntime, context);
audioNode = analyser;
}
_ = audioNodeSlim.Release();
return audioNode;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class AudioDestination : Node
private AudioNode? audioNode;
public override Func<AudioContext, Task<AudioNode>> AudioNode => async (context) =>
{
_ = await audioNodeSlim.WaitAsync(200);
audioNode ??= await context.GetDestinationAsync();
_ = audioNodeSlim.Release();
return audioNode;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class BiquadFilter : Node
private AudioNode? audioNode;
public override Func<AudioContext, Task<AudioNode>> AudioNode => async (context) =>
{
_ = await audioNodeSlim.WaitAsync(200);
if (audioNode is null)
{
BiquadFilterOptions options = new();
Expand All @@ -24,6 +25,7 @@ public class BiquadFilter : Node
BiquadFilterNode oscillator = await BiquadFilterNode.CreateAsync(context.JSRuntime, context, options);
audioNode = oscillator;
}
_ = audioNodeSlim.Release();
return audioNode;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Gain : Node
private AudioNode? audioNode;
public override Func<AudioContext, Task<AudioNode>> AudioNode => async (context) =>
{
_ = await audioNodeSlim.WaitAsync(200);
if (audioNode is null)
{
GainOptions options = new();
Expand All @@ -31,6 +32,7 @@ public class Gain : Node
GainNode oscillator = await GainNode.CreateAsync(context.JSRuntime, context, options);
audioNode = oscillator;
}
_ = audioNodeSlim.Release();
return audioNode;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public class MediaStreamAudioSource : Node
private AudioNode? audioNode;
public override Func<AudioContext, Task<AudioNode>> AudioNode => async (context) =>
{
_ = await audioNodeSlim.WaitAsync(200);
if (audioNode is null)
{
await SetMediaStreamAudioSourceNode(context);
}
return audioNode!;
_ = audioNodeSlim.Release();
};

public async Task SetMediaStreamAudioSourceNode(AudioContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace KristofferStrube.Blazor.WebAudio.WasmExample.AudioEditor;

public abstract class Node : Rect, ITaskQueueable
{
protected readonly SemaphoreSlim audioNodeSlim = new(1, 1);

public Dictionary<string, Func<AudioContext, Task<AudioParam>>> AudioParams { get; set; } = new();
public virtual Dictionary<string, int> AudioParamPositions { get; set; } = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public Oscillator(IElement element, SVGEditor.SVGEditor svg) : base(element, svg
};

private AudioNode? audioNode;

public override Func<AudioContext, Task<AudioNode>> AudioNode => async (context) =>
{
_ = await audioNodeSlim.WaitAsync(200);
if (audioNode is null)
{
Console.WriteLine("NEW Oscillator!: " + Id);
OscillatorOptions options = new();
if (Frequency is { } f)
{
Expand All @@ -38,6 +41,7 @@ public Oscillator(IElement element, SVGEditor.SVGEditor svg) : base(element, svg
await oscillator.StartAsync();
audioNode = oscillator;
}
_ = audioNodeSlim.Release();
return audioNode;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ else

public async ValueTask DisposeAsync()
{
await AudioEditor.StopAsync();
if (AudioEditor is not null)
{
await AudioEditor.StopAsync();
}
}

private static string ToGzip(string value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
public async Task AudioParamUpdatedAsync()
{
if (AudioParam is null) return;
Console.WriteLine("SET: " + value);
await AudioParam.SetValueAsync(value);
UpdateCallback?.Invoke(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


@code {
private string relativeUri => NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
private string relativeUri => NavigationManager.ToBaseRelativePath(NavigationManager.Uri.Split("?")[0]);

protected string page => (string.IsNullOrEmpty(relativeUri) ? "Index" : relativeUri) + ".razor";
}

0 comments on commit 4accf2d

Please sign in to comment.