Cabbage Logo
Back to Cabbage Site

Having a difficult time understanding why this isn't just playing back audio at half speed, but also doing some really weird skipping

So this reads audio to a buffer and then reads from the table at half speed. But it’s not seemless, there’s some skipping happening and I can not wrap my head around why.

I’m using a 16 second sample at 120 bpm and a samplerate of 44100 in my tests.

half time.csd (2.9 KB)

Yo holy shit this is basically half time. So when the buffer is 88200 at a sample rate of 44100 at 120 bpm the 3rd and 4th beat gets skipped and only the first and second gets played back. I don’t understand why though??

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. :+1:

form caption("Untitled") size(400, 300), guiMode("queue") pluginId("def1")
button bounds(24, 18, 80, 40) channel("sample"), text("Sample Me!"), latched(0)
-n -d
; 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
    aOut tab a(kReadIndex), iFn
    ;you should probably test that kCnt isn't greater than the table size..
    kReadIndex = (kReadIndex<kCnt) ? kReadIndex+1 : 0

xout aOut

;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

;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] 

Thank you, I will give that a go too! I was actually trying to recreate half time from cableguys hahaha, I just didn’t expect it to be so easy! But I understand now that playing the audio back at half speed means it is equal to the full size of the buffer, thus only half of the table can be played back before the buffer resets. Very cool! Thanks very much for the help.

1 Like