Skip to content
Snippets Groups Projects
Commit 04a1d429 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Add filter

parent c9995c3f
No related branches found
No related tags found
No related merge requests found
#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();
}
......
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
......@@ -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() {
......
......@@ -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() {
......
......@@ -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() {
......@@ -85,3 +99,11 @@ 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment