Any of the table reading/writing opcodes can do this. Each year I get my students to develop a plugin like this as part of their continual assessment. I give them this code to start off:
; Initialize the global variables.
ksmps = 32
nchnls = 2
0dbfs = 1
;create table with space 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
kIndex init 0
aOut init 0
if kTrig == 1 then
tablew aSig, a(kIndex), iFn
kIndex = kIndex<ftlen(iFn) ? kIndex+1 : ftlen(iFn)-1
else
aOut tab a(kIndex), iFn
kIndex = kIndex<ftlen(iFn) ? kIndex+1 : 0
endif
xout aOut
endop
;===============================================
;instrument that calls the Sampler UDO
;===============================================
instr 1
kTime line 0, 1000, 1000
kTrig = (kTime > 5 && kTime < 10 ? 1 : 0)
a1, a2 diskin2 "pianoMood.wav", 1, 0, 1
aOutput Sampler a1, kTrig, 1
outs aOutput, aOutput
endin
I’ll leave it with for to see if you can work out how to go about modifying it to your needs. For what it’s worth, this is what I ask of my students once I give them this code:
-
the user should be able to start and stop recording of a sample in real time
-
the recorded sample should start playing back as soon as the recording stops, and should only play for the length of the recording
-
the user should be able to control the playback speed and direction of the recorded sample
-
include a ‘random’ frequency/speed function
Let me know how you get on!