Looping playback works with a basic two-part phrase

main
Mike Lynch 2023-12-27 15:32:04 +11:00
parent 71fd9e319a
commit 56fae7e54d
1 changed files with 24 additions and 9 deletions

View File

@ -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;
}
}