Merge commit 'origin/caching'
[calf.git] / src / monosynth.cpp
blobb4d8a1f0e416e340e6d75d0f2c77dc66c020b4a4
1 /* Calf DSP Library
2 * Example audio modules - monosynth
4 * Copyright (C) 2001-2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #include <assert.h>
22 #include <memory.h>
23 #include <config.h>
24 #include <complex>
25 #if USE_JACK
26 #include <jack/jack.h>
27 #endif
28 #include <calf/giface.h>
29 #include <calf/modules_synths.h>
31 using namespace dsp;
32 using namespace calf_plugins;
33 using namespace std;
35 float silence[4097];
37 void monosynth_audio_module::activate() {
38 monosynth_audio_module::generate_waves();
39 running = false;
40 output_pos = 0;
41 queue_note_on = -1;
42 stop_count = 0;
43 pitchbend = 1.f;
44 filter.reset();
45 filter2.reset();
46 stack.clear();
49 waveform_family<MONOSYNTH_WAVE_BITS> *monosynth_audio_module::waves;
51 void monosynth_audio_module::generate_waves()
53 float data[1 << MONOSYNTH_WAVE_BITS];
54 bandlimiter<MONOSYNTH_WAVE_BITS> bl;
56 if (waves)
57 return;
59 static waveform_family<MONOSYNTH_WAVE_BITS> waves_data[wave_count];
60 waves = waves_data;
62 enum { S = 1 << MONOSYNTH_WAVE_BITS, HS = S / 2, QS = S / 4, QS3 = 3 * QS };
63 float iQS = 1.0 / QS;
66 // yes these waves don't have really perfect 1/x spectrum because of aliasing
67 // (so what?)
68 for (int i = 0 ; i < HS; i++)
69 data[i] = (float)(i * 1.0 / HS),
70 data[i + HS] = (float)(i * 1.0 / HS - 1.0f);
71 waves[wave_saw].make(bl, data);
73 for (int i = 0 ; i < S; i++)
74 data[i] = (float)(i < HS ? -1.f : 1.f);
75 waves[wave_sqr].make(bl, data);
77 for (int i = 0 ; i < S; i++)
78 data[i] = (float)(i < (64 * S / 2048)? -1.f : 1.f);
79 waves[wave_pulse].make(bl, data);
81 // XXXKF sure this is a waste of space, this will be fixed some day by better bandlimiter
82 for (int i = 0 ; i < S; i++)
83 data[i] = (float)sin(i * M_PI / HS);
84 waves[wave_sine].make(bl, data);
86 for (int i = 0 ; i < QS; i++) {
87 data[i] = i * iQS,
88 data[i + QS] = 1 - i * iQS,
89 data[i + HS] = - i * iQS,
90 data[i + QS3] = -1 + i * iQS;
92 waves[wave_triangle].make(bl, data);
94 for (int i = 0, j = 1; i < S; i++) {
95 data[i] = -1 + j * 1.0 / HS;
96 if (i == j)
97 j *= 2;
99 waves[wave_varistep].make(bl, data);
101 for (int i = 0; i < S; i++) {
102 data[i] = (min(1.f, (float)(i / 64.f))) * (1.0 - i * 1.0 / S) * (-1 + fmod (i * i / 262144.0, 2.0));
104 waves[wave_skewsaw].make(bl, data);
105 for (int i = 0; i < S; i++) {
106 data[i] = (min(1.f, (float)(i / 64.f))) * (1.0 - i * 1.0 / S) * (fmod (i * i / 262144.0, 2.0) < 1.0 ? -1.0 : +1.0);
108 waves[wave_skewsqr].make(bl, data);
110 for (int i = 0; i < S; i++) {
111 if (i < QS3) {
112 float p = i * 1.0 / QS3;
113 data[i] = sin(M_PI * p * p * p);
114 } else {
115 float p = (i - QS3 * 1.0) / QS;
116 data[i] = -0.5 * sin(3 * M_PI * p * p);
119 waves[wave_test1].make(bl, data);
120 for (int i = 0; i < S; i++) {
121 data[i] = exp(-i * 1.0 / HS) * sin(i * M_PI / HS) * cos(2 * M_PI * i / HS);
123 normalize_waveform(data, S);
124 waves[wave_test2].make(bl, data);
125 for (int i = 0; i < S; i++) {
126 //int ii = (i < HS) ? i : S - i;
127 int ii = HS;
128 data[i] = (ii * 1.0 / HS) * sin(i * 3 * M_PI / HS + 2 * M_PI * sin(M_PI / 4 + i * 4 * M_PI / HS)) * sin(i * 5 * M_PI / HS + 2 * M_PI * sin(M_PI / 8 + i * 6 * M_PI / HS));
130 waves[wave_test3].make(bl, data);
131 for (int i = 0; i < S; i++) {
132 data[i] = sin(i * 2 * M_PI / HS + sin(i * 2 * M_PI / HS + 0.5 * M_PI * sin(i * 18 * M_PI / HS)) * sin(i * 1 * M_PI / HS + 0.5 * M_PI * sin(i * 11 * M_PI / HS)));
134 waves[wave_test4].make(bl, data);
135 for (int i = 0; i < S; i++) {
136 data[i] = sin(i * 2 * M_PI / HS + 0.2 * M_PI * sin(i * 13 * M_PI / HS) + 0.1 * M_PI * sin(i * 37 * M_PI / HS)) * sin(i * M_PI / HS + 0.2 * M_PI * sin(i * 15 * M_PI / HS));
138 waves[wave_test5].make(bl, data);
139 for (int i = 0; i < S; i++) {
140 if (i < HS)
141 data[i] = sin(i * 2 * M_PI / HS);
142 else
143 if (i < 3 * S / 4)
144 data[i] = sin(i * 4 * M_PI / HS);
145 else
146 if (i < 7 * S / 8)
147 data[i] = sin(i * 8 * M_PI / HS);
148 else
149 data[i] = sin(i * 8 * M_PI / HS) * (S - i) / (S / 8);
151 waves[wave_test6].make(bl, data);
152 for (int i = 0; i < S; i++) {
153 int j = i >> (MONOSYNTH_WAVE_BITS - 11);
154 data[i] = (j ^ 0x1D0) * 1.0 / HS - 1;
156 waves[wave_test7].make(bl, data);
157 for (int i = 0; i < S; i++) {
158 int j = i >> (MONOSYNTH_WAVE_BITS - 11);
159 data[i] = -1 + 0.66 * (3 & ((j >> 8) ^ (j >> 10) ^ (j >> 6)));
161 waves[wave_test8].make(bl, data);
164 bool monosynth_audio_module::get_static_graph(int index, int subindex, float value, float *data, int points, cairo_iface *context)
166 monosynth_audio_module::generate_waves();
167 if (index == par_wave1 || index == par_wave2) {
168 if (subindex)
169 return false;
170 enum { S = 1 << MONOSYNTH_WAVE_BITS };
171 int wave = dsp::clip(dsp::fastf2i_drm(value), 0, (int)wave_count - 1);
173 float *waveform = waves[wave].original;
174 for (int i = 0; i < points; i++)
175 data[i] = waveform[i * S / points];
176 return true;
178 return false;
181 bool monosynth_audio_module::get_graph(int index, int subindex, float *data, int points, cairo_iface *context)
183 monosynth_audio_module::generate_waves();
184 // printf("get_graph %d %p %d wave1=%d wave2=%d\n", index, data, points, wave1, wave2);
185 if (index == par_filtertype) {
186 if (!running)
187 return false;
188 if (subindex > (is_stereo_filter() ? 1 : 0))
189 return false;
190 for (int i = 0; i < points; i++)
192 typedef complex<double> cfloat;
193 double freq = 20.0 * pow (20000.0 / 20.0, i * 1.0 / points);
195 dsp::biquad_d1<float> &f = subindex ? filter2 : filter;
196 float level = f.freq_gain(freq, srate);
197 if (!is_stereo_filter())
198 level *= filter2.freq_gain(freq, srate);
199 level *= fgain;
201 data[i] = log(level) / log(1024.0) + 0.5;
203 return true;
205 return get_static_graph(index, subindex, *params[index], data, points, context);
208 void monosynth_audio_module::calculate_buffer_ser()
210 for (uint32_t i = 0; i < step_size; i++)
212 float osc1val = osc1.get();
213 float osc2val = osc2.get();
214 float wave = fgain * (osc1val + (osc2val - osc1val) * xfade);
215 wave = filter.process(wave);
216 wave = filter2.process(wave);
217 buffer[i] = wave;
218 fgain += fgain_delta;
222 void monosynth_audio_module::calculate_buffer_single()
224 for (uint32_t i = 0; i < step_size; i++)
226 float osc1val = osc1.get();
227 float osc2val = osc2.get();
228 float wave = fgain * (osc1val + (osc2val - osc1val) * xfade);
229 wave = filter.process(wave);
230 buffer[i] = wave;
231 fgain += fgain_delta;
235 void monosynth_audio_module::calculate_buffer_stereo()
237 for (uint32_t i = 0; i < step_size; i++)
239 float osc1val = osc1.get();
240 float osc2val = osc2.get();
241 float wave1 = osc1val + (osc2val - osc1val) * xfade;
242 float wave2 = phaseshifter.process_ap(wave1);
243 buffer[i] = fgain * filter.process(wave1);
244 buffer2[i] = fgain * filter2.process(wave2);
245 fgain += fgain_delta;
249 void monosynth_audio_module::delayed_note_on()
251 force_fadeout = false;
252 stop_count = 0;
253 porta_time = 0.f;
254 start_freq = freq;
255 target_freq = freq = 440 * pow(2.0, (queue_note_on - 69) / 12.0);
256 ampctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2amp];
257 fltctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2filter];
258 set_frequency();
259 osc1.waveform = waves[wave1].get_level(osc1.phasedelta);
260 osc2.waveform = waves[wave2].get_level(osc2.phasedelta);
261 if (!osc1.waveform) osc1.waveform = silence;
262 if (!osc2.waveform) osc2.waveform = silence;
264 if (!running)
266 if (legato >= 2)
267 porta_time = -1.f;
268 osc1.reset();
269 osc2.reset();
270 filter.reset();
271 filter2.reset();
272 switch((int)*params[par_oscmode])
274 case 1:
275 osc2.phase = 0x80000000;
276 break;
277 case 2:
278 osc2.phase = 0x40000000;
279 break;
280 case 3:
281 osc1.phase = osc2.phase = 0x40000000;
282 break;
283 case 4:
284 osc1.phase = 0x40000000;
285 osc2.phase = 0xC0000000;
286 break;
287 case 5:
288 // rand() is crap, but I don't have any better RNG in Calf yet
289 osc1.phase = rand() << 16;
290 osc2.phase = rand() << 16;
291 break;
292 default:
293 break;
295 envelope.note_on();
296 running = true;
298 if (legato >= 2 && !gate)
299 porta_time = -1.f;
300 gate = true;
301 stopping = false;
302 if (!(legato & 1) || envelope.released()) {
303 envelope.note_on();
305 envelope.advance();
306 queue_note_on = -1;
309 void monosynth_audio_module::set_sample_rate(uint32_t sr) {
310 srate = sr;
311 crate = sr / step_size;
312 odcr = (float)(1.0 / crate);
313 phaseshifter.set_ap(1000.f, sr);
314 fgain = 0.f;
315 fgain_delta = 0.f;
318 void monosynth_audio_module::calculate_step()
320 if (queue_note_on != -1)
321 delayed_note_on();
322 else if (stopping)
324 running = false;
325 dsp::zero(buffer, step_size);
326 if (is_stereo_filter())
327 dsp::zero(buffer2, step_size);
328 return;
330 float porta_total_time = *params[par_portamento] * 0.001f;
332 if (porta_total_time >= 0.00101f && porta_time >= 0) {
333 // XXXKF this is criminal, optimize!
334 float point = porta_time / porta_total_time;
335 if (point >= 1.0f) {
336 freq = target_freq;
337 porta_time = -1;
338 } else {
339 freq = start_freq + (target_freq - start_freq) * point;
340 // freq = start_freq * pow(target_freq / start_freq, point);
341 porta_time += odcr;
344 set_frequency();
345 envelope.advance();
346 float env = envelope.value;
347 cutoff = *params[par_cutoff] * pow(2.0f, env * fltctl * *params[par_envmod] * (1.f / 1200.f));
348 if (*params[par_keyfollow] > 0.01f)
349 cutoff *= pow(freq / 264.f, *params[par_keyfollow]);
350 cutoff = dsp::clip(cutoff , 10.f, 18000.f);
351 float resonance = *params[par_resonance];
352 float e2r = *params[par_envtores];
353 float e2a = *params[par_envtoamp];
354 resonance = resonance * (1 - e2r) + (0.7 + (resonance - 0.7) * env * env) * e2r;
355 float cutoff2 = dsp::clip(cutoff * separation, 10.f, 18000.f);
356 float newfgain = 0.f;
357 if (filter_type != last_filter_type)
359 filter.y2 = filter.y1 = filter.x2 = filter.x1 = filter.y1;
360 filter2.y2 = filter2.y1 = filter2.x2 = filter2.x1 = filter2.y1;
361 last_filter_type = filter_type;
363 switch(filter_type)
365 case flt_lp12:
366 filter.set_lp_rbj(cutoff, resonance, srate);
367 filter2.set_null();
368 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
369 break;
370 case flt_hp12:
371 filter.set_hp_rbj(cutoff, resonance, srate);
372 filter2.set_null();
373 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
374 break;
375 case flt_lp24:
376 filter.set_lp_rbj(cutoff, resonance, srate);
377 filter2.set_lp_rbj(cutoff2, resonance, srate);
378 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
379 break;
380 case flt_lpbr:
381 filter.set_lp_rbj(cutoff, resonance, srate);
382 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
383 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
384 break;
385 case flt_hpbr:
386 filter.set_hp_rbj(cutoff, resonance, srate);
387 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
388 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
389 break;
390 case flt_2lp12:
391 filter.set_lp_rbj(cutoff, resonance, srate);
392 filter2.set_lp_rbj(cutoff2, resonance, srate);
393 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
394 break;
395 case flt_bp6:
396 filter.set_bp_rbj(cutoff, resonance, srate);
397 filter2.set_null();
398 newfgain = ampctl;
399 break;
400 case flt_2bp6:
401 filter.set_bp_rbj(cutoff, resonance, srate);
402 filter2.set_bp_rbj(cutoff2, resonance, srate);
403 newfgain = ampctl;
404 break;
406 float aenv = env;
407 /* isn't as good as expected
408 if (e2a > 1.0) { // extra-steep release on amplitude envelope only
409 if (envelope.state == adsr::RELEASE && env < envelope.sustain) {
410 aenv -= (envelope.sustain - env) * (e2a - 1.0);
411 if (aenv < 0.f) aenv = 0.f;
412 printf("aenv = %f\n", aenv);
414 e2a = 1.0;
417 newfgain *= 1.0 - (1.0 - aenv) * e2a;
418 fgain_delta = (newfgain - fgain) * (1.0 / step_size);
419 switch(filter_type)
421 case flt_lp24:
422 case flt_lpbr:
423 case flt_hpbr: // Oomek's wish
424 calculate_buffer_ser();
425 break;
426 case flt_lp12:
427 case flt_hp12:
428 case flt_bp6:
429 calculate_buffer_single();
430 break;
431 case flt_2lp12:
432 case flt_2bp6:
433 calculate_buffer_stereo();
434 break;
436 if (envelope.state == adsr::STOP || force_fadeout)
438 enum { ramp = step_size * 4 };
439 for (int i = 0; i < step_size; i++)
440 buffer[i] *= (ramp - i - stop_count) * (1.0f / ramp);
441 if (is_stereo_filter())
442 for (int i = 0; i < step_size; i++)
443 buffer2[i] *= (ramp - i - stop_count) * (1.0f / ramp);
444 stop_count += step_size;
445 if (stop_count >= ramp)
446 stopping = true;
450 void monosynth_audio_module::note_on(int note, int vel)
452 queue_note_on = note;
453 last_key = note;
454 queue_vel = vel / 127.f;
455 stack.push(note);
458 void monosynth_audio_module::note_off(int note, int vel)
460 stack.pop(note);
461 // If releasing the currently played note, try to get another one from note stack.
462 if (note == last_key) {
463 if (stack.count())
465 last_key = note = stack.nth(stack.count() - 1);
466 start_freq = freq;
467 target_freq = freq = dsp::note_to_hz(note);
468 set_frequency();
469 if (!(legato & 1)) {
470 envelope.note_on();
471 stopping = false;
472 running = true;
474 return;
476 gate = false;
477 envelope.note_off();
482 void monosynth_audio_module::control_change(int controller, int value)
484 switch(controller)
486 case 120: // all sounds off
487 force_fadeout = true;
488 // fall through
489 case 123: // all notes off
490 gate = false;
491 queue_note_on = -1;
492 envelope.note_off();
493 stack.clear();
494 break;
498 void monosynth_audio_module::deactivate()
500 gate = false;
501 running = false;
502 stopping = false;
503 envelope.reset();
504 stack.clear();