home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 November / CMCD1104.ISO / Software / Complet / ZynAddFX / Setup_ZynAddSubFX-2.1.1.exe / OscilGen.C < prev    next >
Encoding:
C/C++ Source or Header  |  2004-08-25  |  32.7 KB  |  1,157 lines

  1. /*
  2.   ZynAddSubFX - a software synthesizer
  3.  
  4.   OscilGen.C - Waveform generator for ADnote
  5.   Copyright (C) 2002-2004 Nasca Octavian Paul
  6.   Author: Nasca Octavian Paul
  7.  
  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.
  11.  
  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.
  16.  
  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
  20.  
  21. */
  22.  
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <stdio.h>
  26.  
  27. #include "OscilGen.h"
  28. #include "../Effects/Distorsion.h"
  29.  
  30. OscilGen::OscilGen(FFTwrapper *fft_,Resonance *res_):Presets(){
  31.     setpresettype("Poscilgen");
  32.     fft=fft_;
  33.     res=res_;
  34.     oscilFFTfreqs=new REALTYPE[OSCIL_SIZE];
  35.  
  36.     basefuncFFTfreqsQ=NULL;
  37.     basefuncFFTfreqs=NULL;
  38.     outoscilFFTfreqs=NULL;
  39.     randseed=1;
  40.     ADvsPAD=false;
  41.  
  42.     defaults();
  43. };
  44.  
  45. OscilGen::~OscilGen(){
  46.     if (basefuncFFTfreqs!=NULL) delete [] basefuncFFTfreqs;
  47.     if (basefuncFFTfreqsQ!=NULL) delete [] basefuncFFTfreqsQ;
  48.     delete [] oscilFFTfreqs;
  49. };
  50.  
  51.  
  52. void OscilGen::defaults(){
  53.     oldbasefunc=0;oldbasepar=64;oldhmagtype=0;oldwaveshapingfunction=0;oldwaveshaping=64;
  54.     oldbasefuncmodulation=0;oldharmonicshift=0;oldbasefuncmodulationpar1=0;oldbasefuncmodulationpar2=0;oldbasefuncmodulationpar3=0;
  55.     oldmodulation=0;oldmodulationpar1=0;oldmodulationpar2=0;oldmodulationpar3=0;
  56.     for (int i=0;i<MAX_AD_HARMONICS;i++){
  57.     hmag[i]=0.0;
  58.     hphase[i]=0.0;
  59.     Phmag[i]=64;
  60.     Phphase[i]=64;
  61.     };
  62.     Phmag[0]=127;
  63.     Phmagtype=0;
  64.     if (ADvsPAD) Prand=127;//max phase randomness (usefull if the oscil will be imported to a ADsynth from a PADsynth
  65.     else Prand=64;//no randomness
  66.  
  67.     Pcurrentbasefunc=0;
  68.     Pbasefuncpar=64;
  69.  
  70.     Pbasefuncmodulation=0;
  71.     Pbasefuncmodulationpar1=64;
  72.     Pbasefuncmodulationpar2=64;
  73.     Pbasefuncmodulationpar3=32;
  74.  
  75.     Pmodulation=0;
  76.     Pmodulationpar1=64;
  77.     Pmodulationpar2=64;
  78.     Pmodulationpar3=32;
  79.  
  80.     Pwaveshapingfunction=0;
  81.     Pwaveshaping=64;
  82.     Pfiltertype=0;
  83.     Pfilterpar1=64;
  84.     Pfilterpar2=64;
  85.     Pfilterbeforews=0;
  86.     Psatype=0;
  87.     Psapar=64;
  88.  
  89.     Pamprandpower=64;
  90.     Pamprandtype=0;
  91.     
  92.     Pharmonicshift=0;
  93.     Pharmonicshiftfirst=0;
  94.  
  95.     Padaptiveharmonics=0;
  96.     Padaptiveharmonicspower=100;
  97.     Padaptiveharmonicsbasefreq=128;
  98.     
  99.     if (basefuncFFTfreqsQ!=NULL) {
  100.     delete(basefuncFFTfreqsQ);
  101.     basefuncFFTfreqsQ=NULL;
  102.     };
  103.     if (basefuncFFTfreqs!=NULL){
  104.     delete(basefuncFFTfreqs);
  105.     basefuncFFTfreqs=NULL;
  106.     };
  107.     
  108.     for (int i=0;i<OSCIL_SIZE;i++) oscilFFTfreqs[i]=0.0;
  109.     oscilprepared=0;
  110.     oldfilterpars=0;oldsapars=0;
  111.     prepare();
  112. };
  113.  
  114. void OscilGen::convert2sine(int magtype){
  115.     REALTYPE mag[MAX_AD_HARMONICS],phase[MAX_AD_HARMONICS];
  116.     REALTYPE oscil[OSCIL_SIZE],freqs[OSCIL_SIZE];
  117.  
  118.     get(oscil,-1.0);
  119.     FFTwrapper *fft=new FFTwrapper(OSCIL_SIZE);
  120.     fft->smps2freqs(oscil,freqs);
  121.     delete(fft);
  122.  
  123.     REALTYPE max=0.0;
  124.         
  125.     mag[0]=0;
  126.     phase[0]=0;
  127.     for (int i=0;i<MAX_AD_HARMONICS;i++){
  128.         mag[i]=sqrt(pow(oscilFFTfreqs[OSCIL_SIZE-i-1],2)+pow(oscilFFTfreqs[i+1],2.0));
  129.     phase[i]=atan2(oscilFFTfreqs[i+1],oscilFFTfreqs[OSCIL_SIZE-i-1]);
  130.     if (max<mag[i]) max=mag[i];
  131.     };
  132.     if (max<0.00001) max=1.0;
  133.     
  134.     defaults();
  135.         
  136.     for (int i=0;i<MAX_AD_HARMONICS-1;i++){
  137.     REALTYPE newmag=mag[i]/max;
  138.     REALTYPE newphase=phase[i];
  139.  
  140.     Phmag[i]=(int) ((newmag)*64.0)+64;
  141.     
  142.     Phphase[i]=64-(int) (64.0*newphase/PI);
  143.     if (Phphase[i]>127) Phphase[i]=127;
  144.     
  145.     if (Phmag[i]==64) Phphase[i]=64;
  146.     };
  147.     
  148.     prepare();
  149. };
  150.  
  151.  
  152. /* 
  153.  * Base Functions - START 
  154.  */
  155. REALTYPE OscilGen::basefunc_pulse(REALTYPE x,REALTYPE a){
  156.     return((fmod(x,1.0)<a)?-1.0:1.0);
  157. };
  158.  
  159. REALTYPE OscilGen::basefunc_saw(REALTYPE x,REALTYPE a){
  160.     if (a<0.00001) a=0.00001;
  161.     else if (a>0.99999) a=0.99999;
  162.     x=fmod(x,1);
  163.     if (x<a) return(x/a*2.0-1.0);
  164.     else return((1.0-x)/(1.0-a)*2.0-1.0);
  165. };
  166.  
  167. REALTYPE OscilGen::basefunc_triangle(REALTYPE x,REALTYPE a){
  168.     x=fmod(x+0.25,1);
  169.     a=1-a;
  170.     if (a<0.00001) a=0.00001;
  171.     if (x<0.5) x=x*4-1.0;
  172.     else x=(1.0-x)*4-1.0;
  173.     x/=-a;
  174.     if (x<-1.0) x=-1.0;
  175.     if (x>1.0) x=1.0;
  176.     return(x);
  177. };
  178.  
  179. REALTYPE OscilGen::basefunc_power(REALTYPE x,REALTYPE a){
  180.     x=fmod(x,1);
  181.     if (a<0.00001) a=0.00001;
  182.     else if (a>0.99999) a=0.99999;
  183.     return(pow(x,exp((a-0.5)*10.0))*2.0-1.0);
  184. };
  185.  
  186. REALTYPE OscilGen::basefunc_gauss(REALTYPE x,REALTYPE a){
  187.     x=fmod(x,1)*2.0-1.0;
  188.     if (a<0.00001) a=0.00001;
  189.     return(exp(-x*x*(exp(a*8)+5.0))*2.0-1.0);
  190. };
  191.  
  192. REALTYPE OscilGen::basefunc_diode(REALTYPE x,REALTYPE a){
  193.     if (a<0.00001) a=0.00001;
  194.     else if (a>0.99999) a=0.99999;
  195.     a=a*2.0-1.0;
  196.     x=cos((x+0.5)*2.0*PI)-a;
  197.     if (x<0.0) x=0.0;
  198.     return(x/(1.0-a)*2-1.0);
  199. };
  200.  
  201. REALTYPE OscilGen::basefunc_abssine(REALTYPE x,REALTYPE a){
  202.     x=fmod(x,1);
  203.     if (a<0.00001) a=0.00001;
  204.     else if (a>0.99999) a=0.99999;
  205.     return(sin(pow(x,exp((a-0.5)*5.0))*PI)*2.0-1.0);
  206. };
  207.  
  208. REALTYPE OscilGen::basefunc_pulsesine(REALTYPE x,REALTYPE a){
  209.     if (a<0.00001) a=0.00001;
  210.     x=(fmod(x,1)-0.5)*exp((a-0.5)*log(128));
  211.     if (x<-0.5) x=-0.5;
  212.     else if (x>0.5) x=0.5;
  213.     x=sin(x*PI*2.0);
  214.     return(x);
  215. };
  216.  
  217. REALTYPE OscilGen::basefunc_stretchsine(REALTYPE x,REALTYPE a){
  218.     x=fmod(x+0.5,1)*2.0-1.0;
  219.     a=(a-0.5)*4;if (a>0.0) a*=2;
  220.     a=pow(3.0,a);
  221.     REALTYPE b=pow(fabs(x),a);
  222.     if (x<0) b=-b;
  223.     return(-sin(b*PI));
  224. };
  225.  
  226. REALTYPE OscilGen::basefunc_chirp(REALTYPE x,REALTYPE a){
  227.     x=fmod(x,1.0)*2.0*PI;
  228.     a=(a-0.5)*4;if (a<0.0) a*=2.0;
  229.     a=pow(3.0,a);
  230.     return(sin(x/2.0)*sin(a*x*x));
  231. };
  232.  
  233. REALTYPE OscilGen::basefunc_absstretchsine(REALTYPE x,REALTYPE a){
  234.     x=fmod(x+0.5,1)*2.0-1.0;
  235.     a=(a-0.5)*9;
  236.     a=pow(3.0,a);
  237.     REALTYPE b=pow(fabs(x),a);
  238.     if (x<0) b=-b;
  239.     return(-pow(sin(b*PI),2));
  240. };
  241.  
  242. REALTYPE OscilGen::basefunc_chebyshev(REALTYPE x,REALTYPE a){
  243.     a=a*a*a*30.0+1.0;
  244.     return(cos(acos(x*2.0-1.0)*a));
  245. };
  246.  
  247. REALTYPE OscilGen::basefunc_sqr(REALTYPE x,REALTYPE a){
  248.     a=a*a*a*a*160.0+0.001;
  249.     return(-atan(sin(x*2.0*PI)*a));
  250. };
  251. /* 
  252.  * Base Functions - END
  253.  */
  254.  
  255.  
  256. /* 
  257.  * Get the base function
  258.  */
  259. void OscilGen::getbasefunction(REALTYPE *smps){
  260.     int i;    
  261.     REALTYPE par=(Pbasefuncpar+0.5)/128.0;
  262.     if (Pbasefuncpar==64) par=0.5;
  263.     
  264.     REALTYPE basefuncmodulationpar1=Pbasefuncmodulationpar1/127.0,
  265.          basefuncmodulationpar2=Pbasefuncmodulationpar2/127.0,
  266.          basefuncmodulationpar3=Pbasefuncmodulationpar3/127.0;
  267.  
  268.     switch(Pbasefuncmodulation){
  269.         case 1:basefuncmodulationpar1=(pow(2,basefuncmodulationpar1*5.0)-1.0)/10.0;
  270.            basefuncmodulationpar3=floor((pow(2,basefuncmodulationpar3*5.0)-1.0));
  271.            if (basefuncmodulationpar3<0.9999) basefuncmodulationpar3=-1.0;
  272.         break;
  273.         case 2:basefuncmodulationpar1=(pow(2,basefuncmodulationpar1*5.0)-1.0)/10.0;
  274.            basefuncmodulationpar3=1.0+floor((pow(2,basefuncmodulationpar3*5.0)-1.0));
  275.             break;
  276.     case 3:basefuncmodulationpar1=(pow(2,basefuncmodulationpar1*7.0)-1.0)/10.0;
  277.            basefuncmodulationpar3=0.01+(pow(2,basefuncmodulationpar3*16.0)-1.0)/10.0;
  278.         break;
  279.     };    
  280.  
  281. //    printf("%.5f %.5f\n",basefuncmodulationpar1,basefuncmodulationpar3);
  282.  
  283.     for (i=0;i<OSCIL_SIZE;i++) {
  284.     REALTYPE t=i*1.0/OSCIL_SIZE;
  285.  
  286.     switch(Pbasefuncmodulation){
  287.         case 1:t=t*basefuncmodulationpar3+sin((t+basefuncmodulationpar2)*2.0*PI)*basefuncmodulationpar1;//rev
  288.         break;
  289.         case 2:t=t+sin((t*basefuncmodulationpar3+basefuncmodulationpar2)*2.0*PI)*basefuncmodulationpar1;//sine
  290.         break;
  291.         case 3:t=t+pow((1.0-cos((t+basefuncmodulationpar2)*2.0*PI))*0.5,basefuncmodulationpar3)*basefuncmodulationpar1;//power
  292.         break;
  293.     };
  294.     
  295.     t=t-floor(t);
  296.     
  297.     switch (Pcurrentbasefunc){
  298.             case 1:smps[i]=basefunc_triangle(t,par);
  299.             break;
  300.         case 2:smps[i]=basefunc_pulse(t,par);
  301.             break;
  302.         case 3:smps[i]=basefunc_saw(t,par);
  303.             break;
  304.         case 4:smps[i]=basefunc_power(t,par);
  305.             break;
  306.         case 5:smps[i]=basefunc_gauss(t,par);
  307.             break;
  308.         case 6:smps[i]=basefunc_diode(t,par);
  309.             break;
  310.         case 7:smps[i]=basefunc_abssine(t,par);
  311.             break;
  312.         case 8:smps[i]=basefunc_pulsesine(t,par);
  313.             break;
  314.         case 9:smps[i]=basefunc_stretchsine(t,par);
  315.            break;
  316.         case 10:smps[i]=basefunc_chirp(t,par);
  317.            break;
  318.         case 11:smps[i]=basefunc_absstretchsine(t,par);
  319.            break;
  320.         case 12:smps[i]=basefunc_chebyshev(t,par);
  321.            break;
  322.         case 13:smps[i]=basefunc_sqr(t,par);
  323.            break;
  324.         default:smps[i]=-sin(2.0*PI*i/OSCIL_SIZE);
  325.     };
  326.     };
  327. };
  328.  
  329. /* 
  330.  * Filter the oscillator
  331.  */
  332. void OscilGen::oscilfilter(){
  333.     if (Pfiltertype==0) return;
  334.     REALTYPE par=1.0-Pfilterpar1/128.0;
  335.     REALTYPE par2=Pfilterpar2/127.0;
  336.     REALTYPE max=0.0,tmp=0.0,p2,x;
  337.     for (int i=1;i<OSCIL_SIZE/2;i++){
  338.     REALTYPE gain=1.0;
  339.     switch(Pfiltertype){
  340.         case 1: gain=pow(1.0-par*par*par*0.99,i);//lp
  341.             tmp=par2*par2*par2*par2*0.5+0.0001;
  342.             if (gain<tmp) gain=pow(gain,10.0)/pow(tmp,9.0);
  343.             break;
  344.         case 2: gain=1.0-pow(1.0-par*par,i+1);//hp1
  345.             gain=pow(gain,par2*2.0+0.1);
  346.             break;
  347.         case 3: if (par<0.2) par=par*0.25+0.15;
  348.             gain=1.0-pow(1.0-par*par*0.999+0.001,i*0.05*i+1.0);//hp1b
  349.             tmp=pow(5.0,par2*2.0);
  350.             gain=pow(gain,tmp);
  351.             break;
  352.         case 4: gain=i+1-pow(2,(1.0-par)*7.5);//bp1
  353.             gain=1.0/(1.0+gain*gain/(i+1.0));
  354.             tmp=pow(5.0,par2*2.0);
  355.             gain=pow(gain,tmp);
  356.             if (gain<1e-5) gain=1e-5;
  357.             break;
  358.         case 5: gain=i+1-pow(2,(1.0-par)*7.5);//bs1
  359.             gain=pow(atan(gain/(i/10.0+1))/1.57,6);
  360.             gain=pow(gain,par2*par2*3.9+0.1);
  361.             break;
  362.         case 6: tmp=pow(par2,0.33);
  363.             gain=(i+1>pow(2,(1.0-par)*10)?0.0:1.0)*par2+(1.0-par2);//lp2
  364.             break;
  365.         case 7: tmp=pow(par2,0.33);
  366.             //tmp=1.0-(1.0-par2)*(1.0-par2);
  367.             gain=(i+1>pow(2,(1.0-par)*7)?1.0:0.0)*par2+(1.0-par2);//hp2
  368.             if (Pfilterpar1==0) gain=1.0;
  369.             break;
  370.         case 8: tmp=pow(par2,0.33);
  371.             //tmp=1.0-(1.0-par2)*(1.0-par2);
  372.                 gain=(fabs(pow(2,(1.0-par)*7)-i)>i/2+1?0.0:1.0)*par2+(1.0-par2);//bp2
  373.             break;
  374.         case 9: tmp=pow(par2,0.33);
  375.             gain=(fabs(pow(2,(1.0-par)*7)-i)<i/2+1?0.0:1.0)*par2+(1.0-par2);//bs2
  376.             break;
  377.         case 10:tmp=pow(5.0,par2*2.0-1.0);
  378.             tmp=pow(i/32.0,tmp)*32.0;
  379.             if (Pfilterpar2==64) tmp=i;
  380.             gain=cos(par*par*PI/2.0*tmp);//cos
  381.            gain*=gain;
  382.            break;
  383.         case 11:tmp=pow(5.0,par2*2.0-1.0);
  384.             tmp=pow(i/32.0,tmp)*32.0;
  385.             if (Pfilterpar2==64) tmp=i;
  386.             gain=sin(par*par*PI/2.0*tmp);//sin
  387.            gain*=gain;
  388.            break;
  389.         case 12:p2=1.0-par+0.2;
  390.             x=i/(64.0*p2*p2); 
  391.              if (x<0.0) x=0.0;
  392.                 else if (x>1.0) x=1.0;
  393.              tmp=pow(1.0-par2,2.0);
  394.              gain=cos(x*PI)*(1.0-tmp)+1.01+tmp;//low shelf
  395.             break;
  396.         case 13:tmp=(int) (pow(2.0,(1.0-par)*7.2));
  397.             gain=1.0;
  398.             if (i==(int) (tmp)) gain=pow(2.0,par2*par2*8.0);
  399.              break;
  400.     };
  401.     
  402.     
  403.     oscilFFTfreqs[OSCIL_SIZE-i]*=gain;
  404.     oscilFFTfreqs[i]*=gain;
  405.     REALTYPE tmp=oscilFFTfreqs[OSCIL_SIZE-i]*oscilFFTfreqs[OSCIL_SIZE-i]+
  406.              oscilFFTfreqs[i]*oscilFFTfreqs[i];
  407.     if (max<tmp) max=tmp;
  408.     };
  409.  
  410.     max=sqrt(max);
  411.     if (max<1e-10) max=1.0;
  412.     for (int i=1;i<OSCIL_SIZE;i++) oscilFFTfreqs[i]/=max; 
  413. };
  414.  
  415. /* 
  416.  * Change the base function
  417.  */
  418. void OscilGen::changebasefunction(){
  419.     if (basefuncFFTfreqs!=NULL) {
  420.         delete(basefuncFFTfreqs);
  421.         basefuncFFTfreqs=NULL;
  422.     };
  423.  
  424.     if (basefuncFFTfreqs==NULL) basefuncFFTfreqs=new REALTYPE[OSCIL_SIZE];
  425.     if (Pcurrentbasefunc!=0) {
  426.         // I use basefuncfreq for temporary store of the time-domain data 
  427.     // because I don't wish "eat" too much memory with another array
  428.         getbasefunction(basefuncFFTfreqs);
  429.     fft->smps2freqs(basefuncFFTfreqs,NULL);//perform FFT
  430.     basefuncFFTfreqs[0]=0.0;
  431.     } else {
  432.     delete basefuncFFTfreqs;
  433.     basefuncFFTfreqs=NULL;
  434.     }
  435.     oscilprepared=0;
  436.     oldbasefunc=Pcurrentbasefunc;
  437.     oldbasepar=Pbasefuncpar;
  438.     oldbasefuncmodulation=Pbasefuncmodulation;
  439.     oldbasefuncmodulationpar1=Pbasefuncmodulationpar1;
  440.     oldbasefuncmodulationpar2=Pbasefuncmodulationpar2;
  441.     oldbasefuncmodulationpar3=Pbasefuncmodulationpar3;
  442. };
  443.  
  444. /* 
  445.  * Waveshape
  446.  */
  447. void OscilGen::waveshape(){
  448.     int i;
  449.  
  450.     oldwaveshapingfunction=Pwaveshapingfunction;
  451.     oldwaveshaping=Pwaveshaping;
  452.     if (Pwaveshapingfunction==0) return;
  453.  
  454.     oscilFFTfreqs[0]=0.0;//remove the DC
  455.     //reduce the amplitude of the freqs near the nyquist
  456.     for (i=1;i<OSCIL_SIZE/8;i++) {
  457.     REALTYPE tmp=i/(OSCIL_SIZE/8.0);
  458.     oscilFFTfreqs[OSCIL_SIZE/2+i-1]*=tmp;
  459.     oscilFFTfreqs[OSCIL_SIZE/2-i+1]*=tmp;
  460.     };
  461.     fft->freqs2smps(oscilFFTfreqs,NULL);
  462.     //now the oscilFFTfreqs contains *time-domain data* of samples
  463.     //I don't want to allocate another array for this
  464.  
  465.     //Normalize
  466.     REALTYPE max=0.0;
  467.     for (i=0;i<OSCIL_SIZE;i++) 
  468.     if (max<fabs(oscilFFTfreqs[i])) max=fabs(oscilFFTfreqs[i]);
  469.     if (max<0.00001) max=1.0;
  470.     max=1.0/max;for (i=0;i<OSCIL_SIZE;i++) oscilFFTfreqs[i]*=max;
  471.     
  472.     //Do the waveshaping
  473.     waveshapesmps(OSCIL_SIZE,oscilFFTfreqs,Pwaveshapingfunction,Pwaveshaping);
  474.     
  475.     fft->smps2freqs(oscilFFTfreqs,NULL);//perform FFT
  476. };
  477.  
  478.  
  479. /* 
  480.  * Do the Frequency Modulation of the Oscil
  481.  */
  482. void OscilGen::modulation(){
  483.     int i;
  484.  
  485.     oldmodulation=Pmodulation;
  486.     oldmodulationpar1=Pmodulationpar1;
  487.     oldmodulationpar2=Pmodulationpar2;
  488.     oldmodulationpar3=Pmodulationpar3;
  489.     if (Pmodulation==0) return;
  490.  
  491.  
  492.     REALTYPE modulationpar1=Pmodulationpar1/127.0,
  493.          modulationpar2=0.5-Pmodulationpar2/127.0,
  494.          modulationpar3=Pmodulationpar3/127.0;
  495.  
  496.     switch(Pmodulation){
  497.         case 1:modulationpar1=(pow(2,modulationpar1*7.0)-1.0)/100.0;
  498.            modulationpar3=floor((pow(2,modulationpar3*5.0)-1.0));
  499.            if (modulationpar3<0.9999) modulationpar3=-1.0;
  500.         break;
  501.         case 2:modulationpar1=(pow(2,modulationpar1*7.0)-1.0)/100.0;
  502.            modulationpar3=1.0+floor((pow(2,modulationpar3*5.0)-1.0));
  503.             break;
  504.     case 3:modulationpar1=(pow(2,modulationpar1*9.0)-1.0)/100.0;
  505.            modulationpar3=0.01+(pow(2,modulationpar3*16.0)-1.0)/10.0;
  506.         break;
  507.     };    
  508.  
  509.     oscilFFTfreqs[0]=0.0;//remove the DC
  510.     //reduce the amplitude of the freqs near the nyquist
  511.     for (i=1;i<OSCIL_SIZE/8;i++) {
  512.     REALTYPE tmp=i/(OSCIL_SIZE/8.0);
  513.     oscilFFTfreqs[OSCIL_SIZE/2+i-1]*=tmp;
  514.     oscilFFTfreqs[OSCIL_SIZE/2-i+1]*=tmp;
  515.     };
  516.     fft->freqs2smps(oscilFFTfreqs,NULL);
  517.     //now the oscilFFTfreqs contains *time-domain data* of samples
  518.     //I don't want to allocate another array for this
  519.  
  520.     int extra_points=2;
  521.     REALTYPE *in=new REALTYPE[OSCIL_SIZE+extra_points];
  522.  
  523.     //Normalize
  524.     REALTYPE max=0.0;
  525.     for (i=0;i<OSCIL_SIZE;i++) 
  526.     if (max<fabs(oscilFFTfreqs[i])) max=fabs(oscilFFTfreqs[i]);
  527.     if (max<0.00001) max=1.0;
  528.     max=1.0/max;for (i=0;i<OSCIL_SIZE;i++) in[i]=oscilFFTfreqs[i]*max;
  529.     for (i=0;i<extra_points;i++) in[i+OSCIL_SIZE]=oscilFFTfreqs[i]*max;
  530.     
  531.     //Do the modulation
  532.     for (i=0;i<OSCIL_SIZE;i++) {
  533.     REALTYPE t=i*1.0/OSCIL_SIZE;
  534.  
  535.     switch(Pmodulation){
  536.         case 1:t=t*modulationpar3+sin((t+modulationpar2)*2.0*PI)*modulationpar1;//rev
  537.         break;
  538.         case 2:t=t+sin((t*modulationpar3+modulationpar2)*2.0*PI)*modulationpar1;//sine
  539.         break;
  540.         case 3:t=t+pow((1.0-cos((t+modulationpar2)*2.0*PI))*0.5,modulationpar3)*modulationpar1;//power
  541.         break;
  542.     };
  543.     
  544.     t=(t-floor(t))*OSCIL_SIZE;
  545.     
  546.     int poshi=(int) t;
  547.     REALTYPE poslo=t-floor(t);
  548.  
  549.     oscilFFTfreqs[i]=in[poshi]*(1.0-poslo)+in[poshi+1]*poslo;
  550.     };
  551.  
  552.     delete(in);
  553.     fft->smps2freqs(oscilFFTfreqs,NULL);//perform FFT
  554. };
  555.  
  556.  
  557.  
  558. /* 
  559.  * Adjust the spectrum
  560.  */
  561. void OscilGen::spectrumadjust(){
  562.     if (Psatype==0) return;
  563.     REALTYPE par=Psapar/127.0;
  564.     switch(Psatype){
  565.     case 1: par=1.0-par*2.0;
  566.         if (par>=0.0) par=pow(5.0,par);
  567.             else par=pow(8.0,par);
  568.         break;
  569.     case 2: par=pow(10.0,(1.0-par)*3.0)*0.00095;
  570.         break;
  571.     case 3: par=pow(10.0,(1.0-par)*3.0)*0.00095;
  572.         break;
  573.     };
  574.     REALTYPE max=0.0;
  575.     for (int i=0;i<OSCIL_SIZE/2-1;i++){ 
  576.     REALTYPE tmp=pow(oscilFFTfreqs[OSCIL_SIZE-i-1],2)+pow(oscilFFTfreqs[i+1],2.0);
  577.     if (max<tmp) max=tmp;
  578.     };
  579.     max=sqrt(max);
  580.     if (max<1e-8) max=1.0;
  581.     
  582.     for (int i=0;i<OSCIL_SIZE/2-1;i++){
  583.         REALTYPE mag=sqrt(pow(oscilFFTfreqs[OSCIL_SIZE-i-1],2)+pow(oscilFFTfreqs[i+1],2.0))/max;
  584.     REALTYPE phase=atan2(oscilFFTfreqs[i+1],oscilFFTfreqs[OSCIL_SIZE-i-1]);
  585.     
  586.     switch (Psatype){
  587.         case 1: mag=pow(mag,par);
  588.             break;
  589.         case 2: if (mag<par) mag=0.0;
  590.             break;
  591.         case 3: mag/=par;
  592.             if (mag>1.0) mag=1.0;
  593.             break;
  594.     };
  595.     oscilFFTfreqs[OSCIL_SIZE-i-1]=mag*cos(phase);
  596.     oscilFFTfreqs[i+1]=mag*sin(phase);
  597.     };
  598.     
  599. };
  600.  
  601. void OscilGen::shiftharmonics(){
  602.     if (Pharmonicshift==0) return;
  603.     
  604.     REALTYPE hc,hs;
  605.     int harmonicshift=-Pharmonicshift;
  606.     
  607.     if (harmonicshift>0){
  608.     for (int i=OSCIL_SIZE/2-2;i>=0;i--){ 
  609.         int oldh=i-harmonicshift;
  610.         if (oldh<0){
  611.         hc=0.0;
  612.         hs=0.0;
  613.         } else {
  614.         hc=oscilFFTfreqs[oldh+1];
  615.         hs=oscilFFTfreqs[OSCIL_SIZE-oldh-1];
  616.         };
  617.         oscilFFTfreqs[i+1]=hc;
  618.         oscilFFTfreqs[OSCIL_SIZE-i-1]=hs;
  619.     };
  620.     } else {
  621.     for (int i=0;i<OSCIL_SIZE/2-1;i++){ 
  622.         int oldh=i+abs(harmonicshift);
  623.         if (oldh>=(OSCIL_SIZE/2-1)){
  624.         hc=0.0;
  625.         hs=0.0;
  626.         } else {
  627.         hc=oscilFFTfreqs[oldh+1];
  628.         hs=oscilFFTfreqs[OSCIL_SIZE-oldh-1];
  629.         if (fabs(hc)<0.000001) hc=0.0;
  630.         if (fabs(hs)<0.000001) hs=0.0;
  631.         };
  632.         
  633.         oscilFFTfreqs[i+1]=hc;
  634.         oscilFFTfreqs[OSCIL_SIZE-i-1]=hs;
  635.     };
  636.     };
  637.     
  638.     oscilFFTfreqs[0]=0.0;
  639. };
  640.  
  641. /* 
  642.  * Prepare the Oscillator
  643.  */
  644. void OscilGen::prepare(){
  645.    int i,j,k;
  646.    REALTYPE a,b,c,d,hmagnew;
  647.   
  648.    if ((oldbasepar!=Pbasefuncpar)||(oldbasefunc!=Pcurrentbasefunc)||
  649.     (oldbasefuncmodulation!=Pbasefuncmodulation)||
  650.         (oldbasefuncmodulationpar1!=Pbasefuncmodulationpar1)||
  651.     (oldbasefuncmodulationpar2!=Pbasefuncmodulationpar2)||
  652.     (oldbasefuncmodulationpar3!=Pbasefuncmodulationpar3)) 
  653.      changebasefunction();
  654.  
  655.    for (i=0;i<MAX_AD_HARMONICS;i++) hphase[i]=(Phphase[i]-64.0)/64.0*PI/(i+1);
  656.  
  657.    for (i=0;i<MAX_AD_HARMONICS;i++){
  658.       hmagnew=1.0-fabs(Phmag[i]/64.0-1.0);
  659.       switch(Phmagtype){
  660.     case 1:hmag[i]=exp(hmagnew*log(0.01)); break;
  661.     case 2:hmag[i]=exp(hmagnew*log(0.001));break;
  662.     case 3:hmag[i]=exp(hmagnew*log(0.0001));break;
  663.     case 4:hmag[i]=exp(hmagnew*log(0.00001));break;
  664.     default:hmag[i]=1.0-hmagnew;
  665.             break; 
  666.       };
  667.  
  668.       if (Phmag[i]<64) hmag[i]=-hmag[i];
  669.    };
  670.     
  671.    //remove the harmonics where Phmag[i]==64
  672.    for (i=0;i<MAX_AD_HARMONICS;i++) if (Phmag[i]==64) hmag[i]=0.0;
  673.  
  674.  
  675.    for (i=0;i<OSCIL_SIZE;i++) oscilFFTfreqs[i]=0.0;
  676.    if (Pcurrentbasefunc==0) {//the sine case
  677.     for (i=0;i<MAX_AD_HARMONICS;i++){
  678.         oscilFFTfreqs[i+1]=-hmag[i]*sin(hphase[i]*(i+1))/2.0;
  679.         oscilFFTfreqs[OSCIL_SIZE-i-1]=hmag[i]*cos(hphase[i]*(i+1))/2.0;
  680.     };
  681.    } else {
  682.     for (j=0;j<MAX_AD_HARMONICS;j++){
  683.         if (Phmag[j]==64) continue;
  684.         for (i=1;i<OSCIL_SIZE/2;i++){
  685.         k=i*(j+1);if (k>OSCIL_SIZE/2) break;
  686.         a=basefuncFFTfreqs[i];
  687.         b=basefuncFFTfreqs[OSCIL_SIZE-i];
  688.         c=hmag[j]*cos(hphase[j]*k);
  689.         d=hmag[j]*sin(hphase[j]*k);
  690.         oscilFFTfreqs[k]+=a*c-b*d;
  691.         oscilFFTfreqs[OSCIL_SIZE-k]+=a*d+b*c;
  692.         };
  693.     };
  694.  
  695.    };
  696.  
  697.    if (Pharmonicshiftfirst!=0)  shiftharmonics();
  698.  
  699.  
  700.  
  701.    if (Pfilterbeforews==0){
  702.         waveshape();
  703.     oscilfilter();
  704.     } else {
  705.     oscilfilter();
  706.         waveshape();
  707.     };
  708.  
  709.    modulation();
  710.    spectrumadjust();
  711.    if (Pharmonicshiftfirst==0)  shiftharmonics();
  712.  
  713. /*   //normalize (sum or RMS) - the "Full RMS" normalisation is located in the "::get()" function
  714.    if ((Pnormalizemethod==0)||(Pnormalizemethod==1)){
  715.         REALTYPE sum=0;
  716.     for (j=1;j<OSCIL_SIZE/2;j++) {
  717.         REALTYPE term=oscilFFTfreqs[j]*oscilFFTfreqs[j]+oscilFFTfreqs[OSCIL_SIZE-j]*oscilFFTfreqs[OSCIL_SIZE-j];
  718.         if (Pnormalizemethod==0) sum+=sqrt(term);
  719.         else sum+=term;
  720.         };
  721.         if (sum<0.00000001) sum=1.0;
  722.     if (Pnormalizemethod==1) sum=sqrt(sum)*2.0;
  723.     sum*=0.5;   
  724.     for (j=1;j<OSCIL_SIZE;j++) oscilFFTfreqs[j]/=sum; 
  725.    };
  726. */
  727.    oscilFFTfreqs[0]=0.0;//remove the DC
  728.  
  729.    if (ANTI_ALIAS==0) {
  730.     //Perform the IFFT in case of Antialiasing is disabled
  731.     REALTYPE *tmp=new REALTYPE[OSCIL_SIZE];
  732.     for (i=0;i<OSCIL_SIZE;i++) tmp[i]=oscilFFTfreqs[i];
  733.     fft->freqs2smps(tmp,oscilFFTfreqs);
  734.     //now the oscilFFTfreqs contains samples data in TIME-DOMAIN (despite it's name)
  735.     delete(tmp);
  736.    };
  737.    oldhmagtype=Phmagtype;
  738. //   oldnormalizemethod=Pnormalizemethod;
  739.    oldharmonicshift=Pharmonicshift+Pharmonicshiftfirst*256;
  740.  
  741.    oscilprepared=1;
  742. };
  743.  
  744. void OscilGen::adaptiveharmonic(REALTYPE *freqs,REALTYPE freq){
  745.     if ((Padaptiveharmonics==0)||(freq<1.0)) return;
  746.  
  747.     REALTYPE *infreqs=new REALTYPE[OSCIL_SIZE];
  748.     for (int i=0;i<OSCIL_SIZE;i++) {
  749.     infreqs[i]=freqs[i];
  750.     freqs[i]=0.0;
  751.     };
  752.     
  753.     REALTYPE hc=0.0,hs=0.0;
  754.     REALTYPE basefreq=30.0*pow(10.0,Padaptiveharmonicsbasefreq/128.0);
  755.     REALTYPE power=(Padaptiveharmonicspower+1.0)/101.0;
  756.     
  757.     REALTYPE rap=freq/basefreq;
  758.  
  759.     rap=pow(rap,power);
  760.  
  761. //    printf("bf=%g (%d) pow=%g(%d)\n",basefreq,Padaptiveharmonicsbasefreq,power,Padaptiveharmonicspower);
  762.     
  763.     bool down=false;
  764.     if (rap>1.0) {
  765.     rap=1.0/rap;
  766.     down=true;
  767.     };
  768.     
  769.     for (int i=0;i<OSCIL_SIZE/2-2;i++){ 
  770.     REALTYPE h=i*rap;
  771.         int high=(int)(i*rap);
  772.     REALTYPE low=fmod(h,1.0);
  773.  
  774.         if (high>=(OSCIL_SIZE/2-2)){
  775.         break;
  776.     } else {
  777.         if (down){
  778.         freqs[high+1]+=infreqs[i+1];
  779.         freqs[OSCIL_SIZE-high-1]+=infreqs[OSCIL_SIZE-i-1];
  780.         } else {
  781.         hc=infreqs[high+1]*(1.0-low)+infreqs[high+2]*low;
  782.         hs=infreqs[OSCIL_SIZE-high-1]*(1.0-low)+infreqs[OSCIL_SIZE-high-2]*low;
  783.         };
  784.         if (fabs(hc)<0.000001) hc=0.0;
  785.         if (fabs(hs)<0.000001) hs=0.0;
  786.     };
  787.     
  788.     if (!down){    
  789.         if (i==0) {//corect the aplitude of the first harmonic
  790.         hc*=rap;
  791.         hs*=rap;
  792.         };
  793.         freqs[i+1]=hc;
  794.         freqs[OSCIL_SIZE-i-1]=hs;
  795.     };
  796.     };
  797.     
  798.     delete(infreqs);
  799. };
  800.  
  801.  
  802.  
  803. /* 
  804.  * Get the oscillator function
  805.  */
  806. short int OscilGen::get(REALTYPE *smps,REALTYPE freqHz){
  807.     return(this->get(smps,freqHz,0));
  808. };
  809.  
  810. void OscilGen::newrandseed(unsigned int randseed){
  811.     this->randseed=randseed;
  812. };
  813.  
  814. /* 
  815.  * Get the oscillator function
  816.  */
  817. short int OscilGen::get(REALTYPE *smps,REALTYPE freqHz,int resonance){
  818.     int i;
  819.     int nyquist,outpos;
  820.     
  821.     if ((oldbasepar!=Pbasefuncpar)||(oldbasefunc!=Pcurrentbasefunc)||(oldhmagtype!=Phmagtype)
  822.     ||(oldwaveshaping!=Pwaveshaping)||(oldwaveshapingfunction!=Pwaveshapingfunction)) oscilprepared=0;
  823.     if (oldfilterpars!=Pfiltertype*256+Pfilterpar1+Pfilterpar2*65536+Pfilterbeforews*16777216){ 
  824.     oscilprepared=0;
  825.     oldfilterpars=Pfiltertype*256+Pfilterpar1+Pfilterpar2*65536+Pfilterbeforews*16777216;
  826.     };
  827.     if (oldsapars!=Psatype*256+Psapar){ 
  828.     oscilprepared=0;
  829.     oldsapars=Psatype*256+Psapar;
  830.     };
  831.  
  832.     if ((oldbasefuncmodulation!=Pbasefuncmodulation)||
  833.         (oldbasefuncmodulationpar1!=Pbasefuncmodulationpar1)||
  834.     (oldbasefuncmodulationpar2!=Pbasefuncmodulationpar2)||
  835.     (oldbasefuncmodulationpar3!=Pbasefuncmodulationpar3)) 
  836.         oscilprepared=0;
  837.  
  838.     if ((oldmodulation!=Pmodulation)||
  839.         (oldmodulationpar1!=Pmodulationpar1)||
  840.     (oldmodulationpar2!=Pmodulationpar2)||
  841.     (oldmodulationpar3!=Pmodulationpar3)) 
  842.         oscilprepared=0;
  843.  
  844.     if (oldharmonicshift!=Pharmonicshift+Pharmonicshiftfirst*256) oscilprepared=0;
  845.     
  846.     if (oscilprepared!=1) prepare();
  847.  
  848.     outpos=(int)((RND*2.0-1.0)*(REALTYPE) OSCIL_SIZE*(Prand-64.0)/64.0);
  849.     outpos=(outpos+2*OSCIL_SIZE) % OSCIL_SIZE;
  850.     
  851.     if (ANTI_ALIAS==0) {//Anti-Aliasing OFF
  852.     for (i=0;i<OSCIL_SIZE;i++) smps[i]=oscilFFTfreqs[i];
  853.     if (Prand!=64) return(outpos);
  854.         else return(0);
  855.     };
  856.  
  857.     //Anti-Aliasing ON
  858.     outoscilFFTfreqs=new REALTYPE[OSCIL_SIZE];
  859.     for (i=0;i<OSCIL_SIZE;i++) outoscilFFTfreqs[i]=0.0;
  860.  
  861.     nyquist=(int)(0.5*SAMPLE_RATE/fabs(freqHz))+2;
  862.     if (ADvsPAD) nyquist=(int)(OSCIL_SIZE/2);
  863.     if (nyquist>OSCIL_SIZE/2) nyquist=OSCIL_SIZE/2;
  864.     
  865.     
  866.     int realnyquist=nyquist;
  867.     
  868.     if (Padaptiveharmonics!=0) nyquist=OSCIL_SIZE/2;
  869.     for (i=1;i<nyquist-1;i++) {
  870.         outoscilFFTfreqs[i]=oscilFFTfreqs[i];
  871.         outoscilFFTfreqs[OSCIL_SIZE-i]=oscilFFTfreqs[OSCIL_SIZE-i];
  872.     };
  873.  
  874.     adaptiveharmonic(outoscilFFTfreqs,freqHz);
  875.  
  876.     nyquist=realnyquist;
  877.     if (Padaptiveharmonics){//do the antialiasing in the case of adaptive harmonics
  878.         for (i=nyquist;i<OSCIL_SIZE/2;i++) {
  879.         outoscilFFTfreqs[i]=0;
  880.         outoscilFFTfreqs[OSCIL_SIZE-i]=0;
  881.     };
  882.     };
  883.  
  884.  
  885.     // Randomness (each harmonic), the block type is computed 
  886.     // in ADnote by setting start position according to this setting
  887.     if ((Prand>64)&&(freqHz>=0.0)&&(!ADvsPAD)){
  888.         REALTYPE rnd,angle,a,b,c,d;
  889.         rnd=PI*pow((Prand-64.0)/64.0,2.0);
  890.         for (i=1;i<nyquist-1;i++){//to Nyquist only for AntiAliasing
  891.             angle=rnd*i*RND;
  892.         a=outoscilFFTfreqs[i];
  893.         b=outoscilFFTfreqs[OSCIL_SIZE-i];
  894.         c=cos(angle);
  895.         d=sin(angle);
  896.         outoscilFFTfreqs[i]=a*c-b*d;
  897.         outoscilFFTfreqs[OSCIL_SIZE-i]=a*d+b*c;
  898.     };    
  899.     };
  900.  
  901.  
  902.  
  903.  
  904.     //Harmonic Amplitude Randomness
  905.     if ((freqHz>0.1)&&(!ADvsPAD)) {
  906.     unsigned int realrnd=rand();
  907.     srand(randseed);
  908.     REALTYPE power=Pamprandpower/127.0;
  909.     REALTYPE normalize=1.0/(1.2-power);
  910.     switch (Pamprandtype){
  911.         case 1: power=power*2.0-0.5;
  912.             power=pow(15.0,power);
  913.             for (i=1;i<nyquist-1;i++){
  914. //            REALTYPE x=(i-1)/3.0;if (x>1.0) x=1.0;x*=x;
  915.                     REALTYPE amp=pow(RND,power)*normalize;
  916. //            amp=1.0-x+amp*x;
  917.             outoscilFFTfreqs[i]*=amp;
  918.             outoscilFFTfreqs[OSCIL_SIZE-i]*=amp;
  919.             };
  920.             break;
  921.         case 2: power=power*2.0-0.5;
  922.             power=pow(15.0,power)*2.0;
  923.             REALTYPE rndfreq=2*PI*RND;
  924.             for (i=1;i<nyquist-1;i++){
  925.                     REALTYPE amp=pow(fabs(sin(i*rndfreq)),power)*normalize;
  926.             outoscilFFTfreqs[i]*=amp;
  927.             outoscilFFTfreqs[OSCIL_SIZE-i]*=amp;
  928.             };
  929.             break;
  930.     };    
  931.     srand(realrnd+1);
  932.     };
  933.  
  934.  
  935.     if ((freqHz>0.1)&&(resonance!=0)) res->applyres(nyquist-1,outoscilFFTfreqs,freqHz);
  936.  
  937.     //Full RMS normalize
  938.     REALTYPE sum=0;
  939.     for (int j=1;j<OSCIL_SIZE/2;j++) {
  940.         REALTYPE term=outoscilFFTfreqs[j]*outoscilFFTfreqs[j]+outoscilFFTfreqs[OSCIL_SIZE-j]*outoscilFFTfreqs[OSCIL_SIZE-j];
  941.         sum+=term;
  942.     };
  943.     if (sum<0.000001) sum=1.0;    
  944.     sum=sqrt(sum);
  945.     for (int j=1;j<OSCIL_SIZE;j++) outoscilFFTfreqs[j]/=sum; 
  946.    
  947.  
  948.     if ((ADvsPAD)&&(freqHz>0.1)){//in this case the smps will contain the freqs
  949.         for (i=1;i<OSCIL_SIZE/2;i++) smps[i-1]=sqrt(outoscilFFTfreqs[i]*outoscilFFTfreqs[i]+outoscilFFTfreqs[OSCIL_SIZE-i]*outoscilFFTfreqs[OSCIL_SIZE-i]);
  950.     } else {
  951.     fft->freqs2smps(outoscilFFTfreqs,smps);
  952.         for (i=0;i<OSCIL_SIZE;i++) smps[i]*=0.25;//correct the amplitude
  953.     };
  954.  
  955.     delete(outoscilFFTfreqs);
  956.     outoscilFFTfreqs=NULL;
  957.     if (Prand<64) return(outpos);
  958.     else return(0);
  959. };
  960.  
  961.  
  962. /* 
  963.  * Get the spectrum of the oscillator for the UI
  964.  */
  965. void OscilGen::getspectrum(int n, REALTYPE *spc,int what){
  966.     if (n>OSCIL_SIZE/2) n=OSCIL_SIZE/2;
  967.     for (int i=1;i<n;i++){
  968.     if (what==0){
  969.         if (ANTI_ALIAS==0) spc[i-1]=0.0;//there is no spectrum available
  970.         else spc[i-1]=sqrt(oscilFFTfreqs[i]*oscilFFTfreqs[i]+
  971.              oscilFFTfreqs[OSCIL_SIZE-i]*oscilFFTfreqs[OSCIL_SIZE-i]);
  972.     } else {
  973.         if (Pcurrentbasefunc==0) spc[i-1]=((i==1)?(1.0):(0.0));
  974.         else spc[i-1]=sqrt(basefuncFFTfreqs[i]*basefuncFFTfreqs[i]+
  975.              basefuncFFTfreqs[OSCIL_SIZE-i]*basefuncFFTfreqs[OSCIL_SIZE-i]);
  976.     };
  977.     };
  978. }
  979.  
  980.  
  981. /* 
  982.  * Convert the oscillator as base function
  983.  */
  984. void OscilGen::useasbase(){
  985.    int i;
  986.  
  987.    if (Pcurrentbasefunc==0) basefuncFFTfreqs=new REALTYPE[OSCIL_SIZE];
  988.    
  989.    for (i=0;i<OSCIL_SIZE;i++) basefuncFFTfreqs[i]=oscilFFTfreqs[i];
  990.  
  991.    oldbasefunc=Pcurrentbasefunc=127;
  992.  
  993.    prepare();
  994. };
  995.  
  996.  
  997. /* 
  998.  * Get the base function for UI
  999.  */
  1000. void OscilGen::getcurrentbasefunction(REALTYPE *smps){
  1001.     if (Pcurrentbasefunc!=0) {
  1002.     for (int i=0;i<OSCIL_SIZE;i++) smps[i]=basefuncFFTfreqs[i];
  1003.     fft->freqs2smps(smps,NULL);
  1004.     } else getbasefunction(smps);//the sine case
  1005. };
  1006.  
  1007.  
  1008. void OscilGen::add2XML(XMLwrapper *xml){
  1009.     xml->addpar("harmonic_mag_type",Phmagtype);
  1010.  
  1011.     xml->addpar("base_function",Pcurrentbasefunc);
  1012.     xml->addpar("base_function_par",Pbasefuncpar);
  1013.     xml->addpar("base_function_modulation",Pbasefuncmodulation);
  1014.     xml->addpar("base_function_modulation_par1",Pbasefuncmodulationpar1);
  1015.     xml->addpar("base_function_modulation_par2",Pbasefuncmodulationpar2);
  1016.     xml->addpar("base_function_modulation_par3",Pbasefuncmodulationpar3);
  1017.  
  1018.     xml->addpar("modulation",Pmodulation);
  1019.     xml->addpar("modulation_par1",Pmodulationpar1);
  1020.     xml->addpar("modulation_par2",Pmodulationpar2);
  1021.     xml->addpar("modulation_par3",Pmodulationpar3);
  1022.  
  1023.     xml->addpar("wave_shaping",Pwaveshaping);
  1024.     xml->addpar("wave_shaping_function",Pwaveshapingfunction);
  1025.  
  1026.     xml->addpar("filter_type",Pfiltertype);
  1027.     xml->addpar("filter_par1",Pfilterpar1);
  1028.     xml->addpar("filter_par2",Pfilterpar2);
  1029.     xml->addpar("filter_before_wave_shaping",Pfilterbeforews);
  1030.  
  1031.     xml->addpar("spectrum_adjust_type",Psatype);
  1032.     xml->addpar("spectrum_adjust_par",Psapar);
  1033.  
  1034.     xml->addpar("rand",Prand);
  1035.     xml->addpar("amp_rand_type",Pamprandtype);
  1036.     xml->addpar("amp_rand_power",Pamprandpower);
  1037.  
  1038.     xml->addpar("harmonic_shift",Pharmonicshift);
  1039.     xml->addparbool("harmonic_shift_first",Pharmonicshiftfirst);
  1040.  
  1041.     xml->addpar("adaptive_harmonics",Padaptiveharmonics);
  1042.     xml->addpar("adaptive_harmonics_base_frequency",Padaptiveharmonicsbasefreq);
  1043.     xml->addpar("adaptive_harmonics_power",Padaptiveharmonicspower);
  1044.  
  1045.     xml->beginbranch("HARMONICS");
  1046.     for (int n=0;n<MAX_AD_HARMONICS;n++){
  1047.         if ((Phmag[n]==64)&&(Phphase[n]==64)) continue;
  1048.         xml->beginbranch("HARMONIC",n+1);
  1049.         xml->addpar("mag",Phmag[n]);
  1050.         xml->addpar("phase",Phphase[n]);
  1051.         xml->endbranch();
  1052.     };
  1053.     xml->endbranch();
  1054.     
  1055.     if (Pcurrentbasefunc==127){
  1056.     REALTYPE max=0.0;
  1057.  
  1058.     for (int i=0;i<OSCIL_SIZE;i++) if (max<fabs(basefuncFFTfreqs[i])) max=fabs(basefuncFFTfreqs[i]);
  1059.     if (max<0.00000001) max=1.0;
  1060.  
  1061.     xml->beginbranch("BASE_FUNCTION");
  1062.         for (int i=1;i<OSCIL_SIZE/2;i++){
  1063.         REALTYPE xc=basefuncFFTfreqs[i]/max;
  1064.             REALTYPE xs=basefuncFFTfreqs[OSCIL_SIZE-i]/max;
  1065.         if ((fabs(xs)>0.00001)&&(fabs(xs)>0.00001)){
  1066.             xml->beginbranch("BF_HARMONIC",i);
  1067.             xml->addparreal("cos",xc);
  1068.             xml->addparreal("sin",xs);
  1069.             xml->endbranch();
  1070.         };
  1071.         };
  1072.     xml->endbranch();
  1073.     };
  1074. };
  1075.  
  1076.  
  1077. void OscilGen::getfromXML(XMLwrapper *xml){
  1078.  
  1079.     Phmagtype=xml->getpar127("harmonic_mag_type",Phmagtype);
  1080.  
  1081.     Pcurrentbasefunc=xml->getpar127("base_function",Pcurrentbasefunc);
  1082.     Pbasefuncpar=xml->getpar127("base_function_par",Pbasefuncpar);
  1083.  
  1084.     Pbasefuncmodulation=xml->getpar127("base_function_modulation",Pbasefuncmodulation);
  1085.     Pbasefuncmodulationpar1=xml->getpar127("base_function_modulation_par1",Pbasefuncmodulationpar1);
  1086.     Pbasefuncmodulationpar2=xml->getpar127("base_function_modulation_par2",Pbasefuncmodulationpar2);
  1087.     Pbasefuncmodulationpar3=xml->getpar127("base_function_modulation_par3",Pbasefuncmodulationpar3);
  1088.  
  1089.     Pmodulation=xml->getpar127("modulation",Pmodulation);
  1090.     Pmodulationpar1=xml->getpar127("modulation_par1",Pmodulationpar1);
  1091.     Pmodulationpar2=xml->getpar127("modulation_par2",Pmodulationpar2);
  1092.     Pmodulationpar3=xml->getpar127("modulation_par3",Pmodulationpar3);
  1093.  
  1094.     Pwaveshaping=xml->getpar127("wave_shaping",Pwaveshaping);
  1095.     Pwaveshapingfunction=xml->getpar127("wave_shaping_function",Pwaveshapingfunction);
  1096.  
  1097.     Pfiltertype=xml->getpar127("filter_type",Pfiltertype);
  1098.     Pfilterpar1=xml->getpar127("filter_par1",Pfilterpar1);
  1099.     Pfilterpar2=xml->getpar127("filter_par2",Pfilterpar2);
  1100.     Pfilterbeforews=xml->getpar127("filter_before_wave_shaping",Pfilterbeforews);
  1101.  
  1102.     Psatype=xml->getpar127("spectrum_adjust_type",Psatype);
  1103.     Psapar=xml->getpar127("spectrum_adjust_par",Psapar);
  1104.  
  1105.     Prand=xml->getpar127("rand",Prand);
  1106.     Pamprandtype=xml->getpar127("amp_rand_type",Pamprandtype);
  1107.     Pamprandpower=xml->getpar127("amp_rand_power",Pamprandpower);
  1108.  
  1109.     Pharmonicshift=xml->getpar("harmonic_shift",Pharmonicshift,-64,64);
  1110.     Pharmonicshiftfirst=xml->getparbool("harmonic_shift_first",Pharmonicshiftfirst);
  1111.  
  1112.     Padaptiveharmonics=xml->getpar("adaptive_harmonics",Padaptiveharmonics,0,127);
  1113.     Padaptiveharmonicsbasefreq=xml->getpar("adaptive_harmonics_base_frequency",Padaptiveharmonicsbasefreq,0,255);
  1114.     Padaptiveharmonicspower=xml->getpar("adaptive_harmonics_power",Padaptiveharmonicspower,0,100);
  1115.  
  1116.  
  1117.     if (xml->enterbranch("HARMONICS")){
  1118.     Phmag[0]=64;Phphase[0]=64;
  1119.     for (int n=0;n<MAX_AD_HARMONICS;n++){
  1120.         if (xml->enterbranch("HARMONIC",n+1)==0) continue;
  1121.         Phmag[n]=xml->getpar127("mag",64);
  1122.         Phphase[n]=xml->getpar127("phase",64);
  1123.         xml->exitbranch();
  1124.     };
  1125.      xml->exitbranch();
  1126.     };
  1127.     
  1128.     if (Pcurrentbasefunc!=0) changebasefunction();
  1129.     
  1130.     
  1131.     if (xml->enterbranch("BASE_FUNCTION")){
  1132.         for (int i=1;i<OSCIL_SIZE/2;i++){
  1133.         if (xml->enterbranch("BF_HARMONIC",i)){
  1134.             basefuncFFTfreqs[i]=xml->getparreal("cos",0.0);
  1135.             basefuncFFTfreqs[OSCIL_SIZE-i]=xml->getparreal("sin",0.0);
  1136.             xml->exitbranch();
  1137.         };
  1138.  
  1139.  
  1140.         };
  1141.     xml->exitbranch();
  1142.  
  1143.     REALTYPE max=0.0;
  1144.  
  1145.     basefuncFFTfreqs[0]=0.0;
  1146.     for (int i=0;i<OSCIL_SIZE;i++) if (max<fabs(basefuncFFTfreqs[i])) max=fabs(basefuncFFTfreqs[i]);
  1147.     if (max<0.00000001) max=1.0;
  1148.  
  1149.     for (int i=0;i<OSCIL_SIZE;i++) if (basefuncFFTfreqs[i]) basefuncFFTfreqs[i]/=max;
  1150.     };
  1151.  
  1152.  
  1153. };
  1154.  
  1155.  
  1156.  
  1157.