160 lines
3.3 KiB
C++
160 lines
3.3 KiB
C++
// Better sequencer which uses interrupts
|
|
|
|
#include <Adafruit_MCP4728.h>
|
|
#include <Wire.h>
|
|
|
|
Adafruit_MCP4728 mcp;
|
|
|
|
|
|
float tuning[37];
|
|
float voltrange = 4.85; // measured this, probably not accurate
|
|
float octave = 4096.0 / voltrange; // number of DAC steps in an octave
|
|
|
|
|
|
// melody
|
|
// int pitch[] = { 1,3,7,2,3,4,3,4,3,4,6,7,3,4,3,1,3, 5, 9, 4, 5, 6, 5, 6, 5, 6, 8, 9, 5, 6, 5, 3};
|
|
// float dur[] = { 0.1,0.00, 0.1, 0.05, 0.1,0.05, 0.05, 0.05,
|
|
// 0.1,0.05, 0.1, 0.05, 0.1,0.05, 0.05, 0.05,
|
|
// 0.1,0.05, 0.1, 0.05, 0.1,0.05, 0.05, 0.05,
|
|
// 0.1,0.05, 0.1, 0.05, 0.1,0.05, 0.05, 0.05,};
|
|
|
|
|
|
int pitch[] = { 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1,-1, -1, -1, -1, };
|
|
int dur[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
|
|
|
|
int note = 0;
|
|
int phrase = 32;
|
|
|
|
int s;
|
|
int bpm = 110;
|
|
float beat_s = 60.0 / (float)bpm;
|
|
float beat_m = 1000.0 * beat_s;
|
|
bool noteon = false;
|
|
int beat = false;
|
|
long notestart, notedur, barstart;
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
float freqint = 60.0 / (float)bpm;
|
|
|
|
int ocr = round(16000000.0 / (1024.0 * freqint * 16)) - 1;
|
|
Serial.println(ocr);
|
|
if( ocr < 65536 ) {
|
|
|
|
cli();
|
|
|
|
TCCR1A = 0;// set entire TCCR1A register to 0
|
|
TCCR1B = 0;// same for TCCR1B
|
|
TCNT1 = 0;//initialize counter value to 0
|
|
OCR1A = ocr;
|
|
// turn on CTC mode
|
|
TCCR1B |= (1 << WGM12);
|
|
// Set CS10 and CS12 bits for 1024 escaler
|
|
TCCR1B |= (1 << CS12) | (1 << CS10);
|
|
// enable timer compare interrupt
|
|
TIMSK1 |= (1 << OCIE1A);
|
|
|
|
sei();
|
|
|
|
} else {
|
|
Serial.println("BPM out of range");
|
|
}
|
|
|
|
if (!mcp.begin(0x64)) {
|
|
while (1) {
|
|
delay(100);
|
|
}
|
|
}
|
|
|
|
// randomSeed(analogRead(A0));
|
|
mcp.setSpeed(800000L);
|
|
make_tuning(7);
|
|
note=0;
|
|
barstart=millis();
|
|
}
|
|
|
|
void make_tuning(int edo) {
|
|
float n0 = 0;
|
|
float edof = (float)edo;
|
|
for( int i = 0; i < 37; i++ ) {
|
|
tuning[i] = round(n0 + octave * (float)i / edof);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// mod_note argument is milliseconds from the start of last note
|
|
float mod_note(int x) {
|
|
return 1 - sq((float)x / 1000.0);
|
|
}
|
|
|
|
// mod_note argument is milliseconds from the start of this bar
|
|
float mod_bar(int x) {
|
|
return 0.5 + 0.5 * sin((float)x / (beat_m * 2));
|
|
}
|
|
|
|
void set_mod(MCP4728_channel_t channel, float v) {
|
|
int o;
|
|
o = round(v * 4095.0);
|
|
if( o > 4095 ) {
|
|
o = 4095;
|
|
}
|
|
if( o < 0 ) {
|
|
o = 0;
|
|
}
|
|
mcp.setChannelValue(channel, o);
|
|
}
|
|
|
|
ISR(TIMER1_COMPA_vect){ // called once every beat
|
|
beat = true;
|
|
}
|
|
|
|
|
|
void loop() {
|
|
float mod;
|
|
int o;
|
|
long now = millis();
|
|
if( beat ) {
|
|
beat = false;
|
|
if( pitch[s] > -1 ) {
|
|
notestart = millis();
|
|
notedur = round(beat_m * dur[s]);
|
|
noteOn(pitch[s]);
|
|
noteon = true;
|
|
}
|
|
s += 1;
|
|
if( s == phrase ) {
|
|
s = 0;
|
|
barstart = notestart;
|
|
}
|
|
} else {
|
|
if( noteon ) {
|
|
set_mod(MCP4728_CHANNEL_C, mod_note(now - notestart));
|
|
if( now - notestart > notedur ) {
|
|
noteOff();
|
|
noteon = false;
|
|
}
|
|
}
|
|
set_mod(MCP4728_CHANNEL_D, mod_bar(now - barstart));
|
|
}
|
|
}
|
|
|
|
|
|
void noteOn(int note) {
|
|
if( note > -1 ) {
|
|
mcp.setChannelValue(MCP4728_CHANNEL_A, tuning[note]);
|
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 4095);
|
|
}
|
|
}
|
|
|
|
void noteOff() {
|
|
Serial.println("off");
|
|
mcp.setChannelValue(MCP4728_CHANNEL_B, 0);
|
|
}
|
|
|
|
|