-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.cpp
398 lines (320 loc) · 12.2 KB
/
sampler.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#pragma once
#include <memory>
#include "hanning.hpp"
#include "sndfile.hh"
#include <fftw3.h>
#include <map>
#include <samplerate.h>
// #include <matplot/matplot.h>
// using namespace matplot;
class Sampler;
typedef struct {
SNDFILE *file;
SF_INFO info;
std::vector<std::shared_ptr<Sampler>> voices;
std::vector<int> indices;
// std::vector<float> samples;
std::vector<int> notes;
int max;
int index;
} callback_data_s;
class Sampler {
private:
SNDFILE *file;
SF_INFO info;
int sig_len, analysis_hopsize, synthesis_hopsize;
int N;
int window_size;
int hop_size_div;
float pitch_ratio;
fftwf_plan p;
fftwf_plan pi;
float *fft_in;
fftwf_complex *fft_out;
public:
int index = 0;
int current_note = 0;
bool running = false;
bool stopping = false;
int it;
bool PHI_UNWRAP;
bool GONGO;
bool INSTANTANEOUS;
int fs;
std::vector<float> samples;
std::map<int, std::vector<float> *> key_samples;
int frames_size() { return key_samples[current_note]->size(); }
void print_stats() {
std::cout << "samples size: " << samples.size();
std::cout << std::endl;
// std::cout << "window[0]: " << window[0] << std::endl;
}
Sampler(const std::string &filename, int N = 1024, int window_size = 1024,
int hop_size_div = 4)
: it(it), N(N), window_size(window_size), hop_size_div(hop_size_div) {
/* Open the soundfile */
file = sf_open(filename.c_str(), SFM_READ, &info);
samples.resize(info.frames * info.channels);
sf_readf_float(file, &samples[0], info.frames);
fs = info.samplerate;
sig_len = samples.size();
p = fftwf_plan_dft_r2c_1d(N, fft_in, fft_out, FFTW_ESTIMATE);
pi = fftwf_plan_dft_c2r_1d(N, fft_out, fft_in, FFTW_ESTIMATE);
pitch_ratio = 1.0f;
/* Close the soundfile */
sf_close(file);
}
~Sampler() {
fftwf_destroy_plan(p);
fftwf_destroy_plan(pi);
free(file);
}
void cleanup() {}
std::vector<float> get_samples(int frameCount, float amp) {
std::vector<float> out(frameCount, 0.0f);
if (current_note != 0 && key_samples.count(current_note)) {
int left = (key_samples[current_note]->size() - 1) - index;
if (left <= frameCount) {
// final buffer
for (int i = 0; i < left; i++) {
out[i] += key_samples[current_note]->at(index + i) * amp;
}
// index += left;
this->running = false;
this->stopping = false;
} else {
for (int i = 0; i < frameCount; i++) {
out[i] += (key_samples[current_note]->at(index + i) * amp);
}
index += frameCount;
}
} else {
current_note = 0;
running = false;
}
return out;
}
float note_to_freq(int note) {
float a = 440.0f; // frequency of A (conmon value is 440Hz)
// float d = 587.33; // freq of D note
// return a * 2^((note−69)/12);
return a * pow(2.0f, ((note - 69.0f) / 12.0f));
}
void forward_fft(float *time_data, std::complex<float> *freq_data) {
fft_out = reinterpret_cast<fftwf_complex *>(freq_data);
fft_in = time_data;
fftwf_execute_dft_r2c(p, fft_in, fft_out);
}
void ifft(float *time_data, std::complex<float> *freq_data) {
fft_out = reinterpret_cast<fftwf_complex *>(freq_data);
fft_in = time_data;
fftwf_execute_dft_r2c(pi, fft_in, fft_out);
}
void resample(float *data_in, float *data_out, int input_size,
int output_size, float pitch_ratio) {
SRC_DATA src_data;
src_data.data_in = data_in;
src_data.data_out = data_out;
src_data.input_frames = input_size;
src_data.output_frames = output_size;
src_data.src_ratio = pitch_ratio;
src_simple(&src_data, 2, 2);
}
// Normalize to [0,2PI):
// float normalize_phase(float x)
// {
// x = fmod(x, 2*PI);
// if (x < 0)
// x += 2*PI;
// return x;
// };
// // unwrap phase [-PI,PI]
// float unwrap(float previous_angle, float new_angle) {
// float d = new_angle - previous_angle;
// d = d > M_PI ? d - 2 * M_PI : (d < -M_PI ? d + 2 * M_PI : d);
// return previous_angle + d;
// }
void calculate_stft_windows() {
std::vector<float> window = hanning(window_size, 0);
int read_ptr = 0;
while (read_ptr <= samples.size() - read_ptr) {
std::vector<float> buffer(N, 0.0f);
std::vector<float> out(N, 0.0f);
std::vector<float> phi(N, 0.0f);
std::vector<std::complex<float>> fft_buffer(N / 2 + 1, 0.0f);
std::vector<std::complex<float>> resynth_buffer(N / 2 + 1, 0.0f);
// read M (window size) samples into buffer
// when N > window_size, the default 0.0 act as zero padding
for (int i = 0; i < window_size; i++) {
// multiply samples by analysis window w[m] of length window_size
buffer[i] = window[i] * samples[read_ptr + i];
}
forward_fft((float *)&buffer[0], (std::complex<float> *)&fft_buffer[0]);
read_ptr += analysis_hopsize;
}
}
void calculate_sample_stft(callback_data_s &data, int note) {
data.index = 0;
float freq = note_to_freq(note);
float div = 440.0;
float r = div / freq;
pitch_ratio = r;
std::cout << "calculating sample for note: " << note << std::endl;
std::vector<float> out_samples(samples.size() * 2, 0.0f);
std::vector<float> *resampled =
new std::vector<float>(samples.size(), 0.0f);
std::vector<float> window = hanning(window_size, 0);
synthesis_hopsize = window_size / hop_size_div;
analysis_hopsize = synthesis_hopsize;
int read_ptr = 0;
int write_ptr = 0;
while (read_ptr <= sig_len - window_size) {
// use N instead of window size, so that
// when N > window_size, the default 0.0 act as zero padding
std::vector<float> buffer(N, 0.0f);
std::vector<float> out(N, 0.0f);
std::vector<float> phi(N, 0.0f);
std::vector<std::complex<float>> fft_buffer(N / 2 + 1, 0.0f);
std::vector<std::complex<float>> resynth_buffer(N / 2 + 1, 0.0f);
// read window size samples into buffer
for (int i = 0; i < window_size; i++) {
// multiply samples by analysis window w[m] of length window_size
if (read_ptr + i <= samples.size())
buffer[i] = window[i] * samples[read_ptr + i];
else {
buffer[i] = 0.0f;
std::cout << "not enough samples for buffer, i: " << read_ptr + i
<< " size: " << samples.size() << std::endl;
}
}
forward_fft((float *)&buffer[0], (std::complex<float> *)&fft_buffer[0]);
for (int i = 0; i < fft_buffer.size(); i++) {
resynth_buffer[i] = fft_buffer[i];
}
ifft((float *)&out[0], (std::complex<float> *)&resynth_buffer[0]);
for (int i = 0; i < window_size; i++) {
if (write_ptr + i <= out_samples.size())
out_samples[write_ptr + i] += window[i] * (out[i] / N);
else {
std::cout << "not enough space in out_samples, i: " << write_ptr + i
<< " size: " << out_samples.size() << std::endl;
}
}
read_ptr += analysis_hopsize;
write_ptr += synthesis_hopsize;
}
resample((float *)&out_samples[0], (float *)&resampled->at(0),
out_samples.size() / 2, resampled->size() / 2, pitch_ratio);
key_samples.insert(std::pair<int, std::vector<float> *>(note, resampled));
}
// calculate sample with phase vocoder pitch shifting
void calculate_sample_pitch_shift(callback_data_s &data, int note) {
data.index = 0;
data.max = samples.size() * 2;
float freq = note_to_freq(note);
float div = 440.0;
float r = div / freq;
pitch_ratio = r;
std::cout << "calculating sample for note: " << note << std::endl;
std::vector<float> out_samples(samples.size() / pitch_ratio * 2, 0.0f);
std::vector<float> *resampled =
new std::vector<float>(samples.size(), 0.0f);
std::vector<float> window = hanning(window_size, 0);
synthesis_hopsize = window_size / hop_size_div;
analysis_hopsize = synthesis_hopsize * pitch_ratio;
int read_ptr = 0;
int write_ptr = 0;
while (read_ptr <= sig_len - window_size) {
// use N instead of window size, so that
// when N > window_size, the default 0.0 act as zero padding
std::vector<float> buffer(N, 0.0f);
std::vector<float> out(N, 0.0f);
std::vector<float> phi(N, 0.0f);
std::vector<std::complex<float>> fft_buffer(N / 2 + 1, 0.0f);
std::vector<std::complex<float>> resynth_buffer(N / 2 + 1, 0.0f);
// read window size samples into buffer
for (int i = 0; i < window_size; i++) {
// multiply samples by analysis window w[m] of length window_size
if (read_ptr + i <= samples.size())
buffer[i] = window[i] * samples[read_ptr + i];
else {
buffer[i] = 0.0f;
std::cout << "not enough samples for buffer, i: " << read_ptr + i
<< " size: " << samples.size() << std::endl;
}
}
forward_fft((float *)&buffer[0], (std::complex<float> *)&fft_buffer[0]);
for (int i = 0; i < fft_buffer.size(); i++) {
// the instantaneous phase or local phase (or simply, phase!)
// is calculated as arg(c(t)) where c(t) is a complex number and
// t is a time increment (like "i" in a for loop).
float phase = std::arg(fft_buffer[i]);
float previous_phase = 0.0f;
if (i > 0) {
previous_phase = std::arg(fft_buffer[i - 1]);
}
// the instantaneous frequency is the "temporal rate of change" of the
// instantaneous phase. When the phase is constrained to the interval of
// (-pi, pi) or (0, 2pi), which is the principal value (?), it is called
// the "wrapped phase". Otherwise, it is called the "unwrapped phase".
float amplitude = abs(fft_buffer[i]);
// float freq = 2.0 * M_PI * (float)i / N;
float freq = i * fs / N;
float target = previous_phase + (freq * analysis_hopsize);
float deviation = phase - target;
float increment = (freq * analysis_hopsize) + deviation;
// fi is the instantaneous frequency: a more accurate frequency
// measurement
if (INSTANTANEOUS) {
float fi = increment / (2 * PI * analysis_hopsize) * fs;
freq = fi;
}
float delta_phi =
(freq * analysis_hopsize) +
std::arg(phase - previous_phase - (freq * analysis_hopsize));
if (GONGO) {
delta_phi = (freq * analysis_hopsize) + phase - previous_phase -
(freq * analysis_hopsize);
}
if (PHI_UNWRAP) {
phi[i] = std::arg(delta_phi * synthesis_hopsize);
} else {
phi[i] = delta_phi * synthesis_hopsize;
}
// phi[i] = previous_phase + increment;
resynth_buffer[i].real(amplitude * cos(phi[i]));
resynth_buffer[i].imag(amplitude * sin(phi[i]));
}
ifft((float *)&out[0], (std::complex<float> *)&resynth_buffer[0]);
for (int i = 0; i < window_size; i++) {
if (write_ptr + i <= out_samples.size())
out_samples[write_ptr + i] += window[i] * (out[i] / window_size);
else {
std::cout << "not enough space in out_samples, i: " << write_ptr + i
<< " size: " << out_samples.size() << std::endl;
}
}
read_ptr += analysis_hopsize;
write_ptr += synthesis_hopsize;
}
resample((float *)&out_samples[0], (float *)&resampled->at(0),
out_samples.size() / 2, resampled->size() / 2, pitch_ratio);
key_samples.insert(std::pair<int, std::vector<float> *>(note, resampled));
}
void calculate_sample(callback_data_s &data, int note) {
data.index = 0;
data.max = samples.size();
std::cout << "calculating sample for note: " << note << std::endl;
std::vector<float> out_samples(samples.size() / pitch_ratio, 0.0f);
float freq = note_to_freq(note);
float div = 440.0f;
float r = div / freq;
pitch_ratio = r;
std::vector<float> *resampled =
new std::vector<float>(samples.size() * pitch_ratio, 0.0f);
out_samples = samples;
resample((float *)&out_samples[0], (float *)&resampled->at(0),
out_samples.size() / 2, resampled->size() / 2, pitch_ratio);
key_samples.insert(std::pair<int, std::vector<float> *>(note, resampled));
}
};