-n
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1
opcode KS, 0, ii
;performs the karplus-strong algorithm
iTab, iTbSiz xin
;calculate the mean of the last two values
iUlt tab_i iTbSiz-1, iTab
iPenUlt tab_i iTbSiz-2, iTab
iNewVal = (iUlt + iPenUlt) / 2
;shift values one position to the right
indx = iTbSiz-2
loop:
iVal tab_i indx, iTab
tabw_i iVal, indx+1, iTab
loop_ge indx, 1, 0, loop
;fill the new value at the beginning of the table
tabw_i iNewVal, 0, iTab
endop
opcode PrintTab, 0, iiS
;prints table content, with a starting string
iTab, iTbSiz, Sout xin
indx = 0
loop:
iVal tab_i indx, iTab
Snew sprintf "%8.3f", iVal
Sout strcat Sout, Snew
loop_lt indx, 1, iTbSiz, loop
puts Sout, 1
endop
instr ShowBuffer
;fill the function table
iTab ftgen 0, 0, -5, -2, 1, -1, 1, 1, -1
iTbLen tableng iTab
;loop cycles (five states)
iCycle = 0
cycle:
Scycle sprintf "Cycle %d:", iCycle
PrintTab iTab, iTbLen, Scycle
;loop states
iState = 0
state:
KS iTab, iTbLen
loop_lt iState, 1, iTbLen, state
loop_lt iCycle, 1, 10, cycle
endin
i "ShowBuffer" 0 1