Read chnget when phasor resets to zero

Hi everyone,

I need a way to read a value from chnget only when a phasor resets to zero. Specifically, here’s the snippet of code I’m working with:

kloop_start = chnget:k("loop_start")
kloop_length = chnget:k("loop_length")
atime phasor (sr*ktimescale)/(kloop_length)
atime = atime * kloop_length/sr + kloop_start/sr 
asigl  mincer atime, kamp, kpitch, 1, ilock, 2048, 10

Currently the kloop_start and kloop_length values are read at every k-frame. What I’d like is that they are only read every time the phasor resets to zero. Ideally, the phasor would immediately incorporate these new values in its vector, even if it’s in the middle of the vector. In other words, I’d like this to be sample-accurate if possible, though I’m not sure if that’s possible in anything other than ksmps = 1.

I see there’s the syncphasor opcode which emits an a-rate pulse when the phasor resets. Maybe that’s the answer, but I’m not sure how I could incorporate that into what I need. Maybe some of you Csound gurus can give me some guidance?


I wrote an example here:

with relevant code for a custom phasor implementation here:

  aphs init 0
  kphs init 0
  kphsperiod init .25 * sr ;; start with .25 seconds for phs
  kndx = 0
  while (kndx < ksmps) do
    aphs[kndx] = kphs / kphsperiod
    kphs += 1 
    if (kphs > kphsperiod) then
      kphs -= kphsperiod
      kphsperiod = int(random:k(1, 4)) * .25 * sr
    kndx += 1 

This is generating the a-rate phasor with a ksmps-loop. When the phs is greater than the phs period it resets and changes to a new randomized phase period. I think you could use this as a basis to work with the loop_start/loop_length values to ensure tempo changes to the cycle only happen at the reset boundary and that it can happen mid-buffer.

Thank you so much, Steven! I’m so glad I asked and you responded because now I understand how to get this done at a more fundamental level. I was able to use your code as the basis for my looper, and it’s working beautifully. This opens up a lot of possibilities. Thanks again!

1 Like