diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/KristofferStrube.Blazor.WebAudio.WasmExample.csproj b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/KristofferStrube.Blazor.WebAudio.WasmExample.csproj index 1c98c1f..f3b9777 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/KristofferStrube.Blazor.WebAudio.WasmExample.csproj +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/KristofferStrube.Blazor.WebAudio.WasmExample.csproj @@ -9,6 +9,7 @@ + diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Drums.razor b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Drums.razor index c50b5f8..254d0f4 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Drums.razor +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Drums.razor @@ -20,7 +20,7 @@

Spectrogram


- + @code { static readonly float[] timpsFrequencies = new float[] { 1.00f, 1.50f, 1.98f, 2.44f }; diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Spectrogram.razor b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Spectrogram.razor index d36a798..74ab0b9 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Spectrogram.razor +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages/Spectrogram.razor @@ -41,7 +41,6 @@ else byte[] frequencyMeasurements = Array.Empty(); float frequency; OscillatorType type; - bool makeMeasurements = false; protected override async Task OnInitializedAsync() { @@ -68,7 +67,6 @@ else public async Task StopSound() { - makeMeasurements = false; if (oscillator is null) return; await oscillator.StopAsync(); oscillator = null; diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor index 937a103..396515e 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor @@ -1,9 +1,11 @@ - - @for (int i = 1; i < data.Count; i++) +@if (data is null) return; + + + @for (int i = 1; i < 100; i++) { for (int j = 0; j < UpperFrequency - LowerFrequency; j++) { - + } } diff --git a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor.cs b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor.cs index bef11b9..58eaaf1 100644 --- a/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor.cs +++ b/samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/SpectrogramPlot.razor.cs @@ -1,14 +1,15 @@ using KristofferStrube.Blazor.WebIDL; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; +using CommunityToolkit.HighPerformance; +using System; namespace KristofferStrube.Blazor.WebAudio.WasmExample.Shared; public partial class SpectrogramPlot : IDisposable { private bool running; - private readonly List data = []; - private readonly List<(double start, double length)> intervals = []; + private byte[,]? data; [Inject] public required IJSRuntime JSRuntime { get; set; } @@ -28,18 +29,13 @@ public partial class SpectrogramPlot : IDisposable [Parameter] public int UpperFrequency { get; set; } = 100; - - - protected override void OnParametersSet() - { - - } - protected override async Task OnAfterRenderAsync(bool _) { if (running || Analyser is null) return; running = true; + data = new byte[UpperFrequency - LowerFrequency, 100]; + int bufferLength = (int)await Analyser.GetFrequencyBinCountAsync(); Uint8Array frequencyDataArray = await Uint8Array.CreateAsync(JSRuntime, Math.Min(bufferLength, UpperFrequency - LowerFrequency)); @@ -48,19 +44,29 @@ protected override async Task OnAfterRenderAsync(bool _) while (running) { await Analyser.GetByteFrequencyDataAsync(frequencyDataArray); - data.Add((await frequencyDataArray.GetByteArrayAsync())[LowerFrequency..UpperFrequency]); + byte[] reading = await frequencyDataArray.GetByteArrayAsync(); DateTimeOffset currentTime = DateTimeOffset.UtcNow; - var intervalLength = (currentTime - lastTime).TotalMilliseconds; - intervals.Add(((lastTime - start).TotalMilliseconds, intervalLength)); + int intervalStart = (int)((lastTime - start).TotalMilliseconds / TimeInSeconds / 10); + int intervalEnd = (int)((currentTime - start).TotalMilliseconds / TimeInSeconds / 10); + lastTime = currentTime; - if ((currentTime - start).TotalMilliseconds > TimeInSeconds * 1000) + for (int i = intervalStart; i < intervalEnd; i++) { - start = DateTimeOffset.UtcNow; - data.Clear(); - intervals.Clear(); + if (i > 99) + { + start = DateTimeOffset.UtcNow; + lastTime = start; + break; + } + for (int j = 0; j < reading.Length; j++) + { + data[j, i] = reading[j]; + } + //Memory2D readingAs2dMemory = reading.AsMemory2D(UpperFrequency - LowerFrequency, 1); + //Memory2D viewInData = buffer.Slice(0, i, UpperFrequency - LowerFrequency, 1); + //readingAs2dMemory.CopyTo(viewInData); } - lastTime = currentTime; StateHasChanged(); await Task.Delay(10);