Skip to content

Commit

Permalink
Merge pull request #4 from mjoes/parametrize-hihat
Browse files Browse the repository at this point in the history
Parametrize hihat
  • Loading branch information
mjoes committed Mar 1, 2024
2 parents 2233d0e + 438c165 commit 1213b54
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
output.*
drumbadum.o
drumbadum
drumbadum
*.png
38 changes: 19 additions & 19 deletions drumbadum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ enum {ARG_NAME,ARG_DUR,ARG_NARGS};

int main(int argc, char** argv) {
// Define parameters for the waveform
uint16_t duration = atoi(argv[ARG_DUR]);
uint16_t sample_rate = 48000;
const uint16_t duration = atoi(argv[ARG_DUR]);
const uint16_t sample_rate = 48000;
uint32_t num_samples = duration * sample_rate; // Number of samples (assuming 1 second at 48kHz)
int16_t samples[num_samples] = {0};
bool t_BD;
Expand All @@ -24,7 +24,7 @@ int main(int argc, char** argv) {
uint32_t trig_SD[2] = {20000, 70000}; // Dummy triggers sample nr

// Initialize and define BassDrum & HiHat processor
HiHat hi_hat;
HiHat hi_hat(sample_rate);
BassDrum bass_drum(sample_rate);
SnareDrum snare_drum;

Expand All @@ -47,29 +47,29 @@ int main(int argc, char** argv) {
}
}
// Generate waveform sample
if (t_BD == 1) {
// bass_drum.Init();
bass_drum.set_frequency(40);
bass_drum.set_envelope(100); // range 1-1000
bass_drum.set_overdrive(10); // range 1-1000
bass_drum.set_harmonics(200); // range 1-1000
bass_drum.set_velocity(1000); // range 1-1000
bass_drum.set_decay(800); // range 1-1000
bass_drum.set_attack(950); // range 1-1000
bass_drum.set_start();
}
// if (t_HH == 1) {
// hi_hat.Init(sample_rate);
// hi_hat.set_decay(decay*10);
// hi_hat.set_start(i);
// if (t_BD == 1) {
// bass_drum.set_frequency(40);
// bass_drum.set_envelope(100); // range 1-1000
// bass_drum.set_overdrive(10); // range 1-1000
// bass_drum.set_harmonics(200); // range 1-1000
// bass_drum.set_velocity(1000); // range 1-1000
// bass_drum.set_decay(800); // range 1-1000
// bass_drum.set_attack(950); // range 1-1000
// bass_drum.set_start();
// }
if (t_HH == 1) {
hi_hat.set_decay(200,0);
hi_hat.set_frequency(5000);
hi_hat.set_bandwidth(1000);
hi_hat.set_start();
}
// if (t_SD == 1) {
// snare_drum.Init(sample_rate);
// snare_drum.set_frequency(frequency*2);
// snare_drum.set_decay(decay);
// snare_drum.set_start(i);
// }
samples[i] = (bass_drum.Process() + hi_hat.Process(i) + snare_drum.Process(i))/3;
samples[i] = (bass_drum.Process() + hi_hat.Process() + snare_drum.Process(i))/3;

t_HH = 0;
t_BD = 0;
Expand Down
2 changes: 1 addition & 1 deletion drums/bass_drum.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class BassDrum {
uint32_t end_i_;
uint32_t length_decay_;
uint16_t length_attack_;
uint16_t sample_rate_;
const uint16_t sample_rate_;
bool running_;
vector<int16_t> flutter_;
BassDrumSculpt BD;
Expand Down
137 changes: 81 additions & 56 deletions drums/hi_hat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,92 @@
#include <cmath>
#include <stdio.h>
#include <random>
#include <functional>
#include "../envelopes.h"

using namespace std;

class HiHat {
public:
HiHat() : rd(), gen(rd()), dis(-32767, 32767) {}
HiHat(
uint16_t sample_rate)
:
sample_rate_(sample_rate),
rd(),
gen(rd()),
dis(-32767, 32767)
{
rel_pos_ = 0;
}
~HiHat() {}

void Init(
uint16_t sample_rate,
size_t rel_pos = 0,
float out = 0.0f,
float seg_tmp = 0.0f,
uint8_t segment = 1)
{
sample_rate_ = sample_rate;
rel_pos_ = rel_pos;
out_ = out;
seg_tmp_ = seg_tmp;
segment_ = segment;
void set_decay(uint16_t decay, bool decay_type = 0) {
decay_type_ = decay_type;
length_decay_ = decay * sample_rate_ / 500;
if (decay_type_ == 0) {
lookup_table_ = exp_env;
} else {
lookup_table_ = log_env;
}
}

void set_frequency(uint16_t frequency) {
frequency_ = frequency;
phi_ = 2 * cos(2 * M_PI * frequency_ / sample_rate_);
}

void set_decay(uint16_t decay) {
decay_ = decay;
void set_bandwidth(uint16_t bandwidth) {
bandwidth_ = bandwidth;
lambda_ = 1 / tan(M_PI * bandwidth_ / sample_rate_);
}

void set_start(size_t start) {
void set_start() {
rel_pos_ = 0;
start_i_ = start;
end_i_ = start + lengthHit();
running_ = true;
end_i_ = length_decay_;
a0 = 1 / (1 + lambda_);
b1 = - lambda_ * phi_ * a0;
b2 = a0 * (lambda_ - 1);
}

int16_t Process(size_t it) {
int16_t Process() {
// Generate waveform sample
if (it > start_i_ && it < end_i_) {
int16_t sample = GenerateSample();
float env = GenerateEnv(rel_pos_);
int16_t out_val = static_cast<int16_t>(sample * env);
rel_pos_ += 1;
if (running_ == false)
return 0;

// Clip sample if necessary
return out_val;
int32_t sample;
sample = bp_filter_2(GenerateSample());
sample *= interpolate_env();
int16_t output = sample;

rel_pos_ += 1;
if (rel_pos_ >= end_i_) {
running_ = false;
}
return 0;
return output;
}

private:
uint16_t frequency_;
float phase_;
uint8_t cross_fade_;
float lambda_;
float phi_;
float a0;
float b1;
float b2;
uint32_t rel_pos_;
uint32_t end_i_;
uint32_t length_decay_;
uint16_t decay_;
uint16_t sample_rate_;
size_t rel_pos_;
size_t start_i_;
size_t end_i_;
float out_;
float seg_tmp_;
uint8_t segment_;
uint16_t bandwidth_;
uint16_t frequency_;
const uint16_t sample_rate_;
int32_t x_n_1 = 0;
int32_t y_n_1 = 0;
int32_t x_n_2 = 0;
int32_t y_n_2 = 0;
bool running_;
bool decay_type_;
const uint16_t* lookup_table_;
vector<int16_t> flutter_;

random_device rd;
mt19937 gen;
Expand All @@ -68,29 +96,26 @@ class HiHat {
int32_t GenerateSample() {
// Replace this with your own waveform generation logic
int32_t sample = dis(gen);

return sample;
}

float GenerateEnv(size_t rel_pos_env) {
float rel_pos_tmp = static_cast<float>(rel_pos_env)/sample_rate_;
switch (segment_) {
case 1:
out_ = 1.0f * exp(-decay_ * (rel_pos_tmp-seg_tmp_));
if (out_ <= 1e-4) {
segment_ = 2;
}
break;
case 2:
out_ = 0.0f;
break;
}

return out_;
int32_t bp_filter_2(int32_t x_n) {
int32_t filtered = a0 * x_n - a0 * x_n_2 - b1 * y_n_1 - b2 * y_n_2;
y_n_2 = y_n_1;
y_n_1 = filtered;
x_n_2 = x_n_1;
x_n_1 = x_n;
return filtered;
}

uint16_t lengthHit() {
uint16_t segment_1 = (static_cast<double>(log(1e-4)) / -decay_) * sample_rate_;
uint16_t total = (segment_1) * 1.1;
return total;
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;
}
};

135 changes: 135 additions & 0 deletions envelopes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const uint16_t log_env[] = {
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65535, 65535, 65535,
65535, 65534, 65534, 65534,
65533, 65532, 65531, 65529,
65527, 65523, 65518, 65511,
65501, 65487, 65467, 65440,
65401, 65347, 65272, 65168,
65022, 64819, 64538, 64149,
63610, 62865, 61836, 60418,
58465, 55780, 52093, 47036,
40108, 30629, 17677, 0,
0,
};

const uint16_t exp_env[] = {
65535, 62568, 59736, 57032,
54450, 51986, 49632, 47386,
45241, 43193, 41238, 39371,
37589, 35887, 34263, 32712,
31231, 29817, 28467, 27179,
25948, 24774, 23652, 22582,
21560, 20584, 19652, 18762,
17913, 17102, 16328, 15589,
14883, 14209, 13566, 12952,
12366, 11806, 11272, 10761,
10274, 9809, 9365, 8941,
8536, 8150, 7781, 7429,
7093, 6772, 6465, 6172,
5893, 5626, 5372, 5128,
4896, 4675, 4463, 4261,
4068, 3884, 3708, 3540,
3380, 3227, 3081, 2941,
2808, 2681, 2560, 2444,
2333, 2228, 2127, 2031,
1939, 1851, 1767, 1687,
1611, 1538, 1468, 1402,
1338, 1278, 1220, 1165,
1112, 1062, 1014, 968,
924, 882, 842, 804,
768, 733, 700, 668,
638, 609, 581, 555,
530, 506, 483, 461,
440, 420, 401, 383,
366, 349, 333, 318,
304, 290, 277, 264,
253, 241, 230, 220,
210, 200, 191, 183,
174, 166, 159, 152,
145, 138, 132, 126,
120, 115, 110, 105,
100, 95, 91, 87,
83, 79, 76, 72,
69, 66, 63, 60,
57, 55, 52, 50,
48, 45, 43, 41,
40, 38, 36, 34,
33, 31, 30, 29,
27, 26, 25, 24,
23, 22, 21, 20,
19, 18, 17, 16,
16, 15, 14, 14,
13, 12, 12, 11,
11, 10, 10, 9,
9, 9, 8, 8,
7, 7, 7, 7,
6, 6, 6, 5,
5, 5, 5, 4,
4, 4, 4, 4,
4, 3, 3, 3,
3, 3, 3, 3,
2, 2, 2, 2,
2, 2, 2, 2,
2, 2, 2, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 0,
0
};
Loading

0 comments on commit 1213b54

Please sign in to comment.