-
Notifications
You must be signed in to change notification settings - Fork 0
/
bass_drum.h
166 lines (141 loc) · 4.78 KB
/
bass_drum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <cstdint>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <random>
#include "../envelopes.h"
using namespace std;
struct BassDrumSculpt {
float overdrive_, velocity_, harmonics_;
uint16_t frequency_, attack_;
uint8_t decay_;
};
struct BassDrumEnv {
float interval_, phase_, slope_, phase_end_;
uint16_t f0_;
};
class BassDrum {
public:
BassDrum(
uint16_t sample_rate)
:
sample_rate_(sample_rate),
flutter_(3)
{
rel_pos_ = 0;
length_attack_t_ = 0.0;
}
~BassDrum() {}
void set_frequency(uint16_t frequency) {
BD.frequency_ = frequency;
}
void set_overdrive(uint16_t overdrive) {
BD.overdrive_ = overdrive / 200.0f + 1.0f;
}
void set_velocity(uint16_t velocity) {
BD.velocity_ = velocity / 1000.0;
}
void set_decay(uint16_t decay) {
length_decay_ = decay * sample_rate_ / 400;
lookup_table_ = exp_env;
}
void set_attack(uint16_t attack) {
BD.attack_ = 1001 - attack;
length_attack_t_ = (1.0f/static_cast<float>(BD.attack_));
length_attack_ = length_attack_t_ * sample_rate_;
}
void set_harmonics(uint16_t harmonics) {
BD.harmonics_ = static_cast<float>(harmonics) / 1000;
}
void set_envelope(uint16_t envelope) {
ENV.f0_ = BD.frequency_ * envelope / 20; // 20 is for a kick drum range, might want to play around with this
ENV.interval_ = 0.05; // TODO: This needs tuning
ENV.slope_ = (BD.frequency_ - ENV.f0_) / ENV.interval_;
ENV.phase_end_ = -2 * M_PI * (ENV.f0_ + BD.frequency_ ) / 2 * ENV.interval_;
}
void set_start() {
rel_pos_ = 0;
running_ = true;
end_i_ = length_attack_ + length_decay_;
for (int i = 0; i < 3; ++i) {
flutter_[i] = d(gen);
}
}
int16_t Process() {
// Generate waveform sample
if (running_ == false)
return 0;
int32_t sample;
float t = static_cast<float>(rel_pos_) / sample_rate_;
sample = GenerateSample(t);
sample += GenerateHarmonics(t);
sample *= BD.velocity_;
sample *= interpolate_env();
int16_t output = Overdrive(sample, 1); // Apply distortion
rel_pos_ += 1;
if (rel_pos_ >= end_i_) {
running_ = false;
}
return output;
}
private:
float length_attack_t_;
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;
BassDrumEnv ENV;
random_device rd{};
mt19937 gen{rd()};
normal_distribution<double> d{0, 1000};
int16_t Overdrive(int32_t value, uint8_t dist_type) {
int16_t clipped_value;
int32_t overdriven_value = (value * BD.overdrive_);
float scaled_overdriven_value = overdriven_value / 32767.0;
switch (dist_type){
case 1: // SOFT clipping 2
if (overdriven_value <= -32767) {
clipped_value = -32767;
}
else if (overdriven_value >= 32767) {
clipped_value = 32767;
}
else {
clipped_value = 32767 * (scaled_overdriven_value - pow(scaled_overdriven_value,3)/3.0) * (3.0/2.0);
}
break;
case 2: // HARD clipping
clipped_value = 0.5 * (fabs(overdriven_value + 32767) - fabs(overdriven_value - 32767));
break;
}
return clipped_value;
}
int16_t GenerateSample(float t) {
int16_t sample;
if (t > ENV.interval_) {
sample = 32767 * sin(2 * M_PI * BD.frequency_ * t + ENV.phase_end_);
} else {
float func = ENV.slope_ * pow(t,2) / 2.0 + ENV.f0_ * t;
sample = 32767 * sin(2.0 * M_PI * func);
}
return sample;
}
int16_t GenerateHarmonics(float t) {
int16_t sample;
sample = 0.2 * 32767.0 * BD.harmonics_ * sin(2 * M_PI * (220.0 + flutter_[0]/125.0) * t + 0.5);
sample += 0.3 * 32767.0 * BD.harmonics_ * sin(2 * M_PI * (BD.frequency_ * 7.8 + flutter_[1]/125.0) * t + 1.2);
sample += 0.2 * 32767.0 * BD.harmonics_ * sin(2 * M_PI * (BD.frequency_ * 10.2 + flutter_[2]/125.0) * t + 2.1);
return sample;
}
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;
}
};