|
27 | 27 | <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")">
|
28 | 28 | Analyser
|
29 | 29 | <br />
|
30 |
| - <select @bind=analyzeOutput> |
| 30 | + <select @bind=SVGElement.Type> |
31 | 31 | <option value="TimeDomain" title="Time Domain Data.">Time Domain</option>
|
32 | 32 | <option value="Frequency" title="Frequency Data.">Frequency</option>
|
33 | 33 | </select>
|
|
49 | 49 |
|
50 | 50 | @code {
|
51 | 51 | private AnalyserNode? analyser;
|
52 |
| - private string analyzeOutput = "TimeDomain"; |
53 | 52 |
|
54 | 53 | private byte[] measurements = Array.Empty<byte>();
|
55 | 54 |
|
|
65 | 64 | analyser = (AnalyserNode)await SVGElement.AudioNode(AudioContext);
|
66 | 65 |
|
67 | 66 | 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); |
69 | 68 |
|
70 | 69 | SVGElement.Running = true;
|
71 | 70 |
|
72 | 71 | while (SVGElement.Running)
|
73 | 72 | {
|
74 | 73 | try
|
75 | 74 | {
|
76 |
| - if (analyzeOutput is "TimeDomain") |
| 75 | + SVGElement.QueuedTasks.Enqueue(async _ => |
77 | 76 | {
|
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 = ""; |
84 | 99 |
|
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); |
89 | 103 | }
|
90 | 104 | catch (Exception)
|
91 | 105 | {
|
|
0 commit comments