Skip to content

Commit e88d9ca

Browse files
Repeat sampler 5 times to get more stable
1 parent b66ddf3 commit e88d9ca

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ else if (peakFrequencyCount > 0)
3636
<button class="btn btn-primary btn-sm" @onclick="() => MakePlaybackMatchFrequency(3520)">Match 3520 Hz</button>
3737
<br />
3838

39-
<span>Average Max Peak: @(Math.Round(peakFrequencySum / peakFrequencyCount, 0)) Hz</span><br />
39+
<span>Average Max Peak: @(Math.Round(peakFrequencySum / peakFrequencyCount, 0)) Hz</span>
40+
<br />
4041
<Plot Data="frequencyMeasurements" />
4142
}
4243
else if (audioBuffer is not null)
@@ -208,15 +209,8 @@ else
208209
if (context is null || audioBuffer is null)
209210
return;
210211

211-
await using AudioBufferSourceNode sourceNode = await AudioBufferSourceNode.CreateAsync(JSRuntime, context, new()
212-
{
213-
Buffer = audioBuffer,
214-
PlaybackRate = playbackRate
215-
});
216-
217212
analyser = await context.CreateAnalyserAsync();
218213
await using AudioDestinationNode destination = await context.GetDestinationAsync();
219-
await sourceNode.ConnectAsync(analyser);
220214
await analyser.ConnectAsync(destination);
221215

222216
int bufferLength = (int)await analyser.GetFrequencyBinCountAsync();
@@ -230,9 +224,24 @@ else
230224
{
231225
makeMeasurements = false;
232226
});
233-
await sourceNode.AddOnEndedEventListenerAsync(endedListener);
234227

235-
await sourceNode.StartAsync(when: 0, offset, duration);
228+
double currentTime = await context.GetCurrentTimeAsync();
229+
230+
int repeats = 5;
231+
for (int i = 0; i < repeats; i++)
232+
{
233+
await using AudioBufferSourceNode sourceNode = await AudioBufferSourceNode.CreateAsync(JSRuntime, context, new()
234+
{
235+
Buffer = audioBuffer,
236+
PlaybackRate = playbackRate
237+
});
238+
await sourceNode.ConnectAsync(analyser);
239+
await sourceNode.StartAsync(when: currentTime + i * duration / playbackRate, offset, duration);
240+
if (i == repeats - 1)
241+
{
242+
await sourceNode.AddOnEndedEventListenerAsync(endedListener);
243+
}
244+
}
236245

237246
peakFrequencySum = 0;
238247
peakFrequencyCount = 1;

0 commit comments

Comments
 (0)