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.

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