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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <stdio.h>
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include "EnvelopeParams.h"
  21. EnvelopeParams::EnvelopeParams(unsigned char Penvstretch_,
  22. unsigned char Pforcedrelease_):Presets()
  23. {
  24. int i;
  25. PA_dt = 10;
  26. PD_dt = 10;
  27. PR_dt = 10;
  28. PA_val = 64;
  29. PD_val = 64;
  30. PS_val = 64;
  31. PR_val = 64;
  32. for(i = 0; i < MAX_ENVELOPE_POINTS; ++i) {
  33. Penvdt[i] = 32;
  34. Penvval[i] = 64;
  35. }
  36. Penvdt[0] = 0; //no used
  37. Penvsustain = 1;
  38. Penvpoints = 1;
  39. Envmode = 1;
  40. Penvstretch = Penvstretch_;
  41. Pforcedrelease = Pforcedrelease_;
  42. Pfreemode = 1;
  43. Plinearenvelope = 0;
  44. store2defaults();
  45. }
  46. EnvelopeParams::~EnvelopeParams()
  47. {}
  48. float EnvelopeParams::getdt(char i)
  49. {
  50. float result = (powf(2.0f, Penvdt[(int)i] / 127.0f * 12.0f) - 1.0f) * 10.0f; //miliseconds
  51. return result;
  52. }
  53. /*
  54. * ADSR/ASR... initialisations
  55. */
  56. void EnvelopeParams::ADSRinit(char A_dt, char D_dt, char S_val, char R_dt)
  57. {
  58. setpresettype("Penvamplitude");
  59. Envmode = 1;
  60. PA_dt = A_dt;
  61. PD_dt = D_dt;
  62. PS_val = S_val;
  63. PR_dt = R_dt;
  64. Pfreemode = 0;
  65. converttofree();
  66. store2defaults();
  67. }
  68. void EnvelopeParams::ADSRinit_dB(char A_dt, char D_dt, char S_val, char R_dt)
  69. {
  70. setpresettype("Penvamplitude");
  71. Envmode = 2;
  72. PA_dt = A_dt;
  73. PD_dt = D_dt;
  74. PS_val = S_val;
  75. PR_dt = R_dt;
  76. Pfreemode = 0;
  77. converttofree();
  78. store2defaults();
  79. }
  80. void EnvelopeParams::ASRinit(char A_val, char A_dt, char R_val, char R_dt)
  81. {
  82. setpresettype("Penvfrequency");
  83. Envmode = 3;
  84. PA_val = A_val;
  85. PA_dt = A_dt;
  86. PR_val = R_val;
  87. PR_dt = R_dt;
  88. Pfreemode = 0;
  89. converttofree();
  90. store2defaults();
  91. }
  92. void EnvelopeParams::ADSRinit_filter(char A_val,
  93. char A_dt,
  94. char D_val,
  95. char D_dt,
  96. char R_dt,
  97. char R_val)
  98. {
  99. setpresettype("Penvfilter");
  100. Envmode = 4;
  101. PA_val = A_val;
  102. PA_dt = A_dt;
  103. PD_val = D_val;
  104. PD_dt = D_dt;
  105. PR_dt = R_dt;
  106. PR_val = R_val;
  107. Pfreemode = 0;
  108. converttofree();
  109. store2defaults();
  110. }
  111. void EnvelopeParams::ASRinit_bw(char A_val, char A_dt, char R_val, char R_dt)
  112. {
  113. setpresettype("Penvbandwidth");
  114. Envmode = 5;
  115. PA_val = A_val;
  116. PA_dt = A_dt;
  117. PR_val = R_val;
  118. PR_dt = R_dt;
  119. Pfreemode = 0;
  120. converttofree();
  121. store2defaults();
  122. }
  123. /*
  124. * Convert the Envelope to freemode
  125. */
  126. void EnvelopeParams::converttofree()
  127. {
  128. switch(Envmode) {
  129. case 1:
  130. Penvpoints = 4;
  131. Penvsustain = 2;
  132. Penvval[0] = 0;
  133. Penvdt[1] = PA_dt;
  134. Penvval[1] = 127;
  135. Penvdt[2] = PD_dt;
  136. Penvval[2] = PS_val;
  137. Penvdt[3] = PR_dt;
  138. Penvval[3] = 0;
  139. break;
  140. case 2:
  141. Penvpoints = 4;
  142. Penvsustain = 2;
  143. Penvval[0] = 0;
  144. Penvdt[1] = PA_dt;
  145. Penvval[1] = 127;
  146. Penvdt[2] = PD_dt;
  147. Penvval[2] = PS_val;
  148. Penvdt[3] = PR_dt;
  149. Penvval[3] = 0;
  150. break;
  151. case 3:
  152. Penvpoints = 3;
  153. Penvsustain = 1;
  154. Penvval[0] = PA_val;
  155. Penvdt[1] = PA_dt;
  156. Penvval[1] = 64;
  157. Penvdt[2] = PR_dt;
  158. Penvval[2] = PR_val;
  159. break;
  160. case 4:
  161. Penvpoints = 4;
  162. Penvsustain = 2;
  163. Penvval[0] = PA_val;
  164. Penvdt[1] = PA_dt;
  165. Penvval[1] = PD_val;
  166. Penvdt[2] = PD_dt;
  167. Penvval[2] = 64;
  168. Penvdt[3] = PR_dt;
  169. Penvval[3] = PR_val;
  170. break;
  171. case 5:
  172. Penvpoints = 3;
  173. Penvsustain = 1;
  174. Penvval[0] = PA_val;
  175. Penvdt[1] = PA_dt;
  176. Penvval[1] = 64;
  177. Penvdt[2] = PR_dt;
  178. Penvval[2] = PR_val;
  179. break;
  180. }
  181. }
  182. void EnvelopeParams::add2XML(XMLwrapper *xml)
  183. {
  184. xml->addparbool("free_mode", Pfreemode);
  185. xml->addpar("env_points", Penvpoints);
  186. xml->addpar("env_sustain", Penvsustain);
  187. xml->addpar("env_stretch", Penvstretch);
  188. xml->addparbool("forced_release", Pforcedrelease);
  189. xml->addparbool("linear_envelope", Plinearenvelope);
  190. xml->addpar("A_dt", PA_dt);
  191. xml->addpar("D_dt", PD_dt);
  192. xml->addpar("R_dt", PR_dt);
  193. xml->addpar("A_val", PA_val);
  194. xml->addpar("D_val", PD_val);
  195. xml->addpar("S_val", PS_val);
  196. xml->addpar("R_val", PR_val);
  197. if((Pfreemode != 0) || (!xml->minimal))
  198. for(int i = 0; i < Penvpoints; ++i) {
  199. xml->beginbranch("POINT", i);
  200. if(i != 0)
  201. xml->addpar("dt", Penvdt[i]);
  202. xml->addpar("val", Penvval[i]);
  203. xml->endbranch();
  204. }
  205. }
  206. void EnvelopeParams::getfromXML(XMLwrapper *xml)
  207. {
  208. Pfreemode = xml->getparbool("free_mode", Pfreemode);
  209. Penvpoints = xml->getpar127("env_points", Penvpoints);
  210. Penvsustain = xml->getpar127("env_sustain", Penvsustain);
  211. Penvstretch = xml->getpar127("env_stretch", Penvstretch);
  212. Pforcedrelease = xml->getparbool("forced_release", Pforcedrelease);
  213. Plinearenvelope = xml->getparbool("linear_envelope", Plinearenvelope);
  214. PA_dt = xml->getpar127("A_dt", PA_dt);
  215. PD_dt = xml->getpar127("D_dt", PD_dt);
  216. PR_dt = xml->getpar127("R_dt", PR_dt);
  217. PA_val = xml->getpar127("A_val", PA_val);
  218. PD_val = xml->getpar127("D_val", PD_val);
  219. PS_val = xml->getpar127("S_val", PS_val);
  220. PR_val = xml->getpar127("R_val", PR_val);
  221. for(int i = 0; i < Penvpoints; ++i) {
  222. if(xml->enterbranch("POINT", i) == 0)
  223. continue;
  224. if(i != 0)
  225. Penvdt[i] = xml->getpar127("dt", Penvdt[i]);
  226. Penvval[i] = xml->getpar127("val", Penvval[i]);
  227. xml->exitbranch();
  228. }
  229. if(!Pfreemode)
  230. converttofree();
  231. }
  232. void EnvelopeParams::defaults()
  233. {
  234. Penvstretch = Denvstretch;
  235. Pforcedrelease = Dforcedrelease;
  236. Plinearenvelope = Dlinearenvelope;
  237. PA_dt = DA_dt;
  238. PD_dt = DD_dt;
  239. PR_dt = DR_dt;
  240. PA_val = DA_val;
  241. PD_val = DD_val;
  242. PS_val = DS_val;
  243. PR_val = DR_val;
  244. Pfreemode = 0;
  245. converttofree();
  246. }
  247. void EnvelopeParams::store2defaults()
  248. {
  249. Denvstretch = Penvstretch;
  250. Dforcedrelease = Pforcedrelease;
  251. Dlinearenvelope = Plinearenvelope;
  252. DA_dt = PA_dt;
  253. DD_dt = PD_dt;
  254. DR_dt = PR_dt;
  255. DA_val = PA_val;
  256. DD_val = PD_val;
  257. DS_val = PS_val;
  258. DR_val = PR_val;
  259. }