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
30 #include "resonance.h"
31 #include "oscillator.h"
36 zyn_oscillator_base_function_pulse(float x
, float a
)
38 return (fmod(x
, 1.0) < a
) ? -1.0 : 1.0;
43 zyn_oscillator_base_function_saw(float x
, float a
)
57 return x
/ a
* 2.0 - 1.0;
61 return (1.0 - x
) / (1.0 - a
) * 2.0 - 1.0;
67 zyn_oscillator_base_function_triangle(float x
, float a
)
69 x
= fmod(x
+ 0.25, 1);
83 x
= (1.0 - x
) * 4 - 1.0;
103 zyn_oscillator_base_function_power(float x
, float a
)
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);
113 zyn_oscillator_base_function_gauss(float x
, float a
)
115 x
= fmod(x
, 1) * 2.0 - 1.0;
122 return exp(-x
* x
* (exp(a
* 8) + 5.0)) * 2.0 - 1.0;
127 zyn_oscillator_base_function_diode(float x
, float a
)
133 else if (a
> 0.99999)
139 x
= cos((x
+ 0.5) * 2.0 * PI
) - a
;
146 return x
/ (1.0 - a
) * 2 - 1.0;
151 zyn_oscillator_base_function_abssine(float x
, float a
)
158 else if (a
> 0.99999)
163 return sin(pow(x
, exp((a
- 0.5) * 5.0)) * PI
) * 2.0 - 1.0;
168 zyn_oscillator_base_function_pulsesine(float x
, float a
)
175 x
= (fmod(x
, 1) - 0.5) * exp((a
- 0.5) * log(128));
186 x
= sin(x
* PI
* 2.0);
193 zyn_oscillator_base_function_stretchsine(float x
, float a
)
197 x
= fmod(x
+ 0.5, 1) * 2.0 - 1.0;
220 zyn_oscillator_base_function_chirp(float x
, float a
)
222 x
= fmod(x
, 1.0) * 2.0 * PI
;
233 return sin(x
/ 2.0) * sin(a
* x
* x
);
238 zyn_oscillator_base_function_absstretchsine(float x
, float a
)
242 x
= fmod(x
+ 0.5,1) * 2.0 - 1.0;
253 return -pow(sin(b
* PI
), 2);
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
);
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
278 zyn_oscillator_get_base_function(
279 struct zyn_oscillator
* oscillator_ptr
,
280 zyn_sample_type
* samples
)
284 float basefuncmodulationpar1
;
285 float basefuncmodulationpar2
;
286 float basefuncmodulationpar3
;
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
)
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;
306 basefuncmodulationpar1
= (pow(2, basefuncmodulationpar1
* 5.0) - 1.0) / 10.0;
307 basefuncmodulationpar3
= 1.0 + floor((pow(2, basefuncmodulationpar3
* 5.0) - 1.0));
310 basefuncmodulationpar1
= (pow(2,basefuncmodulationpar1
*7.0)-1.0)/10.0;
311 basefuncmodulationpar3
= 0.01+(pow(2,basefuncmodulationpar3
*16.0)-1.0)/10.0;
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
)
325 t
= t
* basefuncmodulationpar3
+ sin((t
+ basefuncmodulationpar2
) * 2.0 * PI
) * basefuncmodulationpar1
;
329 t
= t
+ sin((t
* basefuncmodulationpar3
+ basefuncmodulationpar2
) * 2.0 * PI
) * basefuncmodulationpar1
;
333 t
= t
+ pow((1.0 - cos((t
+ basefuncmodulationpar2
) * 2.0 * PI
)) * 0.5, basefuncmodulationpar3
) * basefuncmodulationpar1
;
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
);
346 case ZYN_OSCILLATOR_BASE_FUNCTION_PULSE
:
347 samples
[i
] = zyn_oscillator_base_function_pulse(t
, par
);
349 case ZYN_OSCILLATOR_BASE_FUNCTION_SAW
:
350 samples
[i
] = zyn_oscillator_base_function_saw(t
, par
);
352 case ZYN_OSCILLATOR_BASE_FUNCTION_POWER
:
353 samples
[i
] = zyn_oscillator_base_function_power(t
, par
);
355 case ZYN_OSCILLATOR_BASE_FUNCTION_GAUSS
:
356 samples
[i
] = zyn_oscillator_base_function_gauss(t
, par
);
358 case ZYN_OSCILLATOR_BASE_FUNCTION_DIODE
:
359 samples
[i
] = zyn_oscillator_base_function_diode(t
, par
);
361 case ZYN_OSCILLATOR_BASE_FUNCTION_ABS_SINE
:
362 samples
[i
] = zyn_oscillator_base_function_abssine(t
, par
);
364 case ZYN_OSCILLATOR_BASE_FUNCTION_PULSE_SINE
:
365 samples
[i
] = zyn_oscillator_base_function_pulsesine(t
, par
);
367 case ZYN_OSCILLATOR_BASE_FUNCTION_STRETCH_SINE
:
368 samples
[i
] = zyn_oscillator_base_function_stretchsine(t
, par
);
370 case ZYN_OSCILLATOR_BASE_FUNCTION_CHIRP
:
371 samples
[i
] = zyn_oscillator_base_function_chirp(t
, par
);
373 case ZYN_OSCILLATOR_BASE_FUNCTION_ABS_STRETCH_SINE
:
374 samples
[i
] = zyn_oscillator_base_function_absstretchsine(t
, par
);
376 case ZYN_OSCILLATOR_BASE_FUNCTION_CHEBYSHEV
:
377 samples
[i
] = zyn_oscillator_base_function_chebyshev(t
, par
);
379 case ZYN_OSCILLATOR_BASE_FUNCTION_SQRT
:
380 samples
[i
] = zyn_oscillator_base_function_sqr(t
, par
);
388 /* Shift the harmonics */
391 zyn_oscillator_shift_harmonics(
392 struct zyn_oscillator
* oscillator_ptr
)
399 if (oscillator_ptr
->Pharmonicshift
== 0)
404 harmonicshift
= -oscillator_ptr
->Pharmonicshift
;
408 for (i
=OSCIL_SIZE
/2-2;i
>=0;i
--)
410 oldh
= i
- harmonicshift
;
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
;
428 for (i
= 0 ; i
< OSCIL_SIZE
/ 2 - 1 ; i
++)
430 oldh
= i
+ abs(harmonicshift
);
431 if (oldh
>= (OSCIL_SIZE
/ 2 - 1))
438 hc
= oscillator_ptr
->oscilFFTfreqs
.c
[oldh
+ 1];
439 hs
= oscillator_ptr
->oscilFFTfreqs
.s
[oldh
+ 1];
441 if (fabs(hc
) < 0.000001)
446 if (fabs(hs
) < 0.000001)
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
465 zyn_oscillator_change_base_function(
466 struct zyn_oscillator
* oscillator_ptr
)
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;
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
;
495 zyn_oscillator_waveshape_samples(
497 zyn_sample_type
*smps
,
510 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ATAN
:
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
);
518 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ASYM1
:
520 ws
= ws
* ws
* 32.0 + 0.0001;
524 tmpv
= sin(ws
) + 0.1;
531 for (i
= 0 ; i
< n
; i
++)
533 smps
[i
] = sin(smps
[i
] * (0.1 + ws
- ws
* smps
[i
])) / tmpv
;
536 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_POW
:
538 ws
= ws
* ws
* ws
* 20.0 + 0.0001;
539 for (i
= 0 ; i
< n
; i
++)
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
;
553 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_SINE
:
555 ws
= ws
* ws
* ws
* 32.0 + 0.0001;
565 for (i
= 0 ; i
< n
; i
++)
567 smps
[i
] = sin(smps
[i
] * ws
) / tmpv
;
570 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_QUANTISIZE
:
572 ws
= ws
* ws
+ 0.000001;
573 for (i
= 0 ; i
< n
; i
++)
575 smps
[i
] = floor(smps
[i
] / ws
+ 0.5) * ws
;
578 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ZIGZAG
:
580 ws
= ws
* ws
* ws
* 32 + 0.0001;
590 for (i
= 0 ; i
< n
; i
++)
592 smps
[i
] = asin(sin(smps
[i
] * ws
)) / tmpv
;
595 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_LIMITER
:
597 ws
= pow(2.0, -ws
* ws
* 8.0);
618 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_UPPER_LIMITER
:
620 ws
= pow(2.0, -ws
* ws
* 8.0);
621 for (i
= 0 ; i
< n
; i
++)
624 if (tmp
>ws
) smps
[i
]=ws
;
628 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_LOWER_LIMITER
:
630 ws
=pow(2.0,-ws
*ws
*8.0);
634 if (tmp
<-ws
) smps
[i
]=-ws
;
638 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_INVERSE_LIMITER
:
640 ws
= (pow(2.0, ws
* 6.0) - 1.0) / pow(2.0, 6.0);
641 for (i
= 0 ; i
< n
; i
++)
661 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_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);
669 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_ASYM2
:
671 ws
= ws
* ws
* ws
* 30 + 0.001;
681 for (i
= 0 ; i
< n
; i
++)
684 if (tmp
> -2.0 && tmp
< 1.0)
686 smps
[i
] = tmp
* (1.0 - tmp
) * (tmp
+ 2.0) / tmpv
;
694 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_POW2
:
696 ws
= ws
* ws
* ws
* 32.0 + 0.0001;
699 tmpv
= ws
* (1 + ws
) / 2.0;
706 for (i
= 0 ; i
< n
; i
++)
709 if (tmp
> -1.0 && tmp
< 1.618034)
711 smps
[i
] = tmp
* (1.0 - tmp
) / tmpv
;
723 case ZYN_OSCILLATOR_WAVESHAPE_TYPE_SIGMOID
:
725 ws
= pow(ws
, 5.0) * 80.0 + 0.0001;
732 tmpv
= 0.5 - 1.0 / (exp(ws
) + 1.0);
735 for (i
= 0 ; i
< n
; i
++)
747 tmp
= 0.5 - 1.0 / (exp(tmp
) + 1.0);
749 smps
[i
] = tmp
/ tmpv
;
752 //update to Distorsion::changepar (Ptype max) if there is added more waveshapings functions
761 zyn_oscillator_waveshape(
762 struct zyn_oscillator
* oscillator_ptr
)
768 if (oscillator_ptr
->waveshaping_function
== ZYN_OSCILLATOR_WAVESHAPE_TYPE_NONE
)
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
);
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
]);
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(
813 oscillator_ptr
->temporary_samples_ptr
,
814 oscillator_ptr
->waveshaping_function
,
815 oscillator_ptr
->waveshaping_drive
);
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
827 zyn_oscillator_filter(
828 struct zyn_oscillator
* oscillator_ptr
)
840 if (oscillator_ptr
->Pfiltertype
== 0)
845 par
= 1.0 - oscillator_ptr
->Pfilterpar1
/ 128.0;
846 par2
= oscillator_ptr
->Pfilterpar2
/ 127.0;
850 for (i
= 1 ; i
< OSCIL_SIZE
/ 2 ; i
++)
853 switch(oscillator_ptr
->Pfiltertype
)
857 gain
= pow(1.0 - par
* par
* par
* 0.99, i
);
858 tmp
= par2
* par2
* par2
* par2
* 0.5 + 0.0001;
861 gain
= pow(gain
, 10.0) / pow(tmp
, 9.0);
866 gain
= 1.0 - pow(1.0 - par
* par
, i
+ 1);
867 gain
= pow(gain
, par2
* 2.0 + 0.1);
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
);
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
);
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);
899 tmp
= pow(par2
, 0.33);
900 gain
= (i
+ 1 > pow(2, (1.0 - par
) * 10) ? 0.0 : 1.0) * par2
+ (1.0 - par2
);
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)
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
);
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
);
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)
930 gain
= cos(par
* par
* PI
/ 2.0 * tmp
); // cos
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)
939 gain
= sin(par
* par
* PI
/ 2.0 * tmp
); // sin
942 case 12:p2
=1.0-par
+0.2;
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
950 tmp
= (int)pow(2.0, (1.0 - par
) * 7.2);
954 gain
= pow(2.0, par2
* par2
* 8.0);
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
];
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
988 zyn_oscillator_modulation(
989 struct zyn_oscillator
* oscillator_ptr
)
992 float modulationpar1
;
993 float modulationpar2
;
994 float modulationpar3
;
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)
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
)
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;
1026 modulationpar1
= (pow(2, modulationpar1
* 7.0) - 1.0) / 100.0;
1027 modulationpar3
= 1.0 + floor(pow(2, modulationpar3
* 5.0) - 1.0);
1030 modulationpar1
= (pow(2, modulationpar1
* 9.0) - 1.0) / 100.0;
1031 modulationpar3
= 0.01 + (pow(2, modulationpar3
* 16.0) - 1.0) / 10.0;
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
);
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
]);
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
)
1085 t
= t
* modulationpar3
+ sin((t
+ modulationpar2
) * 2.0 * PI
) * modulationpar1
;
1089 t
= t
+ sin((t
* modulationpar3
+ modulationpar2
) * 2.0 * PI
) * modulationpar1
;
1093 t
= t
+ pow((1.0 - cos((t
+ modulationpar2
) * 2.0 * PI
)) * 0.5, modulationpar3
) * modulationpar1
;
1097 t
= (t
- floor(t
)) * OSCIL_SIZE
;
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
;
1106 zyn_fft_smps2freqs(oscillator_ptr
->fft
, oscillator_ptr
->temporary_samples_ptr
, &oscillator_ptr
->oscilFFTfreqs
);
1110 * Adjust the spectrum
1114 zyn_oscillator_spectrum_adjust(
1115 struct zyn_oscillator
* oscillator_ptr
)
1124 if (oscillator_ptr
->spectrum_adjust_type
== ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_NONE
)
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;
1137 par
= pow(5.0, par
);
1141 par
= pow(8.0, par
);
1144 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_DOWN
:
1145 par
= pow(10.0, (1.0 - par
) * 3.0) * 0.25;
1147 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_UP
:
1148 par
= pow(10.0, (1.0 - par
) * 3.0) * 0.25;
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);
1164 max
= sqrt(max
) / OSCIL_SIZE
* 2.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
);
1180 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_DOWN
:
1186 case ZYN_OSCILLATOR_SPECTRUM_ADJUST_TYPE_THERSHOLD_UP
:
1197 oscillator_ptr
->oscilFFTfreqs
.c
[i
] = mag
* cos(phase
);
1198 oscillator_ptr
->oscilFFTfreqs
.s
[i
] = mag
* sin(phase
);
1203 * Prepare the Oscillator
1207 zyn_oscillator_prepare(
1208 struct zyn_oscillator
* oscillator_ptr
)
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
)
1233 oscillator_ptr
->hmag
[i
] = exp(hmagnew
* log(0.01));
1236 oscillator_ptr
->hmag
[i
] = exp(hmagnew
* log(0.001));
1239 oscillator_ptr
->hmag
[i
] = exp(hmagnew
* log(0.0001));
1242 oscillator_ptr
->hmag
[i
] = exp(hmagnew
* log(0.00001));
1245 oscillator_ptr
->hmag
[i
] = 1.0 - hmagnew
;
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
)
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;
1281 for (j
= 0 ; j
< MAX_AD_HARMONICS
; j
++)
1283 if (oscillator_ptr
->Phmag
[j
] == 64)
1288 for (i
= 1 ; i
< OSCIL_SIZE
/ 2 ; i
++)
1292 if (k
>= OSCIL_SIZE
/ 2)
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
);
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;
1341 zyn_oscillator_defaults(
1342 struct zyn_oscillator
* oscillator_ptr
)
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
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
);
1425 zyn_oscillator_init(
1426 struct zyn_oscillator
* oscillator_ptr
,
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
);
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
);
1459 zyn_oscillator_new_rand_seed(
1460 struct zyn_oscillator
* oscillator_ptr
,
1461 unsigned int randseed
)
1463 oscillator_ptr
->randseed
= randseed
;
1468 zyn_oscillator_adaptive_harmonic(
1469 struct zyn_oscillator
* oscillator_ptr
,
1470 struct zyn_fft_freqs
* freqs_ptr
,
1473 struct zyn_fft_freqs inf
;
1485 if (oscillator_ptr
->Padaptiveharmonics
== 0 /* || freq < 1.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;
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
);
1526 for (i
= 0 ; i
< OSCIL_SIZE
/ 2 - 2 ; i
++)
1529 high
= (int)(i
* rap
);
1532 if (high
>= OSCIL_SIZE
/ 2 - 2)
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
;
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)
1556 if (fabs(hs
) < 0.000001)
1566 // corect the aplitude of the first harmonic
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
);
1586 zyn_oscillator_adaptive_harmonic_post_process(
1587 struct zyn_oscillator
* oscillator_ptr
,
1597 if (oscillator_ptr
->Padaptiveharmonics
<= 1)
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)
1614 for (i
= 0 ; i
< size
; i
++)
1616 // i=0 pt prima armonica,etc.
1626 nh
= (oscillator_ptr
->Padaptiveharmonics
- 3) / 2 + 2;
1627 sub_vs_add
= (oscillator_ptr
->Padaptiveharmonics
- 3) % 2;
1630 for (i
= 0 ; i
< size
; i
++)
1632 if ((i
+ 1) % nh
== 0)
1640 for (i
= 0 ; i
< size
/ nh
- 1 ; i
++)
1642 f
[(i
+ 1) * nh
- 1] += inf
[i
];
1649 * Get the oscillator function
1653 struct zyn_oscillator
* oscillator_ptr
,
1654 zyn_sample_type
*smps
,
1669 unsigned int realrnd
;
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 &&
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
];
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
)
1795 srand(oscillator_ptr
->randseed
);
1796 power
= oscillator_ptr
->Pamprandpower
/ 127.0;
1797 normalize
= 1.0 / (1.2 - power
);
1799 switch (oscillator_ptr
->Pamprandtype
)
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
;
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
;
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
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
];
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
]);
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
++)
1874 if (oscillator_ptr
->Prand
< 64)
1883 //computes the full spectrum of oscil from harmonics,phases and basefunc
1886 void getbasefunction(REALTYPE
*smps
);
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
1895 void convert2sine(int magtype
);
1897 //computes the basefunction and make the FFT; newbasefunc<0 = same basefunc
1898 void changebasefunction();
1902 //Filter the oscillator accotding to Pfiltertype and Pfilterpar
1905 //Adjust the spectrum
1906 void spectrumadjust();
1908 //Shift the harmonics
1909 void shiftharmonics();
1911 //Do the oscil modulation stuff
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
);
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
;
1931 zyn_fft_freqs_init(&freqs
, OSCIL_SIZE
/ 2);
1934 fft
= zyn_fft_create(OSCIL_SIZE
);
1935 zyn_fft_smps2freqs(fft
, oscil
,freqs
);
1936 zyn_fft_destroy(fft
);
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;
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
);
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
++){
1976 spc
[i
-1]=sqrt(oscilFFTfreqs
.c
[i
]*oscilFFTfreqs
.c
[i
]
1977 +oscilFFTfreqs
.s
[i
]*oscilFFTfreqs
.s
[i
]);
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
]);
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(){
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;
2013 * Get the base function for UI
2016 OscilGen::getcurrentbasefunction(REALTYPE
*smps
)
2018 if (Pcurrentbasefunc
!=0)
2020 zyn_fft_freqs2smps(m_fft
, basefuncFFTfreqs
, smps
);
2025 getbasefunction(smps
);