Re-Sync MIDI Event

But, you don’t want to trigger only the one event, do you? You would like \quavers to continue on its own after that point, right?

A few things are still not clear to me about your use case. I can think of a number of things that could happen when you hit the MIDI trigger:

  1. The pattern might continue out of sync until the next barline, and then reset to the beginning of the pattern on that barline.

  2. Or, it might stop playing out of sync notes until the next barline (rest until the barline), and then start from the beginning of the pattern on that barline.

  3. Or, it might immediately determine the next note that would play if it were in sync, and begin playing from that point in the pattern at the right time.

I have no idea which of these you want.

1 or 2 are fairly straightforward with a rescheduling function – it’s fairly easy to transfer control to a different EventStreamPlayer at any time you like. Because Pdef manages the stream player internally, it would probably have to be a new method – but it’s not that hard. It just depends on the idea that a stream player is disposable – the stream carries the pattern’s state, not the stream player, so there is nothing precious or valuable about the player. Just toss it and schedule a new one.

+ EventPatternProxy {
	reschedule { |beats, doReset = false, clock|
		var stream;
		if(this.isPlaying.not) {
			this.play(clock, doReset, beats)
		} {
			stream = player.originalStream;
			player.stop;
			// omit 'player.event' for a TaskProxy equivalent
			player = player.class.new(stream, player.event)
			.play(clock ?? { player.clock }, doReset, beats);
		}
	}
}
// rescheduling demo
Pdef(\x, Pbind(\degree, Pn(Pseries(0, 1, 4), inf), \dur, 1)).play;

// oops, didn't 'quant' to barline
// so, reschedule for the next bar and reset
Pdef(\x).reschedule(Pdef(\x).clock.nextBar, doReset: true);

// or you can do something odd like skip one beat
Pdef(\x).reschedule(Pdef(\x).player.nextBeat + 1);

Pdef(\x).stop;

3 is more difficult because you need to be able to scan the pattern’s contents to find the next note to play. Pdef does have an outset method which (I think) is supposed to do something like that, but I haven’t tried it.

The technique to use might be different depending on the behavior you want – so, at this point, I would suggest that flailing around with the code has not been a wholly successful strategy so far, and it would be better to clarify the desired behavior.

hjh