161 lines
3.3 KiB
Arduino
161 lines
3.3 KiB
Arduino
|
// 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[] = { 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
// -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1,2, -1, -1, -1, };
|
||
|
// int dur[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, };
|
||
|
|
||
|
// hi hat
|
||
|
|
||
|
int pitch[] = { 1, 2, 3,2, 4, 2,5, 0,1, 2, 3,2, 4, 2,5, 0,1, 2, 3,2, 4, 2,5, 0,1, 2, 3,2, 4, 2,5, 0, };
|
||
|
int dur[] = { 0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1, };
|
||
|
|
||
|
|
||
|
int note = 0;
|
||
|
int phrase = 32;
|
||
|
|
||
|
int s;
|
||
|
int bpm = 80;
|
||
|
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(12);
|
||
|
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) {
|
||
|
float xb = x / (0.2 * beat_m);
|
||
|
return 0.5 + 0.5 * xb * (xb - 1) * ( xb - 2);
|
||
|
}
|
||
|
|
||
|
// 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);
|
||
|
}
|
||
|
|
||
|
|