First attempt at looping, it's broken, see "Arduino mistake" in Reaper

for what this sounds like
main
Mike Lynch 2023-12-27 12:33:04 +11:00
parent bd9940b96b
commit f4d75a627e
1 changed files with 16 additions and 10 deletions

View File

@ -35,10 +35,10 @@ the absolute start time should include a count-in
int m1[] = { 0, 2, 4, 5, 7, 9, 11, 12 }; int m1[] = { 0, 2, 4, 5, 7, 9, 11, 12 };
int s1[] = { 3, 5, 7, 9, 11, 13, 15, 17 }; int s1[] = { 0, 3, 5, 7, 9, 11, 13, 15 };
float d1[] = { 1.0, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5 }; float d1[] = { 1.0, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5 };
int m2[] = { 0, 3, 6, 9, 1, 4, 7, 11, 2, 5, 8, 13, 1, 4, 7, 11 }; int m2[] = { 0, 4, 7, 12, 0, 4, 7, 12, 0, 4, 7, 12, 0, 4, 7, 12 };
int s2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; int s2[] = { 0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
float d2[] = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 }; float d2[] = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
@ -46,6 +46,14 @@ float gate = 0.5;
int bpm = 180; int bpm = 180;
float beat_s = 60.0 / (float)bpm; float beat_s = 60.0 / (float)bpm;
int beat_m = round(1000.0 * beat_s); int beat_m = round(1000.0 * beat_s);
// count_in = number of beats since t0 to wait before we startt playing
// repeat = number of beats to repeat - could be computed from the sequence data
long count_in_m = 4 * beat_m;
int repeat = 16;
float voltrange = 5.0; float voltrange = 5.0;
float octave = 4096.0 / voltrange; // number of DAC steps in an octave float octave = 4096.0 / voltrange; // number of DAC steps in an octave
float mod_b = 3.141592653589793 / 4.0; float mod_b = 3.141592653589793 / 4.0;
@ -67,7 +75,6 @@ typedef struct {
sequencer seq1, seq2; sequencer seq1, seq2;
long start_time; long start_time;
int repeat = 8;
void setup(void) { void setup(void) {
Serial.begin(115200); Serial.begin(115200);
@ -110,7 +117,8 @@ void setup(void) {
void loop() { void loop() {
long now = millis(); long now = millis();
float beat = (float)(now - start_time) / (float)beat_m; long beat_int = (now - start_time - count_in_m) % (beat_m * repeat);
float beat = (float)(beat_int) / (float)beat_m;
runSequencer(seq1, now, beat); runSequencer(seq1, now, beat);
runSequencer(seq2, now, beat); runSequencer(seq2, now, beat);
} }
@ -119,21 +127,19 @@ void runSequencer(sequencer& seq, long now, float beat) {
if( seq.s < seq.len - 1 ) { if( seq.s < seq.len - 1 ) {
if( beat > (float)seq.start[seq.s + 1] ) { if( beat > (float)seq.start[seq.s + 1] ) {
seq.s += 1; seq.s += 1;
Serial.print("seq / note ");
Serial.print(seq.s);
Serial.print(" / ");
Serial.println(seq.melody[seq.s]);
seq.rel = now + round((float)beat_m * seq.duration[seq.s]); seq.rel = now + round((float)beat_m * seq.duration[seq.s]);
if( seq.melody[seq.s] > -1 ) { if( seq.melody[seq.s] > -1 ) {
mcp.setChannelValue(seq.pitch, tuning[seq.melody[seq.s]]); mcp.setChannelValue(seq.pitch, tuning[seq.melody[seq.s]]);
mcp.setChannelValue(seq.gate, 4095); mcp.setChannelValue(seq.gate, 4095);
} }
seq.playing = 1; seq.playing = 1;
if( seq.s == seq.len - 1 ) {
seq.s = -1;
}
} }
} }
if( seq.playing ) { if( seq.playing ) {
if( now > seq.rel ) { if( now > seq.rel ) {
Serial.println("release");
mcp.setChannelValue(seq.gate, 0); mcp.setChannelValue(seq.gate, 0);
seq.playing = 0; seq.playing = 0;
} }