| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809 |
- /*
- * Copyright (c) 1983-2013 Richard Dobson and Composers Desktop Project Ltd
- * http://people.bath.ac.uk/masrwd
- * http://www.composersdesktop.com
- * This file is part of the CDP System.
- * The CDP System is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * The CDP System is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public
- * License along with the CDP System; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
-
- // Jan 2009 extended random lfo to support gaussian distribution
- //////////////////////////////////////////////////////////////////////
- #include <math.h>
- #include <memory.h>
- #include <time.h>
- #include "wavetable.h"
- #include <assert.h>
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- #ifndef PI
- #define PI (3.141592634)
- #endif
- #ifndef TWOPI
- #define TWOPI (2.0 * PI)
- #endif
- #define LAMBDA (10.0)
- /* rand stuff from Csound*/
- static const long RANDINT_MAX = 2147483647L; // 2^^31 - 1 */
- static const long BIPOLAR = 0x4fffffff;
- static const long RIA = 16807;
- static const long RIM = 0x7fffffffL;
- static const double dv2_31 = 1.0 / 2147483648.0;
- static const long MAXLEN = 0x1000000L;
- static const long PHMASK = 0x0ffffffL;
- static const double FMAXLEN = (double)(0x1000000L);
- // for tpdf - bit of a hack, but seems close enough!
- static const long TPDFSHIFT = 0x18000000;
- // for gauss, trial and error!
- static const long BISHIFT = 0x16000000;
- fastlfo::fastlfo()
- {
- param.freq = 0.0;
- param.modrange = 0.0;
- m_cur_WaveType = LFO_SINE;
- curfreq = param.freq = 0.0;
- curphase = 0.0;
- incr = 0.0;
- tf = 0;
- kicvt = 0;
- offset= 0.0;
- b_sync = false;
- tpdf = false;
- gauss = false;
- /* for csrand */
- csseed = 12345;
- }
- double fastlfo::csrand()
- {
- long rval;
- rval = randint31(csseed);
- csseed = rval;
- return (double) rval / (double) RANDINT_MAX;
- }
- long fastlfo::init(double srate, double normphase, long seedval, unsigned long ksmps)
- {
- long seed;
- m_srate = srate / ksmps;
- twopiovrsr = TWOPI / m_srate;
- m_inv_srate = 1.0 / m_srate;
- set_WaveType(LFO_SINE);
- phs = (long)(MAXLEN * normphase);
- curphase = TWOPI * normphase;
- if(seedval == 0)
- seed = (long) time(NULL);
- else
- seed = seedval;
- rand = randint31(seed);
- rand = randint31(rand);
- num1 = 0;
- // get 2nd randon val for diff calc
- // TODO: check this! why << 1 ?
- rand = randint31(rand);
- //num2 = (double)(rand<<1) * dv2_31;
- num2 = (double)((long)((unsigned)rand<<1)-BIPOLAR) * dv2_31;
- dfdmax = (num2 - num1) / FMAXLEN;
- kicvt = (long)((double)MAXLEN / m_srate);
- lastval = 0.0;
- ksamps = ksmps;
- if(ksamps==0)
- ksamps++;
- kcount = 1;
- b_sync = false;
- curphs = (long)(normphase * MAXLEN);
- return seed;
- }
- // we keep current wavetype; no reset of random values ;
- /* phase ranged 0 -1 */
- void fastlfo::reset(double phase)
- {
- curphase = TWOPI * phase;
- phs = (long)(MAXLEN * phase);
- curphs = phs; // assume phase offset = 0 here?
- lastval = 0.0;
- b_sync = false;
- kcount = 1;
- }
- #define OSC_WRAPPHASE if(curphase >= TWOPI) curphase -= TWOPI; \
- if(curphase < 0.0) curphase += TWOPI
- // input range 0-1
- void fastlfo::sync_phase(double phase,double phaseoffset,double phaseincr)
- {
- if(b_sync) {
- curphase = phase * TWOPI;
- offset = phaseoffset * TWOPI;
- incr = phaseincr * TWOPI;
- phs_incr = (long)(phaseincr*ksamps * MAXLEN);
- phs = (long)(phase * MAXLEN);
- phs &= PHMASK;
- phs_offset = (long)(phaseoffset * MAXLEN);
- curphs = phs + phs_offset;
- curphs &= PHMASK;
- }
- }
- inline double fastlfo::sinetick(void)
- {
- double val;
- double thisphase = curphase + offset;
- while(thisphase >= TWOPI)
- thisphase -= TWOPI;
- if(kcount==ksamps){
- if(!b_sync){
- val = sin(thisphase);
- if(curfreq != param.freq){
- curfreq = param.freq;
- incr = twopiovrsr * param.freq;
- }
- }
- else
- val = sin(thisphase);
- curphase += incr;
- //OSC_WRAPPHASE;
- if(curphase >= TWOPI)
- curphase -= TWOPI;
- if(curphase < 0.0)
- curphase += TWOPI;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- inline double fastlfo::tritick(void)
- {
- double val;
- double thisphase = curphase + offset;
- while(thisphase >= TWOPI)
- thisphase -= TWOPI;
- if(kcount==ksamps){
- if(!b_sync){
- if(curfreq != param.freq){
- curfreq = param.freq;
- incr = twopiovrsr * param.freq;
- }
- }
- if(thisphase <= PI) {
- val = (4.0 * (thisphase * (1.0 / TWOPI) )) - 1.0;
- }
- else {
- val = 3.0 - 4.0 * (thisphase * (1.0 / TWOPI) );
- }
- curphase += incr;
- OSC_WRAPPHASE;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
-
- }
- inline double fastlfo::squaretick(void)
- {
- double val;
- double thisphase = curphase + offset;
- while(thisphase >= TWOPI)
- thisphase -= TWOPI;
- if(kcount==ksamps){
- if(!b_sync){
- if(curfreq != param.freq){
- curfreq = param.freq;
- incr = twopiovrsr * param.freq;
- }
- }
- if(thisphase <= PI)
- val = 1.0;
- else
- val = -1;
- curphase += incr;
- OSC_WRAPPHASE;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
-
- }
- double fastlfo::sawuptick(void)
- {
- double val;
- double thisphase = curphase + offset;
- while(thisphase >= TWOPI)
- thisphase -= TWOPI;
- if(kcount==ksamps){
- if(!b_sync){
- if(curfreq != param.freq){
- curfreq = param.freq;
- incr = twopiovrsr * param.freq;
- }
- }
- val = (2.0 * (thisphase * (1.0 / TWOPI) )) - 1.0;
- curphase += incr;
- OSC_WRAPPHASE;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
-
- }
- inline double fastlfo::sawdowntick(void)
- {
- double val;
- double thisphase = curphase + offset;
- while(thisphase >= TWOPI)
- thisphase -= TWOPI;
- if(kcount==ksamps){
- if(!b_sync){
- if(curfreq != param.freq){
- curfreq = param.freq;
- incr = twopiovrsr * param.freq;
- }
- }
- val = 1.0 - 2.0 * (thisphase * (1.0 / TWOPI) );
- curphase += incr;
- OSC_WRAPPHASE;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- // some redundant code with phs and curphs here, but keep both for now!
- inline double fastlfo::randomtick(void)
- {
- double val;
- double dval = 0.0;
- int i;
- if(kcount==ksamps){
- if(b_sync){
- val = num1 +(double)(curphs * dfdmax);
- phs += phs_incr;
- curphs += phs_incr;
- if(curphs >= MAXLEN){
- long r = randint31(rand);
- if(tpdf){
- long rr = randint31(r);
- long ri;
- unsigned long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
- }
- else if(gauss){
- int nrands = 16;
- long ri = 0;
- long rr[16];
- rr[0] = rand;
- for(i = 1; i < nrands; i++)
- rr[i] = randint31(rr[i-1]);
- for(i=0;i < nrands;i++)
- ri += rr[i]>>4;
- r = ri - BISHIFT;
- }
- else if(biexp){
- long rr = r;
- double dbiexp = 0.0;
-
- dbiexp = 2.0 * (double)rr * dv2_31;
- while(dbiexp==0.0 || dbiexp == 2.0){
- rr = randint31(rr);
- }
- if(dbiexp > 1.0)
- dval = -log(2.0-dbiexp) / LAMBDA;
- if(dbiexp < 1.0)
- dval = log(dbiexp) / LAMBDA;
- if( dval > 0.9)
- dval = 0.9;
- if(dval < -0.9)
- dval = -0.9;
- r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
- }
- curphs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
- dfdmax = (num2-num1) / FMAXLEN;
- }
- if(phs >= MAXLEN)
- phs &= PHMASK;
- }
- else{
- val = num1 +(double)(phs * dfdmax);
- phs +=(long) (param.freq * kicvt);
- if(phs >= MAXLEN){
- long r = randint31(rand);
- assert(r>0);
- if(tpdf){
- long rr = randint31(r);
- long ri;
- unsigned long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
- }
- else if(gauss){
- int nrands = 16;
- long ri = 0;
- long rr[16];
- rr[0] = rand;
- for(i = 1;i < nrands;i++)
- rr[i]= randint31(rr[i-1]);
- for(i=0;i < nrands;i++)
- ri += rr[i]>>4;
- r = ri - BISHIFT;
- }
- else if(biexp){
- long rr = r;
- double dbiexp = 0.0;
-
- dbiexp = 2.0 * (double)rr * dv2_31;
- while(dbiexp==0.0 || dbiexp == 2.0){
- rr = randint31(rr);
- }
- if(dbiexp > 1.0)
- dval = -log(2.0-dbiexp) / LAMBDA;
- if(dbiexp < 1.0)
- dval = log(dbiexp) / LAMBDA;
- if( dval > 0.9)
- dval = 0.9;
- if(dval < -0.9)
- dval = -0.9;
- r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
- /* assert(r>0);
- assert(r < RANDINT_MAX);
- */
- }
- phs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
- /* NB makes uniform rand not really uniform, more a sort of rounded distr
- * so maybe not ideal for strict plain uniform distr - still while though!
- */
- dfdmax = (num2-num1) / FMAXLEN;
- }
- }
- if(gauss)
- val *= 1.85;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- inline double fastlfo::rand_tpdf_tick(void)
- {
- double val;
- if(kcount==ksamps){
- if(b_sync){
- val = num1 +(double)(curphs * dfdmax);
- phs += phs_incr;
- curphs += phs_incr;
- if(curphs >= MAXLEN){
- long r = randint31(rand);
-
- long rr = randint31(r);
- long ri;
- long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
-
- curphs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
- dfdmax = (num2-num1) / FMAXLEN;
- }
- if(phs >= MAXLEN)
- phs &= PHMASK;
- }
- else{
- val = num1 +(double)(phs * dfdmax);
- /* need this test for audiorate noise; but is it the right test for everywhere? */
- /* time-varying noise rate? */
- if(ksamps > 1)
- phs +=(long) (param.freq * kicvt + 0.5);
- else
- phs = MAXLEN;
- if(phs >= MAXLEN){
- long r = randint31(rand);
- long rr = randint31(r);
- long rsum;
- #ifdef _DEBUG
- double d1,d2,dtpdf;
- d1 = ((2.0 *(double)r) / (double) RANDINT_MAX) - 1.0;
- d2 = ((2.0 *(double)rr) / (double) RANDINT_MAX) - 1.0;
- dtpdf = d1 + d2;
- dtpdf *= 0.5;
- #endif
- rand = rr;
- /* bipolar value = 2*r - 1 or 2* (r - 0.5)*/
- long bipolard2 = 0x40000000;
- r = (r - bipolard2);
- rr = (rr- bipolard2);
- rsum = r + rr;
- double dsum = (double)rsum / (double) RANDINT_MAX ;
- phs &= PHMASK;
- num1 = num2;
- num2 = dsum;
- dfdmax = (num2-num1) / FMAXLEN;
- }
- }
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- inline double fastlfo::rand_gauss_tick(void)
- {
- double val;
- int i;
- if(kcount==ksamps){
- if(b_sync){
- val = num1 +(double)(curphs * dfdmax);
- phs += phs_incr;
- curphs += phs_incr;
- if(curphs >= MAXLEN){
- long r = randint31(rand);
- int nrands = 16;
- long ri = 0;
- long rr[16];
- rr[0] = rand;
- for(i = 1; i < nrands; i++)
- rr[i] = randint31(rr[i-1]);
- for(i=0;i < nrands;i++)
- ri += rr[i]>>4;
- r = ri - BISHIFT;
-
- curphs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
- dfdmax = (num2-num1) / FMAXLEN;
- }
- if(phs >= MAXLEN)
- phs &= PHMASK;
- }
- else{
- val = num1 +(double)(phs * dfdmax);
- if(ksamps > 1)
- phs +=(long) (param.freq * kicvt + 0.5);
- else
- phs = MAXLEN;
-
- if(phs >= MAXLEN){
- long r = randint31(rand);
- int nrands = 16;
- long ri = 0;
- long rr[16];
- rr[0] = rand;
- for(i = 1;i < nrands;i++)
- rr[i]= randint31(rr[i-1]);
- for(i=0;i < nrands;i++)
- ri += rr[i]>>4;
- r = ri - BISHIFT;
-
- phs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
- dfdmax = (num2-num1) / FMAXLEN;
- }
- }
-
- val *= 1.85;
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- inline double fastlfo::rand_exp_tick(void)
- {
- double val;
- double dval = 0.0;
- if(kcount==ksamps){
- if(b_sync){
- val = num1 +(double)(curphs * dfdmax);
- phs += phs_incr;
- curphs += phs_incr;
- if(curphs >= MAXLEN){
- long r = randint31(rand);
- long rr = r;
- double dbiexp = 0.0;
-
- dbiexp = 2.0 * (double)rr * dv2_31;
- while(dbiexp==0.0 || dbiexp == 2.0){
- rr = randint31(rr);
- }
- if(dbiexp > 1.0)
- dval = -log(2.0-dbiexp) / LAMBDA;
- if(dbiexp < 1.0)
- dval = log(dbiexp) / LAMBDA;
- if( dval > 0.9)
- dval = 0.9;
- if(dval < -0.9)
- dval = -0.9;
- r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
-
- curphs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
- dfdmax = (num2-num1) / FMAXLEN;
- }
- if(phs >= MAXLEN)
- phs &= PHMASK;
- }
- else{
- val = num1 +(double)(phs * dfdmax);
- if(ksamps > 1)
- phs +=(long) (param.freq * kicvt + 0.5);
- else
- phs = MAXLEN;
- if(phs >= MAXLEN){
- long r = randint31(rand);
- long rr = r;
- double dbiexp = 0.0;
-
- dbiexp = 2.0 * (double)rr * dv2_31;
- while(dbiexp==0.0 || dbiexp == 2.0){
- rr = randint31(rr);
- }
- if(dbiexp > 1.0)
- dval = -log(2.0-dbiexp) / LAMBDA;
- if(dbiexp < 1.0)
- dval = log(dbiexp) / LAMBDA;
- if( dval > 0.9)
- dval = 0.9;
- if(dval < -0.9)
- dval = -0.9;
- r = ((unsigned long)(dval/dv2_31) +BIPOLAR)>>1;
-
- phs &= PHMASK;
- rand = r;
- num1 = num2;
- num2 = (double)((long)((unsigned)r<<1)-BIPOLAR) * dv2_31;
-
- dfdmax = (num2-num1) / FMAXLEN;
- }
- }
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- inline double fastlfo::randhtick(void)
- {
- double val;
-
- if(kcount==ksamps){
- if(b_sync){
- val = num1;
- phs += phs_incr;
- curphs += phs_incr;
- if(curphs >= MAXLEN){
- long r = randint31(rand);
- if(tpdf){
- long rr = randint31(r);
- long ri;
- unsigned long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
- }
- rand = r;
- num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
- curphs &= PHMASK;
- }
- if(phs >= MAXLEN)
- phs &= PHMASK;
- }
- else {
- val = num1;
- if(ksamps > 1)
- phs +=(long) (param.freq * kicvt + 0.5);
- else
- phs = MAXLEN;
-
- if(phs >= MAXLEN){
- long r = randint31(rand);
- if(tpdf){
- long rr = randint31(r);
- long ri;
- unsigned long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
- }
- phs &= PHMASK;
- rand = r;
- num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
- }
- }
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- inline double fastlfo::randh_tpdf_tick(void)
- {
- double val;
-
- if(kcount==ksamps){
- if(b_sync){
- val = num1;
- phs += phs_incr;
- curphs += phs_incr;
- if(curphs >= MAXLEN){
- long r = randint31(rand);
-
- long rr = randint31(r);
- long ri;
- unsigned long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
-
- rand = r;
- num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
- curphs &= PHMASK;
- }
- if(phs >= MAXLEN)
- phs &= PHMASK;
- }
- else {
- val = num1;
- if(ksamps > 1)
- phs +=(long) (param.freq * kicvt + 0.5);
- else
- phs = MAXLEN;
- if(phs >= MAXLEN){
- long r = randint31(rand);
-
- long rr = randint31(r);
- long ri;
- unsigned long sum = r + rr;
- ri = (long)(sum/2);
- r = ri - TPDFSHIFT;
-
- phs &= PHMASK;
- rand = r;
- num1 = (double)((long)((unsigned)r<<1) - BIPOLAR) * dv2_31;
- }
- }
- lastval = val * param.modrange;
- kcount = 1;
- }
- else
- kcount++;
- return lastval;
- }
- /* return positive value between 0 and RANDINT_MAX */
- inline long fastlfo::randint31(long seed31)
- {
- unsigned long rilo, rihi;
- rilo = RIA * (long)(seed31 & 0xFFFF);
- rihi = RIA * (long)((unsigned long)seed31 >> 16);
- rilo += (rihi & 0x7FFF) << 16;
- if (rilo > (unsigned long) RIM) {
- rilo &= RIM;
- ++rilo;
- }
- rilo += rihi >> 15;
- if (rilo > (unsigned long) RIM) {
- rilo &= RIM;
- ++rilo;
- }
- return (long)rilo;
- }
- bool fastlfo::set_WaveType(LfoWaveType type)
- {
- switch(type){
- case(LFO_SINE):
- tf = &fastlfo::sinetick;
- break;
- case(LFO_TRIANGLE):
- tf = &fastlfo::tritick;
- break;
- case(LFO_SQUARE):
- tf = &fastlfo::squaretick;
- break;
- case(LFO_SAWUP):
- tf = &fastlfo::sawuptick;
- break;
- case(LFO_SAWDOWN):
- tf = &fastlfo::sawdowntick;
- break;
- case(LFO_RANDOM):
- tf = &fastlfo::randomtick;
- break;
- case(LFO_RAND_TPDF):
- tf = &fastlfo::rand_tpdf_tick;
- break;
- case(LFO_RAND_GAUSS):
- tf = &fastlfo::rand_gauss_tick;
- break;
- case(LFO_RAND_EXP):
- tf = &fastlfo::rand_exp_tick;
- break;
- case(LFO_RND_SH):
- tf = &fastlfo::randhtick;
- break;
- case(LFO_RANDH_TPDF):
- tf = &fastlfo::randh_tpdf_tick;
- break;
- default:
- return false;
- }
- m_cur_WaveType = type;
- return true;
- }
|