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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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
  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 <cassert>
  14. #include <rtosc/ports.h>
  15. #include <rtosc/port-sugar.h>
  16. #include "zyn-version.h"
  17. #include "EnvelopeParams.h"
  18. #include "../Misc/Util.h"
  19. #include "../Misc/Time.h"
  20. #define rObject EnvelopeParams
  21. using namespace rtosc;
  22. #define rBegin [](const char *msg, RtData &d) { \
  23. EnvelopeParams *env = (rObject*) d.obj
  24. #define rEnd }
  25. static const rtosc::Ports localPorts = {
  26. rSelf(EnvelopeParams),
  27. rPaste,
  28. #undef rChangeCb
  29. #define rChangeCb if(!obj->Pfreemode) obj->converttofree(); if (obj->time) { \
  30. obj->last_update_timestamp = obj->time->time(); }
  31. rToggle(Pfreemode, "Complex Envelope Definitions"),
  32. #undef rChangeCb
  33. #define rChangeCb if(!obj->Pfreemode) obj->converttofree(); \
  34. if(obj->time) { obj->last_update_timestamp = obj->time->time(); }
  35. rParamZyn(Penvpoints, rProp(internal), "Number of points in complex definition"),
  36. rParamZyn(Penvsustain, "Location of the sustain point"),
  37. rParams(Penvdt, MAX_ENVELOPE_POINTS, "Envelope Delay Times"),
  38. rParams(Penvval, MAX_ENVELOPE_POINTS, "Envelope Values"),
  39. rParamZyn(Penvstretch, rShort("stretch"),
  40. "Stretch with respect to frequency"),
  41. rToggle(Pforcedrelease, rShort("frcr"),
  42. "Force Envelope to fully evaluate"),
  43. rToggle(Plinearenvelope, rShort("lin/log"),
  44. "Linear or Logarithmic Envelopes"),
  45. rParamZyn(PA_dt, rShort("a.dt"), "Attack Time"),
  46. rParamZyn(PA_val, rShort("a.val"), "Attack Value"),
  47. rParamZyn(PD_dt, rShort("d.dt"), "Decay Time"),
  48. rParamZyn(PD_val, rShort("d.val"), "Decay Value"),
  49. rParamZyn(PS_val, rShort("s.val"), "Sustain Value"),
  50. rParamZyn(PR_dt, rShort("r.dt"), "Release Time"),
  51. rParamZyn(PR_val, rShort("r.val"), "Release Value"),
  52. {"Envmode:", rDoc("Envelope variant type"), NULL,
  53. rBegin;
  54. d.reply(d.loc, "i", env->Envmode);
  55. rEnd},
  56. {"envdt", rDoc("Envelope Delay Times"), NULL,
  57. rBegin;
  58. const int N = MAX_ENVELOPE_POINTS;
  59. const int M = rtosc_narguments(msg);
  60. if(M == 0) {
  61. rtosc_arg_t args[N];
  62. char arg_types[N+1] = {0};
  63. for(int i=0; i<N; ++i) {
  64. args[i].f = env->getdt(i);
  65. arg_types[i] = 'f';
  66. }
  67. d.replyArray(d.loc, arg_types, args);
  68. } else {
  69. for(int i=0; i<N && i<M; ++i)
  70. env->Penvdt[i] = env->inv_dt(rtosc_argument(msg, i).f);
  71. }
  72. rEnd},
  73. {"envval", rDoc("Envelope Delay Times"), NULL,
  74. rBegin;
  75. const int N = MAX_ENVELOPE_POINTS;
  76. const int M = rtosc_narguments(msg);
  77. if(M == 0) {
  78. rtosc_arg_t args[N];
  79. char arg_types[N+1] = {0};
  80. for(int i=0; i<N; ++i) {
  81. args[i].f = env->Penvval[i]/127.0f;
  82. arg_types[i] = 'f';
  83. }
  84. d.replyArray(d.loc, arg_types, args);
  85. } else {
  86. for(int i=0; i<N && i<M; ++i) {
  87. env->Penvval[i] = limit(roundf(rtosc_argument(msg,i).f*127.0f), 0.0f, 127.0f);
  88. }
  89. }
  90. rEnd},
  91. {"addPoint:i", rProp(internal) rDoc("Add point to envelope"), NULL,
  92. rBegin;
  93. const int curpoint = rtosc_argument(msg, 0).i;
  94. //int curpoint=freeedit->lastpoint;
  95. if (curpoint<0 || curpoint>env->Penvpoints || env->Penvpoints>=MAX_ENVELOPE_POINTS)
  96. return;
  97. for (int i=env->Penvpoints; i>=curpoint+1; i--) {
  98. env->Penvdt[i]=env->Penvdt[i-1];
  99. env->Penvval[i]=env->Penvval[i-1];
  100. }
  101. if (curpoint==0)
  102. env->Penvdt[1]=64;
  103. env->Penvpoints++;
  104. if (curpoint<=env->Penvsustain)
  105. env->Penvsustain++;
  106. rEnd},
  107. {"delPoint:i", rProp(internal) rDoc("Delete Envelope Point"), NULL,
  108. rBegin;
  109. const int curpoint=rtosc_argument(msg, 0).i;
  110. if(curpoint<1 || curpoint>=env->Penvpoints-1 || env->Penvpoints<=3)
  111. return;
  112. for (int i=curpoint+1;i<env->Penvpoints;i++){
  113. env->Penvdt[i-1]=env->Penvdt[i];
  114. env->Penvval[i-1]=env->Penvval[i];
  115. };
  116. env->Penvpoints--;
  117. if (curpoint<=env->Penvsustain)
  118. env->Penvsustain--;
  119. rEnd},
  120. };
  121. #undef rChangeCb
  122. const rtosc::Ports &EnvelopeParams::ports = localPorts;
  123. EnvelopeParams::EnvelopeParams(unsigned char Penvstretch_,
  124. unsigned char Pforcedrelease_,
  125. const AbsTime *time_):
  126. time(time_), last_update_timestamp(0)
  127. {
  128. PA_dt = 10;
  129. PD_dt = 10;
  130. PR_dt = 10;
  131. PA_val = 64;
  132. PD_val = 64;
  133. PS_val = 64;
  134. PR_val = 64;
  135. for(int i = 0; i < MAX_ENVELOPE_POINTS; ++i) {
  136. Penvdt[i] = 32;
  137. Penvval[i] = 64;
  138. }
  139. Penvdt[0] = 0; //no used
  140. Penvsustain = 1;
  141. Penvpoints = 1;
  142. Envmode = 1;
  143. Penvstretch = Penvstretch_;
  144. Pforcedrelease = Pforcedrelease_;
  145. Pfreemode = 1;
  146. Plinearenvelope = 0;
  147. store2defaults();
  148. }
  149. EnvelopeParams::~EnvelopeParams()
  150. {}
  151. #define COPY(y) this->y = ep.y
  152. void EnvelopeParams::paste(const EnvelopeParams &ep)
  153. {
  154. COPY(Pfreemode);
  155. COPY(Penvpoints);
  156. COPY(Penvsustain);
  157. for(int i=0; i<MAX_ENVELOPE_POINTS; ++i) {
  158. this->Penvdt[i] = ep.Penvdt[i];
  159. this->Penvval[i] = ep.Penvval[i];
  160. }
  161. COPY(Penvstretch);
  162. COPY(Pforcedrelease);
  163. COPY(Plinearenvelope);
  164. COPY(PA_dt);
  165. COPY(PD_dt);
  166. COPY(PR_dt);
  167. COPY(PA_val);
  168. COPY(PD_val);
  169. COPY(PS_val);
  170. COPY(PR_val);
  171. if ( time ) {
  172. last_update_timestamp = time->time();
  173. }
  174. }
  175. #undef COPY
  176. float EnvelopeParams::getdt(char i) const
  177. {
  178. return EnvelopeParams::dt(Penvdt[(int)i]);
  179. }
  180. float EnvelopeParams::dt(char val)
  181. {
  182. return (powf(2.0f, val / 127.0f * 12.0f) - 1.0f) * 10.0f; //miliseconds
  183. }
  184. char EnvelopeParams::inv_dt(float val)
  185. {
  186. int ival = roundf(logf(val/10.0f + 1.0f)/logf(2.0f) * 127.0f/12.0f);
  187. return limit(ival, 0, 127);
  188. }
  189. /*
  190. * ADSR/ASR... initialisations
  191. */
  192. void EnvelopeParams::ADSRinit(char A_dt, char D_dt, char S_val, char R_dt)
  193. {
  194. setpresettype("Penvamplitude");
  195. Envmode = 1;
  196. PA_dt = A_dt;
  197. PD_dt = D_dt;
  198. PS_val = S_val;
  199. PR_dt = R_dt;
  200. Pfreemode = 0;
  201. converttofree();
  202. store2defaults();
  203. }
  204. void EnvelopeParams::ADSRinit_dB(char A_dt, char D_dt, char S_val, char R_dt)
  205. {
  206. setpresettype("Penvamplitude");
  207. Envmode = 2;
  208. PA_dt = A_dt;
  209. PD_dt = D_dt;
  210. PS_val = S_val;
  211. PR_dt = R_dt;
  212. Pfreemode = 0;
  213. converttofree();
  214. store2defaults();
  215. }
  216. void EnvelopeParams::ASRinit(char A_val, char A_dt, char R_val, char R_dt)
  217. {
  218. setpresettype("Penvfrequency");
  219. Envmode = 3;
  220. PA_val = A_val;
  221. PA_dt = A_dt;
  222. PR_val = R_val;
  223. PR_dt = R_dt;
  224. Pfreemode = 0;
  225. converttofree();
  226. store2defaults();
  227. }
  228. void EnvelopeParams::ADSRinit_filter(char A_val,
  229. char A_dt,
  230. char D_val,
  231. char D_dt,
  232. char R_dt,
  233. char R_val)
  234. {
  235. setpresettype("Penvfilter");
  236. Envmode = 4;
  237. PA_val = A_val;
  238. PA_dt = A_dt;
  239. PD_val = D_val;
  240. PD_dt = D_dt;
  241. PR_dt = R_dt;
  242. PR_val = R_val;
  243. Pfreemode = 0;
  244. converttofree();
  245. store2defaults();
  246. }
  247. void EnvelopeParams::ASRinit_bw(char A_val, char A_dt, char R_val, char R_dt)
  248. {
  249. setpresettype("Penvbandwidth");
  250. Envmode = 5;
  251. PA_val = A_val;
  252. PA_dt = A_dt;
  253. PR_val = R_val;
  254. PR_dt = R_dt;
  255. Pfreemode = 0;
  256. converttofree();
  257. store2defaults();
  258. }
  259. /*
  260. * Convert the Envelope to freemode
  261. */
  262. void EnvelopeParams::converttofree()
  263. {
  264. switch(Envmode) {
  265. case 1:
  266. Penvpoints = 4;
  267. Penvsustain = 2;
  268. Penvval[0] = 0;
  269. Penvdt[1] = PA_dt;
  270. Penvval[1] = 127;
  271. Penvdt[2] = PD_dt;
  272. Penvval[2] = PS_val;
  273. Penvdt[3] = PR_dt;
  274. Penvval[3] = 0;
  275. break;
  276. case 2:
  277. Penvpoints = 4;
  278. Penvsustain = 2;
  279. Penvval[0] = 0;
  280. Penvdt[1] = PA_dt;
  281. Penvval[1] = 127;
  282. Penvdt[2] = PD_dt;
  283. Penvval[2] = PS_val;
  284. Penvdt[3] = PR_dt;
  285. Penvval[3] = 0;
  286. break;
  287. case 3:
  288. Penvpoints = 3;
  289. Penvsustain = 1;
  290. Penvval[0] = PA_val;
  291. Penvdt[1] = PA_dt;
  292. Penvval[1] = 64;
  293. Penvdt[2] = PR_dt;
  294. Penvval[2] = PR_val;
  295. break;
  296. case 4:
  297. Penvpoints = 4;
  298. Penvsustain = 2;
  299. Penvval[0] = PA_val;
  300. Penvdt[1] = PA_dt;
  301. Penvval[1] = PD_val;
  302. Penvdt[2] = PD_dt;
  303. Penvval[2] = 64;
  304. Penvdt[3] = PR_dt;
  305. Penvval[3] = PR_val;
  306. break;
  307. case 5:
  308. Penvpoints = 3;
  309. Penvsustain = 1;
  310. Penvval[0] = PA_val;
  311. Penvdt[1] = PA_dt;
  312. Penvval[1] = 64;
  313. Penvdt[2] = PR_dt;
  314. Penvval[2] = PR_val;
  315. break;
  316. }
  317. }
  318. void EnvelopeParams::add2XML(XMLwrapper& xml)
  319. {
  320. xml.addparbool("free_mode", Pfreemode);
  321. xml.addpar("env_points", Penvpoints);
  322. xml.addpar("env_sustain", Penvsustain);
  323. xml.addpar("env_stretch", Penvstretch);
  324. xml.addparbool("forced_release", Pforcedrelease);
  325. xml.addparbool("linear_envelope", Plinearenvelope);
  326. xml.addpar("A_dt", PA_dt);
  327. xml.addpar("D_dt", PD_dt);
  328. xml.addpar("R_dt", PR_dt);
  329. xml.addpar("A_val", PA_val);
  330. xml.addpar("D_val", PD_val);
  331. xml.addpar("S_val", PS_val);
  332. xml.addpar("R_val", PR_val);
  333. if((Pfreemode != 0) || (!xml.minimal))
  334. for(int i = 0; i < Penvpoints; ++i) {
  335. xml.beginbranch("POINT", i);
  336. if(i != 0)
  337. xml.addpar("dt", Penvdt[i]);
  338. xml.addpar("val", Penvval[i]);
  339. xml.endbranch();
  340. }
  341. }
  342. float EnvelopeParams::env_dB2rap(float db) {
  343. return (powf(10.0f, db / 20.0f) - 0.01)/.99f;
  344. }
  345. float EnvelopeParams::env_rap2dB(float rap) {
  346. return 20.0f * log10f(rap * 0.99f + 0.01);
  347. }
  348. /**
  349. since commit 5334d94283a513ae42e472aa020db571a3589fb9, i.e. between
  350. versions 2.4.3 and 2.4.4, the amplitude envelope has been converted
  351. differently from dB to rap for AmplitudeEnvelope (mode 2)
  352. this converts the values read from an XML file once
  353. */
  354. struct version_fixer_t
  355. {
  356. const bool mismatch;
  357. public:
  358. int operator()(int input) const
  359. {
  360. return (mismatch)
  361. // The errors occured when calling env_dB2rap. Let f be the
  362. // conversion function for mode 2 (see Envelope.cpp), then we
  363. // load values with (let "o" be the function composition symbol):
  364. // f^{-1} o (env_dB2rap^{-1}) o dB2rap o f
  365. // from the xml file. This results in the following formula:
  366. ? roundf(127.0f * (0.5f *
  367. log10f( 0.01f + 0.99f *
  368. powf(100, input/127.0f - 1))
  369. + 1))
  370. : input;
  371. }
  372. version_fixer_t(const version_type& fileversion, int env_mode) :
  373. mismatch(fileversion < version_type(2,4,4) &&
  374. (env_mode == 2)) {}
  375. };
  376. void EnvelopeParams::getfromXML(XMLwrapper& xml)
  377. {
  378. Pfreemode = xml.getparbool("free_mode", Pfreemode);
  379. Penvpoints = xml.getpar127("env_points", Penvpoints);
  380. Penvsustain = xml.getpar127("env_sustain", Penvsustain);
  381. Penvstretch = xml.getpar127("env_stretch", Penvstretch);
  382. Pforcedrelease = xml.getparbool("forced_release", Pforcedrelease);
  383. Plinearenvelope = xml.getparbool("linear_envelope", Plinearenvelope);
  384. version_fixer_t version_fix(xml.fileversion(), Envmode);
  385. PA_dt = xml.getpar127("A_dt", PA_dt);
  386. PD_dt = xml.getpar127("D_dt", PD_dt);
  387. PR_dt = xml.getpar127("R_dt", PR_dt);
  388. PA_val = version_fix(xml.getpar127("A_val", PA_val));
  389. PD_val = version_fix(xml.getpar127("D_val", PD_val));
  390. PS_val = version_fix(xml.getpar127("S_val", PS_val));
  391. PR_val = version_fix(xml.getpar127("R_val", PR_val));
  392. for(int i = 0; i < Penvpoints; ++i) {
  393. if(xml.enterbranch("POINT", i) == 0)
  394. continue;
  395. if(i != 0)
  396. Penvdt[i] = xml.getpar127("dt", Penvdt[i]);
  397. Penvval[i] = version_fix(xml.getpar127("val", Penvval[i]));
  398. xml.exitbranch();
  399. }
  400. if(!Pfreemode)
  401. converttofree();
  402. }
  403. void EnvelopeParams::defaults()
  404. {
  405. Penvstretch = Denvstretch;
  406. Pforcedrelease = Dforcedrelease;
  407. Plinearenvelope = Dlinearenvelope;
  408. PA_dt = DA_dt;
  409. PD_dt = DD_dt;
  410. PR_dt = DR_dt;
  411. PA_val = DA_val;
  412. PD_val = DD_val;
  413. PS_val = DS_val;
  414. PR_val = DR_val;
  415. Pfreemode = 0;
  416. converttofree();
  417. }
  418. void EnvelopeParams::store2defaults()
  419. {
  420. Denvstretch = Penvstretch;
  421. Dforcedrelease = Pforcedrelease;
  422. Dlinearenvelope = Plinearenvelope;
  423. DA_dt = PA_dt;
  424. DD_dt = PD_dt;
  425. DR_dt = PR_dt;
  426. DA_val = PA_val;
  427. DD_val = PD_val;
  428. DS_val = PS_val;
  429. DR_val = PR_val;
  430. }