Skip to content

Commit 69d785e

Browse files
Made Analyzer more stable.
1 parent 62ddacb commit 69d785e

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioEditor/AudioEditor.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
while (running)
3333
{
34-
await Task.Delay(100);
34+
await Task.Delay(50);
3535
foreach (ITaskQueueable taskQueueable in SVGEditor.Elements.Where(e => e is ITaskQueueable))
3636
{
3737
while (taskQueueable.QueuedTasks.TryDequeue(out var task))

samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioEditor/NodeEditors/AnalyserEditor.razor

+28-14
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<foreignObject @onpointerdown:stopPropagation=!SVGElement.Selected x="@((SVGElement.X+10).AsString())" y="@((SVGElement.Y+10).AsString())" height="110" width="200" style="border:solid @(SVGElement.StrokeWidth)px @(SVGElement.Stroke);padding:2;pointer-events:@(SVGElement.Selected ? "none" : "inherit");touch-action:@(SVGElement.Selected ? "none" : "inherit")">
2828
Analyser
2929
<br />
30-
<select @bind=analyzeOutput>
30+
<select @bind=SVGElement.Type>
3131
<option value="TimeDomain" title="Time Domain Data.">Time Domain</option>
3232
<option value="Frequency" title="Frequency Data.">Frequency</option>
3333
</select>
@@ -49,7 +49,6 @@
4949

5050
@code {
5151
private AnalyserNode? analyser;
52-
private string analyzeOutput = "TimeDomain";
5352

5453
private byte[] measurements = Array.Empty<byte>();
5554

@@ -65,27 +64,42 @@
6564
analyser = (AnalyserNode)await SVGElement.AudioNode(AudioContext);
6665

6766
int bufferLength = (int)await analyser.GetFrequencyBinCountAsync();
68-
Uint8Array dataArray = await Uint8Array.CreateAsync(AudioContext.JSRuntime, bufferLength);
67+
await using Uint8Array dataArray = await Uint8Array.CreateAsync(AudioContext.JSRuntime, bufferLength);
6968

7069
SVGElement.Running = true;
7170

7271
while (SVGElement.Running)
7372
{
7473
try
7574
{
76-
if (analyzeOutput is "TimeDomain")
75+
SVGElement.QueuedTasks.Enqueue(async _ =>
7776
{
78-
await analyser.GetByteTimeDomainDataAsync(dataArray);
79-
}
80-
else
81-
{
82-
await analyser.GetByteFrequencyDataAsync(dataArray);
83-
}
77+
if (SVGElement.Type is "TimeDomain")
78+
{
79+
await analyser.GetByteTimeDomainDataAsync(dataArray);
80+
measurements = await dataArray.GetAsArrayAsync();
81+
}
82+
else
83+
{
84+
await analyser.GetByteFrequencyDataAsync(dataArray);
85+
86+
var data = (await dataArray.GetAsArrayAsync());
87+
int endOfPlateau = 1;
88+
for (int i = data.Length - 1; i >= 0; i--)
89+
{
90+
if (data[i] != 0)
91+
{
92+
endOfPlateau = i;
93+
break;
94+
}
95+
}
96+
measurements = data[..endOfPlateau];
97+
}
98+
SVGElement._stateRepresentation = "";
8499

85-
measurements = await dataArray.GetAsArrayAsync();
86-
await Task.Delay(10);
87-
SVGElement._stateRepresentation = "";
88-
StateHasChanged();
100+
await InvokeAsync(StateHasChanged);
101+
});
102+
await Task.Delay(50);
89103
}
90104
catch (Exception)
91105
{

samples/KristofferStrube.Blazor.WebAudio.WasmExample/AudioEditor/Nodes/Analyser.cs

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ public Analyser(IElement element, SVGEditor.SVGEditor svg) : base(element, svg)
2424
set => base.Height = 130;
2525
}
2626

27+
public string Type
28+
{
29+
get => Element.GetAttribute("data-type");
30+
set
31+
{
32+
if (value is null)
33+
{
34+
_ = Element.RemoveAttribute("data-type");
35+
}
36+
else
37+
{
38+
Element.SetAttribute("data-type", value);
39+
}
40+
Changed?.Invoke(this);
41+
}
42+
}
43+
2744
public bool Running { get; set; }
2845

2946
public override Type Presenter => typeof(AnalyserEditor);
@@ -40,6 +57,7 @@ public Analyser(IElement element, SVGEditor.SVGEditor svg) : base(element, svg)
4057
StrokeWidth = "2",
4158
Height = 130,
4259
Width = 250,
60+
Type = "TimeDomain",
4361
};
4462

4563
(node.X, node.Y) = SVG.LocalDetransform(SVG.LastRightClick);

samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/Plot.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<svg width="100%" height="@(Height)px" viewBox="0 -5 @Data.Length 261">
1+
<svg width="100%" height="@(Height)px" viewBox="0 -5 2000 261">
22
@for (int i = 1; i < Data.Length; i++)
33
{
4-
<line x1="@(i-1)" x2="@i" y1="@Data[i-1]" y2="@Data[i]" stroke="red" stroke-width="2"></line>
4+
<line x1="@(((i-1) * 2000.0 / @Data.Length).AsString())" x2="@((i * 2000.0 / @Data.Length).AsString())" y1="@Data[i-1]" y2="@Data[i]" stroke="red" stroke-width="2"></line>
55
}
66
</svg>
77

0 commit comments

Comments
 (0)