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.

ADnote.h 11KB

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