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.

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