zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

ADnote.h (11473B)


      1 /*
      2   ZynAddSubFX - a software synthesizer
      3 
      4   ADnote.h - The "additive" synthesizer
      5   Copyright (C) 2002-2005 Nasca Octavian Paul
      6   Author: Nasca Octavian Paul
      7 
      8   This program is free software; you can redistribute it and/or
      9   modify it under the terms of the GNU General Public License
     10   as published by the Free Software Foundation; either version 2
     11   of the License, or (at your option) any later version.
     12 */
     13 
     14 #ifndef AD_NOTE_H
     15 #define AD_NOTE_H
     16 
     17 #include "SynthNote.h"
     18 #include "Envelope.h"
     19 #include "LFO.h"
     20 #include "Portamento.h"
     21 #include "../Params/ADnoteParameters.h"
     22 #include "../Params/Controller.h"
     23 #include "WatchPoint.h"
     24 
     25 //Globals
     26 
     27 /**FM amplitude tune*/
     28 #define FM_AMP_MULTIPLIER 14.71280603f
     29 
     30 #define OSCIL_SMP_EXTRA_SAMPLES 5
     31 
     32 namespace zyn {
     33 
     34 /**The "additive" synthesizer*/
     35 class ADnote:public SynthNote
     36 {
     37     public:
     38         /**Constructor.
     39          * @param pars Note Parameters
     40          * @param spars Synth Engine Agnostic Parameters*/
     41         ADnote(ADnoteParameters *pars, const SynthParams &spars,
     42                 WatchManager *wm=0, const char *prefix=0);
     43         /**Destructor*/
     44         ~ADnote();
     45 
     46         /**Alters the playing note for legato effect*/
     47         void legatonote(const LegatoParams &pars);
     48 
     49         int noteout(float *outl, float *outr);
     50         void releasekey();
     51         bool finished() const;
     52         void entomb(void);
     53 
     54 
     55         virtual SynthNote *cloneLegato(void) override;
     56     private:
     57 
     58         void setupVoice(int nvoice);
     59         int  setupVoiceUnison(int nvoice);
     60         void setupVoiceDetune(int nvoice);
     61         void setupVoiceMod(int nvoice, bool first_run = true);
     62         VecWatchPoint watch_be4_add,watch_after_add, watch_punch, watch_legato;
     63         /**Changes the frequency of an oscillator.
     64          * @param nvoice voice to run computations on
     65          * @param in_freq new frequency*/
     66         void setfreq(int nvoice, float in_freq);
     67         /**Set the frequency of the modulator oscillator*/
     68         void setfreqFM(int nvoice, float in_freq);
     69         /**Computes relative frequency for unison and unison's vibratto.
     70          * Note: Must be called before setfreq* functions.*/
     71         void compute_unison_freq_rap(int nvoice);
     72         /**Compute parameters for next tick*/
     73         void computecurrentparameters();
     74         /**Initializes All Parameters*/
     75         void initparameters(WatchManager *wm, const char *prefix);
     76         /**Deallocate/Cleanup given voice*/
     77         void KillVoice(int nvoice);
     78         /**Deallocate Note resources and voice resources*/
     79         void KillNote();
     80         /**Get the Voice's base frequency*/
     81         inline float getvoicebasefreq(int nvoice, float adjust_log2 = 0.0f) const;
     82         /**Get modulator's base frequency*/
     83         inline float getFMvoicebasefreq(int nvoice) const;
     84         /**Compute the Oscillator's samples.
     85          * Affects tmpwave_unison and updates oscposhi/oscposlo*/
     86         inline void ComputeVoiceOscillator_LinearInterpolation(int nvoice);
     87         /**Compute the Oscillator's samples.
     88          * Affects tmpwave_unison and updates oscposhi/oscposlo
     89          * @todo remove this declaration if it is commented out*/
     90         inline void ComputeVoiceOscillator_SincInterpolation(int nvoice);
     91         /**Compute the Oscillator's samples.
     92          * Affects tmpwave_unison and updates oscposhi/oscposlo
     93          * @todo remove this declaration if it is commented out*/
     94         inline void ComputeVoiceOscillator_CubicInterpolation(int nvoice);
     95         /**Computes the Oscillator samples with mixing.
     96          * updates tmpwave_unison*/
     97         inline void ComputeVoiceOscillatorMix(int nvoice);
     98         /**Computes the Ring Modulated Oscillator.*/
     99         inline void ComputeVoiceOscillatorRingModulation(int nvoice);
    100         /**Computes the Frequency Modulated Oscillator.
    101          * @param FMmode modulation type 0=Phase 1=Frequency*/
    102         inline void ComputeVoiceOscillatorFrequencyModulation(int nvoice,
    103                                                               FMTYPE FMmode);
    104         //  inline void ComputeVoiceOscillatorFrequencyModulation(int nvoice);
    105         /**TODO*/
    106         inline void ComputeVoiceOscillatorPitchModulation(int nvoice);
    107 
    108         /**Generate Noise Samples for Voice*/
    109         inline void ComputeVoiceWhiteNoise(int nvoice);
    110         inline void ComputeVoicePinkNoise(int nvoice);
    111         inline void ComputeVoiceDC(int nvoice);
    112 
    113         /**Fadein in a way that removes clicks but keep sound "punchy"*/
    114         inline void fadein(float *smps) const;
    115 
    116         //GLOBALS
    117         ADnoteParameters &pars;
    118         unsigned char     stereo; //if the note is stereo (allows note Panning)
    119         float note_log2_freq;
    120         float velocity;
    121 
    122         ONOFFTYPE   NoteEnabled;
    123 
    124         /*****************************************************************/
    125         /*                    GLOBAL PARAMETERS                          */
    126         /*****************************************************************/
    127 
    128         struct Global {
    129             void kill(Allocator &memory);
    130             void initparameters(const ADnoteGlobalParam &param,
    131                                 const SYNTH_T &synth,
    132                                 const AbsTime &time,
    133                                 class Allocator &memory,
    134                                 float basefreq, float velocity,
    135                                 bool stereo,
    136                                 WatchManager *wm,
    137                                 const char *prefix);
    138             /******************************************
    139             *     FREQUENCY GLOBAL PARAMETERS        *
    140             ******************************************/
    141             float Detune;  //cents
    142 
    143             Envelope *FreqEnvelope;
    144             LFO      *FreqLfo;
    145 
    146             /********************************************
    147             *     AMPLITUDE GLOBAL PARAMETERS          *
    148             ********************************************/
    149             float Volume;  // [ 0 .. 1 ]
    150 
    151             float Panning;  // [ 0 .. 1 ]
    152 
    153             Envelope *AmpEnvelope;
    154             LFO      *AmpLfo;
    155 
    156             float Fadein_adjustment;
    157             struct {
    158                 int   Enabled;
    159                 float initialvalue, dt, t;
    160             } Punch;
    161 
    162             /******************************************
    163             *        FILTER GLOBAL PARAMETERS        *
    164             ******************************************/
    165             ModFilter *Filter;
    166             Envelope  *FilterEnvelope;
    167             LFO       *FilterLfo;
    168         } NoteGlobalPar;
    169 
    170 
    171 
    172         /***********************************************************/
    173         /*                    VOICE PARAMETERS                     */
    174         /***********************************************************/
    175         struct Voice {
    176             void releasekey();
    177             void kill(Allocator &memory, const SYNTH_T &synth);
    178             /* If the voice is enabled */
    179             ONOFFTYPE Enabled;
    180 
    181             /* if AntiAliasing is enabled */
    182             bool AAEnabled;
    183 
    184             /* Voice Type (sound/noise)*/
    185             int noisetype;
    186 
    187             /* Filter Bypass */
    188             int filterbypass;
    189             /* Filter Fq Control Bypass */
    190             int filterFcCtlBypass;
    191 
    192             /* Delay (ticks) */
    193             int DelayTicks;
    194 
    195             /* Waveform of the Voice */
    196             float *OscilSmp;
    197 
    198             /* preserved for phase mod PWM emulation. */
    199             int phase_offset;
    200 
    201             /* Range of waveform */
    202             float OscilSmpMin, OscilSmpMax;
    203 
    204             /************************************
    205             *     FREQUENCY PARAMETERS          *
    206             ************************************/
    207             int fixedfreq; //if the frequency is fixed to 440 Hz
    208             int fixedfreqET; //if the "fixed" frequency varies according to the note (ET)
    209 
    210             // cents = basefreq*VoiceDetune
    211             float Detune, FineDetune;
    212 
    213             // Bend adjustment
    214             float BendAdjust;
    215 
    216             float OffsetHz;
    217 
    218             Envelope *FreqEnvelope;
    219             LFO      *FreqLfo;
    220 
    221 
    222             /***************************
    223             *   AMPLITUDE PARAMETERS   *
    224             ***************************/
    225 
    226             /* Panning 0.0f=left, 0.5f - center, 1.0f = right */
    227             float Panning;
    228             float Volume;  // [-1.0f .. 1.0f]
    229 
    230             Envelope *AmpEnvelope;
    231             LFO      *AmpLfo;
    232 
    233             /*************************
    234             *   FILTER PARAMETERS    *
    235             *************************/
    236             ModFilter *Filter;
    237             Envelope  *FilterEnvelope;
    238             LFO       *FilterLfo;
    239 
    240 
    241             /****************************
    242             *   MODULLATOR PARAMETERS   *
    243             ****************************/
    244 
    245             FMTYPE FMEnabled;
    246 
    247             unsigned char FMFreqFixed;
    248 
    249             int FMVoice;
    250 
    251             // Voice Output used by other voices if use this as modullator
    252             float *VoiceOut;
    253 
    254             /* Wave of the Voice */
    255             float *FMSmp;
    256 
    257             smooth_float FMVolume;
    258             float FMDetune;  //in cents
    259 
    260             Envelope *FMFreqEnvelope;
    261             Envelope *FMAmpEnvelope;
    262 
    263             /********************************************************/
    264             /*    INTERNAL VALUES OF THE NOTE AND OF THE VOICES     */
    265             /********************************************************/
    266 
    267             //pinking filter (Paul Kellet)
    268             float pinking[14];
    269 
    270             //the size of unison for a single voice
    271             int unison_size;
    272 
    273             //the stereo spread of the unison subvoices (0.0f=mono,1.0f=max)
    274             float unison_stereo_spread;
    275 
    276             //fractional part (skip)
    277             float *oscposlo, *oscfreqlo;
    278 
    279             //integer part (skip)
    280             int *oscposhi, *oscfreqhi;
    281 
    282             //fractional part (skip) of the Modullator
    283             float *oscposloFM, *oscfreqloFM;
    284 
    285             //the unison base_value
    286             float *unison_base_freq_rap;
    287 
    288             //how the unison subvoice's frequency is changed (1.0f for no change)
    289             float *unison_freq_rap;
    290 
    291             //which subvoice has phase inverted
    292             bool *unison_invert_phase;
    293 
    294             //unison vibratto
    295             struct {
    296                 float  amplitude; //amplitude which be added to unison_freq_rap
    297                 float *step; //value which increments the position
    298                 float *position; //between -1.0f and 1.0f
    299             } unison_vibratto;
    300 
    301             //integer part (skip) of the Modullator
    302             unsigned int *oscposhiFM, *oscfreqhiFM;
    303 
    304             //used to compute and interpolate the amplitudes of voices and modullators
    305             float oldamplitude, newamplitude,
    306                   FMoldamplitude, FMnewamplitude;
    307 
    308             //used by Frequency Modulation (for integration)
    309             float *FMoldsmp;
    310 
    311             //1 - if it is the fitst tick (used to fade in the sound)
    312             char firsttick;
    313 
    314         } NoteVoicePar[NUM_VOICES];
    315 
    316         //temporary buffer
    317         float  *tmpwavel;
    318         float  *tmpwaver;
    319         int     max_unison;
    320         float **tmpwave_unison;
    321 
    322         //Filter bypass samples
    323         float *bypassl, *bypassr;
    324 
    325         //interpolate the amplitudes
    326         float globaloldamplitude, globalnewamplitude;
    327 
    328         //Pointer to portamento if note has portamento
    329         Portamento *portamento;
    330 
    331         //how the fine detunes are made bigger or smaller
    332         float bandwidthDetuneMultiplier;
    333 };
    334 
    335 }
    336 
    337 #endif