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.

317 lines
10KB

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