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.

321 lines
11KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. ADnote.h - The "additive" synthesizer
  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 AD_NOTE_H
  12. #define AD_NOTE_H
  13. #include "SynthNote.h"
  14. #include "Envelope.h"
  15. #include "LFO.h"
  16. #include "../Params/ADnoteParameters.h"
  17. #include "../Params/Controller.h"
  18. //Globals
  19. /**FM amplitude tune*/
  20. #define FM_AMP_MULTIPLIER 14.71280603f
  21. #define OSCIL_SMP_EXTRA_SAMPLES 5
  22. /**The "additive" synthesizer*/
  23. class ADnote:public SynthNote
  24. {
  25. public:
  26. /**Constructor.
  27. * @param pars Note Parameters
  28. * @param spars Synth Engine Agnostic Parameters*/
  29. ADnote(ADnoteParameters *pars, SynthParams &spars,
  30. WatchManager *wm=0, const char *prefix=0);
  31. /**Destructor*/
  32. ~ADnote();
  33. /**Alters the playing note for legato effect*/
  34. void legatonote(LegatoParams pars);
  35. int noteout(float *outl, float *outr);
  36. void releasekey();
  37. bool finished() const;
  38. void entomb(void);
  39. virtual SynthNote *cloneLegato(void) override;
  40. private:
  41. /**Changes the frequency of an oscillator.
  42. * @param nvoice voice to run computations on
  43. * @param in_freq new frequency*/
  44. void setfreq(int nvoice, float in_freq);
  45. /**Set the frequency of the modulator oscillator*/
  46. void setfreqFM(int nvoice, float in_freq);
  47. /**Computes relative frequency for unison and unison's vibratto.
  48. * Note: Must be called before setfreq* functions.*/
  49. void compute_unison_freq_rap(int nvoice);
  50. /**Compute parameters for next tick*/
  51. void computecurrentparameters();
  52. /**Initializes All Parameters*/
  53. void initparameters(WatchManager *wm, const char *prefix);
  54. /**Deallocate/Cleanup given voice*/
  55. void KillVoice(int nvoice);
  56. /**Deallocate Note resources and voice resources*/
  57. void KillNote();
  58. /**Get the Voice's base frequency*/
  59. inline float getvoicebasefreq(int nvoice) const;
  60. /**Get modulator's base frequency*/
  61. inline float getFMvoicebasefreq(int nvoice) const;
  62. /**Compute the Oscillator's samples.
  63. * Affects tmpwave_unison and updates oscposhi/oscposlo*/
  64. inline void ComputeVoiceOscillator_LinearInterpolation(int nvoice);
  65. /**Compute the Oscillator's samples.
  66. * Affects tmpwave_unison and updates oscposhi/oscposlo
  67. * @todo remove this declaration if it is commented out*/
  68. inline void ComputeVoiceOscillator_CubicInterpolation(int nvoice);
  69. /**Computes the Oscillator samples with morphing.
  70. * updates tmpwave_unison*/
  71. inline void ComputeVoiceOscillatorMorph(int nvoice);
  72. /**Computes the Ring Modulated Oscillator.*/
  73. inline void ComputeVoiceOscillatorRingModulation(int nvoice);
  74. /**Computes the Frequency Modulated Oscillator.
  75. * @param FMmode modulation type 0=Phase 1=Frequency*/
  76. inline void ComputeVoiceOscillatorFrequencyModulation(int nvoice,
  77. int FMmode);
  78. // inline void ComputeVoiceOscillatorFrequencyModulation(int nvoice);
  79. /**TODO*/
  80. inline void ComputeVoiceOscillatorPitchModulation(int nvoice);
  81. /**Generate Noise Samples for Voice*/
  82. inline void ComputeVoiceWhiteNoise(int nvoice);
  83. inline void ComputeVoicePinkNoise(int nvoice);
  84. inline void ComputeVoiceDC(int nvoice);
  85. /**Fadein in a way that removes clicks but keep sound "punchy"*/
  86. inline void fadein(float *smps) const;
  87. //GLOBALS
  88. ADnoteParameters &pars;
  89. unsigned char stereo; //if the note is stereo (allows note Panning)
  90. int midinote;
  91. float velocity, basefreq;
  92. ONOFFTYPE NoteEnabled;
  93. /*****************************************************************/
  94. /* GLOBAL PARAMETERS */
  95. /*****************************************************************/
  96. struct Global {
  97. void kill(Allocator &memory);
  98. void initparameters(const ADnoteGlobalParam &param,
  99. const SYNTH_T &synth,
  100. const AbsTime &time,
  101. class Allocator &memory,
  102. float basefreq, float velocity,
  103. bool stereo,
  104. WatchManager *wm,
  105. const char *prefix);
  106. /******************************************
  107. * FREQUENCY GLOBAL PARAMETERS *
  108. ******************************************/
  109. float Detune; //cents
  110. Envelope *FreqEnvelope;
  111. LFO *FreqLfo;
  112. /********************************************
  113. * AMPLITUDE GLOBAL PARAMETERS *
  114. ********************************************/
  115. float Volume; // [ 0 .. 1 ]
  116. float Panning; // [ 0 .. 1 ]
  117. Envelope *AmpEnvelope;
  118. LFO *AmpLfo;
  119. float Fadein_adjustment;
  120. struct {
  121. int Enabled;
  122. float initialvalue, dt, t;
  123. } Punch;
  124. /******************************************
  125. * FILTER GLOBAL PARAMETERS *
  126. ******************************************/
  127. ModFilter *Filter;
  128. Envelope *FilterEnvelope;
  129. LFO *FilterLfo;
  130. } NoteGlobalPar;
  131. /***********************************************************/
  132. /* VOICE PARAMETERS */
  133. /***********************************************************/
  134. struct Voice {
  135. void releasekey();
  136. void kill(Allocator &memory, const SYNTH_T &synth);
  137. /* If the voice is enabled */
  138. ONOFFTYPE Enabled;
  139. /* Voice Type (sound/noise)*/
  140. int noisetype;
  141. /* Filter Bypass */
  142. int filterbypass;
  143. /* Delay (ticks) */
  144. int DelayTicks;
  145. /* Waveform of the Voice */
  146. float *OscilSmp;
  147. /* preserved for phase mod PWM emulation. */
  148. int phase_offset;
  149. /* Range of waveform */
  150. float OscilSmpMin, OscilSmpMax;
  151. /************************************
  152. * FREQUENCY PARAMETERS *
  153. ************************************/
  154. int fixedfreq; //if the frequency is fixed to 440 Hz
  155. int fixedfreqET; //if the "fixed" frequency varies according to the note (ET)
  156. // cents = basefreq*VoiceDetune
  157. float Detune, FineDetune;
  158. // Bend adjustment
  159. float BendAdjust;
  160. float OffsetHz;
  161. Envelope *FreqEnvelope;
  162. LFO *FreqLfo;
  163. /***************************
  164. * AMPLITUDE PARAMETERS *
  165. ***************************/
  166. /* Panning 0.0f=left, 0.5f - center, 1.0f = right */
  167. float Panning;
  168. float Volume; // [-1.0f .. 1.0f]
  169. Envelope *AmpEnvelope;
  170. LFO *AmpLfo;
  171. /*************************
  172. * FILTER PARAMETERS *
  173. *************************/
  174. ModFilter *Filter;
  175. Envelope *FilterEnvelope;
  176. LFO *FilterLfo;
  177. /****************************
  178. * MODULLATOR PARAMETERS *
  179. ****************************/
  180. FMTYPE FMEnabled;
  181. unsigned char FMFreqFixed;
  182. int FMVoice;
  183. // Voice Output used by other voices if use this as modullator
  184. float *VoiceOut;
  185. /* Wave of the Voice */
  186. float *FMSmp;
  187. float FMVolume;
  188. float FMDetune; //in cents
  189. Envelope *FMFreqEnvelope;
  190. Envelope *FMAmpEnvelope;
  191. } NoteVoicePar[NUM_VOICES];
  192. /********************************************************/
  193. /* INTERNAL VALUES OF THE NOTE AND OF THE VOICES */
  194. /********************************************************/
  195. //pinking filter (Paul Kellet)
  196. float pinking[NUM_VOICES][14];
  197. //the size of unison for a single voice
  198. int unison_size[NUM_VOICES];
  199. //the stereo spread of the unison subvoices (0.0f=mono,1.0f=max)
  200. float unison_stereo_spread[NUM_VOICES];
  201. //fractional part (skip)
  202. float *oscposlo[NUM_VOICES], *oscfreqlo[NUM_VOICES];
  203. //integer part (skip)
  204. int *oscposhi[NUM_VOICES], *oscfreqhi[NUM_VOICES];
  205. //fractional part (skip) of the Modullator
  206. float *oscposloFM[NUM_VOICES], *oscfreqloFM[NUM_VOICES];
  207. //the unison base_value
  208. float *unison_base_freq_rap[NUM_VOICES];
  209. //how the unison subvoice's frequency is changed (1.0f for no change)
  210. float *unison_freq_rap[NUM_VOICES];
  211. //which subvoice has phase inverted
  212. bool *unison_invert_phase[NUM_VOICES];
  213. //unison vibratto
  214. struct {
  215. float amplitude; //amplitude which be added to unison_freq_rap
  216. float *step; //value which increments the position
  217. float *position; //between -1.0f and 1.0f
  218. } unison_vibratto[NUM_VOICES];
  219. //integer part (skip) of the Modullator
  220. unsigned int *oscposhiFM[NUM_VOICES], *oscfreqhiFM[NUM_VOICES];
  221. //used to compute and interpolate the amplitudes of voices and modullators
  222. float oldamplitude[NUM_VOICES],
  223. newamplitude[NUM_VOICES],
  224. FMoldamplitude[NUM_VOICES],
  225. FMnewamplitude[NUM_VOICES];
  226. //used by Frequency Modulation (for integration)
  227. float *FMoldsmp[NUM_VOICES];
  228. //temporary buffer
  229. float *tmpwavel;
  230. float *tmpwaver;
  231. int max_unison;
  232. float **tmpwave_unison;
  233. //Filter bypass samples
  234. float *bypassl, *bypassr;
  235. //interpolate the amplitudes
  236. float globaloldamplitude, globalnewamplitude;
  237. //1 - if it is the fitst tick (used to fade in the sound)
  238. char firsttick[NUM_VOICES];
  239. //1 if the note has portamento
  240. int portamento;
  241. //how the fine detunes are made bigger or smaller
  242. float bandwidthDetuneMultiplier;
  243. };
  244. #endif