2023-12-27 05:58:09 +00:00
|
|
|
// We Wish You a Merry Christmas
|
2023-12-22 06:45:33 +00:00
|
|
|
#include <Adafruit_MCP4728.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
|
|
|
|
Adafruit_MCP4728 mcp;
|
|
|
|
|
|
|
|
|
2023-12-23 04:57:37 +00:00
|
|
|
float tuning[16];
|
|
|
|
// 0 = A, 8 = F (tonic)
|
|
|
|
|
2023-12-27 05:54:58 +00:00
|
|
|
int m1[] = { 3, 8, 8, 10, 8, 7, 5, 5, 5, 10, 10, 12, 10, 8, 7, 3, 7, 12, 12, 13, 12, 10, 8, 5, 3, 3, 5, 10, 7, 8, 3, 8, 8, 8, 7, 7, 8, 7, 5, 3, 10, 12, 10, 10, 8, 8, 15, 3, 3, 3, 5, 10, 7, 8 };
|
|
|
|
int s1[] = { 0, 2, 4, 5, 6, 7, 8, 10, 12, 14, 16, 17, 18, 19, 20, 22, 24, 26, 28, 29, 30, 31, 32, 34, 36, 37, 38, 40, 42, 44, 48, 50, 52, 54, 56, 60, 62, 64, 66, 68, 72, 74, 76, 77, 78, 79, 80, 82, 84, 85, 86, 88, 90, 92 };
|
|
|
|
float d1[] = { 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4 };
|
2023-12-27 00:18:10 +00:00
|
|
|
|
2023-12-27 05:54:58 +00:00
|
|
|
int m2[] = { 8, 1, 2, 3, 4, 5, 0, 1, 3, 8, 8, 3, 5, 7, 8, 7, 0, 1, 3, 8 };
|
|
|
|
int s2[] = { 2, 8, 14, 20, 26, 32, 36, 38, 42, 44, 50, 56, 62, 68, 74, 80, 84, 86, 90, 92 };
|
|
|
|
float d2[] = { 6, 6, 6, 6, 6, 4, 2, 4, 2, 6, 6, 6, 6, 6, 6, 4, 2, 4, 2, 4 };
|
2023-12-27 00:18:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-27 05:54:58 +00:00
|
|
|
// int m1[] = { 0, 2, 4, 5, 7, 9, 11, 12 };
|
|
|
|
// int s1[] = { 0, 2, 4, 6, 8, 10, 12, 14 };
|
|
|
|
// float d1[] = { 1.0, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5 };
|
|
|
|
// int m2[] = { 0, 4, 7, 12, 0, 4, 7, 12, 0, 4, 7, 12, 0, 4, 7, 12 };
|
|
|
|
// 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 };
|
2023-12-27 00:18:10 +00:00
|
|
|
|
|
|
|
|
2023-12-22 06:45:33 +00:00
|
|
|
float gate = 0.5;
|
2023-12-27 05:54:58 +00:00
|
|
|
int bpm = 320;
|
2023-12-22 06:45:33 +00:00
|
|
|
float beat_s = 60.0 / (float)bpm;
|
2023-12-27 04:16:37 +00:00
|
|
|
int beat = round(1000.0 * beat_s);
|
2023-12-27 01:33:04 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2023-12-27 04:16:37 +00:00
|
|
|
// From here on, all times are in milliseconds to head off floating point errors
|
|
|
|
|
|
|
|
long count_in = 4 * beat;
|
2023-12-27 05:54:58 +00:00
|
|
|
int repeat = 96;
|
2023-12-27 01:33:04 +00:00
|
|
|
|
|
|
|
|
2023-12-22 06:45:33 +00:00
|
|
|
float voltrange = 5.0;
|
|
|
|
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
|
|
|
|
float mod_b = 3.141592653589793 / 4.0;
|
|
|
|
|
|
|
|
typedef struct {
|
2023-12-27 00:18:10 +00:00
|
|
|
int s = -1;
|
2023-12-27 04:32:04 +00:00
|
|
|
bool playing = false;
|
|
|
|
bool looped = false;
|
2023-12-22 06:45:33 +00:00
|
|
|
long play = 0;
|
|
|
|
long rel = 0;
|
|
|
|
long last = 0;
|
|
|
|
long init = 0;
|
|
|
|
int *melody;
|
2023-12-27 00:18:10 +00:00
|
|
|
int *start;
|
|
|
|
float *duration;
|
2023-12-22 06:45:33 +00:00
|
|
|
int len = 0;
|
|
|
|
MCP4728_channel_t pitch;
|
|
|
|
MCP4728_channel_t gate;
|
|
|
|
} sequencer;
|
|
|
|
|
|
|
|
sequencer seq1, seq2;
|
2023-12-27 00:18:10 +00:00
|
|
|
long start_time;
|
2023-12-22 06:45:33 +00:00
|
|
|
|
|
|
|
void setup(void) {
|
|
|
|
Serial.begin(115200);
|
|
|
|
while (!Serial)
|
|
|
|
delay(10); // will pause Zero, Leonardo, etc until serial console opens
|
|
|
|
|
|
|
|
if (!mcp.begin(0x64)) {
|
|
|
|
Serial.println("Failed to find MCP4728 chip");
|
|
|
|
while (1) {
|
|
|
|
delay(10);
|
|
|
|
}
|
|
|
|
Serial.println("MCP4728 initialised");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float n0 = 0;
|
2023-12-23 04:57:37 +00:00
|
|
|
for( int i = 0; i < 16; i++ ) {
|
2023-12-22 06:45:33 +00:00
|
|
|
tuning[i] = round(n0 + octave * (float)i / 12.0);
|
|
|
|
}
|
|
|
|
long now = millis();
|
2023-12-27 00:18:10 +00:00
|
|
|
start_time = now;
|
2023-12-22 06:45:33 +00:00
|
|
|
seq1.last = now;
|
|
|
|
seq1.pitch = MCP4728_CHANNEL_A;
|
|
|
|
seq1.gate = MCP4728_CHANNEL_B;
|
|
|
|
seq1.melody = m1;
|
2023-12-27 00:18:10 +00:00
|
|
|
seq1.start = s1;
|
2023-12-22 06:45:33 +00:00
|
|
|
seq1.duration = d1;
|
|
|
|
seq1.len = sizeof(m1) / sizeof(m1[0]);
|
|
|
|
|
2023-12-27 05:54:58 +00:00
|
|
|
times_to_ms(seq1);
|
|
|
|
|
2023-12-23 04:57:37 +00:00
|
|
|
seq2.last = now;
|
|
|
|
seq2.pitch = MCP4728_CHANNEL_C;
|
|
|
|
seq2.gate = MCP4728_CHANNEL_D;
|
|
|
|
seq2.melody = m2;
|
|
|
|
seq2.duration = d2;
|
2023-12-27 00:18:10 +00:00
|
|
|
seq2.start = s2;
|
2023-12-23 04:57:37 +00:00
|
|
|
seq2.len = sizeof(m2) / sizeof(m2[0]);
|
2023-12-22 06:45:33 +00:00
|
|
|
|
2023-12-27 04:16:37 +00:00
|
|
|
times_to_ms(seq2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void times_to_ms(sequencer& seq) {
|
|
|
|
for( int i = 0; i < seq.len; i++ ) {
|
|
|
|
seq.start[i] *= beat;
|
2023-12-27 05:54:58 +00:00
|
|
|
seq.duration[i] = (float)beat * seq.duration[i] * 0.8;
|
2023-12-27 04:16:37 +00:00
|
|
|
}
|
2023-12-22 06:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-27 04:16:37 +00:00
|
|
|
|
2023-12-22 06:45:33 +00:00
|
|
|
void loop() {
|
|
|
|
long now = millis();
|
2023-12-27 04:16:37 +00:00
|
|
|
long t = now - start_time;
|
|
|
|
long beats = -1;
|
|
|
|
if( t >= count_in ) {
|
|
|
|
beats = (t - count_in) % (beat * repeat);
|
|
|
|
runSequencer(seq1, t, beats);
|
|
|
|
runSequencer(seq2, t, beats);
|
|
|
|
}
|
2023-12-22 06:45:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 04:16:37 +00:00
|
|
|
void runSequencer(sequencer& seq, long now, long beats) {
|
|
|
|
int next = seq.s + 1;
|
|
|
|
int start;
|
2023-12-27 04:32:04 +00:00
|
|
|
|
|
|
|
if( seq.playing ) {
|
|
|
|
if( now > seq.rel ) {
|
|
|
|
mcp.setChannelValue(seq.gate, 0);
|
|
|
|
seq.playing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 04:16:37 +00:00
|
|
|
if( next > seq.len - 1 ) {
|
|
|
|
seq.s = -1;
|
2023-12-27 04:32:04 +00:00
|
|
|
seq.looped = true;
|
2023-12-27 04:16:37 +00:00
|
|
|
return; // don't start the loop yet
|
|
|
|
} else {
|
|
|
|
start = seq.start[next];
|
|
|
|
}
|
2023-12-27 04:32:04 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-12-27 04:16:37 +00:00
|
|
|
if( beats >= start ) {
|
|
|
|
seq.s = next;
|
|
|
|
seq.rel = now + seq.duration[seq.s];
|
|
|
|
if( seq.melody[seq.s] > -1 ) {
|
|
|
|
mcp.setChannelValue(seq.pitch, tuning[seq.melody[seq.s]]);
|
|
|
|
mcp.setChannelValue(seq.gate, 4095);
|
2023-12-22 06:45:33 +00:00
|
|
|
}
|
2023-12-27 04:32:04 +00:00
|
|
|
seq.playing = true;
|
2023-12-22 06:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|