Skip to content

Commit 3a2a295

Browse files
Updated sample to have cone visualizers.
1 parent c22590e commit 3a2a295

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioPannerEditor/PannerNodeShapeEditor.razor

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@inherits ShapeEditor<PannerNodeShape>
55

66
<g transform="translate(@SVGElement.SVG.Translate.x.AsString() @SVGElement.SVG.Translate.y.AsString()) scale(@SVGElement.SVG.Scale.AsString())">
7+
78
<rect @ref=ElementReference
89
@onfocusin="FocusElement"
910
@onfocusout="UnfocusElement"
@@ -17,6 +18,16 @@
1718
fill="@SVGElement.Fill"
1819
transform="rotate(@(SVGElement.Rotation.AsString()) @((SVGElement.X + SVGElement.Width / 2).AsString()) @((SVGElement.Y + SVGElement.Height / 2).AsString()))">
1920
</rect>
21+
<polygon points=@Cone(20)
22+
fill="red"
23+
opacity="0.2"
24+
transform="rotate(@(SVGElement.Rotation.AsString()) @((SVGElement.X + SVGElement.Width / 2).AsString()) @((SVGElement.Y + SVGElement.Height / 2).AsString()))">
25+
</polygon>
26+
<polygon points=@Cone(10)
27+
fill="red"
28+
opacity="0.2"
29+
transform="rotate(@(SVGElement.Rotation.AsString()) @((SVGElement.X + SVGElement.Width / 2).AsString()) @((SVGElement.Y + SVGElement.Height / 2).AsString()))">
30+
</polygon>
2031
</g>
2132
@if (SVGElement.Selected && SVGElement.SVG.EditMode is not EditMode.Add)
2233
{
@@ -29,4 +40,16 @@
2940
x: SVGElement.X + SVGElement.Width / 2 + Math.Sin(-SVGElement.Rotation / 180 * Math.PI) * SVGElement.Height,
3041
y: SVGElement.Y + SVGElement.Height / 2 + Math.Cos(-SVGElement.Rotation / 180 * Math.PI) * SVGElement.Height
3142
);
43+
44+
public string Cone(double angle)
45+
{
46+
List<(double x, double y)> points =
47+
[
48+
(SVGElement.X + SVGElement.Width / 2, SVGElement.Y + SVGElement.Height),
49+
(SVGElement.X + SVGElement.Width / 2 + 10 * Math.Sin(angle / 180 * Math.PI), SVGElement.Y + SVGElement.Height + 10 * Math.Cos(angle / 180 * Math.PI)),
50+
(SVGElement.X + SVGElement.Width / 2 + 10 * Math.Sin(-angle / 180 * Math.PI), SVGElement.Y + SVGElement.Height + 10 * Math.Cos(-angle / 180 * Math.PI)),
51+
];
52+
53+
return string.Join(" ", points.Select(point => $"{point.x.AsString()},{point.y.AsString()}"));
54+
}
3255
}

samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Panning.razor

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
<PageTitle>WebAudio - Panning</PageTitle>
99
<h2>Panning</h2>
1010

11-
<p>On this page we present a view of two speakers directed towards the listener.
11+
<p>
12+
On this page we present a view of two speakers directed towards the listener.
1213
The speakers can then be moved using a <code>PannerNode</code> in 3D space and be directed towards another orientation.
13-
To simplify this demo we have limited ourselves to only edit the position in 2 dimensions</p>
14+
To simplify this demo we have limited ourselves to only edit the position in 2 dimensions
15+
</p>
1416

1517
@if (oscillatorDark is null)
1618
{
@@ -23,19 +25,19 @@ else
2325
<br />
2426
<GainSlider GainNode="gainNode" />
2527

26-
<div style="height: 40vh;">
28+
<div style="height: 30vh;">
2729
<SVGEditor @ref=sVGEditor
28-
Input=@input
29-
InputUpdated="(string s) => { input = s; StateHasChanged(); }"
30-
SupportedElements="supportedElements"
31-
InputRendered="EditorLoaded" />
30+
Input=@input
31+
InputUpdated="(string s) => { input = s; StateHasChanged(); }"
32+
SupportedElements="supportedElements"
33+
InputRendered="EditorLoaded" />
3234
</div>
3335

3436
<h3>Left speaker</h3>
35-
<Plot Data="leftTimeDomainMeasurements" />
37+
<Plot Height=100 Data="leftTimeDomainMeasurements" />
3638

3739
<h3>Right speaker</h3>
38-
<Plot Data="rightTimeDomainMeasurements" />
40+
<Plot Height=100 Data="rightTimeDomainMeasurements" />
3941

4042
@code {
4143
AudioContext context = default!;
@@ -68,12 +70,15 @@ else
6870
{
6971
ConeInnerAngle = 20,
7072
ConeOuterAngle = 40,
73+
MaxDistance = 10
7174
});
7275
pannerDark = await PannerNode.CreateAsync(JSRuntime, context, new()
7376
{
7477
ConeInnerAngle = 20,
7578
ConeOuterAngle = 40,
79+
MaxDistance = 10
7680
});
81+
7782
gainNode = await GainNode.CreateAsync(JSRuntime, context, new() { Gain = 0.1f });
7883
AudioDestinationNode destination = await context.GetDestinationAsync();
7984

@@ -91,19 +96,20 @@ else
9196
await gainNode.ConnectAsync(destination);
9297

9398
int bufferLength = (int)await leftAnalyzerNode.GetFrequencyBinCountAsync();
94-
await using var timeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
99+
await using var leftTimeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
100+
await using var rightTimeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
95101

96-
while(true)
102+
while (true)
97103
{
98104
await Task.Delay(500);
99105

100-
await leftAnalyzerNode.GetByteTimeDomainDataAsync(timeDomainDataArray);
101-
leftTimeDomainMeasurements = await timeDomainDataArray.GetAsArrayAsync();
106+
await leftAnalyzerNode.GetByteTimeDomainDataAsync(leftTimeDomainDataArray);
107+
leftTimeDomainMeasurements = await leftTimeDomainDataArray.GetAsArrayAsync();
102108

103-
await rightAnalyzerNode.GetByteTimeDomainDataAsync(timeDomainDataArray);
104-
rightTimeDomainMeasurements = await timeDomainDataArray.GetAsArrayAsync();
109+
await rightAnalyzerNode.GetByteTimeDomainDataAsync(rightTimeDomainDataArray);
110+
rightTimeDomainMeasurements = await rightTimeDomainDataArray.GetAsArrayAsync();
105111

106-
await Task.Delay(50);
112+
await Task.Delay(10);
107113
StateHasChanged();
108114
}
109115
}

src/KristofferStrube.Blazor.WebAudio/Options/PannerOptions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.ComponentModel;
2-
using System.Text.Json.Serialization;
1+
using System.Text.Json.Serialization;
32

43
namespace KristofferStrube.Blazor.WebAudio;
54

0 commit comments

Comments
 (0)