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

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