-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgranulator.h
345 lines (298 loc) · 7.66 KB
/
granulator.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#pragma once
#include "grain.h"
#include "crc_noise.h"
#include "params.h"
#include "quantize.h"
#define MAX_GRAINS 16
// Let's stick to 16bit samples for now
// This can be templated later
class Granulator
{
public:
Granulator() {}
~Granulator() {}
void Init(float sr, int16_t *start, size_t len, float *env, size_t env_len, bool loop, bool rev)
{
sr_ = sr;
sample_start_ = start;
len_ = len;
env_mem_ = env;
env_len_ = env_len;
Setup(loop, rev);
live_ = filled_ = false;
rng.Init();
// Scales
scales[0].Init(13, chromatic);
scales[1].Init(3, min3_octs);
scales[2].Init(3, maj3_octs);
scales[3].Init(3, fourths_octs);
scales[4].Init(3, fifths_octs);
scales[5].Init(4, min);
scales[6].Init(5, min7);
scales[7].Init(5, minM7);
scales[8].Init(4, maj);
scales[9].Init(5, maj7);
scales[10].Init(5, majM7);
}
void Stop()
{
stop_ = true;
filled_ = false;
}
void Start()
{
stop_ = false;
}
void ReStart()
{
sample_pos_.Reset();
stop_ = false;
}
void Reset(int16_t *start, size_t len, bool loop, bool rev)
{
sample_start_ = start;
len_ = len;
Setup(loop, rev);
live_ = filled_ = false;
}
/*
* This live looping is very naive, shoehorning the record buffer into a Phasor/SamplePhasor
* instead of something specifically designed for it.
* A proper record buffer should be in something like a delay line or circular buffer
* where the start and end points are moved and wrapped accordingly as the write head moves.
* Maybe its possible to retrofit that into the existing grain code with a little more thought and effort
*/
void Live(int16_t *start, size_t len)
{
sample_start_ = start;
len_ = len;
record_buf_ = sample_start_;
write_pos_ = 0;
Setup(false, false);
live_ = true;
filled_ = false;
}
// don't allow this in live mode
void ToggleSampleLoop()
{
if (!live_) {
sample_loop_ = sample_loop_ ? false : true;
sample_pos_.ToggleLoop();
if (sample_loop_) Start();
}
}
void SetGrainDuration(float dur)
{
grain_dur_ = dur;
}
void SetGrainPitch(float pitch)
{
grain_pitch_ = pitch;
}
void SetPitchDist(float dist)
{
pitch_dist_ = dist;
}
void SetScale(float scale)
{
scale_ = (int)roundf(scale * NUM_SCALES);
}
// don't allow this in live mode
void SetScanRate(float rate)
{
if (!live_) {
sample_pos_.SetPitch(rate);
}
}
// dist is % of the sample length
// scatter_dist_ is stored as number of samples
void SetScatterDist(float dist)
{
scatter_dist_ = dist * len_;
}
void ChangeEnv(float *env)
{
env_mem_ = env;
}
void Dispatch(size_t sample_pos)
{
float rand;
float pitch = grain_pitch_;
float pan = pan_;
for (size_t i = 0; i < MAX_GRAINS; i++) {
if (silo[i].IsDone()) {
if (random_pitch_) {
rand = rng.Process();
if (scale_ == 0) {
pitch = fminf(4.0f, fmaxf(0.25f, pitch * (1.0f + (rand * pitch_dist_))));
} else {
pitch = fminf(4.0f, fmaxf(0.25f, pitch * (1.0f + scales[scale_ - 1].Quantize(rand * pitch_dist_))));
}
}
if (random_pan_) {
rand = rng.Process();
pan = fminf(1.0f, fmaxf(0.0f, pan + (0.5f * rand * pan_dist_)));
}
silo[i].Dispatch(sample_pos, grain_dur_, env_mem_, pitch, pan, reverse_grain_);
return;
}
}
}
// density is number of samples until a new grain is dispatched
void SetDensity(int32_t density)
{
density_ = density;
}
// don't allow this in live mode
void ToggleScanReverse()
{
if (!live_) {
sample_pos_.ToggleReverse();
}
}
void ToggleGrainReverse()
{
reverse_grain_ = reverse_grain_ ? false : true;
}
void ToggleScatter()
{
scatter_grain_ = scatter_grain_ ? false : true;
}
void ToggleFreeze()
{
freeze_ = freeze_ ? false : true;
scatter_grain_ = freeze_;
}
void ToggleRandomPitch()
{
random_pitch_ = random_pitch_ ? false : true;
}
void ToggleRandomDensity()
{
random_density_ = random_density_ ? false : true;
}
// All pos are in the range 0 to 1
// They will be multiplied by len_ to get actual size_t sample position
// don't allow this in live mode
void SetSampleStart(float pos)
{
if (!live_) {
sample_pos_.SetStartPos(pos);
}
}
// don't allow this in live mode
void SetSampleEnd(float pos)
{
if (!live_) {
sample_pos_.SetEndPos(pos);
}
}
void SetPan(float pan)
{
pan_ = pan;
}
void SetPanDist(float pan_dist)
{
pan_dist_ = pan_dist;
}
bool IsLive() {
return live_;
}
void ToggleRandomPan()
{
random_pan_ = !random_pan_;
}
sample_t Process(int16_t input)
{
sample_t s;
sample_t out = {0, 0};
float rand;
int32_t offset;
size_t pos = 0;
bool eot = false;
if (stop_) return {0.0f, 0.0f};
if (freeze_)
{
pos = sample_pos_.GetPos();
} else {
if (live_) { record_buf_[write_pos_] = input; }
pos = sample_pos_.Process(&eot);
write_pos_ = pos;
if (eot) { filled_ = true; }
}
if (density_count_-- < 0)
{
if (random_density_) {
rand = rng.Process();
density_count_ = rand * density_;
} else {
density_count_ = density_;
}
if (scatter_grain_) {
rand = rng.Process();
offset = rand * scatter_dist_;
// Do the pragma dance - we take care of wrap around
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
offset += pos;
if (offset < 0) {
offset += len_;
} else if (offset > len_) {
offset -= len_;
}
#pragma GCC diagnostic pop
pos = (size_t)offset;
}
if (!live_ || filled_) { Dispatch(pos); }
}
for (size_t i = 0; i < MAX_GRAINS; i++) {
if (!silo[i].IsDone()) {
s = silo[i].Process();
out.l += s.l;
out.r += s.r;
}
}
if (eot && (sample_loop_ == false)) Stop();
// clamp?
out.l = fminf(1.0f, fmaxf(-1.0f, out.l));
out.r = fminf(1.0f, fmaxf(-1.0f, out.r));
return out;
}
private:
void Setup(bool loop, bool rev)
{
sample_pos_.Init(sr_, len_);
sample_loop_ = loop;
sample_pos_.SetLoop(sample_loop_);
density_count_ = -1;
SetGrainDuration(DEFAULT_GRAIN_DUR);
SetGrainPitch(DEFAULT_GRAIN_PITCH);
SetScanRate(DEFAULT_SCAN_RATE);
SetDensity(sr_/DEFAULT_GRAIN_DENS);
SetScatterDist(DEFAULT_SCATTER_DIST);
SetPitchDist(DEFAULT_PITCH_DIST);
SetScale(DEFAULT_SCALE);
SetPan(DEFAULT_PAN);
SetPanDist(DEFAULT_PAN_DIST);
reverse_grain_ = rev;
sample_pos_.SetReverse(rev);
stop_ = random_pitch_ = scatter_grain_ = random_density_ = random_pan_ = false;
for (size_t i = 0; i < MAX_GRAINS; i++) {
silo[i].Init(sr_, sample_start_, len_, DEFAULT_GRAIN_VOL, env_mem_, env_len_);
}
}
Grain<int16_t> silo[MAX_GRAINS];
Phasor sample_pos_;
int16_t *sample_start_;
size_t len_, env_len_, scatter_dist_, write_pos_, scale_;
int32_t density_, density_count_;
float sr_, grain_dur_, grain_pitch_, pitch_dist_, pan_, pan_dist_;
float *env_mem_;
bool sample_loop_, stop_, reverse_grain_, scatter_grain_, \
random_pitch_, random_density_, freeze_, random_pan_;
daisysp::crc_noise rng;
Scale scales[NUM_SCALES];
// used for live record buffer only
int16_t *record_buf_;
bool live_, filled_;
};