| 1 |
<CsoundSynthesizer> |
| 2 |
<CsOptions> |
| 3 |
-odac |
| 4 |
</CsOptions> |
| 5 |
<CsInstruments> |
| 6 |
sr = 44100 |
| 7 |
ksmps = 10 |
| 8 |
nchnls = 1 |
| 9 |
0dbfs = 1 |
| 10 |
|
| 11 |
; random number generator to a given density function |
| 12 |
; kout random number; k_minimum,k_maximum,i_fn for a density function |
| 13 |
|
| 14 |
opcode rand_density, k, kki |
| 15 |
kmin,kmax,ifn xin |
| 16 |
loop: |
| 17 |
krnd1 random 0,1 |
| 18 |
krnd2 random 0,1 |
| 19 |
k2 table krnd1,ifn,1 |
| 20 |
if krnd2 > k2 kgoto loop |
| 21 |
xout kmin+krnd1*(kmax-kmin) |
| 22 |
endop |
| 23 |
|
| 24 |
; random number generator to a given probability function |
| 25 |
; kout random number |
| 26 |
; in: i_nr number of possible values |
| 27 |
; i_fn1 function for random values |
| 28 |
; i_fn2 probability functionExponential: Generate a uniformly distributed |
| 29 |
; number between 0 and 1 and take its natural logarithm. |
| 30 |
|
| 31 |
opcode rand_probability, k, iii |
| 32 |
inr,ifn1,ifn2 xin |
| 33 |
loop: |
| 34 |
krnd1 random 0,inr |
| 35 |
krnd2 random 0,1 |
| 36 |
k2 table int(krnd1),ifn2,0 |
| 37 |
if krnd2 > k2 kgoto loop |
| 38 |
kout table krnd1,ifn1,0 |
| 39 |
xout kout |
| 40 |
endop |
| 41 |
|
| 42 |
instr 1 |
| 43 |
krnd rand_density 400,800,2 |
| 44 |
aout poscil .1,krnd,1 |
| 45 |
out aout |
| 46 |
endin |
| 47 |
|
| 48 |
instr 2 |
| 49 |
krnd rand_probability p4,p5,p6 |
| 50 |
aout poscil .1,krnd,1 |
| 51 |
out aout |
| 52 |
endin |
| 53 |
|
| 54 |
</CsInstruments> |
| 55 |
<CsScore> |
| 56 |
;sine |
| 57 |
f1 0 32768 10 1 |
| 58 |
;density function |
| 59 |
f2 0 1024 6 1 112 0 800 0 112 1 |
| 60 |
;random values and their relative probability (two dice) |
| 61 |
f3 0 16 -2 2 3 4 5 6 7 8 9 10 11 12 |
| 62 |
f4 0 16 2 1 2 3 4 5 6 5 4 3 2 1 |
| 63 |
;random values and their relative probability |
| 64 |
f5 0 8 -2 400 500 600 800 |
| 65 |
f6 0 8 2 .3 .8 .3 .1 |
| 66 |
|
| 67 |
i1 0 10 |
| 68 |
i2 0 10 4 5 6 |
| 69 |
</CsScore> |
| 70 |
</CsoundSynthesizer> |
| 71 |
;example by martin neukom |