zynaddsubfx

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

Effect.h (7363B)


      1 /*
      2   ZynAddSubFX - a software synthesizer
      3 
      4   Effect.h - this class is inherited by all effects (Reverb, Echo, ...)
      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 EFFECT_H
     15 #define EFFECT_H
     16 
     17 #include "../Misc/Util.h"
     18 #include "../globals.h"
     19 #include "../Params/FilterParams.h"
     20 #include "../Misc/Stereo.h"
     21 #include "../Misc/Sync.h"
     22 
     23 // effect parameters differing between single effects
     24 #ifndef rEffPar
     25 #define rEffPar(name, idx, ...) \
     26   {STRINGIFY(name) "::i",  rProp(parameter) rDefaultDepends(preset) \
     27       rLinear(0,127) DOC(__VA_ARGS__), NULL, rEffParCb(idx)}
     28 #define rEffParOpt(name, idx, ...) \
     29   {STRINGIFY(name) "::i:c:S",  rProp(parameter) rDefaultDepends(preset) \
     30    rProp(enumerated) DOC(__VA_ARGS__), NULL, \
     31        rBOIL_BEGIN \
     32        rCOptionCb_(obj->getpar(idx), obj->changepar(idx, var)) \
     33        rBOIL_END }
     34 #define rEffParTF(name, idx, ...) \
     35   {STRINGIFY(name) "::T:F",  rProp(parameter) rDefaultDepends(preset) \
     36    DOC(__VA_ARGS__), NULL, rEffParTFCb(idx)}
     37 #define rEffParCb(idx) \
     38     [](const char *msg, rtosc::RtData &d) {\
     39         rObject &obj = *(rObject*)d.obj; \
     40         if(rtosc_narguments(msg)) { \
     41             obj.changepar(idx, rtosc_argument(msg, 0).i); \
     42             d.broadcast(d.loc, "i", obj.getpar(idx)); \
     43         } \
     44         else \
     45             d.reply(d.loc, "i", obj.getpar(idx));}
     46 #define rEffParTFCb(idx) \
     47     [](const char *msg, rtosc::RtData &d) {\
     48         rObject &obj = *(rObject*)d.obj; \
     49         if(rtosc_narguments(msg)) { \
     50             obj.changepar(idx, rtosc_argument(msg, 0).T*127); \
     51             d.broadcast(d.loc, obj.getpar(idx)?"T":"F"); \
     52         } \
     53         else \
     54             d.reply(d.loc, obj.getpar(idx)?"T":"F");}
     55 #endif
     56 
     57 // effect parameters common to all effects
     58 #define rEffParCommon(pname, rshort, rdoc, idx, ...) \
     59 {STRINGIFY(pname) "::i", rProp(parameter) rLinear(0,127) \
     60     rShort(rshort) DOC(__VA_ARGS__, rdoc), \
     61     0, \
     62     [](const char *msg, rtosc::RtData &d) \
     63     { \
     64         rObject& eff = *(rObject*)d.obj; \
     65         if(!rtosc_narguments(msg)) \
     66             d.reply(d.loc, "i", eff.getpar(idx)); \
     67         else { \
     68             eff.changepar(idx, rtosc_argument(msg, 0).i); \
     69             d.broadcast(d.loc, "i", eff.getpar(idx)); \
     70         } \
     71     }}
     72 #define rEffParVol(...) rEffParCommon(Pvolume, "amt", "amount of effect", 0, \
     73     __VA_ARGS__)
     74 #define rEffParPan(...) rEffParCommon(Ppanning, "pan", "panning", 1, \
     75     rDefault(64), __VA_ARGS__)
     76 #define rPresetForVolume \
     77     {"presetOfVolume:", rProp(internal), 0, \
     78      rBegin; (void)msg; \
     79      rObject *o = (rObject*)d.obj; \
     80      d.reply(d.loc, "i", o->Ppreset + (16 * o->insertion)); \
     81      rEnd}
     82 
     83 namespace zyn {
     84 
     85 class FilterParams;
     86 class Allocator;
     87 
     88 struct EffectParams
     89 {
     90     /**
     91      * Effect Parameter Constructor
     92      * @param alloc        Realtime Memory Allocator
     93      * @param insertion_   1 when it is an insertion Effect
     94      * @param efxoutl_     Effect output buffer Left channel
     95      * @param efxoutr_     Effect output buffer Right channel
     96      * @param filterpars_  pointer to FilterParams array
     97      * @param Ppreset_     chosen preset
     98      * @return Initialized Effect Parameter object*/
     99     EffectParams(Allocator &alloc_, bool insertion_, float *efxoutl_, float *efxoutr_,
    100             unsigned char Ppreset_, unsigned int srate, int bufsize, FilterParams *filterpars_,
    101             bool filterprotect=false, const AbsTime *time_ = nullptr);
    102 
    103 
    104     Allocator &alloc;
    105     bool insertion;
    106     float *efxoutl;
    107     float *efxoutr;
    108     unsigned char Ppreset;
    109     unsigned int srate;
    110     int bufsize;
    111     FilterParams *filterpars;
    112     bool filterprotect;
    113     const AbsTime *time;
    114 };
    115 
    116 /**this class is inherited by the all effects(Reverb, Echo, ..)*/
    117 class Effect: public Observer
    118 {
    119     public:
    120         Effect(EffectParams pars);
    121         virtual ~Effect() {}
    122 
    123         void update() override {}
    124 
    125         /**
    126          * Get default preset parameter value
    127          * @param npreset chosen preset
    128          * @param npar chosen parameter
    129          * @return the default parameter value
    130          **/
    131         virtual unsigned char getpresetpar(unsigned char npreset, unsigned int npar) = 0;
    132         /**
    133          * Choose a preset
    134          * @param npreset number of chosen preset*/
    135         virtual void setpreset(unsigned char npreset) = 0;
    136         /**Change parameter npar to value
    137          * @param npar chosen parameter
    138          * @param value chosen new value*/
    139         virtual void changepar(int npar, unsigned char value) = 0;
    140         /**Get the value of parameter npar
    141          * @param npar chosen parameter
    142          * @return the value of the parameter in an unsigned char or 0 if it
    143          * does not exist*/
    144         virtual unsigned char getpar(int npar) const = 0;
    145         /**Output result of effect based on the given buffers
    146          *
    147          * This method should result in the effect generating its results
    148          * and placing them into the efxoutl and efxoutr buffers.
    149          * Every Effect should override this method.
    150          *
    151          * @param smpsl Input buffer for the Left channel
    152          * @param smpsr Input buffer for the Right channel
    153          */
    154         void out(float *const smpsl, float *const smpsr);
    155         virtual void out(const Stereo<float *> &smp) = 0;
    156         /**Reset the state of the effect*/
    157         virtual void cleanup(void) {}
    158         virtual float getfreqresponse(float freq) { return freq; }
    159 
    160         unsigned char Ppreset;   /**<Currently used preset*/
    161         float *const  efxoutl; /**<Effect out Left Channel*/
    162         float *const  efxoutr; /**<Effect out Right Channel*/
    163         float outvolume; /**<This is the volume of effect and is public because
    164                           * it is needed in system effects.
    165                           * The out volume of such effects are always 1.0f, so
    166                           * this setting tells me how is the volume to the
    167                           * Master Output only.*/
    168 
    169         float volume;
    170         float speedfactor;    /**relative factor for using the global tempo (BPM)*/
    171 
    172         FilterParams *filterpars; /**<Parameters for filters used by Effect*/
    173 
    174         //Perform L/R crossover
    175         static void crossover(float &a, float &b, float crossover);
    176 
    177     protected:
    178         void setpanning(char Ppanning_);
    179         void setlrcross(char Plrcross_);
    180 
    181         const bool insertion;
    182         //panning parameters
    183         char  Ppanning;
    184         float pangainL;
    185         float pangainR;
    186         char  Plrcross; // L/R mix
    187         float lrcross;
    188 
    189         //Allocator
    190         Allocator &memory;
    191 
    192         const AbsTime *time;
    193 
    194         // current setup
    195         unsigned int samplerate;
    196         int buffersize;
    197 
    198         // alias for above terms
    199         float samplerate_f;
    200         float halfsamplerate_f;
    201         float buffersize_f;
    202         int   bufferbytes;
    203 
    204         inline void alias()
    205         {
    206             samplerate_f     = samplerate;
    207             halfsamplerate_f = samplerate_f / 2.0f;
    208             buffersize_f     = buffersize;
    209             bufferbytes      = buffersize * sizeof(float);
    210         }
    211 };
    212 
    213 }
    214 
    215 #endif