32
8/9/2019 Tutorial 8addSynthRandom (1) http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 1/32 / * Tu t o r i a l 8 Ad i t i v e Syn t h e s i s , Ra ndom Numb e r s , CPU u s a ge Ha r mon i c Se r i e s a nd Wa v e Sh a p e Why , a f t e r nea r l y 50 y ea r s of e l ec t r on i c mu s i c and i n s t r umen t des i gn , c an we st i l l t el l i f an i n s t r umen t i s s y nt hes i z ed? Wha t mo s t s y nt he t i c m ode l s lac k ar e c omp l e x i t y a nd c ha os . Ev e n s amp l ed i n s t r umen t s do no t s ound r ea l bec au s e an e x ac t dup l i c a t i on o f a s i ng l e no t e f r om an y i n s tr umen t i s no t a t r ue r ep r es en t a t i on o f t ha t i n s tr umen t , r a t he r a s i ng l e i n s t a nc e o f man y poss i b i l i t i es . A s i ng l e h i gh r eso l u t i on d i g i t a l pho t o o f a l eaf dup l i ca t ed hund r ed s of t i mes wou l d no t l ook l i k e a t r ee . A l l t he l eav es on a t r ee a r e d i e r en t , and t hey c hange i n r ea l t i me . The s ame i s tr ue fo r a pi ano . Eac h k e y has it s own ha r mon i c nge r p r i n t . Ev e n th e s a me k e y when s t r uc k s uc ces si v e l y wi l l p r oduc e d i e r en t pa r t i a l s d epend i ng on t he pos i t i on o f t he v i b r a t i ng s tr i ng when i t i s st r uc k . Rea l i n s tr umen t s ar e c omp l ex and c hao t i c . Eac h no t e i s un i que . Though t he gene r a l upe r ha r mon i c c on t en t r ema i n s c on s t an t , t he sma l l de t a i l s c hange . SC i s a r ea l - t i me s y nt hes i s pac k age t ha t i s c apab l e o f , ev en i n v i t es a nd enc ou r ages t h i s l ev el o f comp l ex i t y . We w i l l s t ar t t he t heo r y o f syn t hes is wi t h t he mo s t i n t r i c at e and c ompu t a t i on i n t en s i v e me t hod ( and mo s t r ewa r d i ng i n t er ms o f r esu l t s) ; add i t i v e syn t hes i s .

Tutorial 8addSynthRandom (1)

Embed Size (px)

Citation preview

Page 1: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 1/32

/*

Tutorial 8

Additive S ynthesis, Random Numbers, CPU usageHarmonic Series and W ave Shape

Why, after nearly 5 0 years o f electronic m usic a nd instrument design, canwe still tell if aninstrument is syn thesized? What most synthetic m odels lack a recomplexity an d ch aos. Evensampled instruments d o not sound real because an exact duplication of a

single note from anyinstrument is not a true representation of that instrument, rather a singleinstance of manypossibilities. A single high resolution digital photo of a leaf duplicatedhundreds o f timeswould not look like a tree. All the leaves on a tree are different, and theychange in real time.The same is t rue for a p iano. Each key has i ts o wn harmonic n gerprint.

Even the sam e keywhen struck successively will produce different partials d epending on theposition of thevibrating string when it is s truck. Real instruments a re complex a ndchaotic. Each note isunique. Though the general upper harmonic co ntent remains co nstant, thesmall detailschange. SC is a real-time synthesis p ackage that is c apable of, even

invites an d e ncouragesthis level of complexity. We will start the theory of synthesis w ith themost intricate and computation intensive method (and most rewarding interms of results);additive synthesis.

Page 2: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 2/32

Harmonic Series and W ave S hape

In an earlier tutorial we learned that the harmonic series is responsible forthe character of asound. The presence and strength of each harmonic d enes t his ch aracter.But how does asingle string or vibrating body generate a series of related frequencies?

Density and tension being equal, short strings vibrate faster than long. Astring that wouldnormally vibrate at 100 Hz d ivided in half (e.g. by p ressing your nger onthe nger board at

the half-way point) will vibrate twice as fast, or at 200 Hz. If you shorten itto one third itsoriginal length, by p ressing your nger higher, it will vibrate three times asfast or 300 Hz.Shortening it to one fourth generates a frequency four times f aster; 400 Hz.Dividing a stringin such a way will produce harmonics. Harmonics a re the foundation forscale d egrees o f

western music. The illustration below shows t he pitches t hat correspondwith each upperharmonic. The lowest C is a bout 65 Hz, the next is 1 30, then 195, 260(middle C), 325, 390,455, 520, 585, 650, 715, 780, 845, 910, 975, 1040.

Most strings ( and musical instrument bodies) vibrate at all thosefrequencies si multaneously.

While a string is vibrating at its full length it is also vibrating in halves. Thathalf vibratingmotion is twice as fast. It is also vibrating in thirds, fourths, fths, and soon. So a singlestring vibrating at 65 Hz is a lso producing the frequencies 1 30, 195, etc.

Page 3: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 3/32

Timbre is n ot only the presence, but also the amplitude of these harmonics.Each of theharmonics i s a sine wave. As a general rule the higher harmonics h ave alower amplitude, butthe actual amplitude of each harmonic w ill differ with each timbre. Thepresence andamplitudes o f upper harmonics a re the tonal ngerprint of any sound. Aviolin may have astronger third harmonic, weaker fourth, no fth, weak sixth, while atrombone will have aweak third, strong fourth, and so on. Using spectrographs o ne can see theunique ngerprint

of each timbre.

It is a lso important to understand that timbre (hence the shape of the waveand presence ofupper harmonics) changes i n real time. Most musicians st rive for, but neverachieve perfecttone quality. Yet it is this very failure that makes the sound natural.Synthesized sounds have

perfectly homogonous t ones a nd we don't like that. For example, asawtooth wave isactually a series of sine waves tuned to the harmonic s eries with relativelylower amplitudes.The same is t rue of other complex waves; square, triangle, even noise. Allcan be c onstructedusing individual sine waves. That is t he theory o f additive synthesis.

Additive Synthesis

Most synthesizers o ffer a variety o f wave forms f or building sounds but ifthe shape of thewave is p redened you have no control (outside of ltering) over theindividual harmonics.

Page 4: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 4/32

But they a re also incapable of additive synthesis, since they h ave only fouror ve sineoscillators a nd maybe two or three envelope generators. But additivesynthesis a llows f orindependent control over each wave’s p arameters su ch as a mplitude orenvelope, and S C is avirtual warehouse of synthesis VCOs. With o nly a few lines o f code you cancreate hundredsof partials with individual envelopes. But let's start with a dozen.

If we were to look at a sonogram of a sawtooth wave a t 200 Hz, we wouldsee that not only

does i t have all harmonics, but they are very e ven and consistent. Each ofthese upperharmonics can be thought of as s ine waves w ith frequencies i n multiples t othe lowest,or fundamental.

To reconstruct this sa wtooth we need individual sine waves t hat areharmonic, or multiples o f

the fundamental. Here is a crude version built on a fundamental of 400.Adding together 12sine waves w ould normally d istort, so they a re all scaled down by 0 .1.

Adding sines together*/

({(

SinOsc .ar(400) + SinOsc .ar(800) + SinOsc .ar(1200) +SinOsc .ar(1600) + SinOsc .ar(2000) + SinOsc .ar(2400) +SinOsc .ar(2800) + SinOsc .ar(3200) + SinOsc .ar(3600) +SinOsc .ar(4000) + SinOsc .ar(4400) + SinOsc .ar(4800)

)*0.1}.sco e)

Page 5: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 5/32

Page 6: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 6/32

SinOsc .ar( *1% "$&' 1)% SinOsc .ar( *2% "$&' 1/2)%SinOsc .ar( *3% "$&' 1/3)% SinOsc .ar( *4% "$&' 1/4)%SinOsc .ar( * % "$&' 1/ )% SinOsc .ar( *6% "$&' 1/6)%SinOsc .ar( * % "$&' 1/ )% SinOsc .ar( *8% "$&' 1/8)%

SinOsc .ar( * % "$&' 1/ )% SinOsc .ar( *10% "$&' 1/10)%SinOsc .ar( *11% "$&' 1/11)% SinOsc .ar( *12% "$&' 1/12)

}.sco e(12))

/*This patch illustrates how harmonic waves interact. About a quarter of theway across t hisgraph you can see how all the sines a re in synch and are pushing towardtheir peak. This iswhat creates the sharp edge of a saw tooth wave. As t he higher harmonicsmove downwardthey begin to cancel out the energy of the lower waves. The aggregate ofthis p rocess is agradually de scending ramp o f energy a s m ore h armonics m ove into thenegative phase oftheir cycle, until the end of the pattern where you see they a re all more orless in the negativepart of the graph. This i s t he bottom of the saw wave.

Changing the harmonics w ill change t he c haracter of the sound. Changingthe amplitudes o fany si ngle harmonic w ill subtly c hange the character. Try replacing any m ulargument (ahigher one) with a MouseX.kr, isolating the amplitude of that singleharmonic. Notice the

change. Below is t he same patch, but each amplitude is b eing graduallycontrolled with anLFNoise1. Notice that the fundamental pitch stays the same, while the tonequality ch anges.Our brains a dd together all the sine waves a nd we hear it as a single pitch.Also note that this

Page 7: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 7/32

is not ltering (covered below), but additive.

Additive saw with modulation*/

({ar s eed , 14- , 300-i .ne (

SinOsc .ar( *1% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/1)%

SinOsc .ar( *2% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/2)%

SinOsc .ar( *3% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/3)%

SinOsc .ar( *4% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/4)%

SinOsc .ar( * % "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/ )%

SinOsc .ar( *6% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/6)%

SinOsc .ar( * % "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/ )%

SinOsc .ar( *8% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/8)%

SinOsc .ar( * % "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/ )%

SinOsc .ar( *10% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/10)%

SinOsc .ar( *11% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/11)%

SinOsc .ar( *12% "$&' 5 oise1 .7r(rrand(s eed% s eed*2)% 0. %0. )/12))*0.}.sco e(1)) /*Try c hanging the value for speed, which controls t he frequency of theLFNoise. Then tryreplacing all the LFNoise0 ugens w ith an LFNoise1 (use nd and replace).Finally, replace f= 100 with f = someOtherControl such as a sine wave, another LFNoise o r

Page 8: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 8/32

a mouse.

Next we are going to add an envelope. We could replace the last *0.5 witha single envelopeto control the amplitude of all the oscillators a t once. And that's h ow mostsynthesizers withlimited modules d o it. But since we are working with code we can assign anenvelope to eachoscillator, making each harmonic i ndependent, resulting in a more naturalsound.

These e xamples ar e u nnecessarily redundant (a so rt of shotgun a pproach)

to illustrateadditive methods cl early. Even so, with these few dozen lines w e havealready su rpassed thecapacity o f most commercial synthesizers. Granted, this e xercise is a bittedious, perhapsgiving you a glimpse of the pioneering efforts o f early e lectronic c omposersworking onpatch style synthesizers.

Additive saw with independent envelopes*/

({ , 100-t , " $&se .7r(1/3)-i .ne (

SinOsc .ar( *1% "$&' 9n :en .7r( 9n . erc(0% 1.4)% t)/1)%SinOsc .ar( *2% "$&' 9n :en .7r( 9n . erc(0% 1.1)% t)/2)%SinOsc .ar( *3% "$&' 9n :en .7r( 9n . erc(0% 2)% t)/3)%SinOsc .ar( *4% "$&' 9n :en .7r( 9n . erc(0% 1)% t)/4)%SinOsc .ar( * % "$&' 9n :en .7r( 9n . erc(0% 1.8)% t)/ )%SinOsc .ar( *6% "$&' 9n :en .7r( 9n . erc(0% 2. )% t)/6)%SinOsc .ar( * % "$&' 9n :en .7r( 9n . erc(0% 4)% t)/ )%SinOsc .ar( *8% "$&' 9n :en .7r( 9n . erc(0% 0.3)% t)/8)%

Page 9: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 9/32

SinOsc .ar( * % "$&' 9n :en .7r( 9n . erc(0% 1)% t)/ )%SinOsc .ar( *10% "$&' 9n :en .7r( 9n . erc(0% 3.6)% t)/10)%SinOsc .ar( *11% "$&' 9n :en .7r( 9n . erc(0% 2.3)% t)/11)%SinOsc .ar( *12% "$&' 9n :en .7r( 9n . erc(0% 1.1)% t)/12)

)*0.

}.sco e(1))

/*

Shortcuts

There is a lways a n easier way to do things i n SC. Limitations a re often inthe person writingthe code. We can use multi-channel expansion, covered in the previous

chapter, to expand asingle line of code into an array o f items. In the patch above the array(everything betweenthe two braces “[“ and “]”) is lled with oscillators that are written out fully.The Mix combines t hem all into o ne c hannel.

additive synthesis with array expansion*/

i .ne ( SinOsc .ar(100)% SinOsc .ar(200)% SinOsc .ar(300) )

// ;he res$&ts are the sa"e ith this code

i .ne ( SinOsc .ar( 100% 200% 300 ))

/*

In the example below, the rst midicps r eturns a single value. The secondreturns an array ofvalues. The next line shows a shortcut for writing an array that contains aseries o f numbers.The last shows t he patch above written using this t echnique. To thecompiler, they a re the

Page 10: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 10/32

same. The a dvantage is e conomy in typing. It is a lso more concise andeasier to read.

additive synthesis w ith array e xpansion & shortcuts

*/

"idic s(60)-

"idic s( 60% 62% 64% 6 % % 6 % 1 )-

(1..12) // shortc$t or 1% 2% 3% 4% % 6% % 8% % 10% 11% 12

"idic s((1..24)*60)

(1% 3..13) // "eans 1% 3% % % % 11% 13

"idic s((60% 63.. 2))

// additi e s<nthesis ith shortc$ts

({ , 100-t , " $&se .7r(1/3)-i .ne (

SinOsc .ar(*(1..12)%"$&' 9n :en .7r(

9n . erc(0% 1)%t%&e e&Sca&e' 1/(1..12)%ti"eSca&e' 1.4% 1.1% 2% 1% 1.8% 2. % 4% 0.3% 1% 3.6% 2.3% 1.1

))

)*0.}.sco e(1))

/*

The timescale needs t o be written out because it is n ot a logicalseries t hat can be expressedwith a shortcut. But if I don't really c are what each value is, as long asit's b etween 1 and 3, I

Page 11: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 11/32

could use rrand(1.0, 3.0).dup(12) to generate the array o f values.

One situation where you might want to use a collection of uniquevalues is in describingchords o r scales w hich have no simple geometric p attern, as sh ownbelow.

additive synthesis with array expansion*/

({t , " $&se .7r(1/3)-i .ne (

SinOsc .ar(60% 64% 6 % 1% 4% 8 ."idic s%"$&' 9n :en .7r(

9n . erc(0% 1)%t%&e e&Sca&e' 1/(1..6)%ti"eSca&e' rrand(1.0% 3.0).d$

))

)* 0.3% 0.3}.sco e(1))

({i .ne (

=an2 .ar(SinOsc .ar(

60% 62% 63% 6 % 6 % 68% 1% 2 ."idic s%"$&' 5 oise1 .7r(rrand(0.1% 0. ).d$ (8)% 0. % 0. )

)%1.0.rand2.d$ (8)

))*0.2}.sco e(1))

/*

Filling an array

Page 12: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 12/32

Another version of the patch below uses Array .ll to do all the typing, so tospeak, for us.Array.ll generates a n array b ased on a function. Each item in the array is a

result of thefunction. The rst example simply lls t he array with random numbers. Thesecond lls a narray with sine waves. The rst argument for Array.ll is t he number of items i nthe arrayand the second is t he function.

Array.ll*/

Arra< .>&&(16% {100.rand})

Arra< .>&&(16% {SinOsc .ar(100.rand)})

/*When you g ive so meone a t ask, such as cu tting up apples, you often wantto supply differentinstructions f or subsequent repetitions. For example, cut up ten apples b ut

leave the rst onewhole, cut the second one in half, the third in threes, the fourth in fourths,and so on. You cansee the pattern. Rather than tell the person each time the number of cuts Icould just say counteach time and cut it into that many p ieces. In order to do this, the systemneeds to knowwhich iteration it is on. It needs a counter .

The Array object does h ave a counter to keep track o f the number ofrepetitions a nd thatnumber is p assed to the function by way o f an argument. You see this n ewsyntax within thefunction: arg counter . You can name this a rgument anything you like. It's

Page 13: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 13/32

like a variable, butit comes from outside the function. In this case I call it counter. Run each ofthe lines belowand c heck the results in the post window. The last two e xamples sh ow analternatesyntax: |counter| same as a rg counter.

Array.ll with arg*/

//>&& an arra< ith the n$"?er o this iterationArra< .>&&(16% {arg co$nter- co$nter})

//>&& an arra< ith rando" n$"?ersArra< .>&&(16% {arg co$nter- co$nter% rrand(20% 100) })

//>&& an arra< ith "$&ti &es o co$nter ith shorthandArra< .>&&(16% {@co$nter@ co$nter*3})

//>&& an arra< ith SinOsc o?#ects% each ith a re o the co$nter*100Arra< .>&&(16% {@co$nter@ SinOsc .ar(co$nter*100)})

/*

To build a harmonic se ries yo u want to multiply so me fundamental by 1,then 2, 3, 4, etc.The counter can be used to calculate t he freq o f each SinOsc a nd thelevelScale of eachEnvGen. The counter begins a t 0, not 1, and I don't want to start withfreq*0, so I use thevariable partial to increase it by 1 . The code below generates 1 6 upperpartials, eachproportionally s ofter, with a random decay b etween 0.1 and 2.1. Thefund*partial ensuresthey are all harmonic. The 1/partial is the levelScale for the envelope. Therst will be 1/1,he next 1/2, 1/3, 1/4, etc. Array.ll returns a n array o f SinOsc ugens a ndMix.ar mixes t hem

Page 14: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 14/32

all down to one channel.

Additive saw wave, separate decays*/

({ar gate% $nd-gate , " $&se .7r(1/3)-$nd , o$seB .7r( 0% 1000)-i .ne (

Arra< .>&&(16%{ arg co$nter-ar artia&-artia& , co$nter + 1-SinOsc .ar( $nd* artia&) *9n :en .7r( 9n .adsr(0% 0% 1.0% ;Cand .7r(0.2% 2.0% gate))%

gate% 1/ artia&)}))*0.2 //o era&& o&$"e

}.sco e(1))

/*I began this s ection praising SC's c apacity for complexity a nd richness.The patch above isn'tthat astonishing, but you should recognize the more natural sound in thedifferent decay r atesfor each partial. Compare it with this p atch, which uses o nly o ne envelopefor all partials.

Additive saw wave, same decays*/

({ar gate% $nd% en -gate , o$seD$tton .7r(0% 1% 0)-$nd , o$seB .7r( 0% 1000)-en , 9n .adsr(0% 0% 1.0% 2.0)-i .ne (

Page 15: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 15/32

Arra< .>&&(16%{ arg co$nter-ar artia&-artia& , co$nter + 1-SinOsc .ar( $nd* artia&) *

9n :en .7r(en % gate% 1/ artia&)}))*0.2 //o era&& o&$"e

}.sco e(1))

/*When working with code based synthesis ( as o pposed to graphics) it iseasy to duplicate asingle simple idea to achieve complex sounds quickly. This is especiallyevident withadditive synthesis. Take for example this s imple patch, where theamplitude of an oscillatoris b eing controlled by another sine wave.

Single sine with control*/

{ SinOsc .ar(400% "$&' SinOsc .ar(1/3% "$&' 0. % add' 0. ))}. &a<

/*We ca n use t his si ngle m odel to build a much more co mplex sou nd byadding together sinewaves a s h armonics. Mix.ll combines t he ll logic u sed with Array.ll andthe Mix use dpreviously. It generates a n array of ugens a nd mixes t hem down. It alsohas a co unterargument that can be used to calculate upper harmonics.

Gaggle o f sines*/

Page 16: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 16/32

({

ar har"onics , 16% $nd , 0-i .>&&(har"onics%

{ arg co$nt-=an2 .ar(

5SinOsc .ar($nd * (co$nt + 1)% // ca&c$&ates each

har"onic"$&' 5SinOsc .7r(rrand(1/3% 1/6)% "$&' 0. %

add' 0. ))%1.0.rand2)

}) / (2*har"onics)

}. &a<-)

/*

Inharmonic s pectra

Each of the sine waves i n the examples a bove is a multiple (f*1, f*2, f*3,f*4, etc.) o f thefundamental. This i s a harmonic sp ectrum . Most pitched instruments h ave

harmonic s pectra.Non-pitched instruments su ch as g ongs, bells, and cymbals t end towardinharmonic sp ectra ,or a set of frequencies t hat are not multiples o f the fundamental. Togenerate an inharmonicspectrum you need to enter unrelated values f or each sine wave, forexample, 72, 135, 173,239, 267, 306, 355, 473, 512, 572, and 626.

In the examples a bove the amplitude o f each upper harmonic wascalculated in relation to it'sposition; higher harmonics were softer. But in enharmonic s pectra theamplitudes d on't have a pattern.For example, 0.25, 0.11, 0.12, 0.04, 0.1, 0.15, 0.05, 0.01, 0.03, 0.02, and

Page 17: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 17/32

0.12.

Additive s ynthesis ca n be used to generate pure wave forms, such assawtooth, square, ortriangle, but these are available on commercial synthesizers. It is t rue, youhave more c ontrolover the upper partials, but at least these other wave forms d o exist. Butwave f orms withenharmonic sp ectra do not. Commercial synthesizers g o straight fromperfect wave forms t operfect noise. Nothing in between.

Here is a patch that adds t ogether these frequencies a nd amplitudes.

Inharmonic s pectrum*/({ i .ne (

SinOsc .ar(2% 13 % 1 3% 23 % 26 % 306% 3 % 4 3% 12% 2% 626 %

0%// hase0.2 % 0.11% 0.12% 0.04% 0.1% 0.1 % 0.0 % 0.01% 0.03% 0.02% 0.12

))}.sco e(1))

/*

Random Numbers, Perception

I used a deck o f cards t o come up with the frequencies in the examplesabove. We say t hey

are "random" because there is n o apparent pattern. But randomness i s amatter of context andperception. A single number with no context cannot really b e random.As an illustration: Can, you, predict, the, next, number, in, this, series?116, 966, 7733428, 843, 6398, 686237, 46, 8447, _____

Page 18: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 18/32

Page 19: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 19/32

computer it is n otrandom at all. It's l ike a deck of cards t hat has b een shuffled once, but isnever shuffled again.When you run a p rogram that generates su pposedly random events (suchas a dice or cardgame), the c omputer looks for values from this se quence of numbers. Theproblem is i talways st arts a t the beginning, so you always g et the same values a t rst,like using the samedeck of cards f rom the top of the deck without cutting or reshuffling. Theorder will seemrandomized the rst few times t hrough the series, but will never change.

After two or threerepetitions w e will remember the pattern and it won't be random anymore.Part of random, tous, is d ifferent numbers e ach time. So h ow do yo u get it to do different numbers? You cu t the d eck. To cut thedeck of a randomnumber generator you start at a different point in the series. This is called a

random seed .You give the computer a n umber as a seed which is u sed to co unt into therandom numbersequence. The computer then starts u sing numbers f rom that point.

There is o ne more problem. If you know the seed, then the resultingsequence o f numbers isstill not really random to you. You need to choose a random seed. I mean,

you need tochoose a random seed randomly. I mean, that random seed has to berandom (perplexing?).The solution to this p roblem is t o use the internal clock of a CPU, which is

just a series ofnumbers r apidly ying by. If each number on the clock were m atched to a

Page 20: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 20/32

number in therandom sequence, then in a sense yo u a re zipping through the randomsequence (like rifflingthe shuffled deck). Grabbing a number from the clock and u sing it as aseed is like stickingyour nger in the riffling deck: you get a random position at that moment. Itseems a littleconvoluted, but that's h ow it was ( and is) done.

SC does a r andom seed a utomatically be hind the scenes. Each time yo urun a line like10.rand SC rst reads t he number on its i nternal clock to use as a seed. It

then moves into therandom number generator sequence and starts its se quence of choicesfrom that point. It israndom to us because we can't predict what number the clock gave o r theorder of thenumbers at that point.

That’s h ow you get pseudo-random choices. But there are also situations

where you w ant“random” choices, but same “random” choices. In this ca se you would usethe same seedover and over to reproduce the same series o f events. (For example, manycomputer cardgames a llow you to replay the previous h and. This i s h ow it's done.) Agiven random seed isuseful when you a re debugging a cr ash and want to reproduce the same

error every time.Also you might nd a particular variation of random events t hat you like andwill want toreproduce it exactly.

First I'll demonstrate some random choices and then some random choices

Page 21: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 21/32

using a seed.There are a number of random functions in S C. The message rand se nt toany number willreturn a random choice between 0 and that number. 55.rand will return aninteger (e.g. 1, 2,3) between 0 and 55 (not including 55). 55.0.rand will return a oating pointnumber (e.g.3.3452, 1.2354) between 0.0 and 55.0. Try b oth lines s everal times e ach tosee how randomnumbers ar e chosen.

rand*/

10.rand-

10.0.rand-

/*Running the lines se veral times o ver and o ver is a bit cumbersome, sobelow is a m ethod of

testing random choices u sing the dup m essage, which lls a n array w ithduplicate returns o fthe function.

Test a random array*/

{100.rand}.d$ (20)

/*

Error from not using a function*/

(100.rand).d$ (20)

Page 22: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 22/32

/*It picks a random number, but it uses t hat same number each time. Therst example uses a

random number choice enclosed in a function. A function means " run thisline of code" while100.rand on its ow n means pick a random number. {100.rand} means p icka random numbereach t ime, 100.rand means p ick a r andom number and use it each t ime.

Below is t he same example using a seed on the client side (the languagean programming

side). Run the rst line several times t o see how it lls a n array w ithrandom values. Thenrun the second line several times. Since you are seeding the randomgenerator each time youwill get the same numbers i n the array. Try ch anging the seed to somethingother than 5.You'll get a new series, but the same series e ach time. (Not very "random"after four runs.)

Client random seed*/

{100.rand}.d$ (20)-

this;hread .randSeed , - {100.rand}.d$ (20)-

/*This d oes n ot, however, affect the server, which plays the sounds o nce youdesign a patch.To see d a random process on the server use R andSeed. This uge n can betriggered, so youcan reset the seed over and over. The rst argument is t he trigger, thesecond the seed. Runthe rst and second examples s everal times. Let the third one run. It will

Page 23: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 23/32

reset every 5seconds.

Server random seed

*/

// diEerent e er< ti"e{ SinOsc .ar( 5 oise0 .7r( % 12% 2)."idic s% "$&' 0. )}. &a<

// sa"e e er< ti"e({CandSeed .7r(1% 1 6)-SinOsc .ar( 5 oise0 .7r( % 12% 2)."idic s% "$&' 0. )}. &a<)

// resets e er< seconds({CandSeed .7r( " $&se .7r(1/ )% 1 6)-SinOsc .ar( 5 oise0 .7r( % 12% 2)."idic s% "$&' 0. )}. &a<)

/*

Finally, what if you want the seed to be chosen in the regular way (usingthe clock), but youwant to know what it is s o you can reproduce that series l ater fordebugging or because youliked that version? Here's how.

Post clock se ed*/

this;hread .randSeed , Fate .seed. ost&n- {100.rand}.d$ (20)-

({CandSeed .7r( " $&se .7r(1/ )% Fate .seed. ost&n)-SinOsc .ar( 5 oise0 .7r( % 12% 2)."idic s% "$&' 0. )}. &a<

Page 24: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 24/32

) /*

The seed is t aken from the clock a nd posted to the data window. If youwant to reproduce

that version, replace Date.seed.postln with the number posted to thewindow.

In conclusion, one could argue that a generative composition is n ot reallyrandom, since itcan be duplicated with the same se ed. It seems random because w ehaven’t heard that seriesof numbers b efore. But each seed (billons o f them), represents a specic

repeatable version.So instead of a random process, it can be thought of as a billion possiblevariations, eachidentied by that seed. The code you write then is the DNA for billions ofvariations, ofwhich o ne is cho sen at each p erformance, and ca n be identied a ndrepeated with the seed.

Here are som e other ways to ge nerate random numbers:*/

rand(30) //ret$rns a rando" a&$e ?et een 0 and 2

rand2(20) //ret$rns a rando" a&$e ?et een G20 and 20

rrand(20% 100) //ret$rns a rando" a&$e ?et een 20 and 100

/*

Bells

Upper harmonic s tructure is a “timbre” ngerprint. It helps u s d istinguishwhat the sound is(violin, ute, voice), and even subtle variations b etween instances o f the

Page 25: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 25/32

same soun d (yourmom with a cold). Even though a collection of frequencies a re inharmonic,a "random"collection, we still can easily distinguish it from any other "random" set offrequencies a sillustrated with the bell patches below.

The code below generates a set of bell like sounds, each with it's o wnpseudo-randomspectrum. Try reading the code from the center out. The innermost SinOschas a randomfrequency b etween 50 and 4000. It is m ultiplied by an envelope with a

decay bet ween 0.2and 3.0 and a volume between 0 and 1 . This co de is inside a function withthe dup m essagewith 12 a s an argument. The dup message creates a n a rray of SinOscugens w ith differingfrequencies and envelopes. Since the trigger is created outside thefunction, all the Ugensshare this single trigger. The attacks will be the same and it will appear as

a single sound.Those 12 sine waves, or inharmonic frequencies, are mixed down usingMix.ar and pannedwith Pan2. Try st opping playback a nd running the rst example again.Repeat this f our orve times. Can you tell that some frequencies h ave longer or shorter decayrates?

With each execution the computer chooses a different set of randomnumbers from therandom number generator to create each bell. Could you enter a line ofcode that would use arandom seed and produce the s ame b ell collection twice in a row?

Page 26: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 26/32

Page 27: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 27/32

CPU Usage SC is n ot an unlimited supply of synthesis co mponents. We are limited b y

processingpower. Additive synthesis is costly, and the cpu information in the serverwindow is n ow important.It shows average a nd peak cpu a nd number of Ugens. Run thepatches a bove several times ( without stopping the previous s ound) orincrease the n umber ofpartials t o see what happens when you max out the cpu load.

On my laptop I can get it up to over 5000 ugens and be at only 40%. That'sa far cry from thetwo dozen modules o n vintage gear.

Go back to the single bell patch and increase the number of partials.Watch the CPU increase,but also notice how the sound changes a s yo u add partials. What is t heresult of 200+inharmonic partials?

As the number of random partials increase t he sound becomes m ore a ndmore unfocuseduntil it approaches n oise. That is c overed in the next chapter.

Just a footnote about the last practice examples b elow, diverging,converging, and decayinggongs: I like t hese examples b ecause they so und s o cool, and they a re sodifferent fromanything in real life, but also because they were articially conceived.*/

({ar trigger% $nd-

Page 28: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 28/32

trigger , F$st .7r(3/ )-$nd , rrand(100% 400)-i .ar(

Arra< .>&&(16%{ arg co$nter-

ar artia&-artia& , co$nter + 1-=an2 .ar(

SinOsc .ar( $nd* artia&) *9n :en .7r( 9n .adsr(0% 0% 1.0% .0)%

trigger% 1/ artia&) * "a (0% 5 oise1 .7r(rrand( .0% 12.0)))% 1.0.rand2)

}))*0. //o era&& o&$"e

}. &a<)

//Se era& o the a?o e "i ed do n

({ar trigger% $nd% Hash nst-Hash nst , Arra< .>&&( %{

trigger , F$st .7r(3/ )-$nd , rrand(100% 400)-=an2 .ar(

i .ar(Arra< .>&&(16%{ arg co$nter-ar artia&-artia& , co$nter + 1-

SinOsc .ar( $nd* artia&) *9n :en .7r( 9n .adsr(0% 0% 1.0% .0)%

trigger% 1/ artia&) * "a (0% 5 oise1 .7r(rrand( .0% 12.0)))

}))*0.2%

1.0.rand2)})-i .ar(Hash nst)*0.6}. &a<)

// :agg&e o sines arations

Page 29: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 29/32

({

ar har"onics , 16% $nd , 0% s eeds-s eeds , 1% 2% 3% 4% % 6% % 8% % 10% 11 / -i .>&&(har"onics%

{ arg co$nt-=an2 .ar(5SinOsc .ar(

$nd * (co$nt + 1)%"$&' "a (0%

5SinOsc .7r(s eeds. ra At(co$nt))))%1.0.rand2)

}) / (2*har"onics)

}. &a<-)

// Ise "o$se to change $nda"enta&

({

ar har"onics , 16% $nd% s eeds-s eeds , 1% 2% 3% 4% % 6% % 8% % 10% 11 /20-$nd , ( o$seB .7r(0% 36).ro$nd( ) + 24)."idic s-i .>&&(har"onics%

{ arg co$nt-=an2 .ar(

5SinOsc .ar($nd * (co$nt + 1)%"$&' "a (0% 5SinOsc .7r(s eeds.choose)))%

1.0.rand2)}

) / (2*har"onics)}. &a<-)

({

ar har"onics , 16% $nd-$nd , ( o$seB .7r(0% 36).ro$nd( ) + 24)."idic s-i .>&&(har"onics%

{ arg co$nt-=an2 .ar(

5SinOsc .ar(

Page 30: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 30/32

$nd * (co$nt + 1)%"$&' "a (0% 5SinOsc .7r(rrand(1% 1/3)% "$&'

20).so tc&i ))%1.0.rand2)

}

) / (2*har"onics)}. &a<-)

({

ar har"onics , 16-i .>&&(har"onics%

{ arg co$nt-=an2 .ar(

5SinOsc .ar(

e rand(100% 2000)%"$&' "a (0% 5SinOsc .7r(rrand(1/3%1/6))*rrand(0.1% 0. )))%

1.0.rand2)}

) / (2*har"onics)}. &a<-)

// Fissi ating and con erging gongs i&&$strates ho a atch can ?e ?$i&t// ro" d$ &icating one idea- c&assic additi e s<nthesis. t a&so sho s// ho additi e s<nthesis can ?e $sed to contro& each har"onic.// isten in stereo to hear the har"onics di erge.

(

{ar d$r , 6% ?ase% aen % en % o$t% trig-?ase , Cand (40% 100)-trig , SinOsc .ar(1/10)-o$t , i .>&&(1 %{

ar thisF$r-thisF$r , d$r * rrand(0. % 1.0)-aen , 9n :en .7r( 9n . erc(0% thisF$r)% trig)-en , 9n :en .7r( 9n .ne ( 0% 0% 1% 0 % 0.2 *thisF$r%

0. *thisF$r% 0 )% trig)-=an2 .ar( SinOsc .ar( Cand (?ase% ?ase * 12) *

5 oise1 .7r(10% "$&' 0.02 * en % add' 1)% // re

Page 31: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 31/32

"$&' aen // a")% ( 1% G1 .choose) * en )

}) * 0.0 -o$t}. &a<(s)-

{ar d$r , 6% ?ase% aen % en % o$t% trig% det$ne-?ase , Cand (40% 60)-det$ne , 0.1- // increase this n$"?er to det$ne the second ?e&&trig , SinOsc .ar(1/10% i)-o$t , i .>&&(1 %{ arg co$nt-

ar thisF$r-thisF$r , d$r * rrand(0. % 1.0)-

aen , 9n :en .7r( 9n . erc(0% thisF$r)% trig)-en , 9n :en .7r( 9n .ne ( 1% 1% 0% 1 % 0.0 *thisF$r%0. *thisF$r% 0 )% trig)-

=an2 .ar( SinOsc .ar( ?ase*(co$nt+1+ det$ne.rand) *5 oise1 .7r(10% "$&' 0.02 * en % add' 1)% // re"$&' aen // a"

)% ( 1% G1 .choose) * en )}) * 0.0 -o$t}. &a<(s)-)

// Feca<ing ?e&&

({ar aen % en % o$t% trig% d$r% ?ase-d$r , rrand(1.0% 6.0)-?ase , e rand(100% 1000)-trig , " $&se .7r(1/6)-o$t , i .ar(

Arra< .>&&(1 %{arg co$nt-ar thisF$r-thisF$r , d$r * rrand(0. % 1.0)-aen , 9n :en .7r(

9n .ne ( 0% 1% 0.4% 1% 0 % 0% 0. % 0. % 0 )% trig%ti"eSca&e' thisF$r)-

en , 9n :en .7r(9n .ne ( 0% 0% 0. % 0. % 0 % 0.2 % 0. % 0.2 % 0 )%

Page 32: Tutorial 8addSynthRandom (1)

8/9/2019 Tutorial 8addSynthRandom (1)

http://slidepdf.com/reader/full/tutorial-8addsynthrandom-1 32/32

trig% ti"eSca&e' thisF$r)-=an2 .ar( SinOsc .ar( Cand (?ase% ?ase * 12) *

5 oise1 .7r(10% "$&' 0.1 * en % add' 1)% // re"$&' aen // a"

)% ( 1% G1 .choose) * en )

})) * 9n :en .7r( 9n .&inen(0% d$r% 0)%" $&se .7r(6)% ti"eSca&e' d$r%&e e&Sca&e' 0.0 % doneAction' 2)-

o$t*0.3-}. &a<-)