From 3a2a2951808765f5dfaeb9edf245071fed4997af Mon Sep 17 00:00:00 2001 From: KristofferStrube Date: Sun, 12 Jan 2025 01:23:28 +0100 Subject: [PATCH] Updated sample to have cone visualizers. --- .../PannerNodeShapeEditor.razor | 23 +++++++++++ .../Pages/Panning.razor | 38 +++++++++++-------- .../Options/PannerOptions.cs | 3 +- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioPannerEditor/PannerNodeShapeEditor.razor b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioPannerEditor/PannerNodeShapeEditor.razor index 2baefed..ff1a3bc 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioPannerEditor/PannerNodeShapeEditor.razor +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioPannerEditor/PannerNodeShapeEditor.razor @@ -4,6 +4,7 @@ @inherits ShapeEditor + + + + + @if (SVGElement.Selected && SVGElement.SVG.EditMode is not EditMode.Add) { @@ -29,4 +40,16 @@ x: SVGElement.X + SVGElement.Width / 2 + Math.Sin(-SVGElement.Rotation / 180 * Math.PI) * SVGElement.Height, y: SVGElement.Y + SVGElement.Height / 2 + Math.Cos(-SVGElement.Rotation / 180 * Math.PI) * SVGElement.Height ); + + public string Cone(double angle) + { + List<(double x, double y)> points = + [ + (SVGElement.X + SVGElement.Width / 2, SVGElement.Y + SVGElement.Height), + (SVGElement.X + SVGElement.Width / 2 + 10 * Math.Sin(angle / 180 * Math.PI), SVGElement.Y + SVGElement.Height + 10 * Math.Cos(angle / 180 * Math.PI)), + (SVGElement.X + SVGElement.Width / 2 + 10 * Math.Sin(-angle / 180 * Math.PI), SVGElement.Y + SVGElement.Height + 10 * Math.Cos(-angle / 180 * Math.PI)), + ]; + + return string.Join(" ", points.Select(point => $"{point.x.AsString()},{point.y.AsString()}")); + } } \ No newline at end of file diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Panning.razor b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Panning.razor index c00bdd6..7248558 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Panning.razor +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Panning.razor @@ -8,9 +8,11 @@ WebAudio - Panning

Panning

-

On this page we present a view of two speakers directed towards the listener. +

+ On this page we present a view of two speakers directed towards the listener. The speakers can then be moved using a PannerNode in 3D space and be directed towards another orientation. - To simplify this demo we have limited ourselves to only edit the position in 2 dimensions

+ To simplify this demo we have limited ourselves to only edit the position in 2 dimensions +

@if (oscillatorDark is null) { @@ -23,19 +25,19 @@ else
-
+
+ Input=@input + InputUpdated="(string s) => { input = s; StateHasChanged(); }" + SupportedElements="supportedElements" + InputRendered="EditorLoaded" />

Left speaker

- +

Right speaker

- + @code { AudioContext context = default!; @@ -68,12 +70,15 @@ else { ConeInnerAngle = 20, ConeOuterAngle = 40, + MaxDistance = 10 }); pannerDark = await PannerNode.CreateAsync(JSRuntime, context, new() { ConeInnerAngle = 20, ConeOuterAngle = 40, + MaxDistance = 10 }); + gainNode = await GainNode.CreateAsync(JSRuntime, context, new() { Gain = 0.1f }); AudioDestinationNode destination = await context.GetDestinationAsync(); @@ -91,19 +96,20 @@ else await gainNode.ConnectAsync(destination); int bufferLength = (int)await leftAnalyzerNode.GetFrequencyBinCountAsync(); - await using var timeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength); + await using var leftTimeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength); + await using var rightTimeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength); - while(true) + while (true) { await Task.Delay(500); - await leftAnalyzerNode.GetByteTimeDomainDataAsync(timeDomainDataArray); - leftTimeDomainMeasurements = await timeDomainDataArray.GetAsArrayAsync(); + await leftAnalyzerNode.GetByteTimeDomainDataAsync(leftTimeDomainDataArray); + leftTimeDomainMeasurements = await leftTimeDomainDataArray.GetAsArrayAsync(); - await rightAnalyzerNode.GetByteTimeDomainDataAsync(timeDomainDataArray); - rightTimeDomainMeasurements = await timeDomainDataArray.GetAsArrayAsync(); + await rightAnalyzerNode.GetByteTimeDomainDataAsync(rightTimeDomainDataArray); + rightTimeDomainMeasurements = await rightTimeDomainDataArray.GetAsArrayAsync(); - await Task.Delay(50); + await Task.Delay(10); StateHasChanged(); } } diff --git a/src/KristofferStrube.Blazor.WebAudio/Options/PannerOptions.cs b/src/KristofferStrube.Blazor.WebAudio/Options/PannerOptions.cs index 141f884..e82d2e2 100644 --- a/src/KristofferStrube.Blazor.WebAudio/Options/PannerOptions.cs +++ b/src/KristofferStrube.Blazor.WebAudio/Options/PannerOptions.cs @@ -1,5 +1,4 @@ -using System.ComponentModel; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace KristofferStrube.Blazor.WebAudio;