| 1 |
<CsoundSynthesizer> |
| 2 |
<CsOptions> |
| 3 |
-n |
| 4 |
</CsOptions> |
| 5 |
<CsInstruments> |
| 6 |
sr = 44100 |
| 7 |
ksmps = 32 |
| 8 |
nchnls = 1 |
| 9 |
0dbfs = 1 |
| 10 |
|
| 11 |
opcode KS, 0, ii |
| 12 |
;performs the karplus-strong algorithm |
| 13 |
iTab, iTbSiz xin |
| 14 |
;calculate the mean of the last two values |
| 15 |
iUlt tab\_i iTbSiz-1, iTab |
| 16 |
iPenUlt tab\_i iTbSiz-2, iTab |
| 17 |
iNewVal = (iUlt + iPenUlt) / 2 |
| 18 |
;shift values one position to the right |
| 19 |
indx = iTbSiz-2 |
| 20 |
loop: |
| 21 |
iVal tab\_i indx, iTab |
| 22 |
tabw\_i iVal, indx+1, iTab |
| 23 |
loop\_ge indx, 1, 0, loop |
| 24 |
;fill the new value at the beginning of the table |
| 25 |
tabw\_i iNewVal, 0, iTab |
| 26 |
endop |
| 27 |
|
| 28 |
opcode PrintTab, 0, iiS |
| 29 |
;prints table content, with a starting string |
| 30 |
iTab, iTbSiz, Sin xin |
| 31 |
indx = 0 |
| 32 |
Sout strcpy Sin |
| 33 |
loop: |
| 34 |
iVal tab_i indx, iTab |
| 35 |
Snew sprintf "%8.3f", iVal |
| 36 |
Sout strcat Sout, Snew |
| 37 |
loop\_lt indx, 1, iTbSiz, loop |
| 38 |
puts Sout, 1 |
| 39 |
endop |
| 40 |
|
| 41 |
instr ShowBuffer |
| 42 |
;fill the function table |
| 43 |
iTab ftgen 0, 0, -5, -2, 1, -1, 1, 1, -1 |
| 44 |
iTbLen tableng iTab |
| 45 |
;loop cycles (five states) |
| 46 |
iCycle = 0 |
| 47 |
cycle: |
| 48 |
Scycle sprintf "Cycle %d:", iCycle |
| 49 |
PrintTab iTab, iTbLen, Scycle |
| 50 |
;loop states |
| 51 |
iState = 0 |
| 52 |
state: |
| 53 |
KS iTab, iTbLen |
| 54 |
loop\_lt iState, 1, iTbLen, state |
| 55 |
loop\_lt iCycle, 1, 10, cycle |
| 56 |
endin |
| 57 |
|
| 58 |
</CsInstruments> |
| 59 |
<CsScore> |
| 60 |
i "ShowBuffer" 0 1 |
| 61 |
</CsScore> |
| 62 |
</CsoundSynthesizer> |
| 63 |
;example by joachim heintz |