Newer
Older
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
68
69
#include "MIDIUSB.h"
#include <math.h>
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
uint16_t period = 0;
uint64_t cycle = 0;
uint8_t state = 0;
void note(uint16_t frq);
void setup() {
Serial.begin(115200);
pinMode(10, OUTPUT);
}
uint16_t midi2frq(uint8_t number) {
// Serial.print(number);
// Serial.print(" | ");
double _n = (double)((double)number-(double)69);
// Serial.print(_n);
// Serial.print(" | ");
double d = pow((double)2, _n/(double)12) * (double)440;
// Serial.println(d);
return (uint16_t)d;
}
uint8_t lastNote = 0;
void loop() {
midiEventPacket_t rx;
rx = MidiUSB.read();
if (rx.header != 0) {
char buffer[256];
sprintf(buffer, "header: 0x%X b1: %d b2: %d b3: %d", rx.header, rx.byte1, rx.byte2, rx.byte3);
Serial.println(buffer);
if ((rx.header & 0x09) == 0x09) {
lastNote = rx.byte2;
note(midi2frq(rx.byte2));
Serial.println("ONNNNNNN");
} else if ((rx.header & 0x08) == 0x08 && lastNote == rx.byte2) {
period = 0;
Serial.println("OFFFFFFFF");
} /* else {
char buffer[256];
sprintf(buffer, "header: 0x%X b1: %d b2: %d b3: %d", rx.byte1, rx.byte2, rx.byte3);
Serial.println(buffer);
} */
}
if (period) {
state ^= 1;
digitalWrite(10, state);
} else {
digitalWrite(10, LOW);
}
cycle += 1;
delayMicroseconds(period);
}
void note(uint16_t frq) {
period = 1000000 / frq / 2;
}