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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #define COPY(y) this->y = ep.y
  108. void EnvelopeParams::paste(const EnvelopeParams &ep)
  109. {
  110. COPY(Pfreemode);
  111. COPY(Penvpoints);
  112. COPY(Penvsustain);
  113. for(int i=0; i<MAX_ENVELOPE_POINTS; ++i) {
  114. this->Penvdt[i] = ep.Penvdt[i];
  115. this->Penvval[i] = ep.Penvval[i];
  116. }
  117. COPY(Penvstretch);
  118. COPY(Pforcedrelease);
  119. COPY(Plinearenvelope);
  120. COPY(PA_dt);
  121. COPY(PD_dt);
  122. COPY(PR_dt);
  123. COPY(PA_val);
  124. COPY(PD_val);
  125. COPY(PS_val);
  126. COPY(PR_val);
  127. }
  128. #undef COPY
  129. float EnvelopeParams::getdt(char i) const
  130. {
  131. return EnvelopeParams::dt(Penvdt[(int)i]);
  132. }
  133. float EnvelopeParams::dt(char val)
  134. {
  135. return (powf(2.0f, val / 127.0f * 12.0f) - 1.0f) * 10.0f; //miliseconds
  136. }
  137. /*
  138. * ADSR/ASR... initialisations
  139. */
  140. void EnvelopeParams::ADSRinit(char A_dt, char D_dt, char S_val, char R_dt)
  141. {
  142. setpresettype("Penvamplitude");
  143. Envmode = 1;
  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::ADSRinit_dB(char A_dt, char D_dt, char S_val, char R_dt)
  153. {
  154. setpresettype("Penvamplitude");
  155. Envmode = 2;
  156. PA_dt = A_dt;
  157. PD_dt = D_dt;
  158. PS_val = S_val;
  159. PR_dt = R_dt;
  160. Pfreemode = 0;
  161. converttofree();
  162. store2defaults();
  163. }
  164. void EnvelopeParams::ASRinit(char A_val, char A_dt, char R_val, char R_dt)
  165. {
  166. setpresettype("Penvfrequency");
  167. Envmode = 3;
  168. PA_val = A_val;
  169. PA_dt = A_dt;
  170. PR_val = R_val;
  171. PR_dt = R_dt;
  172. Pfreemode = 0;
  173. converttofree();
  174. store2defaults();
  175. }
  176. void EnvelopeParams::ADSRinit_filter(char A_val,
  177. char A_dt,
  178. char D_val,
  179. char D_dt,
  180. char R_dt,
  181. char R_val)
  182. {
  183. setpresettype("Penvfilter");
  184. Envmode = 4;
  185. PA_val = A_val;
  186. PA_dt = A_dt;
  187. PD_val = D_val;
  188. PD_dt = D_dt;
  189. PR_dt = R_dt;
  190. PR_val = R_val;
  191. Pfreemode = 0;
  192. converttofree();
  193. store2defaults();
  194. }
  195. void EnvelopeParams::ASRinit_bw(char A_val, char A_dt, char R_val, char R_dt)
  196. {
  197. setpresettype("Penvbandwidth");
  198. Envmode = 5;
  199. PA_val = A_val;
  200. PA_dt = A_dt;
  201. PR_val = R_val;
  202. PR_dt = R_dt;
  203. Pfreemode = 0;
  204. converttofree();
  205. store2defaults();
  206. }
  207. /*
  208. * Convert the Envelope to freemode
  209. */
  210. void EnvelopeParams::converttofree()
  211. {
  212. switch(Envmode) {
  213. case 1:
  214. Penvpoints = 4;
  215. Penvsustain = 2;
  216. Penvval[0] = 0;
  217. Penvdt[1] = PA_dt;
  218. Penvval[1] = 127;
  219. Penvdt[2] = PD_dt;
  220. Penvval[2] = PS_val;
  221. Penvdt[3] = PR_dt;
  222. Penvval[3] = 0;
  223. break;
  224. case 2:
  225. Penvpoints = 4;
  226. Penvsustain = 2;
  227. Penvval[0] = 0;
  228. Penvdt[1] = PA_dt;
  229. Penvval[1] = 127;
  230. Penvdt[2] = PD_dt;
  231. Penvval[2] = PS_val;
  232. Penvdt[3] = PR_dt;
  233. Penvval[3] = 0;
  234. break;
  235. case 3:
  236. Penvpoints = 3;
  237. Penvsustain = 1;
  238. Penvval[0] = PA_val;
  239. Penvdt[1] = PA_dt;
  240. Penvval[1] = 64;
  241. Penvdt[2] = PR_dt;
  242. Penvval[2] = PR_val;
  243. break;
  244. case 4:
  245. Penvpoints = 4;
  246. Penvsustain = 2;
  247. Penvval[0] = PA_val;
  248. Penvdt[1] = PA_dt;
  249. Penvval[1] = PD_val;
  250. Penvdt[2] = PD_dt;
  251. Penvval[2] = 64;
  252. Penvdt[3] = PR_dt;
  253. Penvval[3] = PR_val;
  254. break;
  255. case 5:
  256. Penvpoints = 3;
  257. Penvsustain = 1;
  258. Penvval[0] = PA_val;
  259. Penvdt[1] = PA_dt;
  260. Penvval[1] = 64;
  261. Penvdt[2] = PR_dt;
  262. Penvval[2] = PR_val;
  263. break;
  264. }
  265. }
  266. void EnvelopeParams::add2XML(XMLwrapper *xml)
  267. {
  268. xml->addparbool("free_mode", Pfreemode);
  269. xml->addpar("env_points", Penvpoints);
  270. xml->addpar("env_sustain", Penvsustain);
  271. xml->addpar("env_stretch", Penvstretch);
  272. xml->addparbool("forced_release", Pforcedrelease);
  273. xml->addparbool("linear_envelope", Plinearenvelope);
  274. xml->addpar("A_dt", PA_dt);
  275. xml->addpar("D_dt", PD_dt);
  276. xml->addpar("R_dt", PR_dt);
  277. xml->addpar("A_val", PA_val);
  278. xml->addpar("D_val", PD_val);
  279. xml->addpar("S_val", PS_val);
  280. xml->addpar("R_val", PR_val);
  281. if((Pfreemode != 0) || (!xml->minimal))
  282. for(int i = 0; i < Penvpoints; ++i) {
  283. xml->beginbranch("POINT", i);
  284. if(i != 0)
  285. xml->addpar("dt", Penvdt[i]);
  286. xml->addpar("val", Penvval[i]);
  287. xml->endbranch();
  288. }
  289. }
  290. void EnvelopeParams::getfromXML(XMLwrapper *xml)
  291. {
  292. Pfreemode = xml->getparbool("free_mode", Pfreemode);
  293. Penvpoints = xml->getpar127("env_points", Penvpoints);
  294. Penvsustain = xml->getpar127("env_sustain", Penvsustain);
  295. Penvstretch = xml->getpar127("env_stretch", Penvstretch);
  296. Pforcedrelease = xml->getparbool("forced_release", Pforcedrelease);
  297. Plinearenvelope = xml->getparbool("linear_envelope", Plinearenvelope);
  298. PA_dt = xml->getpar127("A_dt", PA_dt);
  299. PD_dt = xml->getpar127("D_dt", PD_dt);
  300. PR_dt = xml->getpar127("R_dt", PR_dt);
  301. PA_val = xml->getpar127("A_val", PA_val);
  302. PD_val = xml->getpar127("D_val", PD_val);
  303. PS_val = xml->getpar127("S_val", PS_val);
  304. PR_val = xml->getpar127("R_val", PR_val);
  305. for(int i = 0; i < Penvpoints; ++i) {
  306. if(xml->enterbranch("POINT", i) == 0)
  307. continue;
  308. if(i != 0)
  309. Penvdt[i] = xml->getpar127("dt", Penvdt[i]);
  310. Penvval[i] = xml->getpar127("val", Penvval[i]);
  311. xml->exitbranch();
  312. }
  313. if(!Pfreemode)
  314. converttofree();
  315. }
  316. void EnvelopeParams::defaults()
  317. {
  318. Penvstretch = Denvstretch;
  319. Pforcedrelease = Dforcedrelease;
  320. Plinearenvelope = Dlinearenvelope;
  321. PA_dt = DA_dt;
  322. PD_dt = DD_dt;
  323. PR_dt = DR_dt;
  324. PA_val = DA_val;
  325. PD_val = DD_val;
  326. PS_val = DS_val;
  327. PR_val = DR_val;
  328. Pfreemode = 0;
  329. converttofree();
  330. }
  331. void EnvelopeParams::store2defaults()
  332. {
  333. Denvstretch = Penvstretch;
  334. Dforcedrelease = Pforcedrelease;
  335. Dlinearenvelope = Plinearenvelope;
  336. DA_dt = PA_dt;
  337. DD_dt = PD_dt;
  338. DR_dt = PR_dt;
  339. DA_val = PA_val;
  340. DD_val = PD_val;
  341. DS_val = PS_val;
  342. DR_val = PR_val;
  343. }