wavetable.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. * Copyright (c) 1983-2013 Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. * This file is part of the CDP System.
  6. * The CDP System is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * The CDP System is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU Lesser General Public License for more details.
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with the CDP System; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. // Jan 2009 extended random lfo to support gaussian distribution
  21. //////////////////////////////////////////////////////////////////////
  22. #include <math.h>
  23. #include <memory.h>
  24. #include <time.h>
  25. #include "wavetable.h"
  26. #include <assert.h>
  27. //////////////////////////////////////////////////////////////////////
  28. // Construction/Destruction
  29. //////////////////////////////////////////////////////////////////////
  30. #ifndef PI
  31. #define PI (3.141592634)
  32. #endif
  33. #ifndef TWOPI
  34. #define TWOPI (2.0 * PI)
  35. #endif
  36. #define LAMBDA (10.0)
  37. /* rand stuff from Csound*/
  38. static const long RANDINT_MAX = 2147483647L; // 2^^31 - 1 */
  39. static const long BIPOLAR = 0x4fffffff;
  40. static const long RIA = 16807;
  41. static const long RIM = 0x7fffffffL;
  42. static const double dv2_31 = 1.0 / 2147483648.0;
  43. static const long MAXLEN = 0x1000000L;
  44. static const long PHMASK = 0x0ffffffL;
  45. static const double FMAXLEN = (double)(0x1000000L);
  46. // for tpdf - bit of a hack, but seems close enough!
  47. static const long TPDFSHIFT = 0x18000000;
  48. // for gauss, trial and error!
  49. static const long BISHIFT = 0x16000000;
  50. fastlfo::fastlfo()
  51. {
  52. param.freq = 0.0;
  53. param.modrange = 0.0;
  54. m_cur_WaveType = LFO_SINE;
  55. curfreq = param.freq = 0.0;
  56. curphase = 0.0;
  57. incr = 0.0;
  58. tf = 0;
  59. kicvt = 0;
  60. offset= 0.0;
  61. b_sync = false;
  62. tpdf = false;
  63. gauss = false;
  64. /* for csrand */
  65. csseed = 12345;
  66. }
  67. double fastlfo::csrand()
  68. {
  69. long rval;
  70. rval = randint31(csseed);
  71. csseed = rval;
  72. return (double) rval / (double) RANDINT_MAX;
  73. }
  74. long fastlfo::init(double srate, double normphase, long seedval, unsigned long ksmps)
  75. {
  76. long seed;
  77. m_srate = srate / ksmps;
  78. twopiovrsr = TWOPI / m_srate;
  79. m_inv_srate = 1.0 / m_srate;
  80. set_WaveType(LFO_SINE);
  81. phs = (long)(MAXLEN * normphase);
  82. curphase = TWOPI * normphase;
  83. if(seedval == 0)
  84. seed = (long) time(NULL);
  85. else
  86. seed = seedval;
  87. rand = randint31(seed);
  88. rand = randint31(rand);
  89. num1 = 0;
  90. // get 2nd randon val for diff calc
  91. // TODO: check this! why << 1 ?
  92. rand = randint31(rand);
  93. //num2 = (double)(rand<<1) * dv2_31;
  94. num2 = (double)((long)((unsigned)rand<<1)-BIPOLAR) * dv2_31;
  95. dfdmax = (num2 - num1) / FMAXLEN;
  96. kicvt = (long)((double)MAXLEN / m_srate);
  97. lastval = 0.0;
  98. ksamps = ksmps;
  99. if(ksamps==0)
  100. ksamps++;
  101. kcount = 1;
  102. b_sync = false;
  103. curphs = (long)(normphase * MAXLEN);
  104. return seed;
  105. }
  106. // we keep current wavetype; no reset of random values ;
  107. /* phase ranged 0 -1 */
  108. void fastlfo::reset(double phase)
  109. {
  110. curphase = TWOPI * phase;
  111. phs = (long)(MAXLEN * phase);
  112. curphs = phs; // assume phase offset = 0 here?
  113. lastval = 0.0;
  114. b_sync = false;
  115. kcount = 1;
  116. }
  117. #define OSC_WRAPPHASE if(curphase >= TWOPI) curphase -= TWOPI; \
  118. if(curphase < 0.0) curphase += TWOPI
  119. // input range 0-1
  120. void fastlfo::sync_phase(double phase,double phaseoffset,double phaseincr)
  121. {
  122. if(b_sync) {
  123. curphase = phase * TWOPI;
  124. offset = phaseoffset * TWOPI;
  125. incr = phaseincr * TWOPI;
  126. phs_incr = (long)(phaseincr*ksamps * MAXLEN);
  127. phs = (long)(phase * MAXLEN);
  128. phs &= PHMASK;
  129. phs_offset = (long)(phaseoffset * MAXLEN);
  130. curphs = phs + phs_offset;
  131. curphs &= PHMASK;
  132. }
  133. }
  134. inline double fastlfo::sinetick(void)
  135. {
  136. double val;
  137. double thisphase = curphase + offset;
  138. while(thisphase >= TWOPI)
  139. thisphase -= TWOPI;
  140. if(kcount==ksamps){
  141. if(!b_sync){
  142. val = sin(thisphase);
  143. if(curfreq != param.freq){
  144. curfreq = param.freq;
  145. incr = twopiovrsr * param.freq;
  146. }
  147. }
  148. else
  149. val = sin(thisphase);
  150. curphase += incr;
  151. //OSC_WRAPPHASE;
  152. if(curphase >= TWOPI)
  153. curphase -= TWOPI;
  154. if(curphase < 0.0)
  155. curphase += TWOPI;
  156. lastval = val * param.modrange;
  157. kcount = 1;
  158. }
  159. else
  160. kcount++;
  161. return lastval;
  162. }
  163. inline double fastlfo::tritick(void)
  164. {
  165. double val;
  166. double thisphase = curphase + offset;
  167. while(thisphase >= TWOPI)
  168. thisphase -= TWOPI;
  169. if(kcount==ksamps){
  170. if(!b_sync){
  171. if(curfreq != param.freq){
  172. curfreq = param.freq;
  173. incr = twopiovrsr * param.freq;
  174. }
  175. }
  176. if(thisphase <= PI) {
  177. val = (4.0 * (thisphase * (1.0 / TWOPI) )) - 1.0;
  178. }
  179. else {
  180. val = 3.0 - 4.0 * (thisphase * (1.0 / TWOPI) );
  181. }
  182. curphase += incr;
  183. OSC_WRAPPHASE;
  184. lastval = val * param.modrange;
  185. kcount = 1;
  186. }
  187. else
  188. kcount++;
  189. return lastval;
  190. }
  191. inline double fastlfo::squaretick(void)
  192. {
  193. double val;
  194. double thisphase = curphase + offset;
  195. while(thisphase >= TWOPI)
  196. thisphase -= TWOPI;
  197. if(kcount==ksamps){
  198. if(!b_sync){
  199. if(curfreq != param.freq){
  200. curfreq = param.freq;
  201. incr = twopiovrsr * param.freq;
  202. }
  203. }
  204. if(thisphase <= PI)
  205. val = 1.0;
  206. else
  207. val = -1;
  208. curphase += incr;
  209. OSC_WRAPPHASE;
  210. lastval = val * param.modrange;
  211. kcount = 1;
  212. }
  213. else
  214. kcount++;
  215. return lastval;
  216. }
  217. double fastlfo::sawuptick(void)
  218. {
  219. double val;
  220. double thisphase = curphase + offset;
  221. while(thisphase >= TWOPI)
  222. thisphase -= TWOPI;
  223. if(kcount==ksamps){
  224. if(!b_sync){
  225. if(curfreq != param.freq){
  226. curfreq = param.freq;
  227. incr = twopiovrsr * param.freq;
  228. }
  229. }
  230. val = (2.0 * (thisphase * (1.0 / TWOPI) )) - 1.0;
  231. curphase += incr;
  232. OSC_WRAPPHASE;
  233. lastval = val * param.modrange;
  234. kcount = 1;
  235. }
  236. else
  237. kcount++;
  238. return lastval;
  239. }
  240. inline double fastlfo::sawdowntick(void)
  241. {
  242. double val;
  243. double thisphase = curphase + offset;
  244. while(thisphase >= TWOPI)
  245. thisphase -= TWOPI;
  246. if(kcount==ksamps){
  247. if(!b_sync){
  248. if(curfreq != param.freq){
  249. curfreq = param.freq;
  250. incr = twopiovrsr * param.freq;
  251. }
  252. }
  253. val = 1.0 - 2.0 * (thisphase * (1.0 / TWOPI) );
  254. curphase += incr;
  255. OSC_WRAPPHASE;
  256. lastval = val * param.modrange;
  257. kcount = 1;
  258. }
  259. else
  260. kcount++;
  261. return lastval;
  262. }
  263. // some redundant code with phs and curphs here, but keep both for now!
  264. inline double fastlfo::randomtick(void)
  265. {
  266. double val;
  267. double dval = 0.0;
  268. int i;
  269. if(kcount==ksamps){
  270. if(b_sync){
  271. val = num1 +(double)(curphs * dfdmax);
  272. phs += phs_incr;
  273. curphs += phs_incr;
  274. if(curphs >= MAXLEN){
  275. long r = randint31(rand);
  276. if(tpdf){
  277. long rr = randint31(r);
  278. long ri;
  279. unsigned long sum = r + rr;
  280. ri = (long)(sum/2);
  281. r = ri - TPDFSHIFT;
  282. }
  283. else if(gauss){
  284. int nrands = 16;
  285. long ri = 0;
  286. long rr[16];
  287. rr[0] = rand;
  288. for(i = 1; i < nrands; i++)
  289. rr[i] = randint31(rr[i-1]);
  290. for(i=0;i < nrands;i++)
  291. ri += rr[i]>>4;
  292. r = ri - BISHIFT;
  293. }
  294. else if(biexp){
  295. long rr = r;
  296. double dbiexp = 0.0;
  297. dbiexp = 2.0 * (double)rr * dv2_31;
  298. while(dbiexp==0.0 || dbiexp == 2.0){
  299. rr = randint31(rr);
  300. }
  301. if(dbiexp > 1.0)
  302. dval = -log(2.0-dbiexp) / LAMBDA;
  303. if(dbiexp < 1.0)
  304. dval = log(dbiexp) / LAMBDA;
  305. if( dval > 0.9)
  306. dval = 0.9;
  307. if(dval < -0.9)
  308. dval = -0.9;
  309. r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
  310. }
  311. curphs &= PHMASK;
  312. rand = r;
  313. num1 = num2;
  314. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  315. dfdmax = (num2-num1) / FMAXLEN;
  316. }
  317. if(phs >= MAXLEN)
  318. phs &= PHMASK;
  319. }
  320. else{
  321. val = num1 +(double)(phs * dfdmax);
  322. phs +=(long) (param.freq * kicvt);
  323. if(phs >= MAXLEN){
  324. long r = randint31(rand);
  325. assert(r>0);
  326. if(tpdf){
  327. long rr = randint31(r);
  328. long ri;
  329. unsigned long sum = r + rr;
  330. ri = (long)(sum/2);
  331. r = ri - TPDFSHIFT;
  332. }
  333. else if(gauss){
  334. int nrands = 16;
  335. long ri = 0;
  336. long rr[16];
  337. rr[0] = rand;
  338. for(i = 1;i < nrands;i++)
  339. rr[i]= randint31(rr[i-1]);
  340. for(i=0;i < nrands;i++)
  341. ri += rr[i]>>4;
  342. r = ri - BISHIFT;
  343. }
  344. else if(biexp){
  345. long rr = r;
  346. double dbiexp = 0.0;
  347. dbiexp = 2.0 * (double)rr * dv2_31;
  348. while(dbiexp==0.0 || dbiexp == 2.0){
  349. rr = randint31(rr);
  350. }
  351. if(dbiexp > 1.0)
  352. dval = -log(2.0-dbiexp) / LAMBDA;
  353. if(dbiexp < 1.0)
  354. dval = log(dbiexp) / LAMBDA;
  355. if( dval > 0.9)
  356. dval = 0.9;
  357. if(dval < -0.9)
  358. dval = -0.9;
  359. r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
  360. /* assert(r>0);
  361. assert(r < RANDINT_MAX);
  362. */
  363. }
  364. phs &= PHMASK;
  365. rand = r;
  366. num1 = num2;
  367. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  368. /* NB makes uniform rand not really uniform, more a sort of rounded distr
  369. * so maybe not ideal for strict plain uniform distr - still while though!
  370. */
  371. dfdmax = (num2-num1) / FMAXLEN;
  372. }
  373. }
  374. if(gauss)
  375. val *= 1.85;
  376. lastval = val * param.modrange;
  377. kcount = 1;
  378. }
  379. else
  380. kcount++;
  381. return lastval;
  382. }
  383. inline double fastlfo::rand_tpdf_tick(void)
  384. {
  385. double val;
  386. if(kcount==ksamps){
  387. if(b_sync){
  388. val = num1 +(double)(curphs * dfdmax);
  389. phs += phs_incr;
  390. curphs += phs_incr;
  391. if(curphs >= MAXLEN){
  392. long r = randint31(rand);
  393. long rr = randint31(r);
  394. long ri;
  395. long sum = r + rr;
  396. ri = (long)(sum/2);
  397. r = ri - TPDFSHIFT;
  398. curphs &= PHMASK;
  399. rand = r;
  400. num1 = num2;
  401. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  402. dfdmax = (num2-num1) / FMAXLEN;
  403. }
  404. if(phs >= MAXLEN)
  405. phs &= PHMASK;
  406. }
  407. else{
  408. val = num1 +(double)(phs * dfdmax);
  409. /* need this test for audiorate noise; but is it the right test for everywhere? */
  410. /* time-varying noise rate? */
  411. if(ksamps > 1)
  412. phs +=(long) (param.freq * kicvt + 0.5);
  413. else
  414. phs = MAXLEN;
  415. if(phs >= MAXLEN){
  416. long r = randint31(rand);
  417. long rr = randint31(r);
  418. long rsum;
  419. #ifdef _DEBUG
  420. double d1,d2,dtpdf;
  421. d1 = ((2.0 *(double)r) / (double) RANDINT_MAX) - 1.0;
  422. d2 = ((2.0 *(double)rr) / (double) RANDINT_MAX) - 1.0;
  423. dtpdf = d1 + d2;
  424. dtpdf *= 0.5;
  425. #endif
  426. rand = rr;
  427. /* bipolar value = 2*r - 1 or 2* (r - 0.5)*/
  428. long bipolard2 = 0x40000000;
  429. r = (r - bipolard2);
  430. rr = (rr- bipolard2);
  431. rsum = r + rr;
  432. double dsum = (double)rsum / (double) RANDINT_MAX ;
  433. phs &= PHMASK;
  434. num1 = num2;
  435. num2 = dsum;
  436. dfdmax = (num2-num1) / FMAXLEN;
  437. }
  438. }
  439. lastval = val * param.modrange;
  440. kcount = 1;
  441. }
  442. else
  443. kcount++;
  444. return lastval;
  445. }
  446. inline double fastlfo::rand_gauss_tick(void)
  447. {
  448. double val;
  449. int i;
  450. if(kcount==ksamps){
  451. if(b_sync){
  452. val = num1 +(double)(curphs * dfdmax);
  453. phs += phs_incr;
  454. curphs += phs_incr;
  455. if(curphs >= MAXLEN){
  456. long r = randint31(rand);
  457. int nrands = 16;
  458. long ri = 0;
  459. long rr[16];
  460. rr[0] = rand;
  461. for(i = 1; i < nrands; i++)
  462. rr[i] = randint31(rr[i-1]);
  463. for(i=0;i < nrands;i++)
  464. ri += rr[i]>>4;
  465. r = ri - BISHIFT;
  466. curphs &= PHMASK;
  467. rand = r;
  468. num1 = num2;
  469. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  470. dfdmax = (num2-num1) / FMAXLEN;
  471. }
  472. if(phs >= MAXLEN)
  473. phs &= PHMASK;
  474. }
  475. else{
  476. val = num1 +(double)(phs * dfdmax);
  477. if(ksamps > 1)
  478. phs +=(long) (param.freq * kicvt + 0.5);
  479. else
  480. phs = MAXLEN;
  481. if(phs >= MAXLEN){
  482. long r = randint31(rand);
  483. int nrands = 16;
  484. long ri = 0;
  485. long rr[16];
  486. rr[0] = rand;
  487. for(i = 1;i < nrands;i++)
  488. rr[i]= randint31(rr[i-1]);
  489. for(i=0;i < nrands;i++)
  490. ri += rr[i]>>4;
  491. r = ri - BISHIFT;
  492. phs &= PHMASK;
  493. rand = r;
  494. num1 = num2;
  495. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  496. dfdmax = (num2-num1) / FMAXLEN;
  497. }
  498. }
  499. val *= 1.85;
  500. lastval = val * param.modrange;
  501. kcount = 1;
  502. }
  503. else
  504. kcount++;
  505. return lastval;
  506. }
  507. inline double fastlfo::rand_exp_tick(void)
  508. {
  509. double val;
  510. double dval = 0.0;
  511. if(kcount==ksamps){
  512. if(b_sync){
  513. val = num1 +(double)(curphs * dfdmax);
  514. phs += phs_incr;
  515. curphs += phs_incr;
  516. if(curphs >= MAXLEN){
  517. long r = randint31(rand);
  518. long rr = r;
  519. double dbiexp = 0.0;
  520. dbiexp = 2.0 * (double)rr * dv2_31;
  521. while(dbiexp==0.0 || dbiexp == 2.0){
  522. rr = randint31(rr);
  523. }
  524. if(dbiexp > 1.0)
  525. dval = -log(2.0-dbiexp) / LAMBDA;
  526. if(dbiexp < 1.0)
  527. dval = log(dbiexp) / LAMBDA;
  528. if( dval > 0.9)
  529. dval = 0.9;
  530. if(dval < -0.9)
  531. dval = -0.9;
  532. r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
  533. curphs &= PHMASK;
  534. rand = r;
  535. num1 = num2;
  536. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  537. dfdmax = (num2-num1) / FMAXLEN;
  538. }
  539. if(phs >= MAXLEN)
  540. phs &= PHMASK;
  541. }
  542. else{
  543. val = num1 +(double)(phs * dfdmax);
  544. if(ksamps > 1)
  545. phs +=(long) (param.freq * kicvt + 0.5);
  546. else
  547. phs = MAXLEN;
  548. if(phs >= MAXLEN){
  549. long r = randint31(rand);
  550. long rr = r;
  551. double dbiexp = 0.0;
  552. dbiexp = 2.0 * (double)rr * dv2_31;
  553. while(dbiexp==0.0 || dbiexp == 2.0){
  554. rr = randint31(rr);
  555. }
  556. if(dbiexp > 1.0)
  557. dval = -log(2.0-dbiexp) / LAMBDA;
  558. if(dbiexp < 1.0)
  559. dval = log(dbiexp) / LAMBDA;
  560. if( dval > 0.9)
  561. dval = 0.9;
  562. if(dval < -0.9)
  563. dval = -0.9;
  564. r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
  565. phs &= PHMASK;
  566. rand = r;
  567. num1 = num2;
  568. num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
  569. dfdmax = (num2-num1) / FMAXLEN;
  570. }
  571. }
  572. lastval = val * param.modrange;
  573. kcount = 1;
  574. }
  575. else
  576. kcount++;
  577. return lastval;
  578. }
  579. inline double fastlfo::randhtick(void)
  580. {
  581. double val;
  582. if(kcount==ksamps){
  583. if(b_sync){
  584. val = num1;
  585. phs += phs_incr;
  586. curphs += phs_incr;
  587. if(curphs >= MAXLEN){
  588. long r = randint31(rand);
  589. if(tpdf){
  590. long rr = randint31(r);
  591. long ri;
  592. unsigned long sum = r + rr;
  593. ri = (long)(sum/2);
  594. r = ri - TPDFSHIFT;
  595. }
  596. rand = r;
  597. num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
  598. curphs &= PHMASK;
  599. }
  600. if(phs >= MAXLEN)
  601. phs &= PHMASK;
  602. }
  603. else {
  604. val = num1;
  605. if(ksamps > 1)
  606. phs +=(long) (param.freq * kicvt + 0.5);
  607. else
  608. phs = MAXLEN;
  609. if(phs >= MAXLEN){
  610. long r = randint31(rand);
  611. if(tpdf){
  612. long rr = randint31(r);
  613. long ri;
  614. unsigned long sum = r + rr;
  615. ri = (long)(sum/2);
  616. r = ri - TPDFSHIFT;
  617. }
  618. phs &= PHMASK;
  619. rand = r;
  620. num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
  621. }
  622. }
  623. lastval = val * param.modrange;
  624. kcount = 1;
  625. }
  626. else
  627. kcount++;
  628. return lastval;
  629. }
  630. inline double fastlfo::randh_tpdf_tick(void)
  631. {
  632. double val;
  633. if(kcount==ksamps){
  634. if(b_sync){
  635. val = num1;
  636. phs += phs_incr;
  637. curphs += phs_incr;
  638. if(curphs >= MAXLEN){
  639. long r = randint31(rand);
  640. long rr = randint31(r);
  641. long ri;
  642. unsigned long sum = r + rr;
  643. ri = (long)(sum/2);
  644. r = ri - TPDFSHIFT;
  645. rand = r;
  646. num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
  647. curphs &= PHMASK;
  648. }
  649. if(phs >= MAXLEN)
  650. phs &= PHMASK;
  651. }
  652. else {
  653. val = num1;
  654. if(ksamps > 1)
  655. phs +=(long) (param.freq * kicvt + 0.5);
  656. else
  657. phs = MAXLEN;
  658. if(phs >= MAXLEN){
  659. long r = randint31(rand);
  660. long rr = randint31(r);
  661. long ri;
  662. unsigned long sum = r + rr;
  663. ri = (long)(sum/2);
  664. r = ri - TPDFSHIFT;
  665. phs &= PHMASK;
  666. rand = r;
  667. num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
  668. }
  669. }
  670. lastval = val * param.modrange;
  671. kcount = 1;
  672. }
  673. else
  674. kcount++;
  675. return lastval;
  676. }
  677. /* return positive value between 0 and RANDINT_MAX */
  678. inline long fastlfo::randint31(long seed31)
  679. {
  680. unsigned long rilo, rihi;
  681. rilo = RIA * (long)(seed31 & 0xFFFF);
  682. rihi = RIA * (long)((unsigned long)seed31 >> 16);
  683. rilo += (rihi & 0x7FFF) << 16;
  684. if (rilo > (unsigned long) RIM) {
  685. rilo &= RIM;
  686. ++rilo;
  687. }
  688. rilo += rihi >> 15;
  689. if (rilo > (unsigned long) RIM) {
  690. rilo &= RIM;
  691. ++rilo;
  692. }
  693. return (long)rilo;
  694. }
  695. bool fastlfo::set_WaveType(LfoWaveType type)
  696. {
  697. switch(type){
  698. case(LFO_SINE):
  699. tf = &fastlfo::sinetick;
  700. break;
  701. case(LFO_TRIANGLE):
  702. tf = &fastlfo::tritick;
  703. break;
  704. case(LFO_SQUARE):
  705. tf = &fastlfo::squaretick;
  706. break;
  707. case(LFO_SAWUP):
  708. tf = &fastlfo::sawuptick;
  709. break;
  710. case(LFO_SAWDOWN):
  711. tf = &fastlfo::sawdowntick;
  712. break;
  713. case(LFO_RANDOM):
  714. tf = &fastlfo::randomtick;
  715. break;
  716. case(LFO_RAND_TPDF):
  717. tf = &fastlfo::rand_tpdf_tick;
  718. break;
  719. case(LFO_RAND_GAUSS):
  720. tf = &fastlfo::rand_gauss_tick;
  721. break;
  722. case(LFO_RAND_EXP):
  723. tf = &fastlfo::rand_exp_tick;
  724. break;
  725. case(LFO_RND_SH):
  726. tf = &fastlfo::randhtick;
  727. break;
  728. case(LFO_RANDH_TPDF):
  729. tf = &fastlfo::randh_tpdf_tick;
  730. break;
  731. default:
  732. return false;
  733. }
  734. m_cur_WaveType = type;
  735. return true;
  736. }