| 1 |
<CsoundSynthesizer> |
| 2 |
<CsOptions> |
| 3 |
-m128 -odac |
| 4 |
</CsOptions> |
| 5 |
<CsInstruments> |
| 6 |
sr = 44100 |
| 7 |
ksmps = 32 |
| 8 |
0dbfs = 1 |
| 9 |
nchnls = 2 |
| 10 |
seed 1 ;change to zero for always changing results |
| 11 |
|
| 12 |
;****SETTINGS FOR PITCHES**** |
| 13 |
;define the pitch street in octave notation |
| 14 |
giLowestPitch = 7 |
| 15 |
giHighestPitch = 9 |
| 16 |
;set pitch startpoint, deviation range and the first direction |
| 17 |
giStartPitch = 8 |
| 18 |
gkPitchDev init 0.2 ;random range for next pitch |
| 19 |
gkPitchDir init 0.1 ;positive = upwards |
| 20 |
|
| 21 |
;****SETTINGS FOR DENSITY**** |
| 22 |
;define the maximum and minimum density (notes per second) |
| 23 |
giLowestDens = 1 |
| 24 |
giHighestDens = 8 |
| 25 |
;set first density |
| 26 |
giStartDens = 3 |
| 27 |
;set possible deviation in range 0..1 |
| 28 |
;0 = no deviation at all |
| 29 |
;1 = possible deviation is between half and twice the current density |
| 30 |
gkDensDev init 0.5 |
| 31 |
;set direction in the same range 0..1 |
| 32 |
;(positive = more dense, shorter notes) |
| 33 |
gkDensDir init 0.1 |
| 34 |
|
| 35 |
;****INSTRUMENT FOR RANDOM WALK**** |
| 36 |
instr walk |
| 37 |
;set initial values |
| 38 |
kPitch init giStartPitch |
| 39 |
kDens init giStartDens |
| 40 |
;trigger impulses according to density |
| 41 |
kTrig metro kDens |
| 42 |
;if the metro ticks |
| 43 |
if kTrig == 1 then |
| 44 |
;1) play current note |
| 45 |
event "i", "play", 0, 1.5/kDens, kPitch |
| 46 |
;2) calculate next pitch |
| 47 |
;define boundaries according to direction |
| 48 |
kLowPchBound = gkPitchDir < 0 ? -gkPitchDev+gkPitchDir : -gkPitchDev |
| 49 |
kHighPchBound = gkPitchDir > 0 ? gkPitchDev+gkPitchDir : gkPitchDev |
| 50 |
;get random value in these boundaries |
| 51 |
kPchRnd random kLowPchBound, kHighPchBound |
| 52 |
;add to current pitch |
| 53 |
kPitch += kPchRnd |
| 54 |
;change direction if maxima are crossed, and report |
| 55 |
if kPitch > giHighestPitch && gkPitchDir > 0 then |
| 56 |
gkPitchDir = -gkPitchDir |
| 57 |
printks " Pitch touched maximum - now moving down.\n", 0 |
| 58 |
elseif kPitch < giLowestPitch && gkPitchDir < 0 then |
| 59 |
gkPitchDir = -gkPitchDir |
| 60 |
printks "Pitch touched minimum - now moving up.\n", 0 |
| 61 |
endif |
| 62 |
;3) calculate next density (= metro frequency) |
| 63 |
;define boundaries according to direction |
| 64 |
kLowDensBound = gkDensDir < 0 ? -gkDensDev+gkDensDir : -gkDensDev |
| 65 |
kHighDensBound = gkDensDir > 0 ? gkDensDev+gkDensDir : gkDensDev |
| 66 |
;get random value in these boundaries |
| 67 |
kDensRnd random kLowDensBound, kHighDensBound |
| 68 |
;get multiplier (so that kDensRnd=1 yields to 2, and kDens=-1 to 1/2) |
| 69 |
kDensMult = 2 ^ kDensRnd |
| 70 |
;multiply with current duration |
| 71 |
kDens *= kDensMult |
| 72 |
;avoid too high values and too low values |
| 73 |
kDens = kDens > giHighestDens*1.5 ? giHighestDens*1.5 : kDens |
| 74 |
kDens = kDens < giLowestDens/1.5 ? giLowestDens/1.5 : kDens |
| 75 |
;change direction if maxima are crossed |
| 76 |
if (kDens > giHighestDens && gkDensDir > 0) || |
| 77 |
(kDens < giLowestDens && gkDensDir < 0) then |
| 78 |
gkDensDir = -gkDensDir |
| 79 |
if kDens > giHighestDens then |
| 80 |
printks " Density touched upper border - now becoming less dense.\n", 0 |
| 81 |
else |
| 82 |
printks " Density touched lower border - now becoming more dense.\n", 0 |
| 83 |
endif |
| 84 |
endif |
| 85 |
endif |
| 86 |
endin |
| 87 |
|
| 88 |
;****INSTRUMENT TO PLAY ONE NOTE**** |
| 89 |
instr play |
| 90 |
;get note as octave and calculate frequency and panning |
| 91 |
iOct = p4 |
| 92 |
iFreq = cpsoct(iOct) |
| 93 |
iPan ntrpol 0, 1, iOct, giLowestPitch, giHighestPitch |
| 94 |
;calculate mode filter quality according to duration |
| 95 |
iQ ntrpol 10, 400, p3, .15, 1.5 |
| 96 |
;generate tone and throw out |
| 97 |
aImp mpulse 1, p3 |
| 98 |
aMode mode aImp, iFreq, iQ |
| 99 |
aOut linen aMode, 0, p3, p3/4 |
| 100 |
aL, aR pan2 aOut, iPan |
| 101 |
outs aL, aR |
| 102 |
endin |
| 103 |
|
| 104 |
</CsInstruments> |
| 105 |
<CsScore> |
| 106 |
i "walk" 0 999 |
| 107 |
</CsScore> |
| 108 |
</CsoundSynthesizer> |
| 109 |
;example by joachim heintz |