ExprSynth.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * ExprSynth.cpp - Implementation of a Frontend to ExprTk
  3. *
  4. * Copyright (c) 2016-2017 Orr Dvori
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #include "ExprSynth.h"
  25. #include <string>
  26. #include <vector>
  27. #include <math.h>
  28. #include <cstdlib>
  29. #include <random>
  30. #include "Xpressive.h"
  31. #include "interpolation.h"
  32. #include "lmms_math.h"
  33. #include "NotePlayHandle.h"
  34. #include "exprtk.hpp"
  35. #define WARN_EXPRTK qWarning("ExprTk exception")
  36. typedef exprtk::symbol_table<float> symbol_table_t;
  37. typedef exprtk::expression<float> expression_t;
  38. typedef exprtk::parser<float> parser_t;
  39. template <typename T,typename Functor,bool optimize>
  40. struct freefunc0 : public exprtk::ifunction<T>
  41. {
  42. using exprtk::ifunction<T>::operator();
  43. freefunc0() : exprtk::ifunction<T>(0) {
  44. if (optimize) { exprtk::disable_has_side_effects(*this); }
  45. }
  46. inline T operator()()
  47. { return Functor::process(); }
  48. };
  49. template <typename T,typename Functor,bool optimize>
  50. struct freefunc1 : public exprtk::ifunction<T>
  51. {
  52. using exprtk::ifunction<T>::operator();
  53. freefunc1() : exprtk::ifunction<T>(1) {
  54. if (optimize) { exprtk::disable_has_side_effects(*this); }
  55. }
  56. inline T operator()(const T& x)
  57. { return Functor::process(x); }
  58. };
  59. template <typename T>
  60. struct IntegrateFunction : public exprtk::ifunction<T>
  61. {
  62. using exprtk::ifunction<T>::operator();
  63. virtual ~IntegrateFunction()
  64. {
  65. delete [] m_counters;
  66. }
  67. IntegrateFunction(const unsigned int* frame, unsigned int sample_rate,unsigned int max_counters) :
  68. exprtk::ifunction<T>(1),
  69. m_frame(frame),
  70. m_sample_rate(sample_rate),
  71. m_max_counters(max_counters),
  72. m_nCounters(0),
  73. m_nCountersCalls(0),
  74. m_cc(0)
  75. {
  76. m_counters=new double[max_counters];
  77. clearArray(m_counters,max_counters);
  78. }
  79. inline T operator()(const T& x)
  80. {
  81. if (*m_frame == 0)
  82. {
  83. ++m_nCountersCalls;
  84. if (m_nCountersCalls > m_max_counters)
  85. {
  86. return 0;
  87. }
  88. m_cc = m_nCounters;
  89. ++m_nCounters;
  90. }
  91. T res = 0;
  92. if (m_cc < m_nCounters)
  93. {
  94. res = m_counters[m_cc];
  95. m_counters[m_cc] += x;
  96. }
  97. m_cc = (m_cc + 1) % m_nCountersCalls;
  98. return res / m_sample_rate;
  99. }
  100. const unsigned int* const m_frame;
  101. const unsigned int m_sample_rate;
  102. const unsigned int m_max_counters;
  103. unsigned int m_nCounters;
  104. unsigned int m_nCountersCalls;
  105. unsigned int m_cc;
  106. double *m_counters;
  107. };
  108. template <typename T>
  109. struct LastSampleFunction : public exprtk::ifunction<T>
  110. {
  111. using exprtk::ifunction<T>::operator();
  112. virtual ~LastSampleFunction()
  113. {
  114. delete [] m_samples;
  115. }
  116. LastSampleFunction(unsigned int history_size) :
  117. exprtk::ifunction<T>(1),
  118. m_history_size(history_size),
  119. m_pivot_last(history_size - 1)
  120. {
  121. m_samples = new T[history_size];
  122. clearArray(m_samples, history_size);
  123. }
  124. inline T operator()(const T& x)
  125. {
  126. if (!std::isnan(x) && !std::isinf(x))
  127. {
  128. const int ix=(int)x;
  129. if (ix>=1 && ix<=m_history_size)
  130. {
  131. return m_samples[(ix + m_pivot_last) % m_history_size];
  132. }
  133. }
  134. return 0;
  135. }
  136. void setLastSample(const T& sample)
  137. {
  138. if (!std::isnan(sample) && !std::isinf(sample))
  139. {
  140. m_samples[m_pivot_last] = sample;
  141. }
  142. if (m_pivot_last == 0)
  143. {
  144. m_pivot_last = m_history_size - 1;
  145. }
  146. else {
  147. --m_pivot_last;
  148. }
  149. }
  150. unsigned int m_history_size;
  151. unsigned int m_pivot_last;
  152. T *m_samples;
  153. };
  154. template <typename T>
  155. struct WaveValueFunction : public exprtk::ifunction<T>
  156. {
  157. using exprtk::ifunction<T>::operator();
  158. WaveValueFunction(const T* v, std::size_t s)
  159. : exprtk::ifunction<T>(1),
  160. m_vec(v),
  161. m_size(s)
  162. {}
  163. inline T operator()(const T& index)
  164. {
  165. return m_vec[(int) ( positiveFraction(index) * m_size )];
  166. }
  167. const T *m_vec;
  168. const std::size_t m_size;
  169. };
  170. template <typename T>
  171. struct WaveValueFunctionInterpolate : public exprtk::ifunction<T>
  172. {
  173. using exprtk::ifunction<T>::operator();
  174. WaveValueFunctionInterpolate(const T* v, std::size_t s)
  175. : exprtk::ifunction<T>(1),
  176. m_vec(v),
  177. m_size(s)
  178. {}
  179. inline T operator()(const T& index)
  180. {
  181. const T x = positiveFraction(index) * m_size;
  182. const int ix = (int)x;
  183. const float xfrc = fraction(x);
  184. return linearInterpolate(m_vec[ix], m_vec[(ix + 1) % m_size], xfrc);
  185. }
  186. const T *m_vec;
  187. const std::size_t m_size;
  188. };
  189. static const unsigned int random_data[257]={
  190. 0xd76a33ec, 0x4a767724, 0xb34ebd08 ,0xf4024196,
  191. 0x17b426e2, 0x8dc6389a, 0x1b5dcb93 ,0xa771bd3f,
  192. 0x078d502e, 0x8980988a, 0x1f64f846 ,0xb5b48ed7,
  193. 0xf0742cfb, 0xe7c66303, 0xc9472876 ,0x6c7494a5,
  194. 0x5c2203a1, 0x23986344, 0x7d344fa0 ,0x4f39474a,
  195. 0x28ac8b2b, 0x10f779b2, 0x6e79e659 ,0x32e44c52,
  196. 0xf790aa55, 0x98b05083, 0xb5d44f1c ,0xe553da04,
  197. 0xa884c6d2, 0x43274953, 0xbcb57404 ,0x43f7d32a,
  198. 0xf1890f8b, 0x019f4dce, 0x5c4ede33 ,0x2dec1a7e,
  199. 0x0f3eab09, 0x2197c93c, 0xae933f42 ,0x80d4b111,
  200. 0x6e5bd30a, 0x17139c70, 0xe15f7eb0 ,0x1798f893,
  201. 0xe1c6be1c, 0xe21edf9b, 0x4702e081 ,0x8a2cb85a,
  202. 0xbf3c1f15, 0x147f4685, 0x9221d731 ,0x3c7580f3,
  203. 0xc1c08498, 0x8e198b35, 0xf821c15a ,0x4d3cd2d4,
  204. 0xad89a3b7, 0xd48f915f, 0xcaf893f0 ,0xa64a4b8e,
  205. 0x20715f54, 0x1ba4de0a, 0x17ac6e91 ,0xd82ea8c0,
  206. 0x638a0ba5, 0xe7a76c0f, 0x486c5476 ,0x334bbd0a,
  207. 0xffe29c55, 0x7247efaf, 0x15f98e83 ,0x7a4a79ac,
  208. 0x350cd175, 0xc7107908, 0xa85c67f7 ,0x9c5002c4,
  209. 0x3cf27d2c, 0x314d8450, 0x05552886 ,0x87a73642,
  210. 0x827832e4, 0x9412cc67, 0x261979e6 ,0xb31da27f,
  211. 0x3e6bbafb, 0x663f1968, 0xd84274e2, 0xdd91d982,
  212. 0xd25c4805, 0x9567f860, 0xab99675c, 0x2254733b,
  213. 0x18799dd7, 0xee328916, 0xb9419a1b, 0x01b7a66f,
  214. 0xbcdc05e1, 0x788de4ae, 0x366e77cf, 0x81a1ebd2,
  215. 0x97be859a, 0x17d4b533, 0x22dab3a9, 0xc99871ea,
  216. 0xc7502c91, 0x4474b65f, 0x655d059d, 0x0ddc1348,
  217. 0x8325909b, 0x4873c155, 0x9fa30438, 0x7250b7a8,
  218. 0x90db2715, 0xf65e1cef, 0x41b74cf4, 0x38fba01c,
  219. 0xe9eefb40, 0x9e5524ea, 0x1d3fc077, 0x04ec39db,
  220. 0x1c0d501c, 0xb93f26d9, 0xf9f910b9, 0x806fce99,
  221. 0x5691ffdf, 0x1e63b27a, 0xf2035d42, 0xd3218a0b,
  222. 0x12eae6db, 0xeba372a9, 0x6f975260, 0xc514ae91,
  223. 0xebddb8ad, 0xc53207c0, 0xdbda57dc, 0x8fb38ef4,
  224. 0xfaa4f1bc, 0x40caf49f, 0xcb394b41, 0x424fc698,
  225. 0xb79a9ece, 0x331202d6, 0xc604ed4d, 0x5e85819f,
  226. 0x67222eda, 0xd976ba71, 0x7d083ec6, 0x040c819e,
  227. 0xc762c934, 0xa6684333, 0x2eaccc54, 0x69dc04b9,
  228. 0x0499cf36, 0x6351f438, 0x6db2dc34, 0x787ae036,
  229. 0x11b5c6ac, 0x552b7227, 0x32a4c993, 0xf7f4c49d,
  230. 0x7ac9e2d9, 0xf3d32020, 0x4ff01f89, 0x6f0e60bb,
  231. 0x3c6ed445, 0x7ca01986, 0x96901ecf, 0xe10df188,
  232. 0x62a6da6d, 0x8deee09f, 0x5347cb66, 0x5249f452,
  233. 0x22704d4d, 0x6221555f, 0x6aa0ea90, 0xe1f7bae3,
  234. 0xd106626f, 0x6365a9db, 0x1989bb81, 0xfc2daa73,
  235. 0x303c60b3, 0xcd867baa, 0x7c5787c2, 0x60082b30,
  236. 0xa68d3a81, 0x15a10f5d, 0x81b21c8a, 0x4bfb82e2,
  237. 0xff01c176, 0xff3c8b65, 0x8cc6bd29, 0xc678d6ff,
  238. 0x99b86508, 0x3c47e314, 0x766ecc05, 0xba186cb0,
  239. 0x42f57199, 0x5ef524f4, 0xb8419750, 0x6ae2a9d0,
  240. 0x291eaa18, 0x4e64b189, 0x506eb1d3, 0x78361d46,
  241. 0x6a2fcb7e, 0xbc0a46de, 0xb557badf, 0xad3de958,
  242. 0xa2901279, 0x491decbf, 0x257383df, 0x94dd19d1,
  243. 0xd0cfbbe2, 0x9063d36d, 0x81e44c3b, 0x973e9cc9,
  244. 0xfbe34690, 0x4eee3034, 0x1c413676, 0xf6735b8f,
  245. 0xf2991aca, 0x0ec85159, 0x6ce00ade, 0xad49de57,
  246. 0x025edf30, 0x42722b67, 0x30cfa6b2, 0x32df8676,
  247. 0x387d4500, 0x97fa67fd, 0x027c994a, 0x77c71d0c,
  248. 0x478eb75a, 0x898370a6, 0x73e7cca3, 0x34ace0ad,
  249. 0xc8ecb388, 0x5375c3aa, 0x9c194d87, 0x1b65246d,
  250. 0xca000bcf, 0x8a0fb13d, 0x81b957b0, 0xac627bfb,
  251. 0xc0fe47e5, 0xf3db0ad8, 0x1c605c7d, 0x5f579884,
  252. 0x63e079b5, 0x3d96f7cf, 0x3edd46e9, 0xc347c61e,
  253. 0x7b2b2a0e, 0x63dfcf51, 0x596781dd, 0x80304c4d,
  254. 0xa66f8b47
  255. };
  256. inline unsigned int rotateLeft(unsigned int x, const int b)
  257. {
  258. if (b > -32 && b < 32 && b != 0)
  259. {
  260. if (b < 0)
  261. {
  262. x= ( x >> (-b) ) | ( x << (32 + b) );
  263. }
  264. else
  265. {
  266. x= ( x << b ) | ( x >> (32 - b) );
  267. }
  268. }
  269. return x;
  270. }
  271. struct RandomVectorSeedFunction : public exprtk::ifunction<float>
  272. {
  273. using exprtk::ifunction<float>::operator();
  274. RandomVectorSeedFunction() :
  275. exprtk::ifunction<float>(2)
  276. { exprtk::disable_has_side_effects(*this); }
  277. static inline float randv(const float& index,int irseed)
  278. {
  279. if (index < 0 || std::isnan(index) || std::isinf(index))
  280. {
  281. return 0;
  282. }
  283. const unsigned int xi = (unsigned int)index;
  284. const unsigned int si = irseed % data_size;
  285. const unsigned int sa = irseed / data_size;
  286. unsigned int res=rotateLeft(random_data[(xi + 23 * si + 1) % data_size] ^ random_data[(xi / data_size + sa) % data_size],sa % 31 + 1);
  287. res ^= rotateLeft(random_data[(3 * xi + si + 13) % data_size],(xi+2*si) % 32) ^rotateLeft( random_data[(xi / data_size + 2 * sa) % data_size],xi % 31 + 1);
  288. return static_cast<int>(res) / (float)(1 << 31);
  289. }
  290. inline float operator()(const float& index,const float& seed)
  291. {
  292. int irseed;
  293. if (seed < 0 || std::isnan(seed) || std::isinf(seed))
  294. {
  295. irseed=0;
  296. }
  297. else
  298. irseed=(int)seed;
  299. return randv(index,irseed);
  300. }
  301. static const int data_size=sizeof(random_data)/sizeof(int);
  302. };
  303. static RandomVectorSeedFunction randsv_func;
  304. struct RandomVectorFunction : public exprtk::ifunction<float>
  305. {
  306. using exprtk::ifunction<float>::operator();
  307. RandomVectorFunction(const unsigned int seed) :
  308. exprtk::ifunction<float>(1),
  309. m_rseed(seed)
  310. { exprtk::disable_has_side_effects(*this); }
  311. inline float operator()(const float& index)
  312. {
  313. return RandomVectorSeedFunction::randv(index,m_rseed);
  314. }
  315. const unsigned int m_rseed;
  316. };
  317. namespace SimpleRandom {
  318. std::mt19937 generator (17); // mt19937 is a standard mersenne_twister_engine
  319. std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
  320. struct float_random_with_engine
  321. {
  322. static inline float process()
  323. {
  324. return dist(generator);
  325. }
  326. };
  327. }
  328. static freefunc0<float,SimpleRandom::float_random_with_engine,false> simple_rand;
  329. class ExprFrontData
  330. {
  331. public:
  332. ExprFrontData(int last_func_samples):
  333. m_rand_vec(SimpleRandom::generator()),
  334. m_integ_func(NULL),
  335. m_last_func(last_func_samples)
  336. {}
  337. ~ExprFrontData()
  338. {
  339. for (int i = 0; i < m_cyclics.size() ; ++i)
  340. {
  341. delete m_cyclics[i];
  342. }
  343. for (int i = 0; i < m_cyclics_interp.size() ; ++i)
  344. {
  345. delete m_cyclics_interp[i];
  346. }
  347. if (m_integ_func)
  348. {
  349. delete m_integ_func;
  350. }
  351. }
  352. symbol_table_t m_symbol_table;
  353. expression_t m_expression;
  354. std::string m_expression_string;
  355. std::vector<WaveValueFunction<float>* > m_cyclics;
  356. std::vector<WaveValueFunctionInterpolate<float>* > m_cyclics_interp;
  357. RandomVectorFunction m_rand_vec;
  358. IntegrateFunction<float> *m_integ_func;
  359. LastSampleFunction<float> m_last_func;
  360. };
  361. struct sin_wave
  362. {
  363. static inline float process(float x)
  364. {
  365. x = positiveFraction(x);
  366. return sinf(x * F_2PI);
  367. }
  368. };
  369. static freefunc1<float,sin_wave,true> sin_wave_func;
  370. struct square_wave
  371. {
  372. static inline float process(float x)
  373. {
  374. x = positiveFraction(x);
  375. if (x >= 0.5f) { return -1.0f; }
  376. else { return 1.0f; }
  377. }
  378. };
  379. static freefunc1<float,square_wave,true> square_wave_func;
  380. struct triangle_wave
  381. {
  382. static inline float process(float x)
  383. {
  384. x=positiveFraction(x);
  385. if (x < 0.25f)
  386. {
  387. return x * 4.0f;
  388. }
  389. else
  390. {
  391. if (x < 0.75f) { return 2.0f - x * 4.0f; }
  392. else { return x * 4.0f - 4.0f; }
  393. }
  394. }
  395. };
  396. static freefunc1<float,triangle_wave,true> triangle_wave_func;
  397. struct saw_wave
  398. {
  399. static inline float process(float x)
  400. {
  401. x=positiveFraction(x);
  402. return 2.0f * x - 1.0f;
  403. }
  404. };
  405. static freefunc1<float,saw_wave,true> saw_wave_func;
  406. struct moogsaw_wave
  407. {
  408. static inline float process(float x)
  409. {
  410. x = positiveFraction(x);
  411. if( x < 0.5f )
  412. {
  413. return -1.0f + x * 4.0f;
  414. }
  415. return 1.0f - 2.0f * x;
  416. }
  417. };
  418. static freefunc1<float,moogsaw_wave,true> moogsaw_wave_func;
  419. struct moog_wave
  420. {
  421. static inline float process(float x)
  422. {
  423. x = positiveFraction(x);
  424. if( x > 0.5f )
  425. {
  426. x = 1.0f - x;
  427. return -1.0f + 4.0f * x * x;
  428. }
  429. return -1.0f + 8.0f * x * x;
  430. }
  431. };
  432. static freefunc1<float,moog_wave,true> moog_wave_func;
  433. struct exp_wave
  434. {
  435. static inline float process(float x)
  436. {
  437. x = positiveFraction(x);
  438. if( x > 0.5f )
  439. {
  440. x = 1.0f - x;
  441. }
  442. return -1.0f + 8.0f * x * x;
  443. }
  444. };
  445. static freefunc1<float,exp_wave,true> exp_wave_func;
  446. struct exp2_wave
  447. {
  448. static inline float process(float x)
  449. {
  450. x = positiveFraction(x);
  451. if( x > 0.5f )
  452. {
  453. return -1.0f + 8.0f * (1.0f-x) * x;
  454. }
  455. return -1.0f + 8.0f * x * x;
  456. }
  457. };
  458. static freefunc1<float,exp2_wave,true> exp2_wave_func;
  459. struct harmonic_cent
  460. {
  461. static inline float process(float x)
  462. {
  463. return powf(2, x / 1200);
  464. }
  465. };
  466. static freefunc1<float,harmonic_cent,true> harmonic_cent_func;
  467. struct harmonic_semitone
  468. {
  469. static inline float process(float x)
  470. {
  471. return powf(2, x / 12);
  472. }
  473. };
  474. static freefunc1<float,harmonic_semitone,true> harmonic_semitone_func;
  475. ExprFront::ExprFront(const char * expr, int last_func_samples)
  476. {
  477. m_valid = false;
  478. try
  479. {
  480. m_data = new ExprFrontData(last_func_samples);
  481. m_data->m_expression_string = expr;
  482. m_data->m_symbol_table.add_pi();
  483. m_data->m_symbol_table.add_constant("e", F_E);
  484. m_data->m_symbol_table.add_constant("seed", SimpleRandom::generator() & max_float_integer_mask);
  485. m_data->m_symbol_table.add_function("sinew", sin_wave_func);
  486. m_data->m_symbol_table.add_function("squarew", square_wave_func);
  487. m_data->m_symbol_table.add_function("trianglew", triangle_wave_func);
  488. m_data->m_symbol_table.add_function("saww", saw_wave_func);
  489. m_data->m_symbol_table.add_function("moogsaww", moogsaw_wave_func);
  490. m_data->m_symbol_table.add_function("moogw", moog_wave_func);
  491. m_data->m_symbol_table.add_function("expw", exp_wave_func);
  492. m_data->m_symbol_table.add_function("expnw", exp2_wave_func);
  493. m_data->m_symbol_table.add_function("cent", harmonic_cent_func);
  494. m_data->m_symbol_table.add_function("semitone", harmonic_semitone_func);
  495. m_data->m_symbol_table.add_function("rand", simple_rand);
  496. m_data->m_symbol_table.add_function("randv", m_data->m_rand_vec);
  497. m_data->m_symbol_table.add_function("randsv", randsv_func);
  498. m_data->m_symbol_table.add_function("last", m_data->m_last_func);
  499. }
  500. catch(...)
  501. {
  502. WARN_EXPRTK;
  503. }
  504. }
  505. ExprFront::~ExprFront()
  506. {
  507. try
  508. {
  509. delete m_data;
  510. }
  511. catch(...)
  512. {
  513. WARN_EXPRTK;
  514. }
  515. }
  516. bool ExprFront::compile()
  517. {
  518. m_valid = false;
  519. try
  520. {
  521. m_data->m_expression.register_symbol_table(m_data->m_symbol_table);
  522. parser_t::settings_store sstore;
  523. sstore.disable_all_logic_ops();
  524. sstore.disable_all_assignment_ops();
  525. sstore.disable_all_control_structures();
  526. parser_t parser(sstore);
  527. m_valid=parser.compile(m_data->m_expression_string, m_data->m_expression);
  528. }
  529. catch(...)
  530. {
  531. WARN_EXPRTK;
  532. }
  533. return m_valid;
  534. }
  535. float ExprFront::evaluate()
  536. {
  537. try
  538. {
  539. if (!m_valid) return 0;
  540. float res = m_data->m_expression.value();
  541. m_data->m_last_func.setLastSample(res);
  542. return res;
  543. }
  544. catch(...)
  545. {
  546. WARN_EXPRTK;
  547. }
  548. return 0;
  549. }
  550. bool ExprFront::add_variable(const char* name, float& ref)
  551. {
  552. try
  553. {
  554. return m_data->m_symbol_table.add_variable(name, ref);
  555. }
  556. catch(...)
  557. {
  558. WARN_EXPRTK;
  559. }
  560. return false;
  561. }
  562. bool ExprFront::add_constant(const char* name, float ref)
  563. {
  564. try
  565. {
  566. return m_data->m_symbol_table.add_constant(name, ref);
  567. }
  568. catch(...)
  569. {
  570. WARN_EXPRTK;
  571. }
  572. return false;
  573. }
  574. bool ExprFront::add_cyclic_vector(const char* name, const float* data, size_t length, bool interp)
  575. {
  576. try
  577. {
  578. if (interp)
  579. {
  580. WaveValueFunctionInterpolate<float> *wvf = new WaveValueFunctionInterpolate<float>(data, length);
  581. m_data->m_cyclics_interp.push_back(wvf);
  582. return m_data->m_symbol_table.add_function(name, *wvf);
  583. }
  584. else
  585. {
  586. WaveValueFunction<float> *wvf = new WaveValueFunction<float>(data, length);
  587. m_data->m_cyclics.push_back(wvf);
  588. return m_data->m_symbol_table.add_function(name, *wvf);
  589. }
  590. }
  591. catch(...)
  592. {
  593. WARN_EXPRTK;
  594. }
  595. return false;
  596. }
  597. size_t find_occurances(const std::string& haystack, const char* const needle)
  598. {
  599. size_t last_pos = 0;
  600. size_t count = 0;
  601. const size_t len = strlen(needle);
  602. if (len > 0)
  603. {
  604. while (last_pos + len <= haystack.length())
  605. {
  606. last_pos = haystack.find(needle, last_pos);
  607. if (last_pos == std::string::npos)
  608. break;
  609. ++count;
  610. last_pos += len;
  611. }
  612. }
  613. return count;
  614. }
  615. void ExprFront::setIntegrate(const unsigned int* const frameCounter, const unsigned int sample_rate)
  616. {
  617. if (m_data->m_integ_func == NULL)
  618. {
  619. const unsigned int ointeg = find_occurances(m_data->m_expression_string,"integrate");
  620. if ( ointeg > 0 )
  621. {
  622. m_data->m_integ_func = new IntegrateFunction<float>(frameCounter,sample_rate,ointeg);
  623. try
  624. {
  625. m_data->m_symbol_table.add_function("integrate",*m_data->m_integ_func);
  626. }
  627. catch(...)
  628. {
  629. WARN_EXPRTK;
  630. }
  631. }
  632. }
  633. }
  634. ExprSynth::ExprSynth(const WaveSample *gW1, const WaveSample *gW2, const WaveSample *gW3,
  635. ExprFront *exprO1, ExprFront *exprO2,
  636. NotePlayHandle *nph, const sample_rate_t sample_rate,
  637. const FloatModel* pan1, const FloatModel* pan2, float rel_trans):
  638. m_exprO1(exprO1),
  639. m_exprO2(exprO2),
  640. m_W1(gW1),
  641. m_W2(gW2),
  642. m_W3(gW3),
  643. m_nph(nph),
  644. m_sample_rate(sample_rate),
  645. m_pan1(pan1),
  646. m_pan2(pan2),
  647. m_rel_transition(rel_trans)
  648. {
  649. m_note_sample = 0;
  650. m_note_rel_sample = 0;
  651. m_note_rel_sec = 0;
  652. m_note_sample_sec = 0;
  653. m_released = 0;
  654. m_frequency = m_nph->frequency();
  655. m_rel_inc = 1000.0 / (m_sample_rate * m_rel_transition);//rel_transition in ms. compute how much increment in each frame
  656. auto init_expression_step2 = [this](ExprFront * e) {
  657. e->add_cyclic_vector("W1", m_W1->m_samples,m_W1->m_length, m_W1->m_interpolate);
  658. e->add_cyclic_vector("W2", m_W2->m_samples,m_W2->m_length, m_W2->m_interpolate);
  659. e->add_cyclic_vector("W3", m_W3->m_samples,m_W3->m_length, m_W3->m_interpolate);
  660. e->add_variable("t", m_note_sample_sec);
  661. e->add_variable("f", m_frequency);
  662. e->add_variable("rel",m_released);
  663. e->add_variable("trel",m_note_rel_sec);
  664. e->setIntegrate(&m_note_sample,m_sample_rate);
  665. e->compile();
  666. };
  667. init_expression_step2(m_exprO1);
  668. init_expression_step2(m_exprO2);
  669. }
  670. ExprSynth::~ExprSynth()
  671. {
  672. if (m_exprO1)
  673. {
  674. delete m_exprO1;
  675. }
  676. if (m_exprO2)
  677. {
  678. delete m_exprO2;
  679. }
  680. }
  681. void ExprSynth::renderOutput(fpp_t frames, sampleFrame *buf)
  682. {
  683. try
  684. {
  685. bool o1_valid = m_exprO1->isValid();
  686. bool o2_valid = m_exprO2->isValid();
  687. if (!o1_valid && !o2_valid)
  688. {
  689. return;
  690. }
  691. float o1 = 0, o2 = 0;
  692. float pn1 = m_pan1->value() * 0.5;
  693. float pn2 = m_pan2->value() * 0.5;
  694. const float new_freq = m_nph->frequency();
  695. const float freq_inc = (new_freq - m_frequency) / frames;
  696. const bool is_released = m_nph->isReleased();
  697. expression_t *o1_rawExpr = &(m_exprO1->getData()->m_expression);
  698. expression_t *o2_rawExpr = &(m_exprO2->getData()->m_expression);
  699. LastSampleFunction<float> * last_func1 = &m_exprO1->getData()->m_last_func;
  700. LastSampleFunction<float> * last_func2 = &m_exprO2->getData()->m_last_func;
  701. if (is_released && m_note_rel_sample == 0)
  702. {
  703. m_note_rel_sample = m_note_sample;
  704. }
  705. if (o1_valid && o2_valid)
  706. {
  707. for (fpp_t frame = 0; frame < frames ; ++frame)
  708. {
  709. if (is_released && m_released < 1)
  710. {
  711. m_released = fmin(m_released+m_rel_inc, 1);
  712. }
  713. o1 = o1_rawExpr->value();
  714. o2 = o2_rawExpr->value();
  715. last_func1->setLastSample(o1);//put result in the circular buffer for the "last" function.
  716. last_func2->setLastSample(o2);
  717. buf[frame][0] = (-pn1 + 0.5) * o1 + (-pn2 + 0.5) * o2;
  718. buf[frame][1] = ( pn1 + 0.5) * o1 + ( pn2 + 0.5) * o2;
  719. m_note_sample++;
  720. m_note_sample_sec = m_note_sample / (float)m_sample_rate;
  721. if (is_released)
  722. {
  723. m_note_rel_sec = (m_note_sample - m_note_rel_sample) / (float)m_sample_rate;
  724. }
  725. m_frequency += freq_inc;
  726. }
  727. }
  728. else
  729. {
  730. if (o2_valid)
  731. {
  732. o1_rawExpr = o2_rawExpr;
  733. last_func1 = last_func2;
  734. pn1 = pn2;
  735. }
  736. for (fpp_t frame = 0; frame < frames ; ++frame)
  737. {
  738. if (is_released && m_released < 1)
  739. {
  740. m_released = fmin(m_released+m_rel_inc, 1);
  741. }
  742. o1 = o1_rawExpr->value();
  743. last_func1->setLastSample(o1);
  744. buf[frame][0] = (-pn1 + 0.5) * o1;
  745. buf[frame][1] = ( pn1 + 0.5) * o1;
  746. m_note_sample++;
  747. m_note_sample_sec = m_note_sample / (float)m_sample_rate;
  748. if (is_released)
  749. {
  750. m_note_rel_sec = (m_note_sample - m_note_rel_sample) / (float)m_sample_rate;
  751. }
  752. m_frequency += freq_inc;
  753. }
  754. }
  755. m_frequency = new_freq;
  756. }
  757. catch(...)
  758. {
  759. WARN_EXPRTK;
  760. }
  761. }