zynaddsubfx

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

globals.h (9141B)


      1 /*
      2   ZynAddSubFX - a software synthesizer
      3 
      4   globals.h - it contains program settings and the program capabilities
      5               like number of parts, of effects
      6   Copyright (C) 2002-2005 Nasca Octavian Paul
      7   Author: Nasca Octavian Paul
      8 
      9   This program is free software; you can redistribute it and/or
     10   modify it under the terms of the GNU General Public License
     11   as published by the Free Software Foundation; either version 2
     12   of the License, or (at your option) any later version.
     13 */
     14 
     15 
     16 #ifndef GLOBALS_H
     17 #define GLOBALS_H
     18 
     19 #if defined(__clang__)
     20 #define REALTIME __attribute__((annotate("realtime")))
     21 #define NONREALTIME __attribute__((annotate("nonrealtime")))
     22 #else
     23 #define REALTIME
     24 #define NONREALTIME
     25 #endif
     26 
     27 //Forward Declarations
     28 
     29 #if defined(HAVE_CPP_STD_COMPLEX)
     30 #include <complex>
     31 #else
     32 namespace std {
     33     template<class T> struct complex;
     34 }
     35 #endif
     36 
     37 namespace rtosc{struct Ports; struct ClonePorts; struct MergePorts; class ThreadLink;}
     38 namespace zyn {
     39 
     40 class  EffectMgr;
     41 class  ADnoteParameters;
     42 struct ADnoteGlobalParam;
     43 class  SUBnoteParameters;
     44 class  PADnoteParameters;
     45 class  SynthNote;
     46 
     47 class  Allocator;
     48 class  AbsTime;
     49 class  RelTime;
     50 
     51 class  Microtonal;
     52 class  XMLwrapper;
     53 class  Resonance;
     54 class  FFTwrapper;
     55 class  EnvelopeParams;
     56 class  LFOParams;
     57 class  FilterParams;
     58 
     59 struct WatchManager;
     60 class  LFO;
     61 class  Envelope;
     62 class  OscilGen;
     63 
     64 class  Controller;
     65 class  Master;
     66 class  Part;
     67 
     68 class  Filter;
     69 class  AnalogFilter;
     70 class  MoogFilter;
     71 class  CombFilter;
     72 class  SVFilter;
     73 class  FormantFilter;
     74 class  ModFilter;
     75 
     76 class  Sync;
     77 
     78 
     79 typedef float fftwf_real;
     80 typedef std::complex<fftwf_real> fft_t;
     81 
     82 /**
     83  * The number of harmonics of additive synth
     84  * This must be smaller than OSCIL_SIZE/2
     85  */
     86 #define MAX_AD_HARMONICS 128
     87 
     88 
     89 /**
     90  * The number of harmonics of substractive
     91  */
     92 #define MAX_SUB_HARMONICS 64
     93 
     94 
     95 /*
     96  * The maximum number of samples that are used for 1 PADsynth instrument(or item)
     97  */
     98 #define PAD_MAX_SAMPLES 64
     99 
    100 
    101 /*
    102  * Number of parts
    103  */
    104 #define NUM_MIDI_PARTS 16
    105 
    106 /*
    107  * Number of Midi channels
    108  */
    109 #define NUM_MIDI_CHANNELS 16
    110 
    111 /*
    112  * The number of voices of additive synth for a single note
    113  */
    114 #define NUM_VOICES 8
    115 
    116 /*
    117  * The polyphony (notes)
    118  */
    119 #define POLYPHONY 60
    120 
    121 /*
    122  * Number of system effects
    123  */
    124 #define NUM_SYS_EFX 4
    125 
    126 
    127 /*
    128  * Number of insertion effects
    129  */
    130 #define NUM_INS_EFX 8
    131 
    132 /*
    133  * Number of part's insertion effects
    134  */
    135 #define NUM_PART_EFX 3
    136 
    137 /*
    138  * Maximum number of the instrument on a part
    139  */
    140 #define NUM_KIT_ITEMS 16
    141 
    142 /*
    143  * Maximum number of "strings" in Sympathetic Resonance Effect
    144  */
    145 #define NUM_SYMPATHETIC_STRINGS 228U // 76*3
    146 
    147 /*
    148  * How is applied the velocity sensing
    149  */
    150 #define VELOCITY_MAX_SCALE 8.0f
    151 
    152 /*
    153  * The maximum length of instrument's name
    154  */
    155 #define PART_MAX_NAME_LEN 30
    156 
    157 /*
    158  * The maximum we allow for an XMZ path
    159  *
    160  * Note that this is an ugly hack.  Finding a compile time path
    161  * max portably is painful.
    162  */
    163 #define XMZ_PATH_MAX 1024
    164 
    165 /*
    166  * The maximum number of bands of the equaliser
    167  */
    168 #define MAX_EQ_BANDS 8
    169 #if (MAX_EQ_BANDS >= 20)
    170 #error "Too many EQ bands in globals.h"
    171 #endif
    172 
    173 
    174 /*
    175  * Maximum filter stages
    176  */
    177 #define MAX_FILTER_STAGES 5
    178 
    179 /*
    180  * Formant filter (FF) limits
    181  */
    182 #define FF_MAX_VOWELS 6
    183 #define FF_MAX_FORMANTS 12
    184 #define FF_MAX_SEQUENCE 8
    185 
    186 /*
    187  * Maximum length of the reverse delay effect
    188  */
    189 #define MAX_REV_DELAY_SECONDS 4.0f
    190 
    191 #define MAX_PRESETTYPE_SIZE 30
    192 
    193 #define LOG_2 0.693147181f
    194 #define PI 3.1415926536f
    195 #define PIDIV2 1.5707963268f
    196 #define LOG_10 2.302585093f
    197 
    198 /*
    199  * For de-pop adjustment
    200  */
    201 #define FADEIN_ADJUSTMENT_SCALE 20
    202 
    203 /*
    204  * Envelope Limits
    205  */
    206 #define MAX_ENVELOPE_POINTS 40
    207 #define MIN_ENVELOPE_DB -400
    208 
    209 /*
    210  * The threshold for the amplitude interpolation used if the amplitude
    211  * is changed (by LFO's or Envelope's). If the change of the amplitude
    212  * is below this, the amplitude is not interpolated
    213  */
    214 #define AMPLITUDE_INTERPOLATION_THRESHOLD 0.0001f
    215 
    216 /*
    217  * How the amplitude threshold is computed
    218  */
    219 #define ABOVE_AMPLITUDE_THRESHOLD(a, b) ((2.0f * fabsf((b) - (a)) \
    220                                           / (fabsf((b) + (a) \
    221                                                   + 0.0000000001f))) > \
    222                                          AMPLITUDE_INTERPOLATION_THRESHOLD)
    223 
    224 /*
    225  * Interpolate Amplitude
    226  */
    227 #define INTERPOLATE_AMPLITUDE(a, b, x, size) ((a) \
    228                                               + ((b) \
    229                                                  - (a)) * (float)(x) \
    230                                               / (float) (size))
    231 
    232 
    233 /*
    234  * dB
    235  */
    236 #define dB2rap(dB) ((expf((dB) * LOG_10 / 20.0f)))
    237 #define rap2dB(rap) ((20 * logf(rap) / LOG_10))
    238 
    239 #define ZERO(data, size) {char *data_ = (char *) data; for(int i = 0; \
    240                                                            i < size; \
    241                                                            i++) \
    242                               data_[i] = 0; }
    243 #define ZERO_float(data, size) {float *data_ = (float *) data; \
    244                                 for(int i = 0; \
    245                                     i < size; \
    246                                     i++) \
    247                                     data_[i] = 0.0f; }
    248 
    249 enum ONOFFTYPE {
    250     OFF = 0, ON = 1
    251 };
    252 
    253 enum MidiControllers {
    254     C_bankselectmsb = 0, C_pitchwheel = 1000, C_NULL = 1001,
    255     C_aftertouch = 1002, C_pitch = 1003,
    256     C_expression    = 11, C_panning = 10, C_bankselectlsb = 32,
    257     C_filtercutoff  = 74, C_filterq = 71, C_bandwidth = 75, C_modwheel = 1,
    258     C_fmamp  = 76,
    259     C_volume = 7, C_sustain = 64, C_allnotesoff = 123, C_allsoundsoff = 120,
    260     C_resetallcontrollers = 121,
    261     C_portamento = 65, C_resonance_center = 77, C_resonance_bandwidth = 78,
    262 
    263     C_dataentryhi = 0x06, C_dataentrylo = 0x26, C_nrpnhi = 99, C_nrpnlo = 98
    264 };
    265 
    266 enum LegatoMsg {
    267     LM_Norm, LM_FadeIn, LM_FadeOut, LM_CatchUp, LM_ToNorm
    268 };
    269 
    270 //is like i=(int)(floor(f))
    271 #ifdef ASM_F2I_YES
    272 #define F2I(f, i)\
    273     do {\
    274         __asm__ __volatile__\
    275             ("fistpl %0" : "=m" (i) : "t" (f - 0.49999999f) : "st");\
    276     } while (false)
    277 #else
    278 #define F2I(f, i)\
    279     do {\
    280         (i) = ((f > 0) ? ((int)(f)) : ((int)(f - 1.0f)));\
    281     } while (false)
    282 #endif
    283 
    284 
    285 
    286 #ifndef  O_BINARY
    287 #define O_BINARY 0
    288 #endif
    289 
    290 template<class T>
    291 class m_unique_array
    292 {
    293     T* ptr = nullptr; //!< @invariant nullptr or pointer to new[]'ed memory
    294 public:
    295     m_unique_array() = default;
    296     m_unique_array(m_unique_array&& other) : ptr(other.ptr) {
    297         other.ptr = nullptr;
    298     }
    299     m_unique_array& operator=(m_unique_array&& other) {
    300         ptr = other.ptr;
    301         other.ptr = nullptr;
    302         return *this;
    303     }
    304     m_unique_array(const m_unique_array& other) = delete;
    305     ~m_unique_array() { delete[] ptr; ptr = nullptr; }
    306     void resize(unsigned sz) {
    307         delete[] ptr;
    308         ptr = new T[sz]; }
    309 
    310     operator T*() { return ptr; }
    311     operator const T*() const { return ptr; }
    312     //T& operator[](unsigned idx) { return ptr[idx]; }
    313     //const T& operator[](unsigned idx) const { return ptr[idx]; }
    314 };
    315 
    316 //temporary include for synth->{samplerate/buffersize} members
    317 struct SYNTH_T {
    318 
    319     SYNTH_T(void)
    320         :samplerate(44100), buffersize(256), oscilsize(1024)
    321     {
    322         alias(false);
    323     }
    324 
    325     SYNTH_T(const SYNTH_T& ) = delete;
    326     SYNTH_T(SYNTH_T&& ) = default;
    327     SYNTH_T& operator=(const SYNTH_T& ) = delete;
    328     SYNTH_T& operator=(SYNTH_T&& ) = default;
    329 
    330     /** the buffer to add noise in order to avoid denormalisation */
    331     m_unique_array<float> denormalkillbuf;
    332 
    333     /**Sampling rate*/
    334     unsigned int samplerate;
    335 
    336     /**
    337      * The size of a sound buffer (or the granularity)
    338      * All internal transfer of sound data use buffer of this size.
    339      * All parameters are constant during this period of time, except
    340      * some parameters(like amplitudes) which are linearly interpolated.
    341      * If you increase this you'll encounter big latencies, but if you
    342      * decrease this the CPU requirements gets high.
    343      */
    344     int buffersize;
    345 
    346     /**
    347      * The size of ADnote Oscillator
    348      * Decrease this => poor quality
    349      * Increase this => CPU requirements gets high (only at start of the note)
    350      */
    351     int oscilsize;
    352 
    353     //Alias for above terms
    354     float samplerate_f;
    355     float halfsamplerate_f;
    356     float buffersize_f;
    357     int   bufferbytes;
    358     float oscilsize_f;
    359 
    360     float dt(void) const
    361     {
    362         return buffersize_f / samplerate_f;
    363     }
    364     void alias(bool randomize=true);
    365     static float numRandom(void); //defined in Util.cpp for now
    366 };
    367 
    368 class smooth_float {
    369 private:
    370     bool init;
    371     float curr_value;
    372     float next_value;
    373 public:
    374     smooth_float() {
    375         init = false;
    376         next_value = curr_value = 0.0f;
    377     };
    378     smooth_float(const float value) {
    379         init = true;
    380         next_value = curr_value = value;
    381     };
    382     operator float() {
    383         const float delta = (next_value - curr_value) / 32.0f;
    384         curr_value += delta;
    385         return (curr_value);
    386     };
    387     void operator =(const float value) {
    388       if (init) {
    389           next_value = value;
    390       } else {
    391           next_value = curr_value = value;
    392           init = true;
    393       }
    394     };
    395     bool isSet() const {
    396         return (init);
    397     };
    398 };
    399 
    400 }
    401 #endif