Skip to content

Commit 6f14c56

Browse files
Update demo to dispose of all nodes when page switches.
1 parent 62280c6 commit 6f14c56

File tree

1 file changed

+45
-9
lines changed
  • samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages

1 file changed

+45
-9
lines changed

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ else
6161
PannerNodeShape speakerLight = default!;
6262
PannerNodeShape speakerDark = default!;
6363

64+
bool disposed = false;
65+
6466
private byte[] leftTimeDomainMeasurements = Array.Empty<byte>();
6567
private byte[] rightTimeDomainMeasurements = Array.Empty<byte>();
6668

@@ -103,7 +105,7 @@ else
103105

104106
await Task.Delay(500);
105107

106-
while (true)
108+
while (!disposed)
107109
{
108110
try
109111
{
@@ -115,7 +117,7 @@ else
115117
}
116118
catch
117119
{
118-
120+
119121
}
120122

121123
await Task.Delay(10);
@@ -127,11 +129,10 @@ else
127129
{
128130
if (!firstRender) return;
129131

130-
while (true)
132+
while (!disposed)
131133
{
132-
StateHasChanged();
133134
await Task.Delay(50);
134-
if (speakerLight is null) continue;
135+
if (speakerLight is null || context is null) continue;
135136

136137
await UpdatePannerPositionAndOrientation(pannerLight, speakerLight);
137138
await UpdatePannerPositionAndOrientation(pannerDark, speakerDark);
@@ -143,16 +144,16 @@ else
143144
double time = await context.GetCurrentTimeAsync();
144145

145146
await using AudioParam zPosition = await panner.GetPositionZAsync();
146-
await zPosition.LinearRampToValueAtTimeAsync((float)(speaker.Y + speaker.Width / 2 + Math.Cos(-speaker.Rotation / 180 * MathF.PI) * speaker.Width / 2), time + 0.1);
147+
await zPosition.LinearRampToValueAtTimeAsync((float)(speaker.Y + speaker.Width / 2 + Math.Cos(-speaker.Rotation / 180 * MathF.PI) * speaker.Width / 2), time + 0.05);
147148

148149
await using AudioParam xPosition = await panner.GetPositionXAsync();
149-
await xPosition.LinearRampToValueAtTimeAsync((float)(speaker.X + speaker.Height / 2 + Math.Sin(-speaker.Rotation / 180 * MathF.PI) * speaker.Width / 2), time + 0.1);
150+
await xPosition.LinearRampToValueAtTimeAsync((float)(speaker.X + speaker.Height / 2 + Math.Sin(-speaker.Rotation / 180 * MathF.PI) * speaker.Width / 2), time + 0.05);
150151

151152
await using AudioParam zOrientation = await panner.GetOrientationZAsync();
152-
await zOrientation.LinearRampToValueAtTimeAsync((float)Math.Cos(-speaker.Rotation / 180 * MathF.PI), time + 0.1);
153+
await zOrientation.LinearRampToValueAtTimeAsync((float)Math.Cos(-speaker.Rotation / 180 * MathF.PI), time + 0.05);
153154

154155
await using AudioParam xOrientation = await panner.GetOrientationXAsync();
155-
await xOrientation.LinearRampToValueAtTimeAsync((float)Math.Sin(-speaker.Rotation / 180 * MathF.PI), time + 0.1);
156+
await xOrientation.LinearRampToValueAtTimeAsync((float)Math.Sin(-speaker.Rotation / 180 * MathF.PI), time + 0.05);
156157
}
157158

158159
public void EditorLoaded()
@@ -198,7 +199,42 @@ else
198199

199200
public async ValueTask DisposeAsync()
200201
{
202+
disposed = true;
201203
await StopSound();
204+
if (context is not null)
205+
{
206+
await context.DisposeAsync();
207+
context = default!;
208+
}
209+
if (pannerLight is not null)
210+
{
211+
await pannerLight.DisconnectAsync();
212+
await pannerLight.DisposeAsync();
213+
}
214+
if (pannerDark is not null)
215+
{
216+
await pannerDark.DisconnectAsync();
217+
await pannerDark.DisposeAsync();
218+
}
219+
if (gainNode is not null)
220+
{
221+
await gainNode.DisconnectAsync();
222+
await gainNode.DisposeAsync();
223+
}
224+
if (splitterNode is not null)
225+
{
226+
await splitterNode.DisposeAsync();
227+
}
228+
if (leftAnalyzerNode is not null)
229+
{
230+
await leftAnalyzerNode.DisconnectAsync();
231+
await leftAnalyzerNode.DisposeAsync();
232+
}
233+
if (rightAnalyzerNode is not null)
234+
{
235+
await rightAnalyzerNode.DisconnectAsync();
236+
await rightAnalyzerNode.DisposeAsync();
237+
}
202238
}
203239
}
204240

0 commit comments

Comments
 (0)