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.

EnvelopeParams.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. EnvelopeParams.cpp - Parameters for Envelope
  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 modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <cassert>
  20. #include <rtosc/ports.h>
  21. #include <rtosc/port-sugar.h>
  22. #include "EnvelopeParams.h"
  23. #include "../Misc/Util.h"
  24. #define rObject EnvelopeParams
  25. using namespace rtosc;
  26. static const rtosc::Ports localPorts = {
  27. rSelf(EnvelopeParams),
  28. rPaste,
  29. #undef rChangeCb
  30. #define rChangeCb if(!obj->Pfreemode) obj->converttofree();
  31. rToggle(Pfreemode, "Complex Envelope Definitions"),
  32. #undef rChangeCb
  33. #define rChangeCb
  34. rParamZyn(Penvpoints, rProp(internal), "Number of points in complex definition"),
  35. rParamZyn(Penvsustain, rProp(internal), "Location of the sustain point"),
  36. rParams(Penvdt, MAX_ENVELOPE_POINTS, "Envelope Delay Times"),
  37. rParams(Penvval, MAX_ENVELOPE_POINTS, "Envelope Values"),
  38. rParamZyn(Penvstretch, "Stretch with respect to frequency"),
  39. rToggle(Pforcedrelease, "Force Envelope to fully evaluate"),
  40. rToggle(Plinearenvelope, "Linear or Logarithmic Envelopes"),
  41. rParamZyn(PA_dt, "Attack Time"),
  42. rParamZyn(PA_val, "Attack Value"),
  43. rParamZyn(PD_dt, "Decay Time"),
  44. rParamZyn(PD_val, "Decay Value"),
  45. rParamZyn(PS_val, "Sustain Value"),
  46. rParamZyn(PR_dt, "Release Time"),
  47. rParamZyn(PR_val, "Release Value"),
  48. {"addPoint:i", rProp(internal) rDoc("Add point to envelope"), NULL, [](const char *msg, RtData &d)
  49. {
  50. EnvelopeParams *env = (rObject*) d.obj;
  51. const int curpoint = rtosc_argument(msg, 0).i;
  52. //int curpoint=freeedit->lastpoint;
  53. if (curpoint<0 || curpoint>env->Penvpoints || env->Penvpoints>=MAX_ENVELOPE_POINTS)
  54. return;
  55. for (int i=env->Penvpoints; i>=curpoint+1; i--) {
  56. env->Penvdt[i]=env->Penvdt[i-1];
  57. env->Penvval[i]=env->Penvval[i-1];
  58. }
  59. if (curpoint==0) {
  60. env->Penvdt[1]=64;
  61. }
  62. env->Penvpoints++;
  63. if (curpoint<=env->Penvsustain) env->Penvsustain++;
  64. }},
  65. {"delPoint:i", rProp(internal) rDoc("Delete Envelope Point"), NULL, [](const char *msg, RtData &d)
  66. {
  67. EnvelopeParams *env = (rObject*) d.obj;
  68. const int curpoint=rtosc_argument(msg, 0).i;
  69. if(curpoint<1 || curpoint>=env->Penvpoints-1 || env->Penvpoints<=3)
  70. return;
  71. for (int i=curpoint+1;i<env->Penvpoints;i++){
  72. env->Penvdt[i-1]=env->Penvdt[i];
  73. env->Penvval[i-1]=env->Penvval[i];
  74. };
  75. env->Penvpoints--;
  76. if (curpoint<=env->Penvsustain)
  77. env->Penvsustain--;
  78. }},
  79. };
  80. const rtosc::Ports &EnvelopeParams::ports = localPorts;
  81. EnvelopeParams::EnvelopeParams(unsigned char Penvstretch_,
  82. unsigned char Pforcedrelease_)
  83. {
  84. PA_dt = 10;
  85. PD_dt = 10;
  86. PR_dt = 10;
  87. PA_val = 64;
  88. PD_val = 64;
  89. PS_val = 64;
  90. PR_val = 64;
  91. for(int i = 0; i < MAX_ENVELOPE_POINTS; ++i) {
  92. Penvdt[i] = 32;
  93. Penvval[i] = 64;
  94. }
  95. Penvdt[0] = 0; //no used
  96. Penvsustain = 1;
  97. Penvpoints = 1;
  98. Envmode = 1;
  99. Penvstretch = Penvstretch_;
  100. Pforcedrelease = Pforcedrelease_;
  101. Pfreemode = 1;
  102. Plinearenvelope = 0;
  103. store2defaults();
  104. }
  105. EnvelopeParams::~EnvelopeParams()
  106. {}
  107. void EnvelopeParams::paste(const EnvelopeParams &ep)
  108. {
  109. //Avoid undefined behavior
  110. if(&ep == this)
  111. return;
  112. char *base = (char*)&this->Pfreemode;
  113. char *end = (char*)&this->DR_val;
  114. assert(end-base > 0);
  115. memcpy((char*)&this->Pfreemode, (const char*)&ep.Pfreemode, 1+end-base);
  116. }
  117. float EnvelopeParams::getdt(char i) const
  118. {
  119. return EnvelopeParams::dt(Penvdt[(int)i]);
  120. }
  121. float EnvelopeParams::dt(char val)
  122. {
  123. return (powf(2.0f, val / 127.0f * 12.0f) - 1.0f) * 10.0f; //miliseconds
  124. }
  125. /*
  126. * ADSR/ASR... initialisations
  127. */
  128. void EnvelopeParams::ADSRinit(char A_dt, char D_dt, char S_val, char R_dt)
  129. {
  130. setpresettype("Penvamplitude");
  131. Envmode = 1;
  132. PA_dt = A_dt;
  133. PD_dt = D_dt;
  134. PS_val = S_val;
  135. PR_dt = R_dt;
  136. Pfreemode = 0;
  137. converttofree();
  138. store2defaults();
  139. }
  140. void EnvelopeParams::ADSRinit_dB(char A_dt, char D_dt, char S_val, char R_dt)
  141. {
  142. setpresettype("Penvamplitude");
  143. Envmode = 2;
  144. PA_dt = A_dt;
  145. PD_dt = D_dt;
  146. PS_val = S_val;
  147. PR_dt = R_dt;
  148. Pfreemode = 0;
  149. converttofree();
  150. store2defaults();
  151. }
  152. void EnvelopeParams::ASRinit(char A_val, char A_dt, char R_val, char R_dt)
  153. {
  154. setpresettype("Penvfrequency");
  155. Envmode = 3;
  156. PA_val = A_val;
  157. PA_dt = A_dt;
  158. PR_val = R_val;
  159. PR_dt = R_dt;
  160. Pfreemode = 0;
  161. converttofree();
  162. store2defaults();
  163. }
  164. void EnvelopeParams::ADSRinit_filter(char A_val,
  165. char A_dt,
  166. char D_val,
  167. char D_dt,
  168. char R_dt,
  169. char R_val)
  170. {
  171. setpresettype("Penvfilter");
  172. Envmode = 4;
  173. PA_val = A_val;
  174. PA_dt = A_dt;
  175. PD_val = D_val;
  176. PD_dt = D_dt;
  177. PR_dt = R_dt;
  178. PR_val = R_val;
  179. Pfreemode = 0;
  180. converttofree();
  181. store2defaults();
  182. }
  183. void EnvelopeParams::ASRinit_bw(char A_val, char A_dt, char R_val, char R_dt)
  184. {
  185. setpresettype("Penvbandwidth");
  186. Envmode = 5;
  187. PA_val = A_val;
  188. PA_dt = A_dt;
  189. PR_val = R_val;
  190. PR_dt = R_dt;
  191. Pfreemode = 0;
  192. converttofree();
  193. store2defaults();
  194. }
  195. /*
  196. * Convert the Envelope to freemode
  197. */
  198. void EnvelopeParams::converttofree()
  199. {
  200. switch(Envmode) {
  201. case 1:
  202. Penvpoints = 4;
  203. Penvsustain = 2;
  204. Penvval[0] = 0;
  205. Penvdt[1] = PA_dt;
  206. Penvval[1] = 127;
  207. Penvdt[2] = PD_dt;
  208. Penvval[2] = PS_val;
  209. Penvdt[3] = PR_dt;
  210. Penvval[3] = 0;
  211. break;
  212. case 2:
  213. Penvpoints = 4;
  214. Penvsustain = 2;
  215. Penvval[0] = 0;
  216. Penvdt[1] = PA_dt;
  217. Penvval[1] = 127;
  218. Penvdt[2] = PD_dt;
  219. Penvval[2] = PS_val;
  220. Penvdt[3] = PR_dt;
  221. Penvval[3] = 0;
  222. break;
  223. case 3:
  224. Penvpoints = 3;
  225. Penvsustain = 1;
  226. Penvval[0] = PA_val;
  227. Penvdt[1] = PA_dt;
  228. Penvval[1] = 64;
  229. Penvdt[2] = PR_dt;
  230. Penvval[2] = PR_val;
  231. break;
  232. case 4:
  233. Penvpoints = 4;
  234. Penvsustain = 2;
  235. Penvval[0] = PA_val;
  236. Penvdt[1] = PA_dt;
  237. Penvval[1] = PD_val;
  238. Penvdt[2] = PD_dt;
  239. Penvval[2] = 64;
  240. Penvdt[3] = PR_dt;
  241. Penvval[3] = PR_val;
  242. break;
  243. case 5:
  244. Penvpoints = 3;
  245. Penvsustain = 1;
  246. Penvval[0] = PA_val;
  247. Penvdt[1] = PA_dt;
  248. Penvval[1] = 64;
  249. Penvdt[2] = PR_dt;
  250. Penvval[2] = PR_val;
  251. break;
  252. }
  253. }
  254. void EnvelopeParams::add2XML(XMLwrapper *xml)
  255. {
  256. xml->addparbool("free_mode", Pfreemode);
  257. xml->addpar("env_points", Penvpoints);
  258. xml->addpar("env_sustain", Penvsustain);
  259. xml->addpar("env_stretch", Penvstretch);
  260. xml->addparbool("forced_release", Pforcedrelease);
  261. xml->addparbool("linear_envelope", Plinearenvelope);
  262. xml->addpar("A_dt", PA_dt);
  263. xml->addpar("D_dt", PD_dt);
  264. xml->addpar("R_dt", PR_dt);
  265. xml->addpar("A_val", PA_val);
  266. xml->addpar("D_val", PD_val);
  267. xml->addpar("S_val", PS_val);
  268. xml->addpar("R_val", PR_val);
  269. if((Pfreemode != 0) || (!xml->minimal))
  270. for(int i = 0; i < Penvpoints; ++i) {
  271. xml->beginbranch("POINT", i);
  272. if(i != 0)
  273. xml->addpar("dt", Penvdt[i]);
  274. xml->addpar("val", Penvval[i]);
  275. xml->endbranch();
  276. }
  277. }
  278. void EnvelopeParams::getfromXML(XMLwrapper *xml)
  279. {
  280. Pfreemode = xml->getparbool("free_mode", Pfreemode);
  281. Penvpoints = xml->getpar127("env_points", Penvpoints);
  282. Penvsustain = xml->getpar127("env_sustain", Penvsustain);
  283. Penvstretch = xml->getpar127("env_stretch", Penvstretch);
  284. Pforcedrelease = xml->getparbool("forced_release", Pforcedrelease);
  285. Plinearenvelope = xml->getparbool("linear_envelope", Plinearenvelope);
  286. PA_dt = xml->getpar127("A_dt", PA_dt);
  287. PD_dt = xml->getpar127("D_dt", PD_dt);
  288. PR_dt = xml->getpar127("R_dt", PR_dt);
  289. PA_val = xml->getpar127("A_val", PA_val);
  290. PD_val = xml->getpar127("D_val", PD_val);
  291. PS_val = xml->getpar127("S_val", PS_val);
  292. PR_val = xml->getpar127("R_val", PR_val);
  293. for(int i = 0; i < Penvpoints; ++i) {
  294. if(xml->enterbranch("POINT", i) == 0)
  295. continue;
  296. if(i != 0)
  297. Penvdt[i] = xml->getpar127("dt", Penvdt[i]);
  298. Penvval[i] = xml->getpar127("val", Penvval[i]);
  299. xml->exitbranch();
  300. }
  301. if(!Pfreemode)
  302. converttofree();
  303. }
  304. void EnvelopeParams::defaults()
  305. {
  306. Penvstretch = Denvstretch;
  307. Pforcedrelease = Dforcedrelease;
  308. Plinearenvelope = Dlinearenvelope;
  309. PA_dt = DA_dt;
  310. PD_dt = DD_dt;
  311. PR_dt = DR_dt;
  312. PA_val = DA_val;
  313. PD_val = DD_val;
  314. PS_val = DS_val;
  315. PR_val = DR_val;
  316. Pfreemode = 0;
  317. converttofree();
  318. }
  319. void EnvelopeParams::store2defaults()
  320. {
  321. Denvstretch = Penvstretch;
  322. Dforcedrelease = Pforcedrelease;
  323. Dlinearenvelope = Plinearenvelope;
  324. DA_dt = PA_dt;
  325. DD_dt = PD_dt;
  326. DR_dt = PR_dt;
  327. DA_val = PA_val;
  328. DD_val = PD_val;
  329. DS_val = PS_val;
  330. DR_val = PR_val;
  331. }