Skip to content

Commit

Permalink
Bassdrum: Use lookup table for envelope
Browse files Browse the repository at this point in the history
  • Loading branch information
mjoes committed Mar 13, 2024
1 parent 97a8b3b commit 32c7904
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions drums/bass_drum.h
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <algorithm>
#include <random>
#include "../envelopes.h"

using namespace std;

Expand Down Expand Up @@ -44,8 +45,8 @@ class BassDrum {
}

void set_decay(uint16_t decay) {
BD.decay_ = 52-(decay/20+1);
length_decay_ = static_cast<float>(log(1e-4)) / -BD.decay_ * sample_rate_;
length_decay_ = decay * sample_rate_ / 400;
lookup_table_ = exp_env;
}

void set_attack(uint16_t attack) {
Expand Down Expand Up @@ -86,7 +87,7 @@ class BassDrum {
sample = GenerateSample(t);
sample += GenerateHarmonics(t);
sample *= BD.velocity_;
sample *= GenerateEnv(t);
sample *= interpolate_env();
int16_t output = Overdrive(sample, 1); // Apply distortion

rel_pos_ += 1;
Expand All @@ -101,6 +102,7 @@ class BassDrum {
uint32_t rel_pos_, end_i_, length_decay_;
uint16_t length_attack_;
const uint16_t sample_rate_;
const uint16_t* lookup_table_;
vector<int16_t> flutter_;
bool running_;
BassDrumSculpt BD;
Expand Down Expand Up @@ -153,13 +155,12 @@ class BassDrum {
return sample;
}

float GenerateEnv(float t) {
float out_;
if ( rel_pos_ >= length_attack_ ) {
out_ = 1.0f * exp(-BD.decay_ * (t-length_attack_t_));
} else {
out_ = BD.attack_ * t;
}
return out_;
float interpolate_env(){
float pos = static_cast<float>(rel_pos_) / length_decay_ * 256.0;
float frac = pos - int(pos);
uint16_t a = lookup_table_[int(pos)];
uint16_t b = lookup_table_[int(pos) + 1];
uint16_t output = a + frac * (b - a);
return output / 65535.0;
}
};

0 comments on commit 32c7904

Please sign in to comment.