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.

187 lines
6.0KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Effect.h - this class is inherited by the all effects(Reverb, Echo, ..)
  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 EFFECT_H
  12. #define EFFECT_H
  13. #include "../Misc/Util.h"
  14. #include "../globals.h"
  15. #include "../Params/FilterParams.h"
  16. #include "../Misc/Stereo.h"
  17. // bug: the effect parameters can currently be set, but such values
  18. // will not be saved into XML files
  19. #ifndef rEffPar
  20. #define rEffPar(name, idx, ...) \
  21. {STRINGIFY(name) "::i", rProp(parameter) rDefaultDepends(preset) \
  22. DOC(__VA_ARGS__), NULL, rEffParCb(idx)}
  23. #define rEffParTF(name, idx, ...) \
  24. {STRINGIFY(name) "::T:F", rProp(parameter) rDefaultDepends(preset) \
  25. DOC(__VA_ARGS__), NULL, rEffParTFCb(idx)}
  26. #define rEffParCb(idx) \
  27. [](const char *msg, rtosc::RtData &d) {\
  28. rObject &obj = *(rObject*)d.obj; \
  29. if(rtosc_narguments(msg)) \
  30. obj.changepar(idx, rtosc_argument(msg, 0).i); \
  31. else \
  32. d.reply(d.loc, "i", obj.getpar(idx));}
  33. #define rEffParTFCb(idx) \
  34. [](const char *msg, rtosc::RtData &d) {\
  35. rObject &obj = *(rObject*)d.obj; \
  36. if(rtosc_narguments(msg)) \
  37. obj.changepar(idx, rtosc_argument(msg, 0).T*127); \
  38. else \
  39. d.reply(d.loc, obj.getpar(idx)?"T":"F");}
  40. #endif
  41. #define rEffParCommon(pname, rshort, rdoc, idx, ...) \
  42. {STRINGIFY(pname) "::i", rProp(parameter) rLinear(0,127) \
  43. rShort(rshort) rDoc(rdoc), \
  44. 0, \
  45. [](const char *msg, rtosc::RtData &d) \
  46. { \
  47. rObject& eff = *(rObject*)d.obj; \
  48. if(!rtosc_narguments(msg)) \
  49. d.reply(d.loc, "i", eff.getpar(idx)); \
  50. else { \
  51. eff.changepar(0, rtosc_argument(msg, 0).i); \
  52. d.broadcast(d.loc, "i", eff.getpar(idx)); \
  53. } \
  54. }}
  55. #define rEffParVol(...) rEffParCommon(Pvolume, "amt", "amount of effect", 0, \
  56. __VA_ARGS__)
  57. #define rEffParPan(...) rEffParCommon(Ppanning, "pan", "panning", 1, \
  58. __VA_ARGS__)
  59. namespace zyncarla {
  60. class FilterParams;
  61. class Allocator;
  62. struct EffectParams
  63. {
  64. /**
  65. * Effect Parameter Constructor
  66. * @param alloc Realtime Memory Allocator
  67. * @param insertion_ 1 when it is an insertion Effect
  68. * @param efxoutl_ Effect output buffer Left channel
  69. * @param efxoutr_ Effect output buffer Right channel
  70. * @param filterpars_ pointer to FilterParams array
  71. * @param Ppreset_ chosen preset
  72. * @return Initialized Effect Parameter object*/
  73. EffectParams(Allocator &alloc_, bool insertion_, float *efxoutl_, float *efxoutr_,
  74. unsigned char Ppreset_, unsigned int srate, int bufsize, FilterParams *filterpars_,
  75. bool filterprotect=false);
  76. Allocator &alloc;
  77. bool insertion;
  78. float *efxoutl;
  79. float *efxoutr;
  80. unsigned char Ppreset;
  81. unsigned int srate;
  82. int bufsize;
  83. FilterParams *filterpars;
  84. bool filterprotect;
  85. };
  86. /**this class is inherited by the all effects(Reverb, Echo, ..)*/
  87. class Effect
  88. {
  89. public:
  90. Effect(EffectParams pars);
  91. virtual ~Effect() {}
  92. /**
  93. * Choose a preset
  94. * @param npreset number of chosen preset*/
  95. virtual void setpreset(unsigned char npreset) = 0;
  96. /**Change parameter npar to value
  97. * @param npar chosen parameter
  98. * @param value chosen new value*/
  99. virtual void changepar(int npar, unsigned char value) = 0;
  100. /**Get the value of parameter npar
  101. * @param npar chosen parameter
  102. * @return the value of the parameter in an unsigned char or 0 if it
  103. * does not exist*/
  104. virtual unsigned char getpar(int npar) const = 0;
  105. /**Output result of effect based on the given buffers
  106. *
  107. * This method should result in the effect generating its results
  108. * and placing them into the efxoutl and efxoutr buffers.
  109. * Every Effect should overide this method.
  110. *
  111. * @param smpsl Input buffer for the Left channel
  112. * @param smpsr Input buffer for the Right channel
  113. */
  114. void out(float *const smpsl, float *const smpsr);
  115. virtual void out(const Stereo<float *> &smp) = 0;
  116. /**Reset the state of the effect*/
  117. virtual void cleanup(void) {}
  118. virtual float getfreqresponse(float freq) { return freq; }
  119. unsigned char Ppreset; /**<Currently used preset*/
  120. float *const efxoutl; /**<Effect out Left Channel*/
  121. float *const efxoutr; /**<Effect out Right Channel*/
  122. float outvolume; /**<This is the volume of effect and is public because
  123. * it is needed in system effects.
  124. * The out volume of such effects are always 1.0f, so
  125. * this setting tells me how is the volume to the
  126. * Master Output only.*/
  127. float volume;
  128. FilterParams *filterpars; /**<Parameters for filters used by Effect*/
  129. //Perform L/R crossover
  130. static void crossover(float &a, float &b, float crossover);
  131. protected:
  132. void setpanning(char Ppanning_);
  133. void setlrcross(char Plrcross_);
  134. const bool insertion;
  135. //panning parameters
  136. char Ppanning;
  137. float pangainL;
  138. float pangainR;
  139. char Plrcross; // L/R mix
  140. float lrcross;
  141. //Allocator
  142. Allocator &memory;
  143. // current setup
  144. unsigned int samplerate;
  145. int buffersize;
  146. // alias for above terms
  147. float samplerate_f;
  148. float halfsamplerate_f;
  149. float buffersize_f;
  150. int bufferbytes;
  151. inline void alias()
  152. {
  153. samplerate_f = samplerate;
  154. halfsamplerate_f = samplerate_f / 2.0f;
  155. buffersize_f = buffersize;
  156. bufferbytes = buffersize * sizeof(float);
  157. }
  158. };
  159. }
  160. #endif