From 56fae7e54d43ef76885ba9e496862cd9b702c92a Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Wed, 27 Dec 2023 15:32:04 +1100 Subject: [PATCH] Looping playback works with a basic two-part phrase --- Xmas/Xmas.ino | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/Xmas/Xmas.ino b/Xmas/Xmas.ino index 0799ee9..2ecb4a3 100644 --- a/Xmas/Xmas.ino +++ b/Xmas/Xmas.ino @@ -62,7 +62,8 @@ float mod_b = 3.141592653589793 / 4.0; typedef struct { int s = -1; - int playing = 0; + bool playing = false; + bool looped = false; long play = 0; long rel = 0; long last = 0; @@ -142,13 +143,33 @@ void loop() { void runSequencer(sequencer& seq, long now, long beats) { int next = seq.s + 1; int start; + + if( seq.playing ) { + if( now > seq.rel ) { + mcp.setChannelValue(seq.gate, 0); + seq.playing = false; + } + } + if( next > seq.len - 1 ) { seq.s = -1; - next = 0; + seq.looped = true; return; // don't start the loop yet } else { start = seq.start[next]; } + + if( seq.looped ) { // reached the end and waiting for the start of the bar + if( beats < seq.last ) { + seq.looped = false; // bar has restarted, start waiting + seq.s = -1; + next = 0; + } else { + return; + } + } + seq.last = beats; + if( beats >= start ) { seq.s = next; seq.rel = now + seq.duration[seq.s]; @@ -156,13 +177,7 @@ void runSequencer(sequencer& seq, long now, long beats) { mcp.setChannelValue(seq.pitch, tuning[seq.melody[seq.s]]); mcp.setChannelValue(seq.gate, 4095); } - seq.playing = 1; - } - if( seq.playing ) { - if( now > seq.rel ) { - mcp.setChannelValue(seq.gate, 0); - seq.playing = 0; - } + seq.playing = true; } }