Expose voice detune parameter of the lv2 plugin
[zyn.git] / oscillator.c
blobd7cbfa042d43703e3bdb95e99785079845e111e6
1 /*
2 ZynAddSubFX - a software synthesizer
4 OscilGen.C - Waveform generator for ADnote
5 Copyright (C) 2002-2005 Nasca Octavian Paul
6 Author: Nasca Octavian Paul
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of version 2 of the GNU General Public License
10 as published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License (version 2) for more details.
17 You should have received a copy of the GNU General Public License (version 2)
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <assert.h>
28 #include "globals.h"
29 #include "fft.h"
30 #include "resonance.h"
31 #include "oscillator.h"
32 #include "log.h"
34 static
35 zyn_sample_type
36 zyn_oscillator_base_function_pulse(float x, float a)
38 return (fmod(x, 1.0) < a) ? -1.0 : 1.0;
41 static
42 zyn_sample_type
43 zyn_oscillator_base_function_saw(float x, float a)
45 if (a < 0.00001)
47 a = 0.00001;
49 else if (a > 0.99999)
51 a = 0.99999;
54 x = fmod(x, 1);
55 if (x < a)
57 return x / a * 2.0 - 1.0;
59 else
61 return (1.0 - x) / (1.0 - a) * 2.0 - 1.0;
65 static
66 zyn_sample_type
67 zyn_oscillator_base_function_triangle(float x, float a)
69 x = fmod(x + 0.25, 1);
70 a = 1 - a;
72 if (a < 0.00001)
74 a = 0.00001;
77 if (x < 0.5)
79 x = x * 4 - 1.0;
81 else
83 x = (1.0 - x) * 4 - 1.0;
86 x /= -a;
88 if (x <- 1.0)
90 x = -1.0;
93 if (x > 1.0)
95 x = 1.0;
98 return x;
101 static
102 zyn_sample_type
103 zyn_oscillator_base_function_power(float x, float a)
105 x=fmod(x,1);
106 if (a<0.00001) a=0.00001;
107 else if (a>0.99999) a=0.99999;
108 return(pow(x,exp((a-0.5)*10.0))*2.0-1.0);
111 static
112 zyn_sample_type
113 zyn_oscillator_base_function_gauss(float x, float a)
115 x = fmod(x, 1) * 2.0 - 1.0;
117 if (a < 0.00001)
119 a = 0.00001;
122 return exp(-x * x * (exp(a * 8) + 5.0)) * 2.0 - 1.0;
125 static
126 zyn_sample_type
127 zyn_oscillator_base_function_diode(float x, float a)
129 if (a < 0.00001)
131 a = 0.00001;
133 else if (a > 0.99999)
135 a = 0.99999;
138 a = a * 2.0 - 1.0;
139 x = cos((x + 0.5) * 2.0 * PI) - a;
141 if (x < 0.0)
143 x = 0.0;
146 return x / (1.0 - a) * 2 - 1.0;
149 static
150 zyn_sample_type
151 zyn_oscillator_base_function_abssine(float x, float a)
153 x = fmod(x, 1);
154 if (a < 0.00001)
156 a = 0.00001;
158 else if (a > 0.99999)
160 a = 0.99999;
163 return sin(pow(x, exp((a - 0.5) * 5.0)) * PI) * 2.0 - 1.0;
166 static
167 zyn_sample_type
168 zyn_oscillator_base_function_pulsesine(float x, float a)
170 if (a < 0.00001)
172 a = 0.00001;
175 x = (fmod(x, 1) - 0.5) * exp((a - 0.5) * log(128));
177 if (x < -0.5)
179 x = -0.5;
181 else if (x > 0.5)
183 x = 0.5;
186 x = sin(x * PI * 2.0);
188 return x;
191 static
192 zyn_sample_type
193 zyn_oscillator_base_function_stretchsine(float x, float a)
195 float b;
197 x = fmod(x + 0.5, 1) * 2.0 - 1.0;
199 a = (a - 0.5) * 4;
201 if (a > 0.0)
203 a *= 2;
206 a = pow(3.0, a);
208 b = pow(fabs(x), a);
210 if (x < 0)
212 b = -b;
215 return -sin(b * PI);
218 static
219 zyn_sample_type
220 zyn_oscillator_base_function_chirp(float x, float a)
222 x = fmod(x, 1.0) * 2.0 * PI;
224 a = (a - 0.5) * 4;
226 if (a < 0.0)
228 a *= 2.0;
231 a = pow(3.0, a);
233 return sin(x / 2.0) * sin(a * x * x);
236 static
237 zyn_sample_type
238 zyn_oscillator_base_function_absstretchsine(float x, float a)
240 float b;
242 x = fmod(x + 0.5,1) * 2.0 - 1.0;
243 a = (a - 0.5) * 9;
244 a = pow(3.0, a);
246 b = pow(fabs(x), a);
248 if (x < 0)
250 b = -b;
253 return -pow(sin(b * PI), 2);
256 static
257 zyn_sample_type
258 zyn_oscillator_base_function_chebyshev(float x, float a)
260 a = a * a * a * 30.0 + 1.0;
262 return cos(acos(x * 2.0 - 1.0) * a);
265 static
266 zyn_sample_type
267 zyn_oscillator_base_function_sqr(float x, float a)
269 a = a * a * a * a * 160.0 + 0.001;
271 return -atan(sin(x * 2.0 * PI) * a);
275 * Get the base function
277 void
278 zyn_oscillator_get_base_function(
279 struct zyn_oscillator * oscillator_ptr,
280 zyn_sample_type * samples)
282 int i;
283 float par;
284 float basefuncmodulationpar1;
285 float basefuncmodulationpar2;
286 float basefuncmodulationpar3;
287 float t;
289 par = oscillator_ptr->base_function_adjust;
291 basefuncmodulationpar1 = oscillator_ptr->Pbasefuncmodulationpar1 / 127.0;
292 basefuncmodulationpar2 = oscillator_ptr->Pbasefuncmodulationpar2 / 127.0;
293 basefuncmodulationpar3 = oscillator_ptr->Pbasefuncmodulationpar3 / 127.0;
295 switch(oscillator_ptr->Pbasefuncmodulation)
297 case 1:
298 basefuncmodulationpar1 = (pow(2, basefuncmodulationpar1 * 5.0) - 1.0) / 10.0;
299 basefuncmodulationpar3 = floor(pow(2, basefuncmodulationpar3 * 5.0) - 1.0);
300 if (basefuncmodulationpar3 < 0.9999)
302 basefuncmodulationpar3 = -1.0;
304 break;
305 case 2:
306 basefuncmodulationpar1 = (pow(2, basefuncmodulationpar1 * 5.0) - 1.0) / 10.0;
307 basefuncmodulationpar3 = 1.0 + floor((pow(2, basefuncmodulationpar3 * 5.0) - 1.0));
308 break;
309 case 3:
310 basefuncmodulationpar1 = (pow(2,basefuncmodulationpar1*7.0)-1.0)/10.0;
311 basefuncmodulationpar3 = 0.01+(pow(2,basefuncmodulationpar3*16.0)-1.0)/10.0;
312 break;
315 LOG_DEBUG("%.5f %.5f", basefuncmodulationpar1, basefuncmodulationpar3);
317 for (i = 0 ; i < OSCIL_SIZE ; i++)
319 t = i * 1.0 / OSCIL_SIZE;
321 switch (oscillator_ptr->Pbasefuncmodulation)
323 case 1:
324 // rev
325 t = t * basefuncmodulationpar3 + sin((t + basefuncmodulationpar2) * 2.0 * PI) * basefuncmodulationpar1;
326 break;
327 case 2:
328 // sine
329 t = t + sin((t * basefuncmodulationpar3 + basefuncmodulationpar2) * 2.0 * PI) * basefuncmodulationpar1;
330 break;
331 case 3:
332 // power
333 t = t + pow((1.0 - cos((t + basefuncmodulationpar2) * 2.0 * PI)) * 0.5, basefuncmodulationpar3) * basefuncmodulationpar1;
334 break;
337 t = t - floor(t);
339 switch (oscillator_ptr->base_function)
341 case ZYN_OSCILLATOR_BASE_FUNCTION_SINE:
342 samples[i] = -sin(2.0 * PI * i / OSCIL_SIZE);
343 case ZYN_OSCILLATOR_BASE_FUNCTION_TRIANGLE:
344 samples[i] = zyn_oscillator_base_function_triangle(t, par);
345 break;
346 case ZYN_OSCILLATOR_BASE_FUNCTION_PULSE:
347 samples[i] = zyn_oscillator_base_function_pulse(t, par);
348 break;
349 case ZYN_OSCILLATOR_BASE_FUNCTION_SAW:
350 samples[i] = zyn_oscillator_base_function_saw(t, par);
351 break;
352 case ZYN_OSCILLATOR_BASE_FUNCTION_POWER:
353 samples[i] = zyn_oscillator_base_function_power(t, par);
354 break;
355 case ZYN_OSCILLATOR_BASE_FUNCTION_GAUSS:
356 samples[i] = zyn_oscillator_base_function_gauss(t, par);
357 break;
358 case ZYN_OSCILLATOR_BASE_FUNCTION_DIODE:
359 samples[i] = zyn_oscillator_base_function_diode(t, par);
360 break;
361 case ZYN_OSCILLATOR_BASE_FUNCTION_ABS_SINE:
362 samples[i] = zyn_oscillator_base_function_abssine(t, par);
363 break;
364 case ZYN_OSCILLATOR_BASE_FUNCTION_PULSE_SINE:
365 samples[i] = zyn_oscillator_base_function_pulsesine(t, par);
366 break;
367 case ZYN_OSCILLATOR_BASE_FUNCTION_STRETCH_SINE:
368 samples[i] = zyn_oscillator_base_function_stretchsine(t, par);
369 break;
370 case ZYN_OSCILLATOR_BASE_FUNCTION_CHIRP:
371 samples[i] = zyn_oscillator_base_function_chirp(t, par);
372 break;
373 case ZYN_OSCILLATOR_BASE_FUNCTION_ABS_STRETCH_SINE:
374 samples[i] = zyn_oscillator_base_function_absstretchsine(t, par);
375 break;
376 case ZYN_OSCILLATOR_BASE_FUNCTION_CHEBYSHEV:
377 samples[i] = zyn_oscillator_base_function_chebyshev(t, par);
378 break;
379 case ZYN_OSCILLATOR_BASE_FUNCTION_SQRT:
380 samples[i] = zyn_oscillator_base_function_sqr(t, par);
381 break;
382 default:
383 assert(0);
388 /* Shift the harmonics */
389 static
390 void
391 zyn_oscillator_shift_harmonics(
392 struct zyn_oscillator * oscillator_ptr)
394 int i;
395 REALTYPE hc,hs;
396 int harmonicshift;
397 int oldh;
399 if (oscillator_ptr->Pharmonicshift == 0)
401 return;
404 harmonicshift = -oscillator_ptr->Pharmonicshift;
406 if (harmonicshift>0)
408 for (i=OSCIL_SIZE/2-2;i>=0;i--)
410 oldh = i - harmonicshift;
411 if (oldh < 0)
413 hc = 0.0;
414 hs = 0.0;
416 else
418 hc = oscillator_ptr->oscilFFTfreqs.c[oldh + 1];
419 hs = oscillator_ptr->oscilFFTfreqs.s[oldh + 1];
422 oscillator_ptr->oscilFFTfreqs.c[i + 1] = hc;
423 oscillator_ptr->oscilFFTfreqs.s[i + 1] = hs;
426 else
428 for (i = 0 ; i < OSCIL_SIZE / 2 - 1 ; i++)
430 oldh = i + abs(harmonicshift);
431 if (oldh >= (OSCIL_SIZE / 2 - 1))
433 hc = 0.0;
434 hs = 0.0;
436 else
438 hc = oscillator_ptr->oscilFFTfreqs.c[oldh + 1];
439 hs = oscillator_ptr->oscilFFTfreqs.s[oldh + 1];
441 if (fabs(hc) < 0.000001)
443 hc = 0.0;
446 if (fabs(hs) < 0.000001)
448 hs = 0.0;
452 oscillator_ptr->oscilFFTfreqs.c[i + 1] = hc;
453 oscillator_ptr->oscilFFTfreqs.s[i + 1] = hs;
457 oscillator_ptr->oscilFFTfreqs.c[0] = 0.0;
461 * Change the base function
463 static
464 void
465 zyn_oscillator_change_base_function(
466 struct zyn_oscillator * oscillator_ptr)
468 int i;
470 if (oscillator_ptr->base_function != ZYN_OSCILLATOR_BASE_FUNCTION_SINE)
472 zyn_oscillator_get_base_function(oscillator_ptr, oscillator_ptr->temporary_samples_ptr);
473 zyn_fft_smps2freqs(oscillator_ptr->fft, oscillator_ptr->temporary_samples_ptr, &oscillator_ptr->basefuncFFTfreqs);
474 oscillator_ptr->basefuncFFTfreqs.c[0] = 0.0;
476 else
478 for (i = 0 ; i < OSCIL_SIZE / 2 ; i++)
480 oscillator_ptr->basefuncFFTfreqs.s[i] = 0.0;
481 oscillator_ptr->basefuncFFTfreqs.c[i] = 0.0;
483 //in this case basefuncFFTfreqs_ are not used
486 oscillator_ptr->prepared = false;
487 oscillator_ptr->base_function_needs_prepare = false;
488 oscillator_ptr->oldbasefuncmodulation = oscillator_ptr->Pbasefuncmodulation;
489 oscillator_ptr->oldbasefuncmodulationpar1 = oscillator_ptr->Pbasefuncmodulationpar1;
490 oscillator_ptr->oldbasefuncmodulationpar2 = oscillator_ptr->Pbasefuncmodulationpar2;
491 oscillator_ptr->oldbasefuncmodulationpar3 = oscillator_ptr->Pbasefuncmodulationpar3;
494 void
495 zyn_oscillator_waveshape_samples(
496 int n,
497 zyn_sample_type *smps,
498 unsigned int type,
499 float drive)
501 int i;
502 float ws;
503 float tmpv;
504 zyn_sample_type tmp;
506 ws = drive / 100.0;
508 switch (type)
510 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ATAN:
511 // Arctangent
512 ws = pow(10, ws * ws * 3.0) - 1.0 + 0.001;
513 for (i = 0 ; i < n ; i++)
515 smps[i] = atan(smps[i] * ws) / atan(ws);
517 break;
518 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ASYM1:
519 // Asymmetric
520 ws = ws * ws * 32.0 + 0.0001;
522 if (ws<1.0)
524 tmpv = sin(ws) + 0.1;
526 else
528 tmpv = 1.1;
531 for (i = 0 ; i < n ; i++)
533 smps[i] = sin(smps[i] * (0.1 + ws - ws * smps[i])) / tmpv;
535 break;
536 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_POW:
537 // Pow
538 ws = ws * ws * ws * 20.0 + 0.0001;
539 for (i = 0 ; i < n ; i++)
541 smps[i] *= ws;
542 if (fabs(smps[i])<1.0)
544 smps[i]=(smps[i]-pow(smps[i],3.0))*3.0;
545 if (ws<1.0) smps[i]/=ws;
547 else
549 smps[i] = 0.0;
552 break;
553 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_SINE:
554 // Sine
555 ws = ws * ws * ws * 32.0 + 0.0001;
556 if (ws < 1.57)
558 tmpv = sin(ws);
560 else
562 tmpv = 1.0;
565 for (i = 0 ; i < n ; i++)
567 smps[i] = sin(smps[i] * ws) / tmpv;
569 break;
570 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_QUANTISIZE:
571 // Quantisize
572 ws = ws * ws + 0.000001;
573 for (i = 0 ; i < n ; i++)
575 smps[i] = floor(smps[i] / ws + 0.5) * ws;
577 break;
578 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ZIGZAG:
579 // Zigzag
580 ws = ws * ws * ws * 32 + 0.0001;
581 if (ws < 1.0)
583 tmpv = sin(ws);
585 else
587 tmpv = 1.0;
590 for (i = 0 ; i < n ; i++)
592 smps[i] = asin(sin(smps[i] * ws)) / tmpv;
594 break;
595 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_LIMITER:
596 // Limiter
597 ws = pow(2.0, -ws * ws * 8.0);
598 for (i=0;i<n;i++)
600 tmp = smps[i];
601 if (fabs(tmp) > ws)
603 if (tmp >= 0.0)
605 smps[i] = 1.0;
607 else
609 smps[i] = -1.0;
612 else
614 smps[i] /= ws;
617 break;
618 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_UPPER_LIMITER:
619 // Upper Limiter
620 ws = pow(2.0, -ws * ws * 8.0);
621 for (i = 0 ; i < n ; i++)
623 tmp = smps[i];
624 if (tmp>ws) smps[i]=ws;
625 smps[i]*=2.0;
627 break;
628 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_LOWER_LIMITER:
629 // Lower Limiter
630 ws=pow(2.0,-ws*ws*8.0);
631 for (i=0;i<n;i++)
633 tmp = smps[i];
634 if (tmp<-ws) smps[i]=-ws;
635 smps[i]*=2.0;
637 break;
638 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_INVERSE_LIMITER:
639 // Inverse Limiter
640 ws = (pow(2.0, ws * 6.0) - 1.0) / pow(2.0, 6.0);
641 for (i = 0 ; i < n ; i++)
643 tmp = smps[i];
644 if (fabs(tmp) > ws)
646 if (tmp >= 0.0)
648 smps[i] = tmp - ws;
650 else
652 smps[i] = tmp + ws;
655 else
657 smps[i] = 0;
660 break;
661 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_CLIP:
662 // Clip
663 ws = pow(5, ws * ws * 1.0) - 1.0;
664 for (i = 0 ; i < n ; i++)
666 smps[i] = smps[i] * (ws + 0.5) * 0.9999 - floor(0.5 + smps[i] * (ws + 0.5) * 0.9999);
668 break;
669 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ASYM2:
670 // Asym2
671 ws = ws * ws * ws * 30 + 0.001;
672 if (ws < 0.3)
674 tmpv = ws;
676 else
678 tmpv=1.0;
681 for (i = 0 ; i < n ; i++)
683 tmp = smps[i] * ws;
684 if (tmp > -2.0 && tmp < 1.0)
686 smps[i] = tmp * (1.0 - tmp) * (tmp + 2.0) / tmpv;
688 else
690 smps[i] = 0.0;
693 break;
694 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_POW2:
695 // Pow2
696 ws = ws * ws * ws * 32.0 + 0.0001;
697 if (ws < 1.0)
699 tmpv = ws * (1 + ws) / 2.0;
701 else
703 tmpv = 1.0;
706 for (i = 0 ; i < n ; i++)
708 tmp = smps[i] * ws;
709 if (tmp > -1.0 && tmp < 1.618034)
711 smps[i] = tmp * (1.0 - tmp) / tmpv;
713 else if (tmp > 0.0)
715 smps[i] = -1.0;
717 else
719 smps[i] = -2.0;
722 break;
723 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_SIGMOID:
724 // sigmoid
725 ws = pow(ws, 5.0) * 80.0 + 0.0001;
726 if (ws > 10.0)
728 tmpv = 0.5;
730 else
732 tmpv = 0.5 - 1.0 / (exp(ws) + 1.0);
735 for (i = 0 ; i < n ; i++)
737 tmp = smps[i] * ws;
738 if (tmp < -10.0)
740 tmp = -10.0;
742 else if (tmp>10.0)
744 tmp = 10.0;
747 tmp = 0.5 - 1.0 / (exp(tmp) + 1.0);
749 smps[i] = tmp / tmpv;
751 break;
752 //update to Distorsion::changepar (Ptype max) if there is added more waveshapings functions
757 * Waveshape
759 static
760 void
761 zyn_oscillator_waveshape(
762 struct zyn_oscillator * oscillator_ptr)
764 int i;
765 float tmp;
766 float max;
768 if (oscillator_ptr->waveshaping_function == ZYN_OSCILLATOR_WAVESHAPE_TYPE_NONE)
770 return;
773 // remove the DC
774 oscillator_ptr->oscilFFTfreqs.c[0] = 0.0;
776 // reduce the amplitude of the freqs near the nyquist
777 for (i = 1 ; i < OSCIL_SIZE / 8 ; i++)
779 tmp = i / (OSCIL_SIZE / 8.0);
780 oscillator_ptr->oscilFFTfreqs.s[OSCIL_SIZE / 2 - i] *= tmp;
781 oscillator_ptr->oscilFFTfreqs.c[OSCIL_SIZE / 2 - i] *= tmp;
784 zyn_fft_freqs2smps(oscillator_ptr->fft, &oscillator_ptr->oscilFFTfreqs, oscillator_ptr->temporary_samples_ptr);
786 // Normalize
788 max = 0.0;
790 for (i = 0 ; i < OSCIL_SIZE ; i++)
792 if (max < fabs(oscillator_ptr->temporary_samples_ptr[i]))
794 max = fabs(oscillator_ptr->temporary_samples_ptr[i]);
798 if (max < 0.00001)
800 max=1.0;
803 max = 1.0 / max;
805 for (i = 0 ; i < OSCIL_SIZE ; i++)
807 oscillator_ptr->temporary_samples_ptr[i] *= max;
810 // Do the waveshaping
811 zyn_oscillator_waveshape_samples(
812 OSCIL_SIZE,
813 oscillator_ptr->temporary_samples_ptr,
814 oscillator_ptr->waveshaping_function,
815 oscillator_ptr->waveshaping_drive);
817 // perform FFT
818 zyn_fft_smps2freqs(oscillator_ptr->fft, oscillator_ptr->temporary_samples_ptr, &oscillator_ptr->oscilFFTfreqs);
822 * Filter the oscillator
824 //Filter the oscillator accotding to Pfiltertype and Pfilterpar
825 static
826 void
827 zyn_oscillator_filter(
828 struct zyn_oscillator * oscillator_ptr)
830 REALTYPE par;
831 REALTYPE par2;
832 REALTYPE max;
833 REALTYPE tmp;
834 REALTYPE p2;
835 REALTYPE x;
836 int i;
837 REALTYPE gain;
838 REALTYPE imax;
840 if (oscillator_ptr->Pfiltertype == 0)
842 return;
845 par = 1.0 - oscillator_ptr->Pfilterpar1 / 128.0;
846 par2 = oscillator_ptr->Pfilterpar2 / 127.0;
847 max = 0.0;
848 tmp = 0.0;
850 for (i = 1 ; i < OSCIL_SIZE / 2 ; i++)
852 gain = 1.0;
853 switch(oscillator_ptr->Pfiltertype)
855 case 1:
856 // lp
857 gain = pow(1.0 - par * par * par * 0.99, i);
858 tmp = par2 * par2 * par2 * par2 * 0.5 + 0.0001;
859 if (gain < tmp)
861 gain = pow(gain, 10.0) / pow(tmp, 9.0);
863 break;
864 case 2:
865 // hp1
866 gain = 1.0 - pow(1.0 - par * par, i + 1);
867 gain = pow(gain, par2 * 2.0 + 0.1);
868 break;
869 case 3:
870 // hp1b
871 if (par < 0.2)
873 par = par * 0.25 + 0.15;
876 gain = 1.0 - pow(1.0 - par * par * 0.999 + 0.001, i * 0.05 * i + 1.0);
877 tmp = pow(5.0, par2 * 2.0);
878 gain = pow(gain, tmp);
879 break;
880 case 4:
881 // bp1
882 gain = i + 1 - pow(2, (1.0 - par) * 7.5);
883 gain = 1.0 / (1.0 + gain * gain / (i + 1.0));
884 tmp = pow(5.0, par2 * 2.0);
885 gain = pow(gain, tmp);
886 if (gain < 1e-5)
888 gain = 1e-5;
890 break;
891 case 5:
892 // bs1
893 gain = i + 1 - pow(2, (1.0 - par) * 7.5);
894 gain = pow(atan(gain / (i / 10.0 + 1)) / 1.57, 6);
895 gain = pow(gain, par2 * par2 * 3.9 + 0.1);
896 break;
897 case 6:
898 // lp2
899 tmp = pow(par2, 0.33);
900 gain = (i + 1 > pow(2, (1.0 - par) * 10) ? 0.0 : 1.0) * par2 + (1.0 - par2);
901 break;
902 case 7:
903 // hp2
904 tmp = pow(par2,0.33);
905 //tmp = 1.0 - (1.0 - par2) * (1.0 - par2);
906 gain = (i + 1 > pow(2, (1.0 - par) * 7) ? 1.0 : 0.0) * par2 + (1.0 - par2);
907 if (oscillator_ptr->Pfilterpar1 == 0)
909 gain = 1.0;
911 break;
912 case 8:
913 // bp2
914 tmp = pow(par2, 0.33);
915 //tmp = 1.0 - (1.0 - par2) * (1.0 - par2);
916 gain = (fabs(pow(2, (1.0 - par) * 7) - i) > i / 2 + 1 ? 0.0 : 1.0) * par2 + (1.0 - par2);
917 break;
918 case 9:
919 // bs2
920 tmp = pow(par2, 0.33);
921 gain = (fabs(pow(2, (1.0 - par) * 7) - i) < i / 2 + 1 ? 0.0 : 1.0) * par2 + (1.0 - par2);
922 break;
923 case 10:
924 tmp = pow(5.0, par2 * 2.0 - 1.0);
925 tmp = pow(i / 32.0, tmp) * 32.0;
926 if (oscillator_ptr->Pfilterpar2 == 64)
928 tmp = i;
930 gain = cos(par * par * PI / 2.0 * tmp); // cos
931 gain *= gain;
932 break;
933 case 11:tmp=pow(5.0,par2*2.0-1.0);
934 tmp=pow(i/32.0,tmp)*32.0;
935 if (oscillator_ptr->Pfilterpar2 == 64)
937 tmp = i;
939 gain = sin(par * par * PI / 2.0 * tmp); // sin
940 gain *= gain;
941 break;
942 case 12:p2=1.0-par+0.2;
943 x=i/(64.0*p2*p2);
944 if (x<0.0) x=0.0;
945 else if (x>1.0) x=1.0;
946 tmp=pow(1.0-par2,2.0);
947 gain=cos(x*PI)*(1.0-tmp)+1.01+tmp;//low shelf
948 break;
949 case 13:
950 tmp = (int)pow(2.0, (1.0 - par) * 7.2);
951 gain = 1.0;
952 if (i == (int)tmp)
954 gain = pow(2.0, par2 * par2 * 8.0);
956 break;
960 oscillator_ptr->oscilFFTfreqs.s[i] *= gain;
961 oscillator_ptr->oscilFFTfreqs.c[i] *= gain;
962 tmp = oscillator_ptr->oscilFFTfreqs.s[i] * oscillator_ptr->oscilFFTfreqs.s[i] + oscillator_ptr->oscilFFTfreqs.c[i] * oscillator_ptr->oscilFFTfreqs.c[i];
963 if (max < tmp)
965 max = tmp;
969 max = sqrt(max);
970 if (max < 1e-10)
972 max = 1.0;
975 imax = 1.0 / max;
976 for (i = 1 ; i < OSCIL_SIZE / 2 ; i++)
978 oscillator_ptr->oscilFFTfreqs.s[i] *= imax;
979 oscillator_ptr->oscilFFTfreqs.c[i] *= imax;
984 * Do the Frequency Modulation of the Oscil
986 static
987 void
988 zyn_oscillator_modulation(
989 struct zyn_oscillator * oscillator_ptr)
991 int i;
992 float modulationpar1;
993 float modulationpar2;
994 float modulationpar3;
995 float tmp;
996 float max;
997 float t;
998 int poshi;
999 float poslo;
1001 oscillator_ptr->oldmodulation = oscillator_ptr->Pmodulation;
1002 oscillator_ptr->oldmodulationpar1 = oscillator_ptr->Pmodulationpar1;
1003 oscillator_ptr->oldmodulationpar2 = oscillator_ptr->Pmodulationpar2;
1004 oscillator_ptr->oldmodulationpar3 = oscillator_ptr->Pmodulationpar3;
1006 if (oscillator_ptr->Pmodulation==0)
1008 return;
1011 modulationpar1 = oscillator_ptr->Pmodulationpar1 / 127.0;
1012 modulationpar2 = 0.5 - oscillator_ptr->Pmodulationpar2 / 127.0;
1013 modulationpar3 = oscillator_ptr->Pmodulationpar3 / 127.0;
1015 switch(oscillator_ptr->Pmodulation)
1017 case 1:
1018 modulationpar1 = (pow(2, modulationpar1 * 7.0) - 1.0) / 100.0;
1019 modulationpar3 = floor((pow(2, modulationpar3 * 5.0) - 1.0));
1020 if (modulationpar3 < 0.9999)
1022 modulationpar3 = -1.0;
1024 break;
1025 case 2:
1026 modulationpar1 = (pow(2, modulationpar1 * 7.0) - 1.0) / 100.0;
1027 modulationpar3 = 1.0 + floor(pow(2, modulationpar3 * 5.0) - 1.0);
1028 break;
1029 case 3:
1030 modulationpar1 = (pow(2, modulationpar1 * 9.0) - 1.0) / 100.0;
1031 modulationpar3 = 0.01 + (pow(2, modulationpar3 * 16.0) - 1.0) / 10.0;
1032 break;
1035 oscillator_ptr->oscilFFTfreqs.c[0] = 0.0; // remove the DC
1037 // reduce the amplitude of the freqs near the nyquist
1038 for (i = 1 ; i < OSCIL_SIZE / 8 ; i++)
1040 tmp = i / (OSCIL_SIZE / 8.0);
1041 oscillator_ptr->oscilFFTfreqs.s[OSCIL_SIZE / 2 - i] *= tmp;
1042 oscillator_ptr->oscilFFTfreqs.c[OSCIL_SIZE / 2 - i] *= tmp;
1045 zyn_fft_freqs2smps(oscillator_ptr->fft, &oscillator_ptr->oscilFFTfreqs, oscillator_ptr->temporary_samples_ptr);
1047 // Normalize
1049 max = 0.0;
1051 for (i = 0 ; i < OSCIL_SIZE ; i++)
1053 if (max < fabs(oscillator_ptr->temporary_samples_ptr[i]))
1055 max = fabs(oscillator_ptr->temporary_samples_ptr[i]);
1059 if (max < 0.00001)
1061 max = 1.0;
1064 max = 1.0 / max;
1066 for (i=0 ; i < OSCIL_SIZE ; i++)
1068 oscillator_ptr->modulation_temp[i] = oscillator_ptr->temporary_samples_ptr[i] * max;
1071 for (i = 0 ; i < ZYN_OSCILLATOR_EXTRA_POINTS ; i++)
1073 oscillator_ptr->modulation_temp[i + OSCIL_SIZE] = oscillator_ptr->temporary_samples_ptr[i] * max;
1076 // Do the modulation
1077 for (i = 0 ; i < OSCIL_SIZE ; i++)
1079 t = i * 1.0 / OSCIL_SIZE;
1081 switch(oscillator_ptr->Pmodulation)
1083 case 1:
1084 // rev
1085 t = t * modulationpar3 + sin((t + modulationpar2) * 2.0 * PI) * modulationpar1;
1086 break;
1087 case 2:
1088 //sine
1089 t = t + sin((t * modulationpar3 + modulationpar2) * 2.0 * PI) * modulationpar1;
1090 break;
1091 case 3:
1092 // power
1093 t = t + pow((1.0 - cos((t + modulationpar2) * 2.0 * PI)) * 0.5, modulationpar3) * modulationpar1;
1094 break;
1097 t = (t - floor(t)) * OSCIL_SIZE;
1099 poshi = (int)t;
1100 poslo = t - floor(t);
1102 oscillator_ptr->temporary_samples_ptr[i] = oscillator_ptr->modulation_temp[poshi] * (1.0 - poslo) + oscillator_ptr->modulation_temp[poshi + 1] * poslo;
1105 // perform FFT
1106 zyn_fft_smps2freqs(oscillator_ptr->fft, oscillator_ptr->temporary_samples_ptr, &oscillator_ptr->oscilFFTfreqs);
1110 * Adjust the spectrum
1112 static
1113 void
1114 zyn_oscillator_spectrum_adjust(
1115 struct zyn_oscillator * oscillator_ptr)
1117 float par;
1118 int i;
1119 float max;
1120 float tmp;
1121 float mag;
1122 float phase;
1124 if (oscillator_ptr->spectrum_adjust_type == ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_NONE)
1126 return;
1129 par = oscillator_ptr->spectrum_adjust / 100.0;
1131 switch (oscillator_ptr->spectrum_adjust_type)
1133 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_POW:
1134 par = 1.0 - par * 2.0;
1135 if (par >= 0.0)
1137 par = pow(5.0, par);
1139 else
1141 par = pow(8.0, par);
1143 break;
1144 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_DOWN:
1145 par = pow(10.0, (1.0 - par) * 3.0) * 0.25;
1146 break;
1147 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_UP:
1148 par = pow(10.0, (1.0 - par) * 3.0) * 0.25;
1149 break;
1150 default:
1151 assert(0);
1154 max = 0.0;
1155 for (i = 0 ; i < OSCIL_SIZE / 2 ; i++)
1157 tmp = pow(oscillator_ptr->oscilFFTfreqs.c[i], 2) + pow(oscillator_ptr->oscilFFTfreqs.s[i], 2.0);
1158 if (max < tmp)
1160 max = tmp;
1164 max = sqrt(max) / OSCIL_SIZE * 2.0;
1165 if (max < 1e-8)
1167 max = 1.0;
1170 for (i = 0 ; i < OSCIL_SIZE / 2 ; i++)
1172 mag = sqrt(pow(oscillator_ptr->oscilFFTfreqs.s[i], 2) + pow(oscillator_ptr->oscilFFTfreqs.c[i], 2.0)) / max;
1173 phase = atan2(oscillator_ptr->oscilFFTfreqs.s[i], oscillator_ptr->oscilFFTfreqs.c[i]);
1175 switch (oscillator_ptr->spectrum_adjust_type)
1177 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_POW:
1178 mag = pow(mag, par);
1179 break;
1180 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_DOWN:
1181 if (mag < par)
1183 mag = 0.0;
1185 break;
1186 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_UP:
1187 mag /= par;
1188 if (mag > 1.0)
1190 mag = 1.0;
1192 break;
1193 default:
1194 assert(0);
1197 oscillator_ptr->oscilFFTfreqs.c[i] = mag * cos(phase);
1198 oscillator_ptr->oscilFFTfreqs.s[i] = mag * sin(phase);
1203 * Prepare the Oscillator
1205 static
1206 void
1207 zyn_oscillator_prepare(
1208 struct zyn_oscillator * oscillator_ptr)
1210 int i, j, k;
1211 REALTYPE a, b, c, d, hmagnew;
1213 if (oscillator_ptr->base_function_needs_prepare ||
1214 (oscillator_ptr->oldbasefuncmodulation != oscillator_ptr->Pbasefuncmodulation) ||
1215 (oscillator_ptr->oldbasefuncmodulationpar1 != oscillator_ptr->Pbasefuncmodulationpar1) ||
1216 (oscillator_ptr->oldbasefuncmodulationpar2 != oscillator_ptr->Pbasefuncmodulationpar2) ||
1217 (oscillator_ptr->oldbasefuncmodulationpar3 != oscillator_ptr->Pbasefuncmodulationpar3))
1219 zyn_oscillator_change_base_function(oscillator_ptr);
1222 for (i = 0 ; i < MAX_AD_HARMONICS ; i++)
1224 oscillator_ptr->hphase[i] = (oscillator_ptr->Phphase[i] - 64.0) / 64.0 * PI / (i + 1);
1227 for (i = 0 ; i < MAX_AD_HARMONICS ; i++)
1229 hmagnew = 1.0 - fabs(oscillator_ptr->Phmag[i] / 64.0 - 1.0);
1230 switch(oscillator_ptr->Phmagtype)
1232 case 1:
1233 oscillator_ptr->hmag[i] = exp(hmagnew * log(0.01));
1234 break;
1235 case 2:
1236 oscillator_ptr->hmag[i] = exp(hmagnew * log(0.001));
1237 break;
1238 case 3:
1239 oscillator_ptr->hmag[i] = exp(hmagnew * log(0.0001));
1240 break;
1241 case 4:
1242 oscillator_ptr->hmag[i] = exp(hmagnew * log(0.00001));
1243 break;
1244 default:
1245 oscillator_ptr->hmag[i] = 1.0 - hmagnew;
1246 break;
1249 if (oscillator_ptr->Phmag[i] < 64)
1251 oscillator_ptr->hmag[i] =- oscillator_ptr->hmag[i];
1255 // remove the harmonics where Phmag[i] == 64
1256 for (i = 0 ; i < MAX_AD_HARMONICS ; i++)
1258 if (oscillator_ptr->Phmag[i]==64)
1260 oscillator_ptr->hmag[i] = 0.0;
1264 for (i=0 ; i < OSCIL_SIZE / 2 ; i++)
1266 oscillator_ptr->oscilFFTfreqs.c[i] = 0.0;
1267 oscillator_ptr->oscilFFTfreqs.s[i] = 0.0;
1270 if (oscillator_ptr->base_function == ZYN_OSCILLATOR_BASE_FUNCTION_SINE)
1272 // the sine case
1273 for (i = 0 ; i < MAX_AD_HARMONICS ; i++)
1275 oscillator_ptr->oscilFFTfreqs.c[i + 1] = -oscillator_ptr->hmag[i] * sin(oscillator_ptr->hphase[i] * (i + 1)) / 2.0;
1276 oscillator_ptr->oscilFFTfreqs.s[i + 1] = oscillator_ptr->hmag[i] * cos(oscillator_ptr->hphase[i] * (i + 1)) / 2.0;
1279 else
1281 for (j = 0 ; j < MAX_AD_HARMONICS ; j++)
1283 if (oscillator_ptr->Phmag[j] == 64)
1285 continue;
1288 for (i = 1 ; i < OSCIL_SIZE / 2 ; i++)
1290 k = i * (j + 1);
1292 if (k >= OSCIL_SIZE / 2)
1294 break;
1297 a = oscillator_ptr->basefuncFFTfreqs.c[i];
1298 b = oscillator_ptr->basefuncFFTfreqs.s[i];
1299 c = oscillator_ptr->hmag[j] * cos(oscillator_ptr->hphase[j] * k);
1300 d = oscillator_ptr->hmag[j] * sin(oscillator_ptr->hphase[j] * k);
1301 oscillator_ptr->oscilFFTfreqs.c[k] += a * c - b * d;
1302 oscillator_ptr->oscilFFTfreqs.s[k] += a * d + b * c;
1307 if (oscillator_ptr->Pharmonicshiftfirst != 0)
1309 zyn_oscillator_shift_harmonics(oscillator_ptr);
1312 if (oscillator_ptr->Pfilterbeforews == 0)
1314 zyn_oscillator_waveshape(oscillator_ptr);
1315 zyn_oscillator_filter(oscillator_ptr);
1317 else
1319 zyn_oscillator_filter(oscillator_ptr);
1320 zyn_oscillator_waveshape(oscillator_ptr);
1323 zyn_oscillator_modulation(oscillator_ptr);
1324 zyn_oscillator_spectrum_adjust(oscillator_ptr);
1326 if (oscillator_ptr->Pharmonicshiftfirst == 0)
1328 zyn_oscillator_shift_harmonics(oscillator_ptr);
1331 oscillator_ptr->oscilFFTfreqs.c[0] = 0.0;
1333 oscillator_ptr->oldhmagtype = oscillator_ptr->Phmagtype;
1334 oscillator_ptr->oldharmonicshift = oscillator_ptr->Pharmonicshift + oscillator_ptr->Pharmonicshiftfirst * 256;
1336 oscillator_ptr->prepared = true;
1339 static
1340 void
1341 zyn_oscillator_defaults(
1342 struct zyn_oscillator * oscillator_ptr)
1344 int i;
1346 oscillator_ptr->oldhmagtype = 0;
1347 oscillator_ptr->oldbasefuncmodulation = 0;
1348 oscillator_ptr->oldharmonicshift = 0;
1349 oscillator_ptr->oldbasefuncmodulationpar1 = 0;
1350 oscillator_ptr->oldbasefuncmodulationpar2 = 0;
1351 oscillator_ptr->oldbasefuncmodulationpar3 = 0;
1352 oscillator_ptr->oldmodulation = 0;
1353 oscillator_ptr->oldmodulationpar1 = 0;
1354 oscillator_ptr->oldmodulationpar2 = 0;
1355 oscillator_ptr->oldmodulationpar3 = 0;
1357 for (i = 0 ; i < MAX_AD_HARMONICS ; i++)
1359 oscillator_ptr->hmag[i] = 0.0;
1360 oscillator_ptr->hphase[i] = 0.0;
1361 oscillator_ptr->Phmag[i] = 64;
1362 oscillator_ptr->Phphase[i] = 64;
1365 oscillator_ptr->Phmag[0] = 127;
1366 oscillator_ptr->Phmagtype = 0;
1367 if (oscillator_ptr->ADvsPAD)
1369 oscillator_ptr->Prand = 127; // max phase randomness (usefull if the oscil will be imported to a ADsynth from a PADsynth
1371 else
1373 oscillator_ptr->Prand = 64; // no randomness
1376 oscillator_ptr->base_function = ZYN_OSCILLATOR_BASE_FUNCTION_SINE;
1377 oscillator_ptr->base_function_adjust = 0.5;
1379 oscillator_ptr->Pbasefuncmodulation = 0;
1380 oscillator_ptr->Pbasefuncmodulationpar1 = 64;
1381 oscillator_ptr->Pbasefuncmodulationpar2 = 64;
1382 oscillator_ptr->Pbasefuncmodulationpar3 = 32;
1384 oscillator_ptr->Pmodulation = 0;
1385 oscillator_ptr->Pmodulationpar1 = 64;
1386 oscillator_ptr->Pmodulationpar2 = 64;
1387 oscillator_ptr->Pmodulationpar3 = 32;
1389 oscillator_ptr->waveshaping_function = ZYN_OSCILLATOR_WAVESHAPE_TYPE_NONE;
1390 oscillator_ptr->waveshaping_drive = 50;
1391 oscillator_ptr->Pfiltertype = 0;
1392 oscillator_ptr->Pfilterpar1 = 64;
1393 oscillator_ptr->Pfilterpar2 = 64;
1394 oscillator_ptr->Pfilterbeforews = 0;
1395 oscillator_ptr->spectrum_adjust_type = ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_NONE;
1396 oscillator_ptr->spectrum_adjust = 50;
1398 oscillator_ptr->Pamprandpower = 64;
1399 oscillator_ptr->Pamprandtype = 0;
1401 oscillator_ptr->Pharmonicshift = 0;
1402 oscillator_ptr->Pharmonicshiftfirst = 0;
1404 oscillator_ptr->Padaptiveharmonics = 0;
1405 oscillator_ptr->Padaptiveharmonicspower = 100;
1406 oscillator_ptr->Padaptiveharmonicsbasefreq = 128;
1407 oscillator_ptr->Padaptiveharmonicspar = 50;
1409 for (i = 0 ; i < OSCIL_SIZE / 2 ; i++)
1411 oscillator_ptr->oscilFFTfreqs.s[i] = 0.0;
1412 oscillator_ptr->oscilFFTfreqs.c[i] = 0.0;
1413 oscillator_ptr->basefuncFFTfreqs.s[i] = 0.0;
1414 oscillator_ptr->basefuncFFTfreqs.c[i] = 0.0;
1417 oscillator_ptr->prepared = false;
1418 oscillator_ptr->base_function_needs_prepare = true;
1419 oscillator_ptr->oldfilterpars = 0;
1421 zyn_oscillator_prepare(oscillator_ptr);
1424 void
1425 zyn_oscillator_init(
1426 struct zyn_oscillator * oscillator_ptr,
1427 float sample_rate,
1428 zyn_fft_handle fft,
1429 struct zyn_resonance * resonance_ptr,
1430 zyn_sample_type * temporary_samples_ptr,
1431 struct zyn_fft_freqs * oscillator_fft_frequencies_ptr)
1433 oscillator_ptr->sample_rate = sample_rate;
1435 oscillator_ptr->fft = fft;
1436 oscillator_ptr->resonance_ptr = resonance_ptr;
1438 oscillator_ptr->temporary_samples_ptr = temporary_samples_ptr;
1439 oscillator_ptr->oscillator_fft_frequencies_ptr = oscillator_fft_frequencies_ptr;
1441 zyn_fft_freqs_init(&oscillator_ptr->oscilFFTfreqs, OSCIL_SIZE / 2);
1442 zyn_fft_freqs_init(&oscillator_ptr->basefuncFFTfreqs, OSCIL_SIZE / 2);
1444 oscillator_ptr->randseed = 1;
1445 oscillator_ptr->ADvsPAD = false;
1447 zyn_oscillator_defaults(oscillator_ptr);
1450 void
1451 zyn_oscillator_uninit(
1452 struct zyn_oscillator * oscillator_ptr)
1454 zyn_fft_freqs_uninit(&oscillator_ptr->basefuncFFTfreqs);
1455 zyn_fft_freqs_uninit(&oscillator_ptr->oscilFFTfreqs);
1458 void
1459 zyn_oscillator_new_rand_seed(
1460 struct zyn_oscillator * oscillator_ptr,
1461 unsigned int randseed)
1463 oscillator_ptr->randseed = randseed;
1466 static
1467 void
1468 zyn_oscillator_adaptive_harmonic(
1469 struct zyn_oscillator * oscillator_ptr,
1470 struct zyn_fft_freqs * freqs_ptr,
1471 float freq)
1473 struct zyn_fft_freqs inf;
1474 int i;
1475 float hc;
1476 float hs;
1477 float basefreq;
1478 float power;
1479 float rap;
1480 bool down;
1481 float h;
1482 int high;
1483 float low;
1485 if (oscillator_ptr->Padaptiveharmonics == 0 /* || freq < 1.0*/)
1487 return;
1490 if (freq < 1.0)
1492 freq = 440.0;
1495 zyn_fft_freqs_init(&inf, OSCIL_SIZE / 2);
1497 for (i = 0 ; i < OSCIL_SIZE / 2 ; i++)
1499 inf.s[i] = freqs_ptr->s[i];
1500 inf.c[i] = freqs_ptr->c[i];
1501 freqs_ptr->s[i] = 0.0;
1502 freqs_ptr->c[i] = 0.0;
1505 inf.c[0]=0.0;inf.s[0]=0.0;
1507 hc = 0.0;
1508 hs = 0.0;
1509 basefreq = 30.0 * pow(10.0, oscillator_ptr->Padaptiveharmonicsbasefreq / 128.0);
1510 power = (oscillator_ptr->Padaptiveharmonicspower + 1.0) / 101.0;
1512 rap = freq / basefreq;
1514 rap = pow(rap, power);
1516 if (rap > 1.0)
1518 rap = 1.0 / rap;
1519 down = true;
1521 else
1523 down = false;
1526 for (i = 0 ; i < OSCIL_SIZE / 2 - 2 ; i++)
1528 h = i * rap;
1529 high = (int)(i * rap);
1530 low = fmod(h, 1.0);
1532 if (high >= OSCIL_SIZE / 2 - 2)
1534 break;
1536 else
1538 if (down)
1540 freqs_ptr->c[high] += inf.c[i] * (1.0 - low);
1541 freqs_ptr->s[high] += inf.s[i] * (1.0 - low);
1542 freqs_ptr->c[high + 1] += inf.c[i] * low;
1543 freqs_ptr->s[high + 1] += inf.s[i] * low;
1545 else
1547 hc = inf.c[high] * (1.0 - low) + inf.c[high + 1] * low;
1548 hs = inf.s[high] * (1.0 - low) + inf.s[high + 1] * low;
1551 if (fabs(hc) < 0.000001)
1553 hc = 0.0;
1556 if (fabs(hs) < 0.000001)
1558 hs = 0.0;
1562 if (!down)
1564 if (i == 0)
1566 // corect the aplitude of the first harmonic
1567 hc *= rap;
1568 hs *= rap;
1571 freqs_ptr->c[i] = hc;
1572 freqs_ptr->s[i] = hs;
1576 freqs_ptr->c[1] += freqs_ptr->c[0];
1577 freqs_ptr->s[1] += freqs_ptr->s[0];
1578 freqs_ptr->c[0] = 0.0;
1579 freqs_ptr->s[0] = 0.0;
1581 zyn_fft_freqs_uninit(&inf);
1584 static
1585 void
1586 zyn_oscillator_adaptive_harmonic_post_process(
1587 struct zyn_oscillator * oscillator_ptr,
1588 float *f,
1589 int size)
1591 float inf[size];
1592 int i;
1593 float par;
1594 int nh;
1595 int sub_vs_add;
1597 if (oscillator_ptr->Padaptiveharmonics <= 1)
1599 return;
1602 par = oscillator_ptr->Padaptiveharmonicspar * 0.01;
1603 par = 1.0 - pow(1.0 - par, 1.5);
1605 for (i = 0 ; i < size ; i++)
1607 inf[i] = f[i] * par;
1608 f[i] = f[i] * (1.0 - par);
1611 if (oscillator_ptr->Padaptiveharmonics == 2)
1613 // 2n+1
1614 for (i = 0 ; i < size ; i++)
1616 // i=0 pt prima armonica,etc.
1617 if (i % 2 == 0)
1619 f[i] += inf[i];
1623 else
1625 // celelalte moduri
1626 nh = (oscillator_ptr->Padaptiveharmonics - 3) / 2 + 2;
1627 sub_vs_add = (oscillator_ptr->Padaptiveharmonics - 3) % 2;
1628 if (sub_vs_add==0)
1630 for (i = 0 ; i < size ; i++)
1632 if ((i + 1) % nh == 0)
1634 f[i] += inf[i];
1638 else
1640 for (i = 0 ; i < size / nh - 1 ; i++)
1642 f[(i + 1) * nh - 1] += inf[i];
1649 * Get the oscillator function
1651 short
1652 zyn_oscillator_get(
1653 struct zyn_oscillator * oscillator_ptr,
1654 zyn_sample_type *smps,
1655 float freqHz,
1656 bool resonance)
1658 int i;
1659 int nyquist;
1660 int outpos;
1661 int newpars;
1662 int realnyquist;
1663 float rnd;
1664 float angle;
1665 float a;
1666 float b;
1667 float c;
1668 float d;
1669 unsigned int realrnd;
1670 float power;
1671 float normalize;
1672 float amp;
1673 float rndfreq;
1674 float sum;
1675 int j;
1677 if (oscillator_ptr->oldhmagtype != oscillator_ptr->Phmagtype)
1679 oscillator_ptr->prepared = false;
1682 newpars = oscillator_ptr->Pfiltertype * 256;
1683 newpars += oscillator_ptr->Pfilterpar1;
1684 newpars += oscillator_ptr->Pfilterpar2 * 65536;
1685 newpars += oscillator_ptr->Pfilterbeforews * 16777216;
1687 if (oscillator_ptr->oldfilterpars != newpars)
1689 oscillator_ptr->prepared = false;
1690 oscillator_ptr->oldfilterpars = newpars;
1693 if (oscillator_ptr->oldbasefuncmodulation != oscillator_ptr->Pbasefuncmodulation ||
1694 oscillator_ptr->oldbasefuncmodulationpar1 != oscillator_ptr->Pbasefuncmodulationpar1 ||
1695 oscillator_ptr->oldbasefuncmodulationpar2 != oscillator_ptr->Pbasefuncmodulationpar2 ||
1696 oscillator_ptr->oldbasefuncmodulationpar3 != oscillator_ptr->Pbasefuncmodulationpar3)
1698 oscillator_ptr->prepared = false;
1701 if (oscillator_ptr->oldmodulation != oscillator_ptr->Pmodulation ||
1702 oscillator_ptr->oldmodulationpar1 != oscillator_ptr->Pmodulationpar1 ||
1703 oscillator_ptr->oldmodulationpar2 != oscillator_ptr->Pmodulationpar2 ||
1704 oscillator_ptr->oldmodulationpar3 != oscillator_ptr->Pmodulationpar3)
1706 oscillator_ptr->prepared = false;
1709 if (oscillator_ptr->oldharmonicshift != oscillator_ptr->Pharmonicshift + oscillator_ptr->Pharmonicshiftfirst * 256)
1711 oscillator_ptr->prepared = false;
1714 if (!oscillator_ptr->prepared)
1716 zyn_oscillator_prepare(oscillator_ptr);
1719 outpos = (int)((RND * 2.0 - 1.0) * (float)OSCIL_SIZE * (oscillator_ptr->Prand - 64.0) / 64.0);
1720 outpos = (outpos + 2 * OSCIL_SIZE) % OSCIL_SIZE;
1723 for (i = 0 ; i < OSCIL_SIZE / 2 ; i++)
1725 oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] = 0.0;
1726 oscillator_ptr->oscillator_fft_frequencies_ptr->s[i] = 0.0;
1729 nyquist = (int)(0.5 * oscillator_ptr->sample_rate / fabs(freqHz)) + 2;
1731 if (oscillator_ptr->ADvsPAD)
1733 nyquist = (int)(OSCIL_SIZE / 2);
1736 if (nyquist > OSCIL_SIZE / 2)
1738 nyquist = OSCIL_SIZE / 2;
1741 realnyquist = nyquist;
1743 if (oscillator_ptr->Padaptiveharmonics != 0)
1745 nyquist = OSCIL_SIZE / 2;
1748 for (i=1;i<nyquist-1;i++)
1750 oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] = oscillator_ptr->oscilFFTfreqs.c[i];
1751 oscillator_ptr->oscillator_fft_frequencies_ptr->s[i] = oscillator_ptr->oscilFFTfreqs.s[i];
1754 zyn_oscillator_adaptive_harmonic(oscillator_ptr, oscillator_ptr->oscillator_fft_frequencies_ptr, freqHz);
1755 zyn_oscillator_adaptive_harmonic_post_process(oscillator_ptr, &oscillator_ptr->oscillator_fft_frequencies_ptr->c[1], OSCIL_SIZE / 2 - 1);
1756 zyn_oscillator_adaptive_harmonic_post_process(oscillator_ptr, &oscillator_ptr->oscillator_fft_frequencies_ptr->s[1], OSCIL_SIZE / 2 - 1);
1758 nyquist = realnyquist;
1760 // do the antialiasing in the case of adaptive harmonics
1761 if (oscillator_ptr->Padaptiveharmonics)
1763 for (i = nyquist ; i < OSCIL_SIZE / 2 ; i++)
1765 oscillator_ptr->oscillator_fft_frequencies_ptr->s[i]=0;
1766 oscillator_ptr->oscillator_fft_frequencies_ptr->c[i]=0;
1770 // Randomness (each harmonic), the block type is computed
1771 // in ADnote by setting start position according to this setting
1772 if (oscillator_ptr->Prand > 64 &&
1773 freqHz >= 0.0 &&
1774 !oscillator_ptr->ADvsPAD)
1776 rnd = PI * pow((oscillator_ptr->Prand - 64.0) / 64.0, 2.0);
1778 // to Nyquist only for AntiAliasing
1779 for (i = 1 ; i < nyquist - 1 ; i++)
1781 angle = rnd * i * RND;
1782 a = oscillator_ptr->oscillator_fft_frequencies_ptr->c[i];
1783 b = oscillator_ptr->oscillator_fft_frequencies_ptr->s[i];
1784 c = cos(angle);
1785 d = sin(angle);
1786 oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] = a * c - b * d;
1787 oscillator_ptr->oscillator_fft_frequencies_ptr->s[i] = a * d + b * c;
1791 // Harmonic Amplitude Randomness
1792 if (freqHz > 0.1 && !oscillator_ptr->ADvsPAD)
1794 realrnd = rand();
1795 srand(oscillator_ptr->randseed);
1796 power = oscillator_ptr->Pamprandpower / 127.0;
1797 normalize = 1.0 / (1.2 - power);
1799 switch (oscillator_ptr->Pamprandtype)
1801 case 1:
1802 power = power * 2.0 - 0.5;
1803 power = pow(15.0, power);
1804 for (i=1;i<nyquist-1;i++)
1806 amp = pow(RND, power) * normalize;
1807 oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] *= amp;
1808 oscillator_ptr->oscillator_fft_frequencies_ptr->s[i] *= amp;
1810 break;
1811 case 2:
1812 power = power * 2.0 - 0.5;
1813 power = pow(15.0, power) * 2.0;
1814 rndfreq = 2 * PI * RND;
1816 for (i = 1 ; i < nyquist - 1 ; i++)
1818 amp = pow(fabs(sin(i * rndfreq)), power) * normalize;
1819 oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] *= amp;
1820 oscillator_ptr->oscillator_fft_frequencies_ptr->s[i] *= amp;
1822 break;
1825 srand(realrnd + 1);
1828 if (freqHz > 0.1 && resonance)
1830 zyn_resonance_apply(oscillator_ptr->resonance_ptr, nyquist - 1, oscillator_ptr->oscillator_fft_frequencies_ptr, freqHz);
1833 // Full RMS normalize
1834 sum = 0;
1836 for (j = 1 ; j < OSCIL_SIZE / 2 ; j++)
1838 sum += oscillator_ptr->oscillator_fft_frequencies_ptr->c[j] * oscillator_ptr->oscillator_fft_frequencies_ptr->c[j];
1839 sum += oscillator_ptr->oscillator_fft_frequencies_ptr->s[j] * oscillator_ptr->oscillator_fft_frequencies_ptr->s[j];
1842 if (sum < 0.000001)
1844 sum=1.0;
1847 sum = 1.0 / sqrt(sum);
1849 for (j = 1 ; j < OSCIL_SIZE / 2 ; j++)
1851 oscillator_ptr->oscillator_fft_frequencies_ptr->c[j]*=sum;
1852 oscillator_ptr->oscillator_fft_frequencies_ptr->s[j]*=sum;
1855 if (oscillator_ptr->ADvsPAD && freqHz > 0.1)
1857 // in this case the smps will contain the freqs
1858 for (i=1;i<OSCIL_SIZE/2;i++)
1860 smps[i - 1] = sqrt(oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] * oscillator_ptr->oscillator_fft_frequencies_ptr->c[i] + oscillator_ptr->oscillator_fft_frequencies_ptr->s[i] * oscillator_ptr->oscillator_fft_frequencies_ptr->s[i]);
1863 else
1865 zyn_fft_freqs2smps(oscillator_ptr->fft, oscillator_ptr->oscillator_fft_frequencies_ptr, smps);
1867 // correct the amplitude
1868 for (i = 0 ; i < OSCIL_SIZE ; i++)
1870 smps[i] *= 0.25;
1874 if (oscillator_ptr->Prand < 64)
1876 return outpos;
1879 return 0;
1882 #if 0
1883 //computes the full spectrum of oscil from harmonics,phases and basefunc
1884 void prepare();
1886 void getbasefunction(REALTYPE *smps);
1888 //called by UI
1889 void getspectrum(int n,REALTYPE *spc,int what);//what=0 pt. oscil,1 pt. basefunc
1890 void getcurrentbasefunction(REALTYPE *smps);
1891 void useasbase();//convert oscil to base function
1893 void defaults();
1895 void convert2sine(int magtype);
1897 //computes the basefunction and make the FFT; newbasefunc<0 = same basefunc
1898 void changebasefunction();
1899 //Waveshaping
1900 void waveshape();
1902 //Filter the oscillator accotding to Pfiltertype and Pfilterpar
1903 void oscilfilter();
1905 //Adjust the spectrum
1906 void spectrumadjust();
1908 //Shift the harmonics
1909 void shiftharmonics();
1911 //Do the oscil modulation stuff
1912 void modulation();
1914 //Do the adaptive harmonic stuff
1915 void adaptiveharmonic(struct zyn_fft_freqs f, REALTYPE freq);
1917 //Do the adaptive harmonic postprocessing (2n+1,2xS,2xA,etc..)
1918 //this function is called even for the user interface
1919 //this can be called for the sine and components, and for the spectrum
1920 //(that's why the sine and cosine components should be processed with a separate call)
1921 void adaptiveharmonicpostprocess(REALTYPE *f, int size);
1923 void
1924 OscilGen::convert2sine(int magtype)
1926 REALTYPE mag[MAX_AD_HARMONICS],phase[MAX_AD_HARMONICS];
1927 REALTYPE oscil[OSCIL_SIZE];
1928 struct zyn_fft_freqs freqs;
1929 zyn_fft_handle fft;
1931 zyn_fft_freqs_init(&freqs, OSCIL_SIZE / 2);
1933 get(oscil,-1.0);
1934 fft = zyn_fft_create(OSCIL_SIZE);
1935 zyn_fft_smps2freqs(fft, oscil,freqs);
1936 zyn_fft_destroy(fft);
1938 REALTYPE max=0.0;
1940 mag[0]=0;
1941 phase[0]=0;
1942 for (int i=0;i<MAX_AD_HARMONICS;i++){
1943 mag[i]=sqrt(pow(freqs.s[i+1],2)+pow(freqs.c[i+1],2.0));
1944 phase[i]=atan2(freqs.c[i+1],freqs.s[i+1]);
1945 if (max<mag[i]) max=mag[i];
1947 if (max<0.00001) max=1.0;
1949 defaults();
1951 for (int i=0;i<MAX_AD_HARMONICS-1;i++){
1952 REALTYPE newmag=mag[i]/max;
1953 REALTYPE newphase=phase[i];
1955 Phmag[i]=(int) ((newmag)*64.0)+64;
1957 Phphase[i]=64-(int) (64.0*newphase/PI);
1958 if (Phphase[i]>127) Phphase[i]=127;
1960 if (Phmag[i]==64) Phphase[i]=64;
1963 zyn_fft_freqs_uninit(&freqs);
1965 prepare();
1969 * Get the spectrum of the oscillator for the UI
1971 void OscilGen::getspectrum(int n, REALTYPE *spc,int what){
1972 if (n>OSCIL_SIZE/2) n=OSCIL_SIZE/2;
1974 for (int i=1;i<n;i++){
1975 if (what==0){
1976 spc[i-1]=sqrt(oscilFFTfreqs.c[i]*oscilFFTfreqs.c[i]
1977 +oscilFFTfreqs.s[i]*oscilFFTfreqs.s[i]);
1978 } else {
1979 if (Pcurrentbasefunc==0) spc[i-1]=((i==1)?(1.0):(0.0));
1980 else spc[i-1]=sqrt(basefuncFFTfreqs.c[i]*basefuncFFTfreqs.c[i]+
1981 basefuncFFTfreqs.s[i]*basefuncFFTfreqs.s[i]);
1985 if (what==0) {
1986 for (int i=0;i<n;i++) outoscilFFTfreqs.s[i]=outoscilFFTfreqs.c[i]=spc[i+1];
1987 for (int i=n;i<OSCIL_SIZE/2;i++) outoscilFFTfreqs.s[i]=outoscilFFTfreqs.c[i]=0.0;
1988 adaptiveharmonic(outoscilFFTfreqs,0.0);
1989 for (int i=1;i<n;i++) spc[i-1]=outoscilFFTfreqs.s[i];
1990 adaptiveharmonicpostprocess(spc,n-1);
1996 * Convert the oscillator as base function
1998 void OscilGen::useasbase(){
1999 int i;
2001 for (i=0;i<OSCIL_SIZE/2;i++) {
2002 basefuncFFTfreqs.c[i]=oscilFFTfreqs.c[i];
2003 basefuncFFTfreqs.s[i]=oscilFFTfreqs.s[i];
2006 oldbasefunc=Pcurrentbasefunc=127;
2008 prepare();
2013 * Get the base function for UI
2015 void
2016 OscilGen::getcurrentbasefunction(REALTYPE *smps)
2018 if (Pcurrentbasefunc!=0)
2020 zyn_fft_freqs2smps(m_fft, basefuncFFTfreqs, smps);
2022 else
2024 // the sine case
2025 getbasefunction(smps);
2028 #endif