Skip to content

Commit 814447e

Browse files
Simple bass drum.
1 parent 0e83d6e commit 814447e

File tree

1 file changed

+57
-30
lines changed
  • samples/KristofferStrube.Blazor.WebAudio.WasmExample/Pages

1 file changed

+57
-30
lines changed

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

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,23 @@
1111
<p>
1212
Below here we have a <b style="color:orange;">Timpani Drum</b>. Try to hit it!
1313
<br />
14-
<small>Soon I will add a <b style="color:green;">Bass Drum</b> as well.</small>
14+
<small>Soon I will add a <b style="color:grey;">Bass Drum</b> as well.</small>
1515
</p>
1616

1717
<svg width="200" height="200" @onpointerdown=PlayTimps>
1818
<circle cx="100" cy="100" r="90" fill="orange" stroke="orangered"></circle>
1919
</svg>
2020

21-
<h3>Time Domain</h3>
22-
<hr />
23-
<Plot Data="timeDomainMeasurements" />
21+
<svg width="180" height="180" @onpointerdown=PlayBass>
22+
<circle cx="90" cy="90" r="80" fill="silver" stroke="grey"></circle>
23+
</svg>
24+
2425
<h3>Spectrogram</h3>
2526
<hr />
2627
<SpectrogramPlot Analyser=analyser UpperFrequency="40" TimeInSeconds="8" />
2728

2829
@code {
2930
bool makingMeasurements = false;
30-
static readonly float[] timpsFrequencies = new float[] { 1.00f, 1.50f, 1.98f, 2.44f };
31-
static readonly float[] timpsAmplitudes = new float[] { 1, 0.8f, 0.6f, 0.2f };
32-
static readonly float[] timpsDecays = new float[] { 4.5f, 7.5f, 9f, 8.5f };
33-
static readonly double[] lastTimpTime = new double[] { 0, 0, 0, 0 };
3431

3532
AudioContext context = default!;
3633
GainNode mainNode = default!;
@@ -50,24 +47,27 @@
5047
}
5148
}
5249

53-
public async Task Analyze()
54-
{
55-
if (makingMeasurements) return;
56-
makingMeasurements = true;
50+
// public async Task Analyze()
51+
// {
52+
// if (makingMeasurements) return;
53+
// makingMeasurements = true;
5754
58-
int bufferLength = (int)await analyser.GetFrequencyBinCountAsync();
59-
var timeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
55+
// int bufferLength = (int)await analyser.GetFrequencyBinCountAsync();
56+
// var timeDomainDataArray = await Uint8Array.CreateAsync(JSRuntime, bufferLength);
6057
61-
while (makingMeasurements)
62-
{
63-
await analyser.GetByteTimeDomainDataAsync(timeDomainDataArray);
58+
// while (makingMeasurements)
59+
// {
60+
// await analyser.GetByteTimeDomainDataAsync(timeDomainDataArray);
6461
65-
timeDomainMeasurements = await timeDomainDataArray.GetByteArrayAsync();
66-
await Task.Delay(1);
67-
StateHasChanged();
68-
}
69-
}
62+
// timeDomainMeasurements = await timeDomainDataArray.GetByteArrayAsync();
63+
// await Task.Delay(1);
64+
// StateHasChanged();
65+
// }
66+
// }
7067
68+
static readonly float[] timpsFrequencies = new float[] { 1.00f, 1.50f, 1.98f, 2.44f };
69+
static readonly float[] timpsAmplitudes = new float[] { 1, 0.8f, 0.6f, 0.2f };
70+
static readonly float[] timpsDecays = new float[] { 4.5f, 7.5f, 9f, 8.5f };
7171
public async Task PlayTimps(PointerEventArgs eventArgs)
7272
{
7373
await Initialize();
@@ -76,34 +76,32 @@
7676
if (distanceFromMid > 90) return;
7777
float pitch = 100 + distanceFromMid;
7878

79-
GainNode[]? timpOscillatorAmplifiers = new GainNode[4];
79+
GainNode[]? oscillatorAmplifiers = new GainNode[4];
8080
for (int i = 0; i < 4; i++)
8181
{
8282
var oscillator = await OscillatorNode.CreateAsync(JSRuntime, context, new() { Type = OscillatorType.Sine, Frequency = timpsFrequencies[i] * pitch });
8383
var amplifier = await GainNode.CreateAsync(JSRuntime, context, new() { Gain = 0 });
8484
await oscillator.ConnectAsync(amplifier);
8585
await amplifier.ConnectAsync(mainNode);
86-
timpOscillatorAmplifiers[i] = amplifier;
86+
oscillatorAmplifiers[i] = amplifier;
8787
await oscillator.StartAsync();
8888
}
8989

9090
var time = await context.GetCurrentTimeAsync();
9191

9292
for (int i = 0; i < 4; i++)
9393
{
94-
var gain = await timpOscillatorAmplifiers[i].GetGainAsync();
94+
var gain = await oscillatorAmplifiers[i].GetGainAsync();
9595
await gain.SetValueAsync(await gain.GetValueAsync());
9696
await gain.LinearRampToValueAtTimeAsync(timpsAmplitudes[i] / timpsAmplitudes.Sum(), time + 0.3);
97-
var endTime = time + timpsDecays[i] * 0.1;
98-
await gain.LinearRampToValueAtTimeAsync(0, endTime);
99-
lastTimpTime[i] = endTime;
97+
await gain.LinearRampToValueAtTimeAsync(0, time + timpsDecays[i] * 0.1);
10098
}
10199

102100
var noiseCarrier = await OscillatorNode.CreateAsync(JSRuntime, context, new() { Frequency = 100 });
103101
var noiseModulator = await OscillatorNode.CreateAsync(JSRuntime, context, new() { Frequency = 87 });
104102
var noiseRingModulator = await GainNode.CreateAsync(JSRuntime, context, new() { Gain = 0 });
105103
var noiseGain = await noiseRingModulator.GetGainAsync();
106-
var noiseHighPassFilter = await BiquadFilterNode.CreateAsync(JSRuntime, context, new() { Type = BiquadFilterType.Highpass, Frequency = 150, Q = 1 });
104+
var noiseHighPassFilter = await BiquadFilterNode.CreateAsync(JSRuntime, context, new() { Type = BiquadFilterType.Highpass, Frequency = 150, Q = 1 });
107105
var noiseAmplifier = await GainNode.CreateAsync(JSRuntime, context, new() { Gain = 0 });
108106
await noiseCarrier.ConnectAsync(noiseRingModulator);
109107
await noiseModulator.ConnectAsync(noiseGain);
@@ -115,8 +113,37 @@
115113
var noiseAmplifierGain = await noiseAmplifier.GetGainAsync();
116114
await noiseAmplifierGain.LinearRampToValueAtTimeAsync(0.2f, time + 0.3);
117115
await noiseAmplifierGain.LinearRampToValueAtTimeAsync(0, time + timpsDecays.Max() * 0.1);
116+
}
117+
118+
static readonly float[] bassFrequencies = new float[] { 1.00f, 1.86f, 2.72f, 3.64f, 4.50f, 5.46f };
119+
public async Task PlayBass(PointerEventArgs eventArgs)
120+
{
121+
await Initialize();
118122

119-
await Analyze();
123+
float distanceFromMid = (float)Math.Sqrt(Math.Pow(eventArgs.OffsetX - 100, 2) + Math.Pow(eventArgs.OffsetY - 100, 2));
124+
if (distanceFromMid > 80) return;
125+
float pitch = 50 + distanceFromMid / 2;
126+
127+
GainNode[]? oscillatorAmplifiers = new GainNode[4];
128+
for (int i = 0; i < 4; i++)
129+
{
130+
var oscillator = await OscillatorNode.CreateAsync(JSRuntime, context, new() { Type = OscillatorType.Sine, Frequency = bassFrequencies[i] * pitch });
131+
var amplifier = await GainNode.CreateAsync(JSRuntime, context, new() { Gain = 0 });
132+
await oscillator.ConnectAsync(amplifier);
133+
await amplifier.ConnectAsync(mainNode);
134+
oscillatorAmplifiers[i] = amplifier;
135+
await oscillator.StartAsync();
136+
}
137+
138+
var time = await context.GetCurrentTimeAsync();
139+
140+
for (int i = 0; i < 4; i++)
141+
{
142+
var gain = await oscillatorAmplifiers[i].GetGainAsync();
143+
await gain.SetValueAsync(await gain.GetValueAsync());
144+
await gain.LinearRampToValueAtTimeAsync(1 / (float)6, time + 0.3);
145+
await gain.LinearRampToValueAtTimeAsync(0, time + 1);
146+
}
120147
}
121148

122149
public async Task StopSound()

0 commit comments

Comments
 (0)