1
+ /*
2
+
3
+ Basic tutorial to create animations in sync with a MIDI clock
4
+ by: luiscript
5
+
6
+
7
+ Important info about MIDI clock standard:
8
+
9
+ clock 0xF8 (248)
10
+ start 0xFA (250)
11
+ continue 0xFB (251)
12
+ stop 0xFC (252)
13
+
14
+
15
+ */
16
+
17
+ import themidibus.* ; // download this library or you would get an error
18
+
19
+ MidiBus myBus; // this is or MIDI bus object
20
+
21
+ int timing = 0 ; // counts the total of quarter notes
22
+ float lfo = 0.0 ; // this will be our LFO
23
+
24
+ void setup () {
25
+ size (400 , 400 );
26
+ background (0 );
27
+
28
+ // this prints all the available MIDI ports
29
+ MidiBus . list();
30
+
31
+ // select the MIDI port of your preference
32
+ // in my case port 0 named "Bus 1"
33
+ myBus = new MidiBus (this , 0 , " Bus 1" );
34
+ }
35
+
36
+
37
+ // this function will be called when raw MIDI data arrives
38
+ void rawMidi (byte [] data ) {
39
+
40
+ if (data[0 ] == (byte )0xFC ) { // TRUE when MIDI clock stops.
41
+
42
+ // reset timing when clock stops to stay in sync for the next start
43
+ timing = 0 ;
44
+
45
+ } else if (data[0 ] == (byte )0xF8 ) { // TRUE every MIDI clock pulse
46
+
47
+ // we need to increase timing every pulse to get the total count
48
+ timing++ ;
49
+
50
+ // MIDI clock sends 24 ppqn (pulses per quarter note)
51
+ // with this formula, lfo will oscillate between 0 and 1 every quarter note
52
+ lfo = ( timing % 24 ) / 24.0 ;
53
+
54
+ // now you can use lfo to easily animate whatever you want in sync with BPM
55
+ }
56
+ }
57
+
58
+ void draw () {
59
+ background (0 );
60
+
61
+ // since we have lfo oscillating between 0 and 1, we can use it to animate the size of this ellipse in sync
62
+ ellipse (width / 2 , height / 2 , 100 * lfo, 100 * lfo);
63
+ }
64
+
65
+
66
+ // Enjoy
67
+ // @luiscript
0 commit comments