Initial channel implementation

This commit is contained in:
əlemi 2019-06-30 00:39:01 +02:00
parent 68d2631f33
commit 6b2faa76a7

View file

@ -5,6 +5,7 @@
#define BPQN 24 // Ableton sends 24, VCV rack only one, by standard should be 24? #define BPQN 24 // Ableton sends 24, VCV rack only one, by standard should be 24?
#define NOTEOffset 36 #define NOTEOffset 36
#define DRUMSHIFT 6
#define drumOffset 60 #define drumOffset 60
#define MINUTE 60000 #define MINUTE 60000
#define MIDICLOCK 0xf8 #define MIDICLOCK 0xf8
@ -22,6 +23,7 @@ typedef struct OCTAVEStatus { // This struct is for an OCTAVE status. Each
} octst; } octst;
typedef struct SequencerStep { typedef struct SequencerStep {
bool clean = LOW;
bool kboard_s[MAXKEYS]; bool kboard_s[MAXKEYS];
bool dpad_s[MAXDPAD]; bool dpad_s[MAXDPAD];
unsigned short stepnumber; unsigned short stepnumber;
@ -36,7 +38,7 @@ int OCTAVE[4] = { // Pins associated to each OCTAVE's contact bar
int SEND[3] = { // Pins used as sender for capacitive touch buttons int SEND[3] = { // Pins used as sender for capacitive touch buttons
5, 4, 16 }; 5, 4, 16 };
int RECEIVE[3] = { // Pins used as receiver for capacitive touch buttons int RECEIVE[3] = { // Pins used as receiver for capacitive touch buttons
6, 3, 17 }; 6, 1, 17 };
int LEDS[4] = { // Pins used for leds int LEDS[4] = { // Pins used for leds
21, 20, 19, 18 }; 21, 20, 19, 18 };
int OW = 2; // Pin used for overwrite switch int OW = 2; // Pin used for overwrite switch
@ -48,23 +50,27 @@ int ADD = 14; // Pin used for add button
// PLACEHOLDERS // PLACEHOLDERS
byte velocity = 100; // byte velocity = 100; //
byte channel = 1; //
int bpm = 360; // int bpm = 360; //
// SEQUENCER POINTERS // SEQUENCER POINTERS AND RELATED ARRAYS
link head = NULL; link head[6];
link current = NULL; link current[6];
link previous;
unsigned short nstep[6]; // Keeps track of the sequencer steps
bool mute[6];
byte channel; // Current selected channel. Drums are shifted of DRUMSHIFT channels (so channels can only be 6)
// SYSTEM VARIABLES // SYSTEM VARIABLES
unsigned short nstep = 0; // Keeps track of the sequencer steps
int arp = 0; // Keeps track of last played NOTE if arpeggiating int arp = 0; // Keeps track of last played NOTE if arpeggiating
int midiclock = 0; // Used to sync with MIDI clock int midiclock = 0; // Used to sync with MIDI clock
bool add_step = LOW; // This is used to remember the addition of a step bool add_step = LOW; // This is used to remember the addition of a step
bool del_step = LOW; // This is used to remember the deletion of a step
bool chan_up = LOW; // Only for now because I have few buttons :C
int sem_beat = 0; // Basic semaphore used to sync with MIDI beat int sem_beat = 0; // Basic semaphore used to sync with MIDI beat
int sem_gate = 0; // Basic semaphore used for gate timing int sem_gate = 0; // Basic semaphore used for gate timing
unsigned long last_gate = 0; // Gate start time for last sequencer step unsigned long last_gate = 0; // Gate start time for last sequencer step
unsigned long gate_length = 500; // ms of keypress if arpeggiator unsigned long gate_length = 100; // ms of keypress if arpeggiator
bool dpadhit = LOW; // If any drum pad has been hit in this cycle, this is true bool dpadhit = LOW; // If any drum pad has been hit in this cycle, this is true
int npressed; // Number of keys pressed, used to avoid doing anything when no keys are pressed int npressed; // Number of keys pressed, used to avoid doing anything when no keys are pressed
bool kboard[MAXKEYS]; // Last status of keyboard bool kboard[MAXKEYS]; // Last status of keyboard
@ -79,7 +85,7 @@ void setup() {
for (int cButton = 0; cButton < MAXDPAD; cButton++) { // Capacitive Buttons configuration for (int cButton = 0; cButton < MAXDPAD; cButton++) { // Capacitive Buttons configuration
bCap[cButton] = new CapacitiveSensor(SEND[cButton], RECEIVE[cButton]); // Initialized bCap[cButton] = new CapacitiveSensor(SEND[cButton], RECEIVE[cButton]); // Initialized
bCap[cButton]->set_CS_AutocaL_Millis(0xFFFFFFFF); // No recalibration bCap[cButton]->set_CS_AutocaL_Millis(0xFFFFFFFF); // No recalibration
bCap[cButton]->set_CS_Timeout_Millis(10); // Timeout set to 20ms (instead of 2s) bCap[cButton]->set_CS_Timeout_Millis(1); // Timeout set to 20ms (instead of 2s)
dpad[cButton] = LOW; // Button starts LOW dpad[cButton] = LOW; // Button starts LOW
} }
@ -91,60 +97,86 @@ void setup() {
pinMode(OW, INPUT_PULLUP); // Used for overwrite switch pinMode(OW, INPUT_PULLUP); // Used for overwrite switch
pinMode(ADD, INPUT_PULLUP); // Used for overwrite switch pinMode(ADD, INPUT_PULLUP); // Used for overwrite switch
for (int i = 0, i < 6, i++){
current[i] = NULL;
head[i] = NULL;
nstep[i] = 0;
mute[i] = LOW;
}
channel = 1;
for (int i = 0; i < 16; i++) { // Boot up fancyness! for (int i = 0; i < 16; i++) { // Boot up fancyness!
display(i); display(i);
delay(200); delay(200);
} }
display(nstep); // ONLY FOR DEBUG
for (int i=0; i<16, i++){
for (byte chan=1; chan <= 6; chan++) insertStep(chan);
}
display(0);
} }
void loop() { void loop() {
// Serial.println(midiclock);
// Serial.print("Start | ");
// Serial.print(millis());
// Serial.print('\n');
sync(); sync();
add_step = (add_step || !digitalRead(ADD)); // add_step = (add_step || !digitalRead(ADD));
// Serial.print("SPEPS DONE | "); // del_step = (del_step || !digitalRead(DEL));
// Serial.print(millis()); chan_up = (chan_up || !digitalRead(ADD))
// Serial.print('\n');
if (sem_beat > 0) { if (sem_beat > 0) {
sem_beat--; sem_beat--;
if (sem_gate > 0) { // If step was shorter than gate, close all open notes before next step if (sem_gate > 0) { // If step was shorter than gate, close all open notes before next step
sem_gate--; sem_gate--;
for (int i = 0; i < MAXKEYS; i++) if (current->kboard_s[i]) playNOTE(i, !current->kboard_s[i]); for (byte chan = 1; chan <= 6; chan++) {
for (int i = 0; i < MAXDPAD; i++) if (current->dpad_s[i]) playDrum(i, !current->dpad_s[i]); if (mute[chan]) continue;
for (int i = 0; i < MAXKEYS; i++) if (current[chan]->kboard_s[i] && !kboard[i]) playNote(i, !current[chan]->kboard_s[i], chan);
for (int i = 0; i < MAXDPAD; i++) if (current[chan]->dpad_s[i] && !dpad[i]) playDrum(i, !current[chan]->dpad_s[i], chan);
} }
if (add_step) { }
if (add_step && !del_step) {
add_step = LOW; add_step = LOW;
if (nstep < MAXSTEP) insertStep(); if (nstep[channel-1] < MAXSTEP) insertStep(channel-1);
}
if (del_step && !add_step) {
del_step = LOW;
if (nstep[channel-1] < MAXSTEP) deleteStep(channel-1);
}
if (add_step && del_step) {
add_step = LOW;
del_step = LOW;
}
// ONLY FOR NOW because I don't have enough buttons :C
if (chan_up) {
chan_up = LOW;
channel++;
if (channel > 6) channel = (byte) 1;
} }
//if (digitalRead(ADD) && !digitalRead(OW)) deleteStep(); // Placeholder because I miss a button
nextStep(); nextStep();
display(current->stepnumber); display(current[channel-1]->stepnumber);
for (byte chan = 1; chan <= 6; chan++) {
if (current != NULL) { // Play all step notes and begin counting for gate if (mute[chan]) continue;
for (int i = 0; i < MAXKEYS; i++) if (current->kboard_s[i]) playNOTE(i, current->kboard_s[i]); if (current[chan] != NULL) { // Play all step notes and begin counting for gate
for (int i = 0; i < MAXDPAD; i++) if (current->dpad_s[i]) playDrum(i, current->dpad_s[i]); for (int i = 0; i < MAXKEYS; i++) if (current[chan]->kboard_s[i] && !kboard[i]) playNote(i, current[chan]->kboard_s[i]);
for (int i = 0; i < MAXDPAD; i++) if (current[chan]->dpad_s[i] && !dpad[i]) playDrum(i, current[chan]->dpad_s[i]);
}
}
last_gate = millis(); last_gate = millis();
sem_gate++; sem_gate++;
} }
// Serial.print("BEAT ELABORATED AND PLAYED | ");
// Serial.print(millis());
// Serial.print('\n');
}
if (sem_gate > 0 && (millis() - last_gate) > gate_length) { if (sem_gate > 0 && (millis() - last_gate) > gate_length) {
sem_gate--; sem_gate--;
for (int i = 0; i < MAXKEYS; i++) if (current->kboard_s[i]) playNOTE(i, !current->kboard_s[i]); for (byte chan = 1; chan <= 6; chan++) {
for (int i = 0; i < MAXDPAD; i++) if (current->dpad_s[i]) playDrum(i, !current->dpad_s[i]); if (mute[chan]) continue;
// Serial.print("GATE FINISHED | "); for (int i = 0; i < MAXKEYS; i++) if (current[chan]->kboard_s[i] && !kboard[i]) playNote(i, !current[chan]->kboard_s[i], chan);
// Serial.print(millis()); for (int i = 0; i < MAXDPAD; i++) if (current[chan]->dpad_s[i] && !dpad[i]) playDrum(i, !current[chan]->dpad_s[i], chan);
// Serial.print('\n'); }
} }
dpadhit = LOW; dpadhit = LOW;
@ -159,16 +191,11 @@ void loop() {
npressed += eval(scan(cOCTAVE)); npressed += eval(scan(cOCTAVE));
digitalWrite(OCTAVE[cOCTAVE], LOW); digitalWrite(OCTAVE[cOCTAVE], LOW);
} }
// Serial.print("READ KEYBOARD | ");
// Serial.print(millis());
// Serial.print('\n');
if (digitalRead(OW)) { if (digitalRead(OW)) {
if (npressed > 0) for (int i = 0; i < MAXKEYS; i++) current->kboard_s[i] = kboard[i]; if (npressed > 0) for (int i = 0; i < MAXKEYS; i++) current[channel-1]->kboard_s[i] = kboard[i];
if (dpadhit) for (int i = 0; i < MAXDPAD; i++) current->dpad_s[i] = dpad[i]; if (dpadhit) for (int i = 0; i < MAXDPAD; i++) current[channel-1]->dpad_s[i] = dpad[i];
// Serial.print("OVERWRITTEN STUFF | "); current[channel-1]->clean = LOW;
// Serial.print(millis());
// Serial.print('\n');
} }
} }
@ -200,14 +227,14 @@ bool evalButton(CapacitiveSensor* b, bool value, int note_number) {
if (sensor > 15) { if (sensor > 15) {
if (value) return HIGH; if (value) return HIGH;
else { else {
playDrum(note_number, HIGH); playDrum(note_number, HIGH, channel);
return HIGH; return HIGH;
} }
} }
else { else {
if (!value) return LOW; if (!value) return LOW;
else { else {
playDrum(note_number, LOW); playDrum(note_number, LOW, channel);
return LOW; return LOW;
} }
} }
@ -221,7 +248,7 @@ int eval(octst input) {
for (int c = 0; c < 12; c++) { for (int c = 0; c < 12; c++) {
if (input.stat[c] ^ kboard[c + sNOTE]) { if (input.stat[c] ^ kboard[c + sNOTE]) {
playNOTE(c + sNOTE, input.stat[c]); playNote(c + sNOTE, input.stat[c], channel);
kboard[c + sNOTE] = input.stat[c]; kboard[c + sNOTE] = input.stat[c];
} }
if (kboard[c + sNOTE] == HIGH) pressed++; if (kboard[c + sNOTE] == HIGH) pressed++;
@ -229,23 +256,23 @@ int eval(octst input) {
return pressed; return pressed;
} }
void playNOTE(int c, bool status) { void playNote(int c, bool status, byte chan) {
byte n = c + NOTEOffset; byte n = c + NOTEOffset;
if (status == HIGH) { if (status == HIGH) {
MIDI.sendNoteOn(n, velocity, channel); MIDI.sendNoteOn(n, velocity, chan);
} }
else if (status == LOW) { else if (status == LOW) {
MIDI.sendNoteOff(n, velocity, channel); MIDI.sendNoteOff(n, velocity, chan);
} }
} }
void playDrum(int c, bool status) { void playDrum(int c, bool status, byte chan) {
byte n = c + drumOffset; byte n = c + drumOffset;
if (status == HIGH) { if (status == HIGH) {
MIDI.sendNoteOn(n, velocity, (byte)7); MIDI.sendNoteOn(n, velocity, (byte)(chan + DRUMSHIFT));
} }
else if (status == LOW) { else if (status == LOW) {
MIDI.sendNoteOff(n, velocity, (byte)7); MIDI.sendNoteOff(n, velocity, (byte)(chan + DRUMSHIFT));
} }
} }
@ -270,7 +297,7 @@ link newStep() {
return (link)malloc(sizeof(struct SequencerStep)); return (link)malloc(sizeof(struct SequencerStep));
} }
bool insertStep() { bool insertStep(byte chan) {
link newS = newStep(); link newS = newStep();
link buffer; link buffer;
if (newS == NULL) { if (newS == NULL) {
@ -281,42 +308,70 @@ bool insertStep() {
for (int i = 0; i < MAXKEYS; i++) newS->kboard_s[i] = LOW; for (int i = 0; i < MAXKEYS; i++) newS->kboard_s[i] = LOW;
for (int i = 0; i < MAXDPAD; i++) newS->dpad_s[i] = LOW; for (int i = 0; i < MAXDPAD; i++) newS->dpad_s[i] = LOW;
if (current == NULL) { if (current[chan] == NULL) {
newS->next = newS; newS->next = newS;
newS->stepnumber = (unsigned short) 0; newS->stepnumber = (unsigned short) 0;
current = newS; current[chan] = newS;
head = newS; head[chan] = newS;
nstep = 1; nstep[chan] = 1;
} }
else { else {
newS->stepnumber = nstep; newS->stepnumber = nstep;
buffer = current; buffer = current[chan];
while (buffer->next != head) buffer = buffer->next; while (buffer->next != head[chan]) buffer = buffer->next;
buffer->next = newS; buffer->next = newS;
newS->next = head; newS->next = head[chan];
nstep++; nstep[chan]++;
} }
return HIGH; return HIGH;
} }
void nextStep() { void nextStep() {
if (current == NULL) return; for (byte chan=1; chan <= 6; chan++) {
current = current->next; if (current[chan] == NULL) continue;
current[chan] = current[chan]->next;
}
} }
bool deleteStep() { bool deleteStep(byte chan) {
if (nstep < 1) return LOW; if (nstep[chan] < 1) return LOW;
if (nstep == 1) { if (!current[chan]->clean) {
for (int i = 0; i < MAXKEYS; i++) current[chan]->kboard_s[i] = LOW;
for (int i = 0; i < MAXDPAD; i++) current[chan]->dpad_s[i] = LOW;
current[chan]->clean = HIGH;
return LOW;
}
if (nstep[chan] == 1) {
free(current); free(current);
head = NULL; head[chan] = NULL;
current = NULL; current[chan] = NULL;
} }
else { else {
link buffer = current->next->next; link buffer = current[chan];
free(current->next); while (buffer->next != current[chan]) buffer = buffer->next;
current->next = buffer; buffer->next = current[chan]->next;
if (current[chan] == head[chan]) {
head[chan] = head[chan]->next;
int i = 0
buffer = head[chan]
do {
buffer->stepnumber = i;
buffer = buffer->next;
i++;
} while (buffer != head[chan])
} }
nstep--; else {
buffer = buffer->next;
while (buffer != head[chan]) {
buffer->stepnumber--;
buffer = buffer->next;
}
}
free(current[chan]);
buffer = buffer->next;
}
nstep[chan]--;
return HIGH; return HIGH;
} }