Syncing MIDI note on with variable rate PlayBuf loop

I might be missing some super obvious way, but i’m stuck trying to solve this.

i have a function that contains a looping PlayBuf that then gets passed onto some effects for further fun – quite straightforward:

u = { x = PlayBuf.ar(1, b, SinOsc.ar(1).range(0.524, 1.63), loop:1); x = x + (((FX))) }.play(s);

i am looking for a way to send a MIDI note on event (out of SC) every time the loop starts, which would be easy if the duration of the soundfile was constant, but since its playback rate is being modulated, it’s constantly drifting.

I bet i’m overlooking some easy way out…
thanks!

This is easy if you have access to the playback head position, which PlayBuf doesn’t give you…

… but Phasor does. Here, rate can be whatever modulator you need.

// maybe got these arguments wrong
var phase = Phasor.ar(0, rate * BufRateScale.kr(bufnum), 0, BufFrames.kr(bufnum), 0);
var loopTrig = HPZ1.ar(phase) < 0;
var sig = BufRd.ar(1, bufnum, phase);

SendReply.ar(loopTrig, '/looped');

And see OSCFunc / OSCdef to receive the “looped” message.

Btw use triple backticks to enclose code blocks on the forum:

```
code here
```

hjh

Sounds like your use case doesn’t involve minutes-long buffers, but note that when you use BufRd you are limited in how far into your buffer you can access before your audio is degraded.

From the BufRd helpfile:
WARNING: The phase argument only offers precision for addressing 2**24 samples (about 6.3 minutes at 44100Hz).

(if you need to use longer buffers with BufRd, check out https://github.com/esluyter/super-bufrd – helpfiles still a work in progress… for right now see ‘SuperUGens Overview’ helpfile)