-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiDCO.ino
67 lines (55 loc) · 1.27 KB
/
MidiDCO.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <MIDI.h>
#include <List.hpp>
#include "MidiTimer.h"
#include "MidiDacTimer.h"
MidiTimer clockOutput(9, TCCR1A, TCCR1B, OCR1A);
MidiDacTimer4 integratorCV(7);
constexpr uint8_t gatePin{ 8 };
midi::SerialMIDI<HardwareSerial> midiSerial(Serial1);
midi::MidiInterface<midi::SerialMIDI<HardwareSerial>> MIDI((midi::SerialMIDI<HardwareSerial>&)midiSerial);
List<byte> heldNotes;
void setup()
{
clockOutput.begin();
integratorCV.begin();
pinMode(gatePin, OUTPUT);
MIDI.setHandleNoteOn(noteOn);
MIDI.setHandleNoteOff(noteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
noteSet(69);
}
void loop()
{
MIDI.read();
}
void noteOn(midi::Channel channel, byte note, byte velocity)
{
heldNotes.add(note);
noteSet(note);
digitalWrite(gatePin, HIGH);
}
void noteOff(midi::Channel channel, byte note, byte velocity)
{
// Find the note and remove it
for (int i = 0; i < heldNotes.getSize(); i++)
{
if (heldNotes[i] == note)
{
heldNotes.remove(i);
break;
}
}
if (heldNotes.getSize() > 0)
{
noteSet(heldNotes[heldNotes.getSize() - 1]);
}
else
{
digitalWrite(gatePin, LOW);
}
}
void noteSet(byte note)
{
clockOutput.setNote(note);
integratorCV.setFrequency(clockOutput.getFrequency());
}