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.

188 lines
6.7KB

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