I often found myself writing a like-Ptime
-but-doesn’t-add-pause-time idiom.
Basically the problem I had with Ptime is that it “keeps running” even when a stream is paused, e.g.
Pdef(\tt, Ptrace(Pbind(\trt, Ptime()))
fork { Pdef(\tt).play; 3.wait; Pdef(\tt).pause; 3.wait; Pdef(\tt).resume; }
You get something like
( 'trt': 0.0 )
( 'trt': 1.0 )
( 'trt': 2.0 )
( 'trt': 6.0 )
( 'trt': 7.0 )
I.e. Ptime
“counts” the stream while is paused too.
The basic idea is, of course, to just add the time played. Ignoring variable duration (for now), i.e. just “counting events” is something like
Pdef(\tt, Ptrace(Plazy({var td = 0; Pbind(\trt, Pfuncn({td = td + 1}, inf))}))
As it turns out, there’s an “classless” pattern that does this kind of summing already, Pattern.integrate
:
Pdef(\tt, Ptrace(Pbind(\trt, Pseq([1], inf).integrate)));
And to add the actual durations (instead of just counting each event as 1):
Pdef(\tt, Ptrace(Pbind(\trt, Pkey(\dur).integrate)))
Interestingly, a google search for Pkey(\dur).integrate
didn’t return any real hits, so I’ve decided to post this “discovery”.