-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvtomidi.ino
209 lines (157 loc) · 3.89 KB
/
cvtomidi.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// (C) 2020 by [email protected]
// released on AGPL v3.0 in 2021
//#define DEBUG
#define PROGRAM_VERSION 1
#define ADC_NOTE A0
#define GATE 2
#define MODE_SEL 3
#define LED_ACK 13
#define DT_NOTE_OFF 500 /// ms
#define DT_MIN_NOTE_INTERVAL (1000/75)
constexpr double steps = 1024.0 / (10 * 12); // / 10 * 12 when mapping -5...5 to 0...5
#include "digitalWriteFast.h"
volatile bool gate_triggered = false, gate_direction = false;
void gate_change(void) {
gate_direction = !digitalReadFast(GATE);
gate_triggered = true;
}
uint16_t get_sample_low() {
return 1023 - analogRead(ADC_NOTE); // INVERTING(!) op-amp
}
uint16_t get_filtered_sample() {
uint16_t s1 = get_sample_low();
uint16_t s2 = get_sample_low();
if (s1 != s2)
return get_sample_low();
return s1;
}
uint8_t adc_to_note(uint16_t v) {
return 5 + v / steps;
}
void flashLED() {
static bool la_status = false;
digitalWriteFast(LED_ACK, !la_status);
la_status = !la_status;
}
void midiMsg(uint8_t cmd[], uint8_t len) {
static uint8_t prev_cmd = 255;
if (cmd[0] == prev_cmd)
Serial.write(&cmd[1], len - 1);
else {
Serial.write(cmd, len);
prev_cmd = cmd[0];
}
flashLED();
}
void midiReset() {
uint8_t msg[] = { 0xff };
midiMsg(msg, sizeof msg);
}
void noteOn(uint8_t note, uint8_t vol) {
uint8_t msg[] = { 0x90, note, vol };
midiMsg(msg, sizeof msg);
}
void noteOff(const uint8_t note) {
uint8_t msg[] = { 0x80, note, 0 };
midiMsg(msg, sizeof msg);
}
void midiSelectInstrument() {
uint8_t msg[] = { 0xc0, 81 };
midiMsg(msg, sizeof msg);
}
void midiSendProgramVersion(uint8_t v) {
uint8_t msg[10];
msg[0] = msg[9] = 0xff;
for(uint8_t i=0; i<8; i++)
msg[1 + i] = ((v >> i) & 1) ? 0xfe : 0xf8;
midiMsg(msg, sizeof msg);
//
uint8_t msg2[] = {
0xf0, 0x7e, // header
0x00, 0x7f, 0x7f, // device id
0x06, // General Information (sub-ID#1)
0x02, // Identity Reply (sub-ID#2)
0x01, // Manufacturers System Exclusive id code
0x01, 0x00, // Device family code (14 bits, LSB first)
0x01, 0x00, // Device family member code (14 bits, LSB first)
v, 0x00, 0x00, 0x00, // Software revision level. Format device specific
0xf7 // EOC
};
midiMsg(msg2, sizeof msg2);
}
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#else
Serial.begin(31250);
#endif
pinMode(GATE, INPUT);
attachInterrupt(0, gate_change, CHANGE);
pinMode(MODE_SEL, INPUT);
pinMode(LED_ACK, OUTPUT);
digitalWriteFast(LED_ACK, HIGH);
midiSendProgramVersion(PROGRAM_VERSION);
midiReset();
midiSelectInstrument();
noteOn(80, 127);
delay(100);
noteOff(80);
digitalWriteFast(LED_ACK, LOW);
}
uint8_t prevNote = 255;
uint16_t prevVal = 0;
uint32_t notePlayingSince = 0;
void loop() {
if (digitalReadFast(MODE_SEL)) {
if (gate_triggered) {
gate_triggered = false;
if (prevNote != 255) {
#ifdef DEBUG
Serial.println(F("OFF"));
#else
noteOff(prevNote);
#endif
delayMicroseconds(1000000ll / 31250 * 2 * 10);
prevNote = 255;
}
if (gate_direction) { // note on
uint16_t nv = get_filtered_sample();
uint8_t newNote = adc_to_note(nv);
// send note on for newNote
#ifdef DEBUG
Serial.print(F("ON: "));
Serial.println(newNote);
#else
noteOn(newNote, 127);
#endif
delayMicroseconds(DT_MIN_NOTE_INTERVAL);
prevNote = newNote;
}
}
}
else {
delay(DT_MIN_NOTE_INTERVAL);
if (DT_NOTE_OFF > 0 && millis() - notePlayingSince >= DT_NOTE_OFF && prevNote != 255) {
noteOff(prevNote);
prevNote = 255;
}
uint16_t nv = get_filtered_sample();
int16_t diff = nv - prevVal;
if (abs(diff) >= steps || (nv == 0 && abs(diff) >= steps /2)) {
uint8_t newNote = adc_to_note(nv);
// send note off for prevNote
if (prevNote != 255)
noteOff(prevNote);
// send note on for newNote
#ifdef DEBUG
Serial.print(F("ON: "));
Serial.println(newNote);
#else
noteOn(newNote, 127);
#endif
notePlayingSince = millis();
prevVal = nv;
prevNote = newNote;
}
}
}