Select Git revision
MAMI-INPUT.gto
HiHat.h 2.96 KiB
class HiHat {
public:
float frequency;
float base_frequency;
float frequency_range;
float wavetable_start = 0;
int wavetable_window_size = 1048;
EnvelopeSimpleLog volume_envelope;
HPF hpf;
float gain = 1.0;
void setup();
void update();
void reset();
void trigger();
float render();
void advanceOscillator();
void setVolumeDecay(float speed);
void setFrequency(float frequency);
void setWavetableStart(float start);
void setHpfFrequency(float frequency);
private:
// Generate the wavetable using the python script. Make sure the lenght is updated wherever it occures in this script
#include "HihatWavetable.h"
float wavetable_progress = 0.0;
};
void HiHat::setup() {
// Set some parameters
this->volume_envelope.amount = 1.0;
this->hpf.setFrequency(700.0);
this->volume_envelope.setSnap(0.0005);
}
void HiHat::update() {
this->volume_envelope.update();
}
void HiHat::reset() {
this->volume_envelope.reset();
}
void HiHat::trigger() {
this->reset();
this->setWavetableStart(random(this->wavetable_start-0.2, this->wavetable_start+0.2));
}
float HiHat::render() {
float orig_sample = this->volume_envelope.value * this->wavetable[min(104728-1, max(0, (int)this->wavetable_progress))];
float filtered_sample = hpf.render(orig_sample);
float sample = (orig_sample/8 + filtered_sample/2) * this->gain;
return sample / 2 + 511;
}
void HiHat::advanceOscillator() {
this->wavetable_progress += this->frequency;
// Ensure the wavetable progress is within bounds
if (this->wavetable_progress > 104728-1) {
this->wavetable_progress = this->wavetable_start;
}
// Restart the wavetable if we are at the end of the table
if (this->wavetable_progress > (this->wavetable_start + this->wavetable_window_size)) {
this->wavetable_progress = this->wavetable_start;
this->setWavetableStart(random(this->wavetable_start-0.2, this->wavetable_start+0.2));
}