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.
26 #include <jack/jack.h>
28 #include <calf/giface.h>
29 #include <calf/modules_synths.h>
32 using namespace calf_plugins
;
37 void monosynth_audio_module::activate() {
38 monosynth_audio_module::generate_waves();
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
;
59 static waveform_family
<MONOSYNTH_WAVE_BITS
> waves_data
[wave_count
];
62 enum { S
= 1 << MONOSYNTH_WAVE_BITS
, HS
= S
/ 2, QS
= S
/ 4, QS3
= 3 * QS
};
66 // yes these waves don't have really perfect 1/x spectrum because of aliasing
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
++) {
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
;
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
++) {
112 float p
= i
* 1.0 / QS3
;
113 data
[i
] = sin(M_PI
* p
* p
* p
);
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;
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
++) {
141 data
[i
] = sin(i
* 2 * M_PI
/ HS
);
144 data
[i
] = sin(i
* 4 * M_PI
/ HS
);
147 data
[i
] = sin(i
* 8 * M_PI
/ HS
);
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
) {
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
];
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
) {
188 if (subindex
> (is_stereo_filter() ? 1 : 0))
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
);
201 data
[i
] = log(level
) / log(1024.0) + 0.5;
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
);
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
);
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;
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
];
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
;
272 switch((int)*params
[par_oscmode
])
275 osc2
.phase
= 0x80000000;
278 osc2
.phase
= 0x40000000;
281 osc1
.phase
= osc2
.phase
= 0x40000000;
284 osc1
.phase
= 0x40000000;
285 osc2
.phase
= 0xC0000000;
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;
298 if (legato
>= 2 && !gate
)
302 if (!(legato
& 1) || envelope
.released()) {
309 void monosynth_audio_module::set_sample_rate(uint32_t sr
) {
311 crate
= sr
/ step_size
;
312 odcr
= (float)(1.0 / crate
);
313 phaseshifter
.set_ap(1000.f
, sr
);
318 void monosynth_audio_module::calculate_step()
320 if (queue_note_on
!= -1)
325 dsp::zero(buffer
, step_size
);
326 if (is_stereo_filter())
327 dsp::zero(buffer2
, step_size
);
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
;
339 freq
= start_freq
+ (target_freq
- start_freq
) * point
;
340 // freq = start_freq * pow(target_freq / start_freq, point);
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
;
366 filter
.set_lp_rbj(cutoff
, resonance
, srate
);
368 newfgain
= min(0.7f
, 0.7f
/ resonance
) * ampctl
;
371 filter
.set_hp_rbj(cutoff
, resonance
, srate
);
373 newfgain
= min(0.7f
, 0.7f
/ resonance
) * ampctl
;
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
;
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
;
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
;
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
;
396 filter
.set_bp_rbj(cutoff
, resonance
, srate
);
401 filter
.set_bp_rbj(cutoff
, resonance
, srate
);
402 filter2
.set_bp_rbj(cutoff2
, resonance
, srate
);
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);
417 newfgain
*= 1.0 - (1.0 - aenv
) * e2a
;
418 fgain_delta
= (newfgain
- fgain
) * (1.0 / step_size
);
423 case flt_hpbr
: // Oomek's wish
424 calculate_buffer_ser();
429 calculate_buffer_single();
433 calculate_buffer_stereo();
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
)
450 void monosynth_audio_module::note_on(int note
, int vel
)
452 queue_note_on
= note
;
454 queue_vel
= vel
/ 127.f
;
458 void monosynth_audio_module::note_off(int note
, int vel
)
461 // If releasing the currently played note, try to get another one from note stack.
462 if (note
== last_key
) {
465 last_key
= note
= stack
.nth(stack
.count() - 1);
467 target_freq
= freq
= dsp::note_to_hz(note
);
482 void monosynth_audio_module::control_change(int controller
, int value
)
486 case 120: // all sounds off
487 force_fadeout
= true;
489 case 123: // all notes off
498 void monosynth_audio_module::deactivate()