Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.1KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. OscilGen.h - Waveform generator for ADnote
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #ifndef OSCIL_GEN_H
  18. #define OSCIL_GEN_H
  19. #include "../globals.h"
  20. #include <rtosc/ports.h>
  21. #include "../Params/Presets.h"
  22. class OscilGen:public Presets
  23. {
  24. public:
  25. OscilGen(const SYNTH_T &synth, FFTwrapper *fft_, Resonance *res_);
  26. ~OscilGen();
  27. /**computes the full spectrum of oscil from harmonics,phases and basefunc*/
  28. void prepare();
  29. void prepare(fft_t *data);
  30. /**do the antialiasing(cut off higher freqs.),apply randomness and do a IFFT*/
  31. //returns where should I start getting samples, used in block type randomness
  32. short get(float *smps, float freqHz, int resonance = 0);
  33. //if freqHz is smaller than 0, return the "un-randomized" sample for UI
  34. void getbasefunction(float *smps);
  35. //called by UI
  36. void getspectrum(int n, float *spc, int what); //what=0 pt. oscil,1 pt. basefunc
  37. void getcurrentbasefunction(float *smps);
  38. /**convert oscil to base function*/
  39. void useasbase();
  40. void paste(OscilGen &o);
  41. void add2XML(XMLwrapper& xml);
  42. void defaults();
  43. void getfromXML(XMLwrapper& xml);
  44. void convert2sine();
  45. //Parameters
  46. /**
  47. * The hmag and hphase starts counting from 0, so the first harmonic(1) has the index 0,
  48. * 2-nd harmonic has index 1, ..the 128 harminic has index 127
  49. */
  50. unsigned char Phmag[MAX_AD_HARMONICS], Phphase[MAX_AD_HARMONICS]; //the MIDI parameters for mag. and phases
  51. /**The Type of magnitude:
  52. * 0 - Linear
  53. * 1 - dB scale (-40)
  54. * 2 - dB scale (-60)
  55. * 3 - dB scale (-80)
  56. * 4 - dB scale (-100)*/
  57. unsigned char Phmagtype;
  58. unsigned char Pcurrentbasefunc; //The base function used - 0=sin, 1=...
  59. unsigned char Pbasefuncpar; //the parameter of the base function
  60. unsigned char Pbasefuncmodulation; //what modulation is applied to the basefunc
  61. unsigned char Pbasefuncmodulationpar1, Pbasefuncmodulationpar2,
  62. Pbasefuncmodulationpar3; //the parameter of the base function modulation
  63. unsigned char Pwaveshaping, Pwaveshapingfunction;
  64. unsigned char Pfiltertype, Pfilterpar1, Pfilterpar2;
  65. bool Pfilterbeforews;
  66. unsigned char Psatype, Psapar; //spectrum adjust
  67. int Pharmonicshift; //how the harmonics are shifted
  68. int Pharmonicshiftfirst; //if the harmonic shift is done before waveshaping and filter
  69. unsigned char Pmodulation; //what modulation is applied to the oscil
  70. unsigned char Pmodulationpar1, Pmodulationpar2, Pmodulationpar3; //the parameter of the parameters
  71. /**Realtime parameters for ADnote*/
  72. /*the Randomness:
  73. 64=no randomness
  74. 63..0 - block type randomness - 0 is maximum
  75. 65..127 - each harmonic randomness - 127 is maximum*/
  76. unsigned char Prand;
  77. unsigned char Pamprandpower, Pamprandtype; //amplitude randomness
  78. unsigned char Padaptiveharmonics; //the adaptive harmonics status (off=0,on=1,etc..)
  79. unsigned char Padaptiveharmonicsbasefreq; //the base frequency of the adaptive harmonic (30..3000Hz)
  80. unsigned char Padaptiveharmonicspower; //the strength of the effect (0=off,100=full)
  81. unsigned char Padaptiveharmonicspar; //the parameters in 2,3,4.. modes of adaptive harmonics
  82. //makes a new random seed for Amplitude Randomness
  83. //this should be called every note on event
  84. void newrandseed(unsigned int randseed);
  85. bool ADvsPAD; //if it is used by ADsynth or by PADsynth
  86. static const rtosc::MergePorts ports;
  87. static const rtosc::Ports non_realtime_ports;
  88. static const rtosc::Ports realtime_ports;
  89. /* Oscillator Frequencies -
  90. * this is different than the hamonics set-up by the user,
  91. * it may contains time-domain data if the antialiasing is turned off*/
  92. fft_t *oscilFFTfreqs;
  93. fft_t *pendingfreqs;
  94. private:
  95. //This array stores some termporary data and it has OSCIL_SIZE elements
  96. float *tmpsmps;
  97. fft_t *outoscilFFTfreqs;
  98. float *cachedbasefunc;
  99. bool cachedbasevalid;
  100. float hmag[MAX_AD_HARMONICS], hphase[MAX_AD_HARMONICS]; //the magnituides and the phases of the sine/nonsine harmonics
  101. FFTwrapper *fft;
  102. //computes the basefunction and make the FFT; newbasefunc<0 = same basefunc
  103. void changebasefunction(void);
  104. //Waveshaping
  105. void waveshape(fft_t *freqs);
  106. //Filter the oscillator accotding to Pfiltertype and Pfilterpar
  107. void oscilfilter(fft_t *freqs);
  108. //Adjust the spectrum
  109. void spectrumadjust(fft_t *freqs);
  110. //Shift the harmonics
  111. void shiftharmonics(fft_t *freqs);
  112. //Do the oscil modulation stuff
  113. void modulation(fft_t *freqs);
  114. float userfunc(float x);
  115. public:
  116. //Check system for needed updates
  117. bool needPrepare(void);
  118. private:
  119. //Do the adaptive harmonic stuff
  120. void adaptiveharmonic(fft_t *f, float freq);
  121. //Do the adaptive harmonic postprocessing (2n+1,2xS,2xA,etc..)
  122. //this function is called even for the user interface
  123. //this can be called for the sine and components, and for the spectrum
  124. //(that's why the sine and cosine components should be processed with a separate call)
  125. void adaptiveharmonicpostprocess(fft_t *f, int size);
  126. //Internal Data
  127. unsigned char oldbasefunc, oldbasepar, oldhmagtype,
  128. oldwaveshapingfunction, oldwaveshaping;
  129. int oldfilterpars, oldsapars, oldbasefuncmodulation,
  130. oldbasefuncmodulationpar1, oldbasefuncmodulationpar2,
  131. oldbasefuncmodulationpar3, oldharmonicshift;
  132. int oldmodulation, oldmodulationpar1, oldmodulationpar2,
  133. oldmodulationpar3;
  134. fft_t *basefuncFFTfreqs; //Base Function Frequencies
  135. int oscilprepared; //1 if the oscil is prepared, 0 if it is not prepared and is need to call ::prepare() before ::get()
  136. Resonance *res;
  137. unsigned int randseed;
  138. public:
  139. const SYNTH_T &synth;
  140. };
  141. typedef float (*filter_func)(unsigned int, float, float);
  142. filter_func getFilter(unsigned char func);
  143. typedef float (*base_func)(float, float);
  144. base_func getBaseFunction(unsigned char func);
  145. #endif