The instrument I posted isn’t really meant for looping a single clip f audio over and over again. But fear not, I have some code that might help with that too. Here is a basic sample that I give to my student for them to hack. Just swap out the soundfile given, for the one you want to play. Hitting the “sample Me!” button will start recording, while releasing the button will cause the sample to play. I think between this and the last one you might be able to get something going.
<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue") pluginId("def1")
button bounds(24, 18, 80, 40) channel("sample"), text("Sample Me!"), latched(0)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
; Initialize the global variables.
; Initialize the global variables.
ksmps = 32
nchnls = 2
0dbfs = 1
;create table with sapce for 11 seconds..
giSample ftgen 1, 0, 524288, 7, 0, 524288, 0
;===============================================
;simple sampler UDO with ksmps set to 1
;===============================================
opcode Sampler, a, aki
setksmps 1
aSig,kTrig, iFn xin
kWriteIndex init 0
aOut init 0
kCnt init 0
if kTrig == 1 then
tablew aSig, a(kWriteIndex), iFn
kWriteIndex = kWriteIndex<ftlen(iFn) ? kWriteIndex+1 : ftlen(iFn)-1
kCnt += 1
kReadIndex = 0
else
aOut tab a(kReadIndex), iFn
;you should probably test that kCnt isn't greater than the table size..
kReadIndex = (kReadIndex<kCnt) ? kReadIndex+1 : 0
endif
xout aOut
endop
;===============================================
;instrument that calls the Sampler UDO
;===============================================
instr 1
kTrig chnget "sample"
a1, a2 diskin2 "120BPMLoop.wav", 1, 0, 1
aOutput Sampler a1, kTrig, 1
printk2 kTrig
outs aOutput, aOutput
endin
</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
f0 z
;starts instrument 1 and runs it for a week
i1 0 [60*60*24*7]
</CsScore>
</CsoundSynthesizer>