+ DSSI: marshal cairo_iface as well
[calf.git] / src / monosynth.cpp
blob1726f70464a36e9873d50facf5a32a18fcb7e87f
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 stop_count = 0;
252 porta_time = 0.f;
253 start_freq = freq;
254 target_freq = freq = 440 * pow(2.0, (queue_note_on - 69) / 12.0);
255 ampctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2amp];
256 fltctl = 1.0 + (queue_vel - 1.0) * *params[par_vel2filter];
257 set_frequency();
258 osc1.waveform = waves[wave1].get_level(osc1.phasedelta);
259 osc2.waveform = waves[wave2].get_level(osc2.phasedelta);
260 if (!osc1.waveform) osc1.waveform = silence;
261 if (!osc2.waveform) osc2.waveform = silence;
263 if (!running)
265 if (legato >= 2)
266 porta_time = -1.f;
267 osc1.reset();
268 osc2.reset();
269 filter.reset();
270 filter2.reset();
271 switch((int)*params[par_oscmode])
273 case 1:
274 osc2.phase = 0x80000000;
275 break;
276 case 2:
277 osc2.phase = 0x40000000;
278 break;
279 case 3:
280 osc1.phase = osc2.phase = 0x40000000;
281 break;
282 case 4:
283 osc1.phase = 0x40000000;
284 osc2.phase = 0xC0000000;
285 break;
286 case 5:
287 // rand() is crap, but I don't have any better RNG in Calf yet
288 osc1.phase = rand() << 16;
289 osc2.phase = rand() << 16;
290 break;
291 default:
292 break;
294 envelope.note_on();
295 running = true;
297 if (legato >= 2 && !gate)
298 porta_time = -1.f;
299 gate = true;
300 stopping = false;
301 if (!(legato & 1) || envelope.released()) {
302 envelope.note_on();
304 envelope.advance();
305 queue_note_on = -1;
308 void monosynth_audio_module::set_sample_rate(uint32_t sr) {
309 srate = sr;
310 crate = sr / step_size;
311 odcr = (float)(1.0 / crate);
312 phaseshifter.set_ap(1000.f, sr);
313 fgain = 0.f;
314 fgain_delta = 0.f;
317 void monosynth_audio_module::calculate_step()
319 if (queue_note_on != -1)
320 delayed_note_on();
321 else if (stopping)
323 running = false;
324 dsp::zero(buffer, step_size);
325 if (is_stereo_filter())
326 dsp::zero(buffer2, step_size);
327 return;
329 float porta_total_time = *params[par_portamento] * 0.001f;
331 if (porta_total_time >= 0.00101f && porta_time >= 0) {
332 // XXXKF this is criminal, optimize!
333 float point = porta_time / porta_total_time;
334 if (point >= 1.0f) {
335 freq = target_freq;
336 porta_time = -1;
337 } else {
338 freq = start_freq + (target_freq - start_freq) * point;
339 // freq = start_freq * pow(target_freq / start_freq, point);
340 porta_time += odcr;
343 set_frequency();
344 envelope.advance();
345 float env = envelope.value;
346 cutoff = *params[par_cutoff] * pow(2.0f, env * fltctl * *params[par_envmod] * (1.f / 1200.f));
347 if (*params[par_keyfollow] > 0.01f)
348 cutoff *= pow(freq / 264.f, *params[par_keyfollow]);
349 cutoff = dsp::clip(cutoff , 10.f, 18000.f);
350 float resonance = *params[par_resonance];
351 float e2r = *params[par_envtores];
352 float e2a = *params[par_envtoamp];
353 resonance = resonance * (1 - e2r) + (0.7 + (resonance - 0.7) * env * env) * e2r;
354 float cutoff2 = dsp::clip(cutoff * separation, 10.f, 18000.f);
355 float newfgain = 0.f;
356 if (filter_type != last_filter_type)
358 filter.y2 = filter.y1 = filter.x2 = filter.x1 = filter.y1;
359 filter2.y2 = filter2.y1 = filter2.x2 = filter2.x1 = filter2.y1;
360 last_filter_type = filter_type;
362 switch(filter_type)
364 case flt_lp12:
365 filter.set_lp_rbj(cutoff, resonance, srate);
366 filter2.set_null();
367 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
368 break;
369 case flt_hp12:
370 filter.set_hp_rbj(cutoff, resonance, srate);
371 filter2.set_null();
372 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
373 break;
374 case flt_lp24:
375 filter.set_lp_rbj(cutoff, resonance, srate);
376 filter2.set_lp_rbj(cutoff2, resonance, srate);
377 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
378 break;
379 case flt_lpbr:
380 filter.set_lp_rbj(cutoff, resonance, srate);
381 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
382 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
383 break;
384 case flt_hpbr:
385 filter.set_hp_rbj(cutoff, resonance, srate);
386 filter2.set_br_rbj(cutoff2, 1.0 / resonance, srate);
387 newfgain = min(0.5f, 0.5f / resonance) * ampctl;
388 break;
389 case flt_2lp12:
390 filter.set_lp_rbj(cutoff, resonance, srate);
391 filter2.set_lp_rbj(cutoff2, resonance, srate);
392 newfgain = min(0.7f, 0.7f / resonance) * ampctl;
393 break;
394 case flt_bp6:
395 filter.set_bp_rbj(cutoff, resonance, srate);
396 filter2.set_null();
397 newfgain = ampctl;
398 break;
399 case flt_2bp6:
400 filter.set_bp_rbj(cutoff, resonance, srate);
401 filter2.set_bp_rbj(cutoff2, resonance, srate);
402 newfgain = ampctl;
403 break;
405 float aenv = env;
406 /* isn't as good as expected
407 if (e2a > 1.0) { // extra-steep release on amplitude envelope only
408 if (envelope.state == adsr::RELEASE && env < envelope.sustain) {
409 aenv -= (envelope.sustain - env) * (e2a - 1.0);
410 if (aenv < 0.f) aenv = 0.f;
411 printf("aenv = %f\n", aenv);
413 e2a = 1.0;
416 newfgain *= 1.0 - (1.0 - aenv) * e2a;
417 fgain_delta = (newfgain - fgain) * (1.0 / step_size);
418 switch(filter_type)
420 case flt_lp24:
421 case flt_lpbr:
422 case flt_hpbr: // Oomek's wish
423 calculate_buffer_ser();
424 break;
425 case flt_lp12:
426 case flt_hp12:
427 case flt_bp6:
428 calculate_buffer_single();
429 break;
430 case flt_2lp12:
431 case flt_2bp6:
432 calculate_buffer_stereo();
433 break;
435 if (envelope.state == adsr::STOP)
437 enum { ramp = step_size * 4 };
438 for (int i = 0; i < step_size; i++)
439 buffer[i] *= (ramp - i - stop_count) * (1.0f / ramp);
440 if (is_stereo_filter())
441 for (int i = 0; i < step_size; i++)
442 buffer2[i] *= (ramp - i - stop_count) * (1.0f / ramp);
443 stop_count += step_size;
444 if (stop_count >= ramp)
445 stopping = true;
449 void monosynth_audio_module::control_change(int controller, int value)
451 switch(controller)
453 case 120: // all sounds off
454 case 123: // all notes off
455 gate = false;
456 envelope.note_off();
457 stack.clear();
458 break;