Skip to content

Commit b0594cf

Browse files
Improvements to marking for phone.
1 parent 9bb0dac commit b0594cf

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ else if (mediaStream is null)
5050
}
5151
else
5252
{
53-
<AmplitudePlot Analyser="analyser" Color="@(recording ? "#F00" : "#000")" />
5453
if (!recording)
5554
{
5655
<button class="btn btn-primary" @onclick="Record">Record</button>
57-
56+
<br />
5857
@if (audioOptions.Count > 0)
5958
{
6059
<label for="audioSource">Audio Source</label>
@@ -70,6 +69,10 @@ else
7069
{
7170
<button class="btn btn-danger" @onclick="StopRecording">Stop Record</button>
7271
}
72+
if (analyser is not null)
73+
{
74+
<AmplitudePlot Analyser="analyser" Color="@(recording ? "#F00" : "#000")" />
75+
}
7376
}
7477

7578
@code {
@@ -99,6 +102,8 @@ else
99102
{
100103
await StopAudioTrack();
101104

105+
await InvokeAsync(StateHasChanged);
106+
102107
try
103108
{
104109
if (context is null)
@@ -271,6 +276,7 @@ else
271276
{
272277
await analyser.DisposeAsync();
273278
}
279+
analyser = null;
274280
}
275281

276282
public async ValueTask DisposeAsync()

samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/AmplitudePlot.razor.cs

+29-22
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,49 @@ protected override async Task OnAfterRenderAsync(bool _)
3737

3838
running = true;
3939

40-
int bufferLength = (int)await Analyser.GetFftSizeAsync();
41-
await using Uint8Array timeDomainData = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
42-
43-
while (running)
40+
try
4441
{
45-
for (int i = 0; i < Width; i++)
42+
int bufferLength = (int)await Analyser.GetFftSizeAsync();
43+
await using Uint8Array timeDomainData = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
44+
45+
while (running)
4646
{
47-
if (!running)
47+
for (int i = 0; i < Width; i++)
4848
{
49-
break;
50-
}
49+
if (!running)
50+
{
51+
break;
52+
}
5153

52-
await Analyser.GetByteTimeDomainDataAsync(timeDomainData);
54+
await Analyser.GetByteTimeDomainDataAsync(timeDomainData);
5355

54-
byte[] reading = await timeDomainData.GetAsArrayAsync();
56+
byte[] reading = await timeDomainData.GetAsArrayAsync();
5557

56-
double amplitude = reading.Average(r => Math.Abs(r - 128)) / 128.0;
58+
double amplitude = reading.Average(r => Math.Abs(r - 128)) / 128.0;
5759

58-
await using (Context2D context = await canvas.GetContext2DAsync())
59-
{
60-
if (i == 0)
60+
await using (Context2D context = await canvas.GetContext2DAsync())
6161
{
62+
if (i == 0)
63+
{
64+
await context.FillAndStrokeStyles.FillStyleAsync($"#fff");
65+
await context.FillRectAsync(0, 0, Width * 10, Height * 10);
66+
}
67+
6268
await context.FillAndStrokeStyles.FillStyleAsync($"#fff");
63-
await context.FillRectAsync(0, 0, Width * 10, Height * 10);
64-
}
69+
await context.FillRectAsync(i * 10, 0, 10, Height * 10);
6570

66-
await context.FillAndStrokeStyles.FillStyleAsync($"#fff");
67-
await context.FillRectAsync(i * 10, 0, 10, Height * 10);
71+
await context.FillAndStrokeStyles.FillStyleAsync(Color);
72+
await context.FillRectAsync(i * 10, (Height * 10 / 2.0) - (amplitude * Height * 10), 10, amplitude * 2 * Height * 10);
73+
}
6874

69-
await context.FillAndStrokeStyles.FillStyleAsync(Color);
70-
await context.FillRectAsync(i * 10, (Height * 10 / 2.0) - (amplitude * Height * 10), 10, amplitude * 2 * Height * 10);
75+
await Task.Delay(1);
7176
}
72-
73-
await Task.Delay(1);
7477
}
7578
}
79+
catch (Exception e)
80+
{
81+
Console.WriteLine(e.Message);
82+
}
7683
}
7784

7885
public void Dispose()

samples/KristofferStrube.Blazor.WebAudio.WasmExample/Shared/AudioSlicer.razor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public partial class AudioSlicer : ComponentBase, IDisposable
1414
private ElementReference wrapper;
1515
private readonly List<float> amplitudes = [];
1616

17-
private string canvasStyle => $"height:{Height}px;width:100%;";
17+
private string canvasStyle => $"height:{Height}px;width:100%;user-select:none;touch-action:none;";
1818

1919
[Inject]
2020
public required IJSRuntime JSRuntime { get; set; }

0 commit comments

Comments
 (0)