Skip to content

Commit fd84dad

Browse files
committed
Added clock example
1 parent 7c6c8a3 commit fd84dad

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* MIDIUSB_clock.ino
3+
*
4+
* Simple example of beat clock based on MIDI pulse messages
5+
* received from software.
6+
*
7+
* Tested on Leonardo with Ableton.
8+
*
9+
* In preferences go to MIDI Sync. Select device Output
10+
* and toggle Sync button, change clock type to Pattern.
11+
* Usually changing Sync Delay is required.
12+
*
13+
* Created: 19/12/2016
14+
* Author: Ernest Warzocha
15+
*/
16+
17+
#include "MIDIUSB.h"
18+
19+
//Pulse per quarter note. Each beat has 24 pulses.
20+
//Tempo is based on software inner BPM.
21+
int ppqn = 0;
22+
23+
void noteOn(byte channel, byte pitch, byte velocity) {
24+
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
25+
MidiUSB.sendMIDI(noteOn);
26+
}
27+
28+
void noteOff(byte channel, byte pitch, byte velocity) {
29+
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
30+
MidiUSB.sendMIDI(noteOff);
31+
}
32+
33+
void setup() {
34+
Serial.begin(115200);
35+
}
36+
37+
void loop() {
38+
39+
midiEventPacket_t rx;
40+
41+
do {
42+
rx = MidiUSB.read();
43+
44+
//Count pulses and send note
45+
if(rx.byte1 == 0xF8){
46+
++ppqn;
47+
48+
if(ppqn == 24){
49+
noteOn(1,48,127);
50+
MidiUSB.flush();
51+
ppqn = 0;
52+
};
53+
}
54+
//Clock start byte
55+
else if(rx.byte1 == 0xFA){
56+
noteOn(1,48,127);
57+
MidiUSB.flush();
58+
ppqn = 0;
59+
}
60+
//Clock stop byte
61+
else if(rx.byte1 == 0xFC){
62+
noteOff(1,48,0);
63+
MidiUSB.flush();
64+
ppqn = 0;
65+
};
66+
67+
} while (rx.header != 0);
68+
69+
}

0 commit comments

Comments
 (0)