AA of of Oscillators and Distortions with Pre Integrated Integrated WaveTables WaveTables
T.Rochebois
Anti Aliasing Aliasing Oscillators and Distortions with Pre Integrated Wave Tables rev 0.4 14 september 2016 by T.Rochebois T.Rochebois* rev0.1: (2 sept 2016) initial release rev0.2: (3 sept 2016) added eample code : !nti !liased 3 "perator #arabola #hase $od%lation &ynth rev0.3: (12 sept 2016) added eample code: 'alsh 'alsh synth. Rev0.4:(14 sept 2016) added eample code:T%bey distortion.
1 Notation v() v(1.3) v v+
%nction %nction eval%ation table access to a table (inte,ers only)
$ost short code eamples are sort o -s (pl%,in lan,%a,e embedded in the Reaper !').
*
/m R rom rom the niversit #aris &%d "rsay (rench (rench Thesis abo%t $%ltiple 'avetable 'avetable &ynthesis) &ynthesis). / or5ed ith the anam omp%ters company in the late 70s on many a%dio al,orithms al,orithms incl%din, the "n,a5% pitch trac5er8 the 9d,9 anti aliasin, method (very similar to bleps) and variants o #'. #'. ontact: &mashed.Transistors;,mail.com
1
AA of of Oscillators and Distortions with Pre Integrated Integrated WaveTables WaveTables
T.Rochebois
Contents 1 %encies.................................................. re>%encies..............................................................................4 ............................4 2.3 i,ital "scillators8 !ccessin, the avetable..................... a vetable............................................ .............................................. .................................4 ..........4 2.4 i,ital =ell: 'avetable !liasin,........................................ !liasin,............................................................... ................................................... ............................ ....? 3 The pop%lar c%re: c%re : $/# $!#s..................................................... $!#s............................................................................ .............................................. ................................6 .........6 4 The old tric5: #re inte,ration..................... inte,ration............................................ .............................................. .............................................. ..........................................6 ...................6 4.1 $emories..................... $emories............................................ .............................................. .............................................. ................................................................ ......................................... 6 4.2 @ac5 to the problem...................................... problem............................................................. .............................................. ......................................................+ ...............................+ 4.3 "versamplin,................. "versamplin,........................................ .............................................. .............................................. .............................................. .......................................+ ................+ 4.4 !rea........................................... !rea.................................................................. .............................................. .............................................. ...................................................+ ............................+ 4.? /nte,ral............................................. /nte,ral.................................................................... .............................................. .............................................. ............................................ .....................A A 4.6 #re inte,ration.................... inte,ration........................................... .............................................. .............................................. .............................................. ...................................A ............A 4.+ ! perect orld B.............................................. B..................................................................... ............................................... ...................................................A ...........................A ? o%sins........................................ o%sins............................................................... .............................................. .............................................. .................................................... ............................. ....7 ?.1 ierentiated #arabolic 'aveorms...................... aveorms............................................. .............................................. .............................................7 ......................7 ?.2 ierentiated #olynomial 'aveorms......................... 'aveorms................................................ ....................................................... ................................ ......7 6 @ac5 to #re inte,rated avetables....................... avetables.............................................. .............................................. .....................................................10 ..............................10 6.1 =i,her order pre inte,rated avetables....................... avetables.............................................. ............................................................10 .....................................10 6.2 !nti !liased istortion...................... istortion............................................. .............................................. ............................................................... ........................................ 10 6.3 !nti !liased #hase $od%lation...................................... $od%lation............................................................. .............................................. ...................................11 ............11 !ppendi ! ommented C&D code c ode eamples........................ eamples............................................... .............................................. ...................................12 ............12 !ppendi !.1 !n !nti !liased 'alsh %nction synth......................................... synth...................................................................12 ..........................12 !ppendi !.2 !n !nti !liased 3 "perator #arabola #hase $od%lation synth..............................24 synth..............................24
2
AA of of Oscillators and Distortions with Pre Integrated Integrated WaveTables WaveTables
T.Rochebois
2 A simple Wavetable Oscillator 2.1 Wave tables !ow to read them ! ave table consists in a table that contains a set o samples that can be looped. The samples represent a cycle o the si,nal. &les means that val%es v o the analo, si,nal have been pic5ed at re,%lar time intervals.
! avetable is very handy8 it allos to access any val%e o the ave at any moment. or eample8 lets say that e ant to 5no hats the val%e o the avetable at position +8 e can tell it directly by accessin, the table. v+
3
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
2.2 Digital Oscillators Phases and "re#$encies !n oscillator oscillates i.e. it cycles at a speciic re>%ency. To do so8 the most convenient ay8 in the di,ital domain8 consists in %sin, a phase accumulator p" that ill remember here e are in the cycle. " p ill be incremented at every tic5 o the main samplin, rate (yo% 5no8 44.15=I or 4AJ=I a5a srate in -s). The rate at hich it ill be incremented dp" controls the frequency o the oscillator. /t consists in a sin,le line o code: p += dp; "%ps8 i or,ot that an oscillator has to cycle8 here p ,oes aay. 'e can i a limit to p and tell him to ,o bac5. Eets say e ant it to stay in the 0 16 ran,e8 e can code: p += dp; p >= 16 ? p -= 16;
2.% Digital Oscillators Accessing the wavetable = 16 ? p -= 16; p0 = floor(p); a = p - floor(p); vp0! + a " dvp0!;
// // // //
phase increment phase cycling integer part of the phase float part of the phase (for linear interpolation)
4
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
2.& Digital !ell' Wavetable Aliasing 9verythin, so%nds ,ood so ar8 b%t hat happens i e read o%r avetable aster B
'e miss some details and ,et some others rom the ori,inal aveorm8 dependin, on the position o the phase acc%m%lator or each iteration.
!nd even orse8 e dont miss or ,et the same details at each cycle o the oscillator. The oscillators o%tp%t is no more cyclic8 there is some aperiodic dirt in the hi,h end o the spectr%m. &ome say it is metallic and cold8 di,ital cold.
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
% The pop$lar c$re' (IP (APs !liasin, occ%rs hen yo% read a avetable ith lots o details too ast. The $/#$!# idea is simple: remove the details hen yo% ant to read ast K /ts hard to remove details on the ly8 so8 the sol%tion consists in preparin, a set o avetables (the $/#$!#). 9ach avetable o the $/#$!# set contains -%st the ri,ht amo%nt o details or a certain ran,e o re>%encies (i.e. $// notes). The main drabac5 is that it %ses lots o memory8 b%t today8 memory is cheap. &o... This is the method i %se or the #'TLsynth. /t is a very eicient method and the most pop%lar antialiasin, method or avetables so ar.
& The old tric)' Pre integration &.1 (emories #re inte,ration is a tric5 / %sed in the 70s. The comp%ters / %sed or my thesis or5 ere not very poer%l and had m%ch less memory than an avera,e laptop o 2016. !s my or5 implied m%ltiple avetables8 / had to ind a c%re to my avetable aliasin, problem. #re inte,ration as the method / opted or.
&.2 *ac) to the problem &o lets ,o bac5 to ho e read avetables. The problem is that e read hat is at a certain point in the avetable. /deally8 e sho%ld read hat is in a certain region o the avetable8 the what happened since last time region.
The what happened since last time region
&.% Oversampling &%re8 b%t ho can e do that B =o can e 5no hat as in the what happened since last time region. 'ell8 e can cheat and ta5e more samples G thats called oversamplin, G b%t it ill cost some #
!
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
and e have to ,%ess ho many oversamples e need.
&.& Area =o can e ,et the mean val%e o the what happened since last time region instead o a ponct%al val%e ta5en in this re,ion B =as ar as i learnt in hi,h school8 the mean val%e is the area %nder the c%rve devided by the idth o the re,ion.
/ts somethin, li5e mean val%e F !Mdp ! is the area. dp is the phase increment (proportional to the re>%ency). =o can e calc%late this area B
&.+ Integral !n area %nder a %nction can be eactly calc%lated by s%bstractin, its inte,rand at the limit points. &ay /v is the inte,rand o v8 e have ! F /v(p) G /v(previo%s p) i.e. ! F /v(p) G /v(p G dp) &o the mean val%e is ( /v(p) G /v(p G dp) ) M dp
&., Pre integration &o8 e need this nice and handy /v() %nction. &orry8 e have to do some maths. The v() %nction G the linearly interpolated samples G is made o line segments. Eets Ioom on a line se,ment8 beteen to samples:
"
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
v(p) F vp0 H a * dvp0N p0 bein, the inte,er part o p and a bein, the loat part. ! %nction /v(p) that has v(p) as its derivative is somethin, li5e: /v(p) F /vp0 H a * vp0 H 1M2 aO2 dvp0 /t is a second order polynomial se,ment. The /v() %nction is made o second order polynomial segments . These polynomial se,ments are deined by the val%es contained in tables v8 dv and the ne table /v. The ne table /v simply contains oset val%es that ens%res contin%ity beteen se,ments. This loo5s li5e a detail b%t it is mandatory: it ens%res that the polynomial se,ments still connects the dots.
&.- A perect world / #re inte,ration allos to calc%late the mean val%e o o%r avetable on any re,ion by s%bstractin, to val%es and dividin, the res%lt by the idth o the re,ion. /t is ar s%perior to the trivial linear interpolation scheme. @%t it is not as ,ood as $/#$!#s. 'hy is that so B &%re8 it ,ives the mean val%e o the what happened since last time region8 b%t that mean value is ar rom a perect anti aliasin, ilter. ! mean val%e provides hats called a bo ilter. The bo ilter re>%ency response is a sinc %nction ( Related:R%nnin, &%m Eopass ilter ). Eets say that it cannot rivalise ith o%rier band limitin, %sed in $/#$!#s. Box filtering is a smoothing filter, Fourier band limiting is a brick wall.
Thats hy #re inte,ration sho%ld not be seen as a perect antialiasin, scheme. To be eicient it can be %sed ith other methods s%ch as oversamplin,. /n this case8 it can be as eective as $/#$!#in, and more importantly it can be etended beyond avetables.
+ Co$sins +.1 Dierentiated Parabolic Waveorms #re inte,ration o avetables have some co%sins: the #'s8 ierenciated #arabolic 'aveorms. ! #' oscillator does not %se ave tables. /t ,enerates a parabolic aveorm based on its phase. This parabola is dierentiated to ,enerate a somehat band limited satooth. "ther aveorms8 s%ch as s>%are and rectan,le are ,enerated by combinin, sateeth.
#
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
#' ere %sed in the late A0s in di,ital synthesiIers.
+.2 Dierentiated Pol0nomial Waveorms /n the late 70s a ,ro%p o my st%dents or5ed at a inal year pro-ect. This inal year pro-ect consisted in codin, a simple band limited oscillator itho%t avetables. The ,enerator sho%ld ecl%sively be based on math %nctions. / s%,,ested the idea o #arabolic 'aveorms and that they may eperiment ith hi,her order #olynomials and hi,her order dierentiators. The res%lts ere >%ite ,ood: lo aliasin, and lo #. The bo ilter implied by the irst order dierentiator as replaced by somethin, li5e the bo ilter o a bo ilter o a bo ilter... hich starts to loo5 li5e a gaussian ilter. The main drabac5 o the method is that it needs hi,h acc%racy. loats ere eno%,h or a third order inte,rationMdierentiation scheme. =i,her order schemes needed do%bles. &ee8 or eample8 this old thread on the JPR or%m: http:MM.5vra%dio.comMor%mMvietopic.phpBpF1+10116Q1+10116
Eater i %sed this method in a P&Ti synth yo% may ind somehere on the net The Pa,abond Jin,. The Pa,abond Jin, P&Ti
, *ac) to Pre integrated wavetables ,.1 !igher order pre integrated wavetables The case o avetables is more sensitive to acc%racy than the ,eneration o sateeth by the #' method. /nstead o a polynomial8 yo% have to deal ith a set o polynomial se,ments and ens%re that calc errors are 5ept lo.
$
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
,.2 Anti Aliased Distortion #re inte,rated tables can be %sed beyond avetables. /n areas not available to $/#$!#in,. "ne s%ch application is distortion. ! distortion is a non linear function o the inp%t si,nal. The non linearity prod%ce lots o overharmonics (niiice) and lots o aliasin, (arrr,). $any years a,o i tried to %se dynamic $/#$!#in, i.e. sitchin, S or crossadin, S beteen $/#$!# levels dependin, on the slew rate o the inp%t... The crossadin, ind%ced aliasin, by itsel. &o lets see ho e can %se pre inte,rated tables to ,et an anti aliased distortion. /ts m%ch li5e a avetable8 b%t instead o accessin, the table ith a phase e ill access it ith the inp%t si,nal. 'e have to ,o rom a serial access to a random access. / tried it8 it or5s. @%t as the inp%t si,nal is m%ch m%ch less predictable than a phase8 calc acc%racy is >%ite delicate. !nyay8 / achieved nice res%lts %sin, a second order scheme even ith complicated distortion. (or eample: #re inte,rated issymetric smooth t%be sat%ration). &econd "rder pre inte,ration H oversamplin, by to may be an eective and ele,ant sol%tion to the problem o aliasin, in non linearities.
,.% Anti Aliased Phase (od$lation istortion is a special case o phase mod%lation. /t is the case ere the re>%ency o the oscillator is Iero. &o8 based on my eperiments ith anti aliasin, distortion8 i ent a step %rther.
eneraly spea5in,8 phase modulation (or re>%ency mod%lation) implies sine oscillators. #hase mod%lation o comple avetable oscillators oten ,enerate too many harmonics and ind%ce stron, aliasin,. ! second orderpre inte,ration allos to implement s%ch a mod%lation. &ee !nti aliased 3 operator #$ synth. or an operational implementation (detailed inormation in the appendi).
1%
AA of Oscillators and Distortions with Pre Integrated WaveTables
A&&endi' A
T.Rochebois
(o))ented *+,- code e'a)&les
These are %lly operational C&D pl%,ins yo% can test and %se %nder Reaper.
A&&endi' A.1
An An Anti Aliased Walsh ,nction s/nth
This C&D synth shos ho a third order dierentiation scheme can be %sed to anti alias a ave table oscillator. e heesy =armonic &ynth is a small C&D synth reminiscent o early di,ital additive synthesiIers. (/t is available or Reaper rom Rea#ac5). /n this =armonic synth8 instead o addin, sine aves8 the basic aveorms are 'alsh se>%ences: sort o s>%are aves. This C&D synth is anti aliased than5s to the third order inte,ration dierentiation scheme i etended to ave tables. =ere is the complete listin,: /"" " #$%& 'amee*heesyarmo,01 " .ot " n additive synth .ased on alsh fnctions " thor 23oche.ois " 4icence 454 " 3773 820 " 9ersion 02: "/ desce *heesy armonic $ynth 01 slider11-1<1<02001>$e2 1 slider0-1<1<02001>$e2 slider:0-1<1<02001>$e2 : slider0-1<1<02001>$e2 slider80-1<1<02001>$e2 8 slider60-1<1<02001>$e2 6 slider@0-1<1<02001>$e2 @ sliderA0-1<1<02001>$e2 A sliderB0-1<1<02001>$e2 B slider100-1<1<02001>$e2 10 slider110-1<1<02001>$e2 11 slider10-1<1<02001>$e2 1 slider1:0-1<1<02001>$e2 1: slider10-1<1<02001>$e2 1 slider180-1<1<02001>$e2 18 slider160-1<1<02001>$e2 16 slider00-<<020001>Cetne (semitones) slider18281<0>9i.rato rate slider020<1<020001>9i.rato depth slider:10<1<020001>5lide rate slider00<1>remolo depth slider:1-:-:<1<020001>ttacD slider:0-:<1<020001>Cecay slider::10<1<020001>$stain slider:--:<1<020001>3elease
11
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
slider10280<1<020001>an slider0-<1>5ain (dE) // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Finit //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, fnction C$3,set10( C $ 3) ( // decay coefs ref time(s) this2 = 1 - eGp(log(02::) / ((10H) " (srate/I37))); this2 = min(this2< 1); this2C = 1 - eGp(log(0210) / ((10HC) " (srate/I37))); this2C = min(this2C< 1); this2$ = $; this23 = 1 - eGp(log(0210) / ((10H3) " (srate/I37))); this23 = min(this23< 1); ); //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, // *ontrol rate processing fnction C$3,Droc(gate trig) local() ( gate === 0 ? this2ttacD$eg = 0; trig ? this2ttacD$eg = 1; this2$3 gate ? this2$3 += this2 " (gate - this2$3) this2$3 += this23 " (gate + 0200000001 - this2$3); // , , , , , , , , , , , , , , , , , , , , , , , , this2ttacD$eg === 1 ? ( this2C$3 += this2 " (128 - this2C$3); this2C$3 >= 1 ? ( this2C$3 = 120; this2ttacD$eg = 0; ); ); this2ttacD$eg === 0 ? ( gate J= 0 ? ( this2C$3 += this2C " (this2$ - this2C$3); ) ( this2C$3 += this23 " (0200000001 - this2C$3); ); ); this2$3 " this2C$3; ); // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, fnction $,init() instance(sal) local(se G G0 G1 G G: G)( sal = ad; ad += :"16; G = 0; loop(:< G0 = (GK 1) ?1-1; G1 = ((GK )>>1)?1-1; G = ((GK )>>)?1-1; G: = ((GK A)>>:)?1-1; G = ((GK16)>>)?1-1; salG + : " 0! = G; salG + : " 1! = G: ; salG + : " ! = G"G:"G; salG + : " :! = G ; salG + : " ! = G1"G" G; salG + : " 8! = G1"G"G: ; salG + : " 6! = G1 "G:"G; salG + : " @! = G1 ;
12
,
,
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
salG + : " A! = G0"G1 "G; salG + : " B! = G0"G1 "G: ; salG + : " 10! = G0"G1"G"G:"G; salG + : " 11! = G0"G1"G ; salG + : " 1! = G0 "G "G; salG + : " 1:! = G0 "G"G: ; salG + : " 1! = G0 "G:"G; salG + : " 18! = G0 ; G += 1; ); ); fnction L,init(nMaG) instance(n G y N O dp ,dp ,dp:) ( this2nMaG = nMaG; n = nMaG; G = ad; ad += n; y = ad; ad += n; N = ad; ad += n; O = ad; ad += n; dp = n"0/srate; ,dp = 1 / dp; ,dp: = ,dp " ,dp " ,dp; ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , fnction L,dc3emove(t n) local(i m)( m = 0; i = 0; loop(n< m += ti!; i += 1; ); m /= n; i = 0; loop(n< ti! -= m; i += 1; ); ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , fnction L,norm(t n) local(i m)( m = 0; i = 0; loop(n< m = maG(ti!< a.s(m)); i += 1; ); m = 121/(m+021); i = 0; loop(n< ti! "= m; i += 1; ); ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , fnction L,integ() instance(G n y N O N y G) local(i)( L,dc3emove(G< n); i = 0; loop(n-1< yi+1! = yi! + Gi!; i += 1; ); L,dc3emove(y< n); i = 0; loop(n-1< Ni+1! = Ni! + yi! + (1/) " Gi!; i += 1; ); L,dc3emove(N< n); i = 0; loop(n< yi! "= 1/; Gi! "= 1/6; i n-1 ? Oi+1! = Oi! + Ni! + yi! + Gi!; i += 1; ); L,dc3emove(O< n); ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , // *all .efore aroc if there is a discontinity of dp ,dp fnction L,disc() instance(n dp ,dp ,dp: p ot O N y G O0 O1 O) local(p0 a)( p -= "dp; p += n " (p 0); p -= n " (p >= n); p0 = pP0; a = p - p0; O0 = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); p += dp; p -= n"(p>=n); p0 = pP0; a = p-p0; O1 = Op0! + a " (Np0! + a " (yp0! + a " Gp0!));
13
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
p += dp; p -= n"(p>=n); p0 = pP0; a = p-p0; O = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , fnction L,aroc() instance(n dp ,dp ,dp: p ot O N y G O0 O1 O O:) local(p0 a)( p += dp; p -= n"(p>=n); p0 = pP0; a = p-p0; O: = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); ot = (O: - O0 + : " (O1 - O)) " ,dp:; O0 = O1; O1 = O; O = O:; ot; ); // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, // Lnit the Oave ta.le ,srate = 1 / srate; I37 = A; ,I37 = 1/I37; piOt2L,init(:); Osh2$,init(); D.'otes = ad; ad += 1A; .end%actor = .end%actor% = 1; // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Fslider gain = H((1/6)"slider); g4eft = gain " cos(028"Qpi"slider1); g3ight = gain " sin(028"Qpi"slider1); dp4fo = "Qpi"slider1":",srate; vi.Min = slider"0208; vi.MaG = 0208 + " vi.Min; adsr2C$3,set10(slider:1< slider:< slider::< slider:); glide*oef = : " ,srate " (021 + 100 " slider: " slider:); G = 0; loop(:< piOt2GG! = slider1 " Osh2salG + : " 0!; se = 1;loop(18< piOt2GG! += slider(se + 1) " Osh2salG + : " se!; se += 1; ); G += 1; ); L,dc3emove(piOt2G<:); L,norm(piOt2G<:); piOt2L,integ(); piOt2L,disc(); // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, F.locD Ohile (midirecv(offset< msg1< msg:)) ( msg = msg: K 0G@%; msg: = msg: >> A; stats = msg1 K QG%0; stats == QGA0 ? ( stats = QGB0; msg: = 0; ); // note off stats == QGB0 ? ( // note on msg: == 0 ? ( D.'otesmsg! = 0; gate = 0; i=0; loop(1A< D.'otesi! J== 0 ? (gate = DE'otesi!; note=i;); i+=1;); gate ? dpc = (piOt2n"0",srate) " H ((note + slider0 - 6B) " (1/1)); ) ( gate == 0 ? trig = 1; D.'otesmsg! = gate = srt(msg: " (1/1@)); note = msg; dpc = (piOt2n"0",srate) " H ((note + slider0 - 6B) " (1/1)); trig ? (dpg = dpc;);
14
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
); ) stats == QGE0 ? ( msg == 1 ? modhl = msg: " (1/1@); // ll notes off msg == 1: ? (trig = gate = 0;i=0; loop(1A< DE'otesi! = 0; note=i;); i+=1;); ) stats == QGC0 ? (aftertoch = msg " (1/1@);) stats === 1 " 16 ? ( .end = (msg: @ P msg) - A1B; .end = 0 ? .end "= 1 / A1B .end "= 1 / A1B1; .end%actor = H.end; ); co,vi. = vi.Min + modhl " (vi.MaG - vi.Min); co,vi. = maG(co,vi.< vi.Min + aftertoch " (vi.MaG - vi.Min)); midisend(offset< msg1< msg:); ); // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Fsample D = 0 ? ( D = I37; d7nv = (adsr2C$3,Droc(gate< trig) - env)",I37; trig = 0; ); D -= 1; // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , D = 0 ? ( D = :; .end%actor% += 0208 " (.end%actor - .end%actor%); slider: >=1 ? dpg = .end%actor% " dpc dpg += glide*oef " (.end%actor% " dpc - dpg); p4fo += dp4fo; p4fo -= "Qpi"(p4fo>0); lfo = sin(p4fo); drem = (1/:)"( 1 + slider " (028"lfo+028 - 1) - trem); co,vi.f += 0208 " (co,vi. - co,vi.f); piOt2dp = dpg " (1 + co,vi.f " lfo); piOt2,dp = 1 / piOt2dp; piOt2,dp: = piOt2,dp " piOt2,dp " piOt2,dp; piOt2L,disc(); ); D -= 1; // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , env += d7nv; trem += drem; ot = min(10
1
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
gi = 0; loop(:< gG = gi"1+A; gi>0? gfG,line(gG< :-1"piOt2Ggi-1!< gG< :-1"piOt2Ggi!) gfG,line(gG< :< gG< :-1"piOt2Ggi!); gfG,line(gG< :-1"piOt2Ggi!< gG+1< :-1"piOt2Ggi!); gi+=1; ); gG = gi"1+A; gfG,line(gG< :-1"piOt2G:1!< gG< :);
/ ill comment and describe part o this code. desce *heesy armonic $ynth slider11-1<1<02001>$e2 1 slider0-1<1<02001>$e2 slider:0-1<1<02001>$e2 : slider0-1<1<02001>$e2 slider80-1<1<02001>$e2 8 slider60-1<1<02001>$e2 6 slider@0-1<1<02001>$e2 @ sliderA0-1<1<02001>$e2 A sliderB0-1<1<02001>$e2 B slider100-1<1<02001>$e2 10 slider110-1<1<02001>$e2 11 slider10-1<1<02001>$e2 1 slider1:0-1<1<02001>$e2 1: slider10-1<1<02001>$e2 1 slider180-1<1<02001>$e2 18 slider160-1<1<02001>$e2 16 222! slider10280<1<020001>an slider0-<1>5ain (dE)
This is the header section hich deines the sliders: i,ital =armonics amplit%des8 et%ne and Pibrato8 9nveloppe and #an controls are deined here. // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Finit //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 222!
The ;init section contains %nction declarations and init code s%ch as the !&R enveloppe code. 'e ont oc%s on that8 ed rather loo5 or hats speciic to this synth.
1!
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
// ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, fnction $,init() instance(sal) local(se G G0 G1 G G: G)( sal = ad; ad += :"16; G = 0; loop(:< G0 = (GK 1) ?1-1; G1 = ((GK )>>1)?1-1; G = ((GK )>>)?1-1; G: = ((GK A)>>:)?1-1; G = ((GK16)>>)?1-1; salG + : " 0! = G; salG + : " 1! = G: ; salG + : " ! = G"G:"G; salG + : " :! = G ; salG + : " ! = G1"G" G; salG + : " 8! = G1"G"G: ; salG + : " 6! = G1 "G:"G; salG + : " @! = G1 ; salG + : " A! = G0"G1 "G; salG + : " B! = G0"G1 "G: ; salG + : " 10! = G0"G1"G"G:"G; salG + : " 11! = G0"G1"G ; salG + : " 1! = G0 "G "G; salG + : " 1:! = G0 "G"G: ; salG + : " 1! = G0 "G:"G; salG + : " 18! = G0 ; G += 1; ); );
That initialises the 16 alsh %nction tables. These tables ill be added to,ether in the #re /nte,rated 'ave Table everytime a slider is moved.
=ere come the #re /nte,rated 'ave Table %nctions.
irst8 the init %nction: fnction L,init(nMaG) instance(n G y N O dp ,dp ,dp:) ( this2nMaG = nMaG; n = nMaG; G = ad; ad += n; y = ad; ad += n; N = ad; ad += n; O = ad; ad += n; dp = n"0/srate; ,dp = 1 / dp; ,dp: = ,dp " ,dp " ,dp; );
it initialiIes a pre inte,rated ave %nction. 8 y8 I and are the tables that ill contain the pre inte,rated val%es. (ad is an address co%nter that 5eeps trac5 o the memory %sa,e in the pl%,in).
1"
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
Then e have some %tility %nctions. // , , , , , , , , , , , fnction L,dc3emove(t m = 0; i = 0; loop(n< m /= n; i = 0; loop(n< );
, , , , , , , , , , , , , , , , , , , , , , , n) local(i m)( m += ti!; i += 1; ); ti! -= m; i += 1; );
This is a %tility %nction that removes the mean val%e () o a table. /t is an important eat%re hen yo% inte,rate periodic tables more than once: it ,%aranties that the inte,rals ill be periodic too. // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , fnction L,norm(t n) local(i m)( m = 0; i = 0; loop(n< m = maG(ti!< a.s(m)); i += 1; ); m = 121/(m+021); i = 0; loop(n< ti! "= m; i += 1; ); );
This is another %tility %nction that ro%,hly normaliIe the content o a table.
"ne o the core %nction is: // , , , , , , , , , , , , , , , , fnction L,integ() instance(G n y N O N y G) local(i)( L,dc3emove(G< n); i = 0; loop(n-1< yi+1! = yi! + L,dc3emove(y< n); i = 0; loop(n-1< Ni+1! = Ni! + L,dc3emove(N< n); i = 0; loop(n< yi! "= 1/; Gi! "= 1/6; i n-1 ? Oi+1! = Oi! + Ni! i += 1; ); L,dc3emove(O< n); );
, , , , , , , , , , , , , , , , , ,
Gi!;
i += 1;
);
yi! + (1/) " Gi!; i += 1;
);
+ yi! + Gi!;
/t is the pre inte,ration method that prepares the 8 y8 I and tables.
The inp%t table is . /t consists o 32 steps8 a mit%re o 'alsh %nctions8 no linear interpolation. irst o all8 e remove its mean val%e by callin, the %tility %nction #/'TLdcRemove(8 n)N
Then e calc%late its irst inte,ral i = 0; loop(n-1< yi+1! = yi! + Gi!;
1#
i += 1;
);
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
and e remove its bias too ith #/'TLdcRemove(y8 n)N
'ith and y e can calc%late / 18 the irst inte,ral. /ts val%e in se,ment n%mber p 0 is: I 1 ( p )= I 1 ( a ) p = y [ p0 ]+ a x [ p 0 ] 0
p0 bein, the inte,er part o p and a bein, its loat part.
The second order inte,ral I is i = 0; loop(n-1< Ni+1! = Ni! + yi! + (1/) " Gi!; i += 1;
);
'hy is the i present8 e sho%ld acc%m%late yi B 'hy does the i have a (1M2) coeicient B 'hy do e acc%m%late Ii to IiH1
&o lets see hat happens in a se,ment. /n the se,ment n%mber p 08 e have 1
2
I 2 ( p )= I 2 ( a ) p = z [ p 0 ]+ a y [ p 0 ]+ a x [ p0 ] 2
0
its derivate is dI 2 ( p ) dp
= I ( p )= y [ p ]+ a x [ p ] 1
0
0
/t does not depend on Ip 0. This relationship beteen / 2 and / 1 ill still be tr%e even i e did not acc%m%late the previo%s val%es o I.
!cc%m%latin, I val%es ens%res that the polynomial se,ments connect the dots. !t the bo%ndaries8 e have I 2 ( p0 +ϵ)≃ I 2 ( p 0−ϵ)
i.e. z [ p0 ]+ ϵ y [ p 0]+
1 2
1
2
2
ϵ x [ p ]≃ z [ p −1 ]+( 1−ϵ) y [ p −1 ]+ ( 1 −ϵ) x [ p −1 ] 0
0
0
2
0
The limit o this e>%ation ,ives %s o%r inte,ration code.
The last step calc%lates 8 the third order inte,ral. /t is mostly similar to the previo%s step8 so i ont ,o into it.
1$
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
=n); p0 = pP0; a = p-p0; O: = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); ot = (O: - O0 + : " (O1 - O)) " ,dp:; O0 = O1; O1 = O; O = O:; ot; );
This is the core %nction o the synth. instance(n dp ,dp ,dp: p ot O N y G O0 O1 O O:)
are the state variables o the oscillator n is the table siIe dp is the phase increment (re>%ency) Ldp is the reciprocal o the phase increment Ldp3 is the c%be o the reciprocal o the phase increment. /t is %sed to normaliIe the dierentiation p is the phase y I are the polynomial tables 0 1 2 3 are delayed val%es o the polynomial p += dp; p -= n"(p>=n);
This is the phase increment and mod%lo (e stay in 08 n ) p0 = pP0; a = p-p0;
&plits p into its inte,er and loat parts O: = Op0! + a " (Np0! + a " (yp0! + a " Gp0!));
alc%lates the third order inte,ral o the point
b%t / already scaled y by (1M2) and by (1M6). !nother optimisation is that / p%t the polynomial in its =orner orm. &o that O: = Op0! + a " (Np0! + a " (yp0! + a " Gp0!));
2%
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
costs only three m%ltiplications and three additions. ot = (O: - O0 + : " (O1 - O)) " ,dp:;
The o%tp%t is the third order dierentiation normaliIed by the c%be o the phase increment. // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , // *all .efore aroc if there is a discontinity of dp ,dp fnction L,disc() instance(n dp ,dp ,dp: p ot O N y G O0 O1 O) local(p0 a)( p -= "dp; p += n " (p 0); p -= n " (p >= n); p0 = pP0; a = p - p0; O0 = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); p += dp; p -= n"(p>=n); p0 = pP0; a = p-p0; O1 = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); p += dp; p -= n"(p>=n); p0 = pP0; a = p-p0; O = Op0! + a " (Np0! + a " (yp0! + a " Gp0!)); );
This %nction %pdates the state variables o an oscillator. /t sho%ld be called every time there is a discontin%ity o dp to prevent ,litches.
222! // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Fslider 222! G = 0; loop(:< piOt2GG! = slider1 " Osh2salG + : " 0!; se = 1;loop(18< piOt2GG! += slider(se + 1) " Osh2salG + : " se!; se += 1; ); G += 1; ); L,dc3emove(piOt2G<:); L,norm(piOt2G<:); piOt2L,integ(); piOt2L,disc();
The ;slider section ta5es in char,e the %pdate o the #re /nte,rated 'ave Table.
irst it is %pdated as a mit%re o 'alsh %nctions: G = 0; loop(:< piOt2GG! = slider1 " Osh2salG + : " 0!; se = 1;loop(18< piOt2GG! += slider(se + 1) " Osh2salG + : " se!; se += 1; );
21
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
G += 1; );
Then the pit. table is normaliIed and pre /nte,rated: L,dc3emove(piOt2G<:); L,norm(piOt2G<:); piOt2L,integ();
litches are prevented by callin, piOt2L,disc();
// ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, F.locD Ohile (midirecv(offset< msg1< msg:)) ( 222! );
Eets have a loo5 to the ;sample section. // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Fsample D = 0 ? ( D = I37; d7nv = (adsr2C$3,Droc(gate< trig) - env)",I37; trig = 0; ); D -= 1;
This is a control rate s%bGsection. This code is r%n every JR!T9 sample. This 5ind o optimisation saves lots o # itho%t compromisin, so%nd >%ality i yo% choose JR!T9 correctly. // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , D = 0 ? ( D = :; .end%actor% += 0208 " (.end%actor - .end%actor%); slider: >=1 ? dpg = .end%actor% " dpc dpg += glide*oef " (.end%actor% " dpc - dpg); p4fo += dp4fo; p4fo -= "Qpi"(p4fo>0); lfo = sin(p4fo); drem = (1/:)"( 1 + slider " (028"lfo+028 - 1) - trem); co,vi.f += 0208 " (co,vi. - co,vi.f); piOt2dp = dpg " (1 + co,vi.f " lfo); piOt2,dp = 1 / piOt2dp; piOt2,dp: = piOt2,dp " piOt2,dp " piOt2,dp;
22
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
piOt2L,disc(); ); D -= 1;
This other s%bGsection is r%n every 32 samples8 it contains %pdates o the vario%s $// controls that aect the pitch o the so%nd. =ere pit.dp8 pit.Ldp and pit.Ldp3 are %pdated and pit.#/'TLdisc() is called to avoid ,litches. // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , env += d7nv; trem += drem; ot = min(10
This code is r%n or every sample. The enveloppe and tremolo are linearly interpolated. env += d7nv; trem += drem;
The o%tp%t is calc%lated ith the 3rd order dierentiator scheme: ot =
min(10
A&&endi' A.2 An An Anti Aliased 3 O&erator Parabola Phase 0odlation s/nth =ere is a mono synth that consist in a chain o three operators. 9ach operator ,enerates a cyclic parabola si,nal that mod%lates the phase o the operator belo it. The anti aliasin, consist o a second order inte,rationMdi etendent to phase mod%lation. =ere is the complete C&D code: descara.ola hase Modlation $ynthesiNer // thor 23oche.ois 0A/016 slider:sl,3=1200010<:<02000001>3atio slidersl,L=1280<:<020001>L slider6sl,31=02BBB@0<:<02000001>3atio 1 slider@sl,L1=0280<:<020001>L1 sliderBsl,30=10<:<02000001>3atio 0 slider11sl,=-:-:<0<02001>ttacD slider1sl,3=0-<1<02001>3elease // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
23
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
Finit fnction L0(G)( G+=16;G-=GP0; (6"G-6)"G+1 ); fnction L1(G)( G+=16;G-=GP0; (("G-:)"G+1)"G ); fnction L(G)( G+=16;G-=GP0; ((028"G-1)"G+028)"G"G ); fnction ,para(dp m) instance(p G0 G1 y0 y1 L,0 L,1 L1,0 L1,1 ot)( p += dp; p >= 1 ? (p -= 1; G0 -= 1; y0 -= 1; ); G1 = G0; L,1 = L,0; G0 = p + m; L,0 = L(G0); y1 = y0; L1,1 = L1,0; y0 = 028 " (G0 + G1); L1,0 = G0 == G1 ? L1(y0) (L,0 - L,1)/(G0 - G1); ot = y0 == y1 ? L0(y0) (L1,0 - L1,1)/(y0 - y1); ); // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, F.locD Ohile (midirecv(offset< msg1< msg:)) ( msg = msg: K 0G@%; msg: = msg: >> A; stats = msg1 K QG%0; stats == QGA0 ? ( stats = QGB0; msg: = 0; ); // note off stats == QGB0 ? ( // note on msg: == 0 ? (msg == note ? gate = 0;) ( gate = srt(msg: " (1/1@)); note = msg; dp = (0/srate)"H((note-6B)"(1/1)); ); ); midisend(offset< msg1< msg:); ); dp0 = sl,30 " dp; dp1 = sl,31 " dp; dp = sl,3 " dp; = 1/(srate"10Hsl,); 3 = 1/(srate"10Hsl,3); // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Fsample env += env>gate ? 3"(gate-env) "(gate-env); y = osc2,para(dp<0); y1 = osc12,para(dp1< env"sl,L " y); y0 = osc02,para(dp0< (028+028"env)"sl,L1 " y1); spl0 = spl1 = min(:
Thats clearly hy / li5e C&D: yo% can test yo%r ideas ith very e code lines.
The irst section describes the sliders that ill control the synth. The second section is the ;init section8 it contains st% the pl%,in ill do at init. /ts also the place here yo% can declare yo%r %nctions. The ;bloc5 section is %s%ally the place here yo% process $// inormation. The ;sample section is called or every sample. The stereo o%tp%t is spl0 and spl1.
/n this eample8 the speciic code is in the %nctions and in the ;sample section (colored red). / ill comment those. fnction L0(G)( G+=16;G-=GP0;
(6"G-6)"G+1
This is o%r parabola8 inte,rated 0 times (hence the /0).
24
);
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
irst8 the parameter is restrained to 081 by a mod%lo %nction : G+=16;G-=GP0;). The coeicients o (6"G-6)"G+1 have been calc%lated so that the mean val%e o /0() over 081 is e>%al to Iero8 this is an important eat%re or inte,ratin, it has a periodic %nction. fnction L1(G)( G+=16;G-=GP0;
(("G-:)"G+1)"G
);
This is o%r parabola8 inte,rated once (hence the /1). &ame thin, as beore8 the parameter is retrained to the 081 interval. The derivative o (("G-:)"G+1)"G is (6"G-6)"G+1 . The inte,ration constant have been chosen so that the mean val%e o /1() over 081 is e>%al to Iero so that it can be inte,rated one more time itho%t a oset. fnction L(G)( G+=16;G-=GP0;
((028"G-1)"G+028)"G"G
);
This is o%r parabola8 inte,rated tice (hence the /2). The second derivative o
((0.5*x-1)*x+0.5)*x*x is (6*x-6)*x+1 .
This time8 e do not intend to inte,rate it once more8 e do not need its oset (a5a mean val%e) to be Iero. &o i ,o or the simplest polynomial.
The %nction fnction ,para(dp m) instance(p G0 G1 y0 y1 L,0 L,1 L1,0 L1,1 ot)( p += dp; p >= 1 ? (p -= 1; G0 -= 1; y0 -= 1; ); G1 = G0; L,1 = L,0; G0 = p + m; L,0 = L(G0); y1 = y0; L1,1 = L1,0; y0 = 028 " (G0 + G1); L1,0 = G0 == G1 ? L1(y0) (L,0 - L,1)/(G0 - G1); ot = y0 == y1 ? L0(y0) (L1,0 - L1,1)/(y0 - y1); );
is the core o the anti aliased parabola operators. This %nction %ses the namespace acility o -s (hich is sort o ob-ect oriented eat%re). /t is called in the ;sample section or each oscillator osc08 osc18 osc2: y = osc2,para(dp<0); y1 = osc12,para(dp1< env"sl,L " y); y0 = osc02,para(dp0< (028+028"env)"sl,L1 " y1);
The %nction has to ar,%ments:
•
dp: the phase increment
• m : the phase mod%lation si,nal The ;sample code is strai,htorard and it ill be easy or yo% to edit it and addMchan,e oscillators.
2
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
&o... fnction ,para(dp m) instance(p G0 G1 y0 y1 L,0 L,1 L1,0 L1,1 ot)( p += dp; p >= 1 ? (p -= 1; G0 -= 1; y0 -= 1; ); G1 = G0; L,1 = L,0; G0 = p + m; L,0 = L(G0); y1 = y0; L1,1 = L1,0; y0 = 028 " (G0 + G1); L1,0 = G0 == G1 ? L1(y0) (L,0 - L,1)/(G0 - G1); ot = y0 == y1 ? L0(y0) (L1,0 - L1,1)/(y0 - y1); ); instance(p G0 G1 y0 y1 L,0 L,1 L1,0 L1,1 ot)
Eists all the internal state variables o an oscillator.
• p is the phase acc%m%lator8 it ill be incremented by dp. /ts ran,e is 08 1 • 0 is pHm8 the total inp%t phase (i.e. incl%din, mod%lation). • 1 is delayed by one sample • •
/2L0 is the second order inte,ral o o%r parabola ta5en at 0
• •
y0 is the mean o 0 and 1 (the val%e in the middle o 0 and 1)
•
/1L0 is the irst order inte,ral o o%r parabola ta5en at y0
• •
/1L1 is the irst order inte,ral o o%r parabola ta5en at y1
/2L1 is the second order inte,ral o o%r parabola ta5en at 1
y1 is y0 delayed by one sample
o%t is the o%tp%t a5a o%r parabola (a5a the Iero order inte,ral o o%r parabola).
Eets have a loo5 at every line: p += dp;
/ncrements the phase p >= 1 ? (p -= 1; G0 -= 1; y0 -= 1; );
%al to 18 decrease p8 0 and y0 by one. This line ,%aranties that p ont ,o beyond one. / also decrease 0 and y0 by the same amo%nt beca%se i need them to be consistent ith p (note: alays be care%l on that point i yo% desi,n yo%r on oscillators). G1 = G0;
1 is the previo%s val%e o 0 L,1 = L,0;
/2L1 is the previo%s val%e o the second order inte,ral o o%r parabola. G0 = p + m;
The ne val%e or 0 is the phase H the mod%lation inp%t L,0 = L(G0);
2!
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
The ne val%e or /2L0 is the second order inte,ral o o%r parabola at 0. y1 = y0; L1,1 = L1,0; y0 = 028 " (G0 + G1);
&ame thin, as beore ith the irst order inte,ral o o%r parabola. L1,0 = G0 == G1 ? L1(y0) (L,0 - L,1)/(G0 - G1);
/s a little tric5y. %al. $ost o the time8 0 KF 1 and e have: L1,0 = (L,0 - L,1)/(G0 - G1);
'e ta5e as irst order inte,ral at point y0 the mean o the inte,ral beteen 0 and 1 by dierentiation, the second order inte,ral.
The problem is hen 0 is near 18 dividin, by Iero is a bad option8 thats hy e have a plan @: directly calc%late /1(y0).
= y0 == y1 ? L0(y0) (L1,0 - L1,1)/(y0 - y1);
/s mostly the same as the previo%s line8 i y0 is close to y18 e ,o to plan @ and eval%ate the parabola directly. "therise8 e ,o or plan ! and e ta5e its mean val%e in the interval y0 y1.
2"
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
A&&endi' A.3 An Anti Aliased Diss/)etric +atration T%bey &at is an antialiased &at%ration C&D pl%,in : http:MMor%m.coc5os.comMshothread.phpBtF1A07?1
The &at%ration %nction is a smooth dissymetric %nction deined in a table. The %nction is deined by linear se,ments. The al,orithm is m%ch similar to the one %sed int e heeIy =armonic &ynth (even i its avetable is deined by steps).
T%bey &at combines 2 interpolationMdecimation and a s econd order inte,ration dierentiation scheme. !s %s%al8 the ;init section contains %nction deinitions and initialisations. init2L3() and dec2L76L?7(0 1) are the interpolator and decimator %nctions. / ont tal5 abo%t these8 they are classic /R desi,n. fnction L,init(n)( this2n = n; this2n16 = n " 16; this2dv = ad; ad += n; this2v this2Lv = ad; ad += n; this2LLv );
= ad; ad += n; = ad; ad += n;
!llocates some tables or the inte,ration scheme. v ill contain the val%es dv ill contain the deltas (or linear interpolation) /v ill contain the irst order inte,ral coeicients //v ill contain the second order inte,ral coeicients. // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, // (performs the pre integrations) fnction L,pdate() instance(n dv v Lv LLv) local(p dcRffset)( // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , // 3emove C* offset from the inpt ta.le dcRffset = v0!; p = 1; loop(n - 1< dcRffset += vp!; p += 1; ); dcRffset /= n; p = 0; loop(n< vp! -= dcRffset; p += 1; ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , // calc the slope .etOeen tOo vales (for linear interpolation) p = 0; loop(n< dvp! = v(p+1) S n! - vp!; p += 1; ); // , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , // %irst polynomial integration Lv dcRffset = Lv0! = 0; 0; p = 0; loop(n - 1< Lvp+1! = Lvp! + vp! + 028 " dvp!; dcRffset += Lvp+1!; p+=1; ); dcRffset /= n; p = 0; loop(n< Lvp! -= dcRffset; p += 1;);
2#
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
// , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , // $econd polynomial integration LLv dcRffset = LLv0! = 0; p = 0; loop(n - 1< LLvp+1! = LLvp! + Lvp! + 028 " vp!+(1/6)"dvp!; dcRffset += LLvp+1!; p+=1; ); dcRffset /= n;p = 0;loop(n< LLvp! -= dcRffset;p+=1;); );
This %nction pre inte,rates the inp%t table v. irst8 it removes its mean val%e to avoid bias during integration (dc"set). Then it calc%lates its delta dvp (or linear interpolation). Then the irst and second order inte,ration are perormed ta5in, acco%nt o v and dv.
ret%rns the /0 i.e. the ra %nction (ith linear interpolation) fnction L,L1(p) instance(n n16 dv v Lv) local(p0 a)( a = p - (p0 = pP0); p0 S= n; ( dvp0! " 028"a + vp0!) " a + Lvp0!; );
ret%rns the irst inte,ration val%e /1(p) fnction L,L(p) instance(n n16 dv v Lv LLv) local(p0 a)( a = p - (p0 = pP0); p0 S= n; (( dvp0! " (1/:)"a + vp0!) " 028"a + Lvp0!) " a + LLvp0!; );
ret%rns the second inte,ration val%e /2(p)
/n a p%re second order dierentiation scheme e sho%ld only %se #/TL/2. #/TL/1 and #/TL/0 are plan @ %nctions that allos to deal ith delicate sit%ations... it is m%ch similar to hat / have done in the #$ parabola synth.
2$
AA of Oscillators and Distortions with Pre Integrated WaveTables
T.Rochebois
=ere is the core %nction o the distortion eect: fnction LR,aroc(m) instance(p n G1 G L,1 L, y0 y1 L1,0 L1,1 ot)( G1 = G; L,1 = L,; G = m; L, = this2L,L(G); y0 = y1; L1,0 = L1,1; y1 = 028 " (G + G1); L1,1 = a.s(G - G1)020001 ? this2L,L1(y1) (L, - L,1) / (G G1); ot = a.s(y1 - y0)0201 ? this2L,L0(028"(y0+y1)) (L1,1 - L1,0) / (y1 y0); );
The main dierence ith the parabola #$ synth is that the /08 /1 and /2 %nctions %se tables instead o an inte,rated parabolic %nction. fnction LR,pdate() instance(p G1 G L,1 L, y0 y1 L1,0 L1,1 ot)( L, = this2L,L(G); L1,1 = G1 == G ? this2L,L1(y1) (L, - this2L,L(G1)) / (G - G1); );
This %nction m%st be called i the table is %pdated to prevent ,litches. // ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, pit2L,init(86); i=0;loop(pit2n< G = i pit2n / ? i i - pit2n; G "= 02; G > 0 ? pit2vi! = G / ((1+a.s(G)H28)H(1/28)) pit2vi! = G / ((1+a.s(G)H20)H(1/20)); i+=1; );
This is the initialisaton code. /t initialiIes the table ith the dissymetric and smooth sat%ration. pit2L,pdate(); pio2LR,linD(pit); pio2LR,pdate();
%pdates and prepare the table. Fsample gCrive$mooth += 0201 " (gCrive - gCrive$mooth); G = gCrive$mooth " spl0; G = maG(-18< min(18
3%