Skip to content

Commit 0a35e7c

Browse files
authored
Implement ElectricSnare sound design (Midi note 40) (#78)
* Add ElectricSnare sound design and link the new sound to percupuff * Simplify implementation of electric snare drum frequency + adjust more values * Address PR comments: remove notch filter, tweak values, update comments
1 parent de1d6d8 commit 0a35e7c

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

dsp/Percupuff.cmajor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Percupuff
3030
noise = std::noise::White;
3131
bassDrum = Drums::BassDrum;
3232
snare = Drums::Snare;
33+
electricSnare = Drums::ElectricSnare;
3334
hihat = Drums::Hihat;
3435
clap = Drums::Clap;
3536
cowbell = Drums::Cowbell;
@@ -52,12 +53,14 @@ namespace Percupuff
5253
p.paramsOut -> cowbell.paramsIn;
5354
p.paramsOut -> hihat.paramsIn;
5455
p.paramsOut -> snare.paramsIn;
56+
p.paramsOut -> electricSnare.paramsIn;
5557
p.paramsOut -> sideStick.paramsIn;
5658

5759
// Send the midi events so the sound processors know when to start playing.
5860
midiIn -> mpe;
5961
mpe -> bassDrum.eventIn;
6062
mpe -> snare.eventIn;
63+
mpe -> electricSnare.eventIn;
6164
mpe -> hihat.eventIn;
6265
mpe -> clap.eventIn;
6366
mpe -> cowbell.eventIn;
@@ -70,6 +73,7 @@ namespace Percupuff
7073
// Some sounds use noise.
7174
noise -> hihat.noiseIn;
7275
noise -> snare.noiseIn;
76+
noise -> electricSnare.noiseIn;
7377
noise -> clap.noiseIn;
7478
noise -> crash.noiseIn;
7579
noise -> claves.noiseIn;
@@ -78,6 +82,7 @@ namespace Percupuff
7882
// it into the gain limiter to adjust how loud they are.
7983
bassDrum -> gainLimiter;
8084
snare -> gainLimiter;
85+
electricSnare -> gainLimiter;
8186
hihat -> gainLimiter;
8287
clap -> gainLimiter;
8388
cowbell -> gainLimiter;

dsp/drums/ElectricSnare.cmajor

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Reference videos:
2+
// https://www.youtube.com/watch?v=Fks15MXxwas
3+
// https://www.youtube.com/watch?v=NifOAvu7KQk
4+
5+
namespace Percupuff
6+
{
7+
namespace Drums
8+
{
9+
// This processor tries to create a electric snare drum sound.
10+
// An oscillator with a triangle waveform is used to create the initial impact of the snare,
11+
// with filtered white noise mixed in for more brightness.
12+
processor ElectricSnare {
13+
input event (std::notes::NoteOn) eventIn;
14+
output stream float<2> out;
15+
input stream float noiseIn;
16+
input event Params paramsIn;
17+
18+
float triggerVelocity = 0.0f;
19+
int midiNotePitch = 0;
20+
float outputLevel = 0.5f;
21+
float panning = 0.0f;
22+
float velocitySensitivity = 1.0f;
23+
24+
event paramsIn(Params p) {
25+
midiNotePitch = int(p.snare2Midi);
26+
outputLevel = p.snare2Level * 0.01f;
27+
panning = p.snare2Panning;
28+
velocitySensitivity = p.snare2Velocity;
29+
}
30+
31+
event eventIn(std::notes::NoteOn n) {
32+
if (int (n.pitch) == midiNotePitch) {
33+
triggerVelocity = sqrt(n.velocity);
34+
}
35+
}
36+
37+
node envelope = Envelope;
38+
node osc1 = Oscillator(OscillatorShape::triangle);
39+
40+
// Filters that shape the white noise.
41+
// Use LpfResonant to add resonance for brightness
42+
LpfResonant lowpass = (0.0f, 0.0f);
43+
44+
void main()
45+
{
46+
osc1.frequencyIn <- 160.0f;
47+
48+
let invSampleRate = 1.0f / float(processor.frequency);
49+
50+
loop
51+
{
52+
while (triggerVelocity == 0) {
53+
advance();
54+
}
55+
56+
let vel = triggerVelocity;
57+
triggerVelocity = 0.0f;
58+
59+
envelope.attackIn <- 0.001f;
60+
envelope.releaseIn <- .2f + vel * 0.1f;
61+
envelope.triggerIn <- void;
62+
envelope.advance();
63+
float gain = envelope.gainOut;
64+
while (gain > 0.0f && triggerVelocity == 0.0f) {
65+
osc1.frequencyModIn <- gain * 0.03f;
66+
float lpSample = lowpass.getSample(noiseIn, 15000.0f, 0.7f, invSampleRate);
67+
let outSample = (sin(osc1.out * (1.0f + vel * 0.5f)) + lpSample * 0.6f) * gain * 0.5f * vel * outputLevel;
68+
69+
float pan = panning * 0.01f;
70+
float leftGain = 0.5f * (1.0f - pan);
71+
float rightGain = 0.5f * (1.0f + pan);
72+
out <- (outSample * leftGain, outSample * rightGain);
73+
74+
osc1.advance();
75+
76+
gain = envelope.gainOut;
77+
envelope.advance();
78+
advance();
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}

percupuff.cmajorpatch

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dsp/Envelope.cmajor",
2121
"dsp/drums/BassDrum.cmajor",
2222
"dsp/drums/Snare.cmajor",
23+
"dsp/drums/ElectricSnare.cmajor",
2324
"dsp/drums/Hihat.cmajor",
2425
"dsp/drums/Clap.cmajor",
2526
"dsp/drums/Cowbell.cmajor",
@@ -35,4 +36,4 @@
3536
"height": 533,
3637
"resizable": false
3738
}
38-
}
39+
}

0 commit comments

Comments
 (0)