168 lines
3.4 KiB
Arduino
168 lines
3.4 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
|
||
|
|
||
|
|
||
|
int note = 0;
|
||
|
|
||
|
// kick
|
||
|
// int pitch[] = { 8, -1, -1, -1, 8,-1, -1, -1, 8,-1, -1, -1, 8, -1, -1, 8,
|
||
|
// 8, -1, -1, -1, 8,-1, -1, -1, -1,-1, 8, -1, -1, 8, -1, 8,
|
||
|
// };
|
||
|
// float dur[] = { 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,1,1,1,1,1,1,1,1,1, };
|
||
|
|
||
|
|
||
|
// hi hat
|
||
|
// int pitch[] = { 8, 8, -1, 8, 8, 8, -1, 8, 8,8 , -1, 8, 8, 8, -1, 8,
|
||
|
// 8, 8, -1, 8, 8, 8, -1, 8, 8,8 , -1, 8, 8, 8, 8, 8,
|
||
|
// };
|
||
|
// float dur[] = { 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,1,1,1,1,1,1,1,1,1, };
|
||
|
|
||
|
// bass triggers
|
||
|
|
||
|
int pitch[] = { 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, -1, 1, -1, -1, -1, };
|
||
|
|
||
|
float dur[] = { 8, 1, 1,1, 1,1,1,1, 1,1,1,1, 4, 1,1,1,
|
||
|
8, 1, 1,1, 1,1,1,1, 1,1,1,1, 4, 1,1,1,};
|
||
|
|
||
|
|
||
|
int phrase = 32;
|
||
|
|
||
|
int s;
|
||
|
int bpm = 110;
|
||
|
int beat_m = 100; // fix me coordinate with timer code
|
||
|
bool noteon = false;
|
||
|
int beat = false;
|
||
|
long notestart, notedur;
|
||
|
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(115200);
|
||
|
|
||
|
float freqint = 60.0 / (float)bpm;
|
||
|
|
||
|
int ocr = round(16 * 10 ^ 6 / (1024.0 * freqint * 16)) - 1
|
||
|
|
||
|
Serial.println(ocr);
|
||
|
|
||
|
if( ocr < 65536 ) {
|
||
|
|
||
|
cli();
|
||
|
|
||
|
|
||
|
|
||
|
//set timer1 interrupt at 1Hz
|
||
|
TCCR1A = 0;// set entire TCCR1A register to 0
|
||
|
TCCR1B = 0;// same for TCCR1B
|
||
|
TCNT1 = 0;//initialize counter value to 0
|
||
|
// set compare match register for 1hz increments
|
||
|
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
|
||
|
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
float mod_f(int x) {
|
||
|
return 1 - sq((float)x / 1000.0);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
ISR(TIMER1_COMPA_vect){ // called once every note
|
||
|
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;
|
||
|
}
|
||
|
} else {
|
||
|
if( noteon ) {
|
||
|
mod = mod_f(now - notestart);
|
||
|
o = round(mod * 4095.0);
|
||
|
if( o > 4095 ) {
|
||
|
o = 4095;
|
||
|
}
|
||
|
if( o < 0 ) {
|
||
|
o = 0;
|
||
|
}
|
||
|
mcp.setChannelValue(MCP4728_CHANNEL_C, o);
|
||
|
if( now - notestart > notedur ) {
|
||
|
noteOff();
|
||
|
noteon = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
|