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.

322 lines
9.0KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Resonance.cpp - Resonance
  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. #include <cmath>
  12. #include <cstdlib>
  13. #include "Resonance.h"
  14. #include "../Misc/Util.h"
  15. #include <rtosc/ports.h>
  16. #include <rtosc/port-sugar.h>
  17. #define rObject Resonance
  18. #define rBegin [](const char *msg, RtData &d) { rObject &o = *(rObject*)d.obj
  19. #define rEnd }
  20. using namespace rtosc;
  21. const rtosc::Ports Resonance::ports = {
  22. rSelf(Resonance),
  23. rPaste,
  24. rToggle(Penabled, rShort("enable"), "resonance enable"),
  25. rToggle(Pprotectthefundamental, rShort("p.fund."), "Disable resonance filter on first harmonic"),
  26. rParams(Prespoints, N_RES_POINTS, "Resonance data points"),
  27. rParamZyn(PmaxdB, rShort("max"), "how many dB the signal may be amplified"),
  28. rParamZyn(Pcenterfreq, rShort("c.freq"), "Center frequency"),
  29. rParamZyn(Poctavesfreq, rShort("oct"), "The number of octaves..."),
  30. rActioni(randomize, rMap(min,0), rMap(max, 2), "Randomize frequency response"),
  31. rActioni(interpolatepeaks, rMap(min,0), rMap(max, 2), "Generate response from peak values"),
  32. rAction(smooth, "Smooth out frequency response"),
  33. rAction(zero, "Reset frequency response"),
  34. //UI Value listeners
  35. {"centerfreq:", rDoc("Get center frequency") rMap(unit, Hz), NULL,
  36. [](const char *, RtData &d)
  37. {d.reply(d.loc, "f", ((rObject*)d.obj)->getcenterfreq());}},
  38. {"octavesfreq:", rDoc("Get center freq of graph"), NULL,
  39. [](const char *, RtData &d)
  40. {d.reply(d.loc, "f", ((rObject*)d.obj)->getoctavesfreq());}},
  41. {"respoints", 0, 0,
  42. rBegin;
  43. if(rtosc_narguments(msg)) {
  44. int i=0;
  45. auto itr = rtosc_itr_begin(msg);
  46. while(!rtosc_itr_end(itr) && i < N_RES_POINTS) {
  47. auto ival = rtosc_itr_next(&itr);
  48. if(ival.type == 'f')
  49. o.Prespoints[i++] = ival.val.f*127;
  50. }
  51. } else {
  52. rtosc_arg_t args[N_RES_POINTS];
  53. char types[N_RES_POINTS+1] = {0};
  54. for(int i=0; i<N_RES_POINTS; ++i) {
  55. args[i].f = o.Prespoints[i]/127.0;
  56. types[i] = 'f';
  57. }
  58. d.replyArray(d.loc, types, args);
  59. }
  60. rEnd},
  61. };
  62. #undef rBegin
  63. #undef rEnd
  64. Resonance::Resonance():Presets()
  65. {
  66. setpresettype("Presonance");
  67. defaults();
  68. }
  69. Resonance::~Resonance(void)
  70. {}
  71. void Resonance::defaults(void)
  72. {
  73. Penabled = 0;
  74. PmaxdB = 20;
  75. Pcenterfreq = 64; //1 kHz
  76. Poctavesfreq = 64;
  77. Pprotectthefundamental = 0;
  78. ctlcenter = 1.0f;
  79. ctlbw = 1.0f;
  80. for(int i = 0; i < N_RES_POINTS; ++i)
  81. Prespoints[i] = 64;
  82. }
  83. /*
  84. * Set a point of resonance function with a value
  85. */
  86. void Resonance::setpoint(int n, unsigned char p)
  87. {
  88. if((n < 0) || (n >= N_RES_POINTS))
  89. return;
  90. Prespoints[n] = p;
  91. }
  92. /*
  93. * Apply the resonance to FFT data
  94. */
  95. void Resonance::applyres(int n, fft_t *fftdata, float freq) const
  96. {
  97. if(Penabled == 0)
  98. return; //if the resonance is disabled
  99. const float l1 = logf(getfreqx(0.0f) * ctlcenter),
  100. l2 = logf(2.0f) * getoctavesfreq() * ctlbw;
  101. //Provide an upper bound for resonance
  102. const float upper =
  103. limit<float>(array_max(Prespoints, N_RES_POINTS), 1.0f, (float)INFINITY);
  104. for(int i = 1; i < n; ++i) {
  105. //compute where the n-th hamonics fits to the graph
  106. const float x = limit((logf(freq*i) - l1) / l2, 0.0f, (float)INFINITY) * N_RES_POINTS;
  107. const float dx = x - floor(x);
  108. const int kx1 = limit<int>(floor(x), 0, N_RES_POINTS - 1);
  109. const int kx2 = limit<int>(kx1 + 1, 0, N_RES_POINTS - 1);
  110. float y =
  111. ((Prespoints[kx1] * (1.0f - dx) + Prespoints[kx2] * dx)
  112. - upper) / 127.0f;
  113. y = powf(10.0f, y * PmaxdB / 20.0f);
  114. if((Pprotectthefundamental != 0) && (i == 1))
  115. y = 1.0f;
  116. fftdata[i] *= y;
  117. }
  118. }
  119. /*
  120. * Gets the response at the frequency "freq"
  121. */
  122. //Requires
  123. // - resonance data
  124. // - max resonance
  125. // - mapping from resonance data to frequency
  126. float Resonance::getfreqresponse(float freq) const
  127. {
  128. const float l1 = logf(getfreqx(0.0f) * ctlcenter),
  129. l2 = logf(2.0f) * getoctavesfreq() * ctlbw;
  130. //Provide an upper bound for resonance
  131. const float upper =
  132. limit<float>(array_max(Prespoints, N_RES_POINTS), 1.0f, INFINITY);
  133. //compute where the n-th hamonics fits to the graph
  134. const float x = limit((logf(freq) - l1) / l2, 0.0f, (float)INFINITY) * N_RES_POINTS;
  135. const float dx = x - floor(x);
  136. const int kx1 = limit<int>(floor(x), 0, N_RES_POINTS - 1);
  137. const int kx2 = limit<int>(kx1 + 1, 0, N_RES_POINTS - 1);
  138. //Interpolate
  139. const float result =
  140. ((Prespoints[kx1] * (1.0f - dx) + Prespoints[kx2] * dx) - upper) / 127.0f;
  141. return powf(10.0f, result * PmaxdB / 20.0f);
  142. }
  143. /*
  144. * Smooth the resonance function
  145. */
  146. void Resonance::smooth(void)
  147. {
  148. float old = Prespoints[0];
  149. for(int i = 0; i < N_RES_POINTS; ++i) {
  150. old = old * 0.4f + Prespoints[i] * 0.6f;
  151. Prespoints[i] = (int) old;
  152. }
  153. old = Prespoints[N_RES_POINTS - 1];
  154. for(int i = N_RES_POINTS - 1; i > 0; i--) {
  155. old = old * 0.4f + Prespoints[i] * 0.6f;
  156. Prespoints[i] = (int) old + 1;
  157. if(Prespoints[i] > 127)
  158. Prespoints[i] = 127;
  159. }
  160. }
  161. /*
  162. * Randomize the resonance function
  163. */
  164. void Resonance::randomize(int type)
  165. {
  166. int r = (int)(RND * 127.0f);
  167. for(int i = 0; i < N_RES_POINTS; ++i) {
  168. Prespoints[i] = r;
  169. if((RND < 0.1f) && (type == 0))
  170. r = (int)(RND * 127.0f);
  171. if((RND < 0.3f) && (type == 1))
  172. r = (int)(RND * 127.0f);
  173. if(type == 2)
  174. r = (int)(RND * 127.0f);
  175. }
  176. smooth();
  177. }
  178. void Resonance::zero(void)
  179. {
  180. for(int i=0; i<N_RES_POINTS; ++i)
  181. setpoint(i,64);
  182. }
  183. /*
  184. * Interpolate the peaks
  185. */
  186. void Resonance::interpolatepeaks(int type)
  187. {
  188. int x1 = 0, y1 = Prespoints[0];
  189. for(int i = 1; i < N_RES_POINTS; ++i)
  190. if((Prespoints[i] != 64) || (i + 1 == N_RES_POINTS)) {
  191. int y2 = Prespoints[i];
  192. for(int k = 0; k < i - x1; ++k) {
  193. float x = (float) k / (i - x1);
  194. if(type == 0)
  195. x = (1 - cosf(x * PI)) * 0.5f;
  196. Prespoints[x1 + k] = (int)(y1 * (1.0f - x) + y2 * x);
  197. }
  198. x1 = i;
  199. y1 = y2;
  200. }
  201. }
  202. /*
  203. * Get the frequency from x, where x is [0..1]; x is the x coordinate
  204. */
  205. float Resonance::getfreqx(float x) const
  206. {
  207. const float octf = powf(2.0f, getoctavesfreq());
  208. return getcenterfreq() / sqrt(octf) * powf(octf, limit(x, 0.0f, 1.0f));
  209. }
  210. /*
  211. * Get the x coordinate from frequency (used by the UI)
  212. */
  213. float Resonance::getfreqpos(float freq) const
  214. {
  215. return (logf(freq) - logf(getfreqx(0.0f))) / logf(2.0f) / getoctavesfreq();
  216. }
  217. /*
  218. * Get the center frequency of the resonance graph
  219. */
  220. float Resonance::getcenterfreq() const
  221. {
  222. return 10000.0f * powf(10, -(1.0f - Pcenterfreq / 127.0f) * 2.0f);
  223. }
  224. /*
  225. * Get the number of octave that the resonance functions applies to
  226. */
  227. float Resonance::getoctavesfreq() const
  228. {
  229. return 0.25f + 10.0f * Poctavesfreq / 127.0f;
  230. }
  231. void Resonance::sendcontroller(MidiControllers ctl, float par)
  232. {
  233. if(ctl == C_resonance_center)
  234. ctlcenter = par;
  235. else
  236. ctlbw = par;
  237. }
  238. #define COPY(y) this->y = r.y
  239. void Resonance::paste(Resonance &r)
  240. {
  241. COPY(Penabled);
  242. for(int i=0; i<N_RES_POINTS; ++i)
  243. this->Prespoints[i] = r.Prespoints[i];
  244. COPY(PmaxdB);
  245. COPY(Pcenterfreq);
  246. COPY(Poctavesfreq);
  247. COPY(Pprotectthefundamental);
  248. COPY(ctlcenter);
  249. COPY(ctlbw);
  250. }
  251. #undef COPY
  252. void Resonance::add2XML(XMLwrapper& xml)
  253. {
  254. xml.addparbool("enabled", Penabled);
  255. if((Penabled == 0) && (xml.minimal))
  256. return;
  257. xml.addpar("max_db", PmaxdB);
  258. xml.addpar("center_freq", Pcenterfreq);
  259. xml.addpar("octaves_freq", Poctavesfreq);
  260. xml.addparbool("protect_fundamental_frequency", Pprotectthefundamental);
  261. xml.addpar("resonance_points", N_RES_POINTS);
  262. for(int i = 0; i < N_RES_POINTS; ++i) {
  263. xml.beginbranch("RESPOINT", i);
  264. xml.addpar("val", Prespoints[i]);
  265. xml.endbranch();
  266. }
  267. }
  268. void Resonance::getfromXML(XMLwrapper& xml)
  269. {
  270. Penabled = xml.getparbool("enabled", Penabled);
  271. PmaxdB = xml.getpar127("max_db", PmaxdB);
  272. Pcenterfreq = xml.getpar127("center_freq", Pcenterfreq);
  273. Poctavesfreq = xml.getpar127("octaves_freq", Poctavesfreq);
  274. Pprotectthefundamental = xml.getparbool("protect_fundamental_frequency",
  275. Pprotectthefundamental);
  276. for(int i = 0; i < N_RES_POINTS; ++i) {
  277. if(xml.enterbranch("RESPOINT", i) == 0)
  278. continue;
  279. Prespoints[i] = xml.getpar127("val", Prespoints[i]);
  280. xml.exitbranch();
  281. }
  282. }