diff --git a/Soft/Drum/Drum.ino b/Soft/Drum/Drum.ino index 4c9e5ae618893887171bd77c475ba979627b2697..3fcb4af7aa70924faf85b71ba1f4d4917d472596 100644 --- a/Soft/Drum/Drum.ino +++ b/Soft/Drum/Drum.ino @@ -1,5 +1,6 @@ #include <hardware/pwm.h> #include "Envelopes.h" +#include "Dsp.h" #include "Hardware.h" #include "Kick.h" #include "Snare.h" @@ -106,6 +107,7 @@ void loop() { // Update Modulation if (mode_switch.mode == 0) { + // Modulation updates for Kick kick.setFrequency(freq_pot/1023.0); // Bigger decay values make the envelope shorter kick.setVolumeDecay(0.000005 + (float)chord_pot/1023.0/3000.0); @@ -114,12 +116,16 @@ void loop() { kick.setPitchAmount(2.0 + (1023.0-(float)inv_pot)/1023.0*8.5); kick.update(); } else if (mode_switch.mode == 1) { + // Modulation updates for Snare snare.setFrequency(freq_pot/1023.0); // Bigger decay values make the envelope shorter - snare.setVolumeDecay(0.000005 + (float)chord_pot/1023.0/3000.0); - snare.setPitchDecay(0.00001 + (float)inv_pot/1023.0/800.0); + snare.setVolumeDecay(0.00005 + (float)chord_pot/1023.0/100.0); + snare.setPitchDecay(0.00001 + (float)inv_pot/1023.0/200.0); // Increase the amount of the pitch envelope if the pitch decay is longer snare.setPitchAmount(2.0 + (1023.0-(float)inv_pot)/1023.0*8.5); + float freq = 0.0+(1023.0-(float)inv_pot)*5.0; + Serial.println(freq); + snare.setLpfFrequency(freq); snare.update(); } diff --git a/Soft/Drum/Dsp.h b/Soft/Drum/Dsp.h new file mode 100644 index 0000000000000000000000000000000000000000..bb5b91373a776cb3c18080767f0ef07ee3d4f0e0 --- /dev/null +++ b/Soft/Drum/Dsp.h @@ -0,0 +1,90 @@ + +class LPF { + public: + float frequency = 100.00; + float resonance = 0.9; + + float render(float sample); + void setFrequency(float frequency); + void addFrequency(float frequency); + void setResonance(float resonance); + void reset(); + + private: + float feedback; + float buf0 = 0.0; + float buf1 = 0.0; +}; + + +float LPF::render(float sample) { + this->buf0 = this->buf0 + this->frequency * (sample - this->buf0 + this->feedback * (this->buf0 - this->buf1)); + this->buf1 = this->buf1 + this->frequency * (this->buf0 - this->buf1); + return this->buf1; +} + +void LPF::setFrequency(float frequency) { + this->frequency = 2.0 * sin(PI * frequency / 32000.0); + this->feedback = this->resonance + this->resonance / (1.0 - this->frequency); +} + +void LPF::addFrequency(float frequency) { + this->frequency += 2.0 * sin(PI * frequency / 32000.0); + this->feedback = this->resonance + this->resonance / (1.0 - this->frequency); +} + +void LPF::setResonance(float resonance) { + this->resonance = resonance; + this->feedback = this->resonance + this->resonance / (1.0 - this->frequency); +} + +void LPF::reset() { + this->buf0 = 0.0; + this->buf1 = 0.0; +} + + + +class HPF { + public: + float frequency = 100.00; + float resonance = 0.9; + + float render(float sample); + void setFrequency(float frequency); + void addFrequency(float frequency); + void setResonance(float resonance); + void reset(); + + private: + float feedback; + float buf0 = 0.0; + float buf1 = 0.0; +}; + + +float HPF::render(float sample) { + this->buf0 = this->buf0 + this->frequency * (sample - this->buf0 + this->feedback * (this->buf0 - this->buf1)); + this->buf1 = this->buf1 + this->frequency * (this->buf0 - this->buf1); + return sample - this->buf1; +} + +void HPF::setFrequency(float frequency) { + this->frequency = 2.0 * sin(PI * frequency / 32000.0); + this->feedback = this->resonance + this->resonance / (1.0 - this->frequency); +} + +void HPF::addFrequency(float frequency) { + this->frequency += 2.0 * sin(PI * frequency / 32000.0); + this->feedback = this->resonance + this->resonance / (1.0 - this->frequency); +} + +void HPF::setResonance(float resonance) { + this->resonance = resonance; + this->feedback = this->resonance + this->resonance / (1.0 - this->frequency); +} + +void HPF::reset() { + this->buf0 = 0.0; + this->buf1 = 0.0; +} \ No newline at end of file diff --git a/Soft/Drum/Envelopes.h b/Soft/Drum/Envelopes.h index f5b7b96a56d4cb6de6adddfe1f51f8c170505677..bcb015cdf7b50c87c8d75d405a755407b1b60a82 100644 --- a/Soft/Drum/Envelopes.h +++ b/Soft/Drum/Envelopes.h @@ -39,10 +39,12 @@ class EnvelopeSimpleLog { float value; float speed = 0.001; float amount = 1.0; + float snap = 0.005; void update(); void reset(); void setAmount(float speed); void setSpeed(float speed); + void setSnap(float snap); }; void EnvelopeSimpleLog::update() { @@ -50,7 +52,7 @@ void EnvelopeSimpleLog::update() { if (this->counter <= 0.0) { this->counter = 0.0; } - this->value = max(0.0, (log10(0.005 + 1.0 - this->counter)*-1.0))/2.3 * this->amount; + this->value = max(0.0, (log10(this->snap + 1.0 - this->counter)*-1.0))/2.3 * this->amount; } void EnvelopeSimpleLog::reset() { @@ -66,6 +68,13 @@ void EnvelopeSimpleLog::setAmount(float amount) { this->amount = amount; } +void EnvelopeSimpleLog::setSnap(float snap) { + this->snap = snap; +} + + + + // ---- A sigh envelope ------- class EnvelopeSigh { public: @@ -81,6 +90,7 @@ class EnvelopeSigh { void setAmount(float speed); void setSpeed(float speed); void setDelay(float speed); + void setSnap(float snap); }; void EnvelopeSigh::update() { diff --git a/Soft/Drum/Kick.h b/Soft/Drum/Kick.h index 56de9f73a3b6c435cfb451bf89566f6837430b63..ce0f895b4378204bb25a6ef6f3dad715c151ec8a 100644 --- a/Soft/Drum/Kick.h +++ b/Soft/Drum/Kick.h @@ -11,6 +11,8 @@ class Kick { EnvelopeSimpleLinear volume_envelope; EnvelopeSimpleLog pitch_envelope; EnvelopeSigh pitch_envelope_sigh; + LPF lpf; + HPF hpf; void setup(); void update(); void reset(); @@ -34,6 +36,10 @@ void Kick::setup() { } this->volume_envelope.amount = 1.0; + lpf.setFrequency(1000.0); + lpf.setResonance(0.99); + hpf.setFrequency(30.0); + hpf.setResonance(0.5); } void Kick::update() { @@ -55,7 +61,10 @@ void Kick::trigger() { } float Kick::render() { - return this->volume_envelope.value * this->wavetable[(int)this->osc_progress] / 2 + 511; + float orig_sample = this->volume_envelope.value * this->wavetable[(int)this->osc_progress]; + float filtered_sample = hpf.render(lpf.render(orig_sample)); + float sample = orig_sample*0.75 + filtered_sample*0.2; + return sample / 2 + 511; } void Kick::advanceOscillator() { diff --git a/Soft/Drum/Snare.h b/Soft/Drum/Snare.h index eb396d59c9984bd5a8298b2924b54013fb105b48..2495ee9b98900b3d9672fc27e3d6bc9970e6fa63 100644 --- a/Soft/Drum/Snare.h +++ b/Soft/Drum/Snare.h @@ -8,9 +8,11 @@ class Snare { float frequency; float base_frequency; float frequency_range; - EnvelopeSimpleLinear volume_envelope; + EnvelopeSimpleLog volume_envelope; EnvelopeSimpleLog pitch_envelope; EnvelopeSigh pitch_envelope_sigh; + LPF lpf; + HPF hpf; void setup(); void update(); void reset(); @@ -21,6 +23,8 @@ class Snare { void setPitchDecay(float speed); void setPitchAmount(float amount); void setFrequency(float frequency); + void setLpfFrequency(float frequency); + void setLpfResonance(float resonance); private: int sinetable[SAMPLES]; @@ -34,11 +38,17 @@ void Snare::setup() { this->sinetable[i] = (sin(2 * M_PI * i / SAMPLES)) * 511; } + // Fill the noise-table with noise for (int i = 0; i < SAMPLES; i++) { this->noisetable[i] = random(0, 1023); } + // Set some parameters this->volume_envelope.amount = 1.0; + this->hpf.setFrequency(200.0); + this->volume_envelope.setSnap(0.0005); + this->pitch_envelope.setSnap(0.0005); + } void Snare::update() { @@ -60,7 +70,11 @@ void Snare::trigger() { } float Snare::render() { - return this->volume_envelope.value * (this->sinetable[(int)this->osc_progress] + this->pitch_envelope.value * noisetable[(int)this->osc_progress]) / 2 + 511; + float orig_sample = this->volume_envelope.value * (this->sinetable[(int)this->osc_progress] + this->pitch_envelope.value * noisetable[(int)this->osc_progress]); + // lpf.addFrequency(pitch_envelope.value*500.0); + float filtered_sample = hpf.render(lpf.render(orig_sample)); + float sample = orig_sample/2 + filtered_sample/2; + return sample / 2 + 511; } void Snare::advanceOscillator() { @@ -84,4 +98,12 @@ void Snare::setPitchAmount(float amount) { void Snare::setFrequency(float frequency) { this->frequency = this->base_frequency + frequency * this->frequency_range; +} + +void Snare::setLpfFrequency(float frequency) { + this->lpf.setFrequency(frequency); +} + +void Snare::setLpfResonance(float resonance) { + this->lpf.setResonance(resonance); } \ No newline at end of file