09.12.2012 Views

The Kyma Language for Sound Design, Version 4.5

The Kyma Language for Sound Design, Version 4.5

The Kyma Language for Sound Design, Version 4.5

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Random Events<br />

<strong>The</strong> next example generates one hundred random pitches with random durations and random placement<br />

in the stereo field. Edit the <strong>Sound</strong> called 100 uni<strong>for</strong>m random to follow along.<br />

To generate one hundred of these events, we first create a random number generator, and then repeat the<br />

code <strong>for</strong> the generating single event one hundred times:<br />

| r t |<br />

r := Random newFor<strong>Kyma</strong>WithSeed: 52.<br />

t := 0.<br />

100 timesRepeat: [<br />

self<br />

keyDownAt: t s<br />

duration: (r next + 1) s<br />

frequency: 3 c + (r next * 36 nn)<br />

velocity: r next.<br />

t := t + r next].<br />

You may recognize this as a Smalltalk program (the implication being that you can use any Smalltalk expressions<br />

and control structures to algorithmically generate your MIDI events)!<br />

Local variables are declared at the top by placing them between vertical lines. <strong>The</strong>n a random number<br />

generator is created and stored in the variable r. Another variable, t, which will be keeping track of time,<br />

is initially set to zero:<br />

| r t |<br />

r := Random newFor<strong>Kyma</strong>WithSeed: 52.<br />

t := 0.<br />

Next we repeat the statement within the square brackets one hundred times. Each time through, the duration<br />

is set to some random number between 1 and 2, the frequency is set to a pitch between 3 c and 6 c<br />

(not limited to integer note numbers), the velocity is set to a random number between 0 and 1, and the<br />

start time of the next event is set to a random time up to 1 second after this event:<br />

100 timesRepeat: [<br />

self<br />

keyDownAt: t s<br />

duration: (r next + 1) s<br />

frequency: 3 c + (r next * 36 nn)<br />

velocity: r next.<br />

t := t + r next].<br />

To hear the results, play 100 uni<strong>for</strong>m random. Take a look inside the Attenuator to see how<br />

!KeyVelocity is really being used to control stereo placement, rather than amplitude.<br />

Random generates random numbers that are evenly distributed between zero and one. Let’s modify our<br />

program slightly, replacing Random as the random number generator with several OneOverF generators,<br />

one <strong>for</strong> pitch, one <strong>for</strong> duration, one <strong>for</strong> velocity, and one <strong>for</strong> the next start time:<br />

| rPch rDur rVel rStart t |<br />

rPch := OneOverF newFor<strong>Kyma</strong>WithSeed: 52 states: 128.<br />

rDur := OneOverF newFor<strong>Kyma</strong>WithSeed: 52 states: 128.<br />

rVel := OneOverF newFor<strong>Kyma</strong>WithSeed: 52 states: 128.<br />

rStart := OneOverF newFor<strong>Kyma</strong>WithSeed: 52 states: 128.<br />

t := 0.<br />

200 timesRepeat: [<br />

self<br />

keyDownAt: t s<br />

duration: rDur next s<br />

frequency: 3 c + ((rPch next * 36) roundTo: 1) nn<br />

velocity: rVel next.<br />

t := t + (rStart next * 0.5)].<br />

138

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!