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.

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