-
Notifications
You must be signed in to change notification settings - Fork 2
/
midi2cv.py
397 lines (337 loc) · 10.6 KB
/
midi2cv.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# midi2cv.py
#
# https://github.com/schollz/midi2cv
#
# convert incoming midi signals to a calibrated voltage
#
# run 'python3 midi2cv.py --tune' to generate a calibraiton
# run 'python3 midi2cv.py --play' to listen to midi
#
import sys
import threading
import time
import os
import json
import math
from subprocess import Popen, PIPE, STDOUT
from signal import signal, SIGINT
import click
import numpy as np
from loguru import logger
import mido
from mido.ports import MultiPort
import termplotlib as tpl
# rail-to-rail voltage
# set this with `--vdd`
rail_to_rail_vdd = 5.2
voltage_adjustment = 0
mb = [1.51, 1.38]
keys_on = 0
#
# mcp4725 functions
#
def init_mcp4725():
global i2c, dac
import board
import busio
import adafruit_mcp4725
i2c = busio.I2C(board.SCL, board.SDA)
dac = adafruit_mcp4725.MCP4725(i2c)
set_voltage(0)
def set_voltage(volts):
global dac
volts += voltage_adjustment
# logger.info("setting voltage={}", volts)
if volts >= 0 and volts < rail_to_rail_vdd:
dac.value = int(round(float(volts) / rail_to_rail_vdd * 65535.0))
#
# frequency / midi / voltage conversoins
#
def freq_to_voltage(freq):
return mb[0] * math.log(freq) + mb[1]
def note_to_freq(note):
a = 440 # frequency of A (common value is 440Hz)
return (a / 32) * (2 ** ((note - 9) / 12))
def note_to_voltage(note):
return freq_to_voltage(note_to_freq(note))
def match_note_to_freq(freq):
closetNote = -1
closestAmount = 10000
closestFreq = 10000
for note in range(1, 90):
f = note_to_freq(note)
if abs(f - freq) < closestAmount:
closestAmount = abs(f - freq)
closetNote = note
closestFreq = f
return closetNote
#
# calibration / tuning
#
def do_tuning():
print(
"""note! before tuning...
- ...make sure that your synth is connected
via the USB audio adapter line-in.
- ...make sure that your synth outputs only
pure tones (turn off effects!).
"""
)
for i in range(5):
print("initiating tuning in {}".format(5 - i), end="\r")
time.sleep(1)
voltage_to_frequency = {}
previous_freq = 0
for voltage in range(260, int(rail_to_rail_vdd * 80), 5):
voltage = float(voltage) / 100.0
freq = sample_frequency_at_voltage(voltage)
if freq < previous_freq:
continue
voltage_to_frequency[voltage] = freq
previous_freq = freq
os.system("clear")
plot_points(voltage_to_frequency)
with open("voltage_to_frequency.json", "w") as f:
f.write(json.dumps(voltage_to_frequency))
def plot_points(voltage_to_frequency):
x = []
y0 = []
for k in voltage_to_frequency:
x.append(float(k))
y0.append(voltage_to_frequency[k])
fig = tpl.figure()
print("\n")
fig.plot(
x,
y0,
plot_command="plot '-' w points",
width=50,
height=20,
xlabel="voltage (v)",
title="frequency (hz) vs voltage",
)
fig.show()
print("\n")
def load_tuning():
global mb
voltage_to_frequency = json.load(open("voltage_to_frequency.json", "rb"))
x = []
y = []
y0 = []
for k in voltage_to_frequency:
x.append(float(k))
y0.append(voltage_to_frequency[k])
y.append(math.log(voltage_to_frequency[k]))
mb = np.polyfit(y, x, 1)
fig = tpl.figure()
print("\n")
fig.plot(
x,
y0,
plot_command="plot '-' w points",
width=60,
height=22,
xlabel="voltage (v)",
title="frequency (hz) vs voltage",
label="freq = exp((volts{:+2.2f})/{:2.2f}) ".format(mb[1], mb[0]),
)
fig.show()
print("\n")
time.sleep(1)
# plx.scatter(x, y,cols=80,rows=10,xlim=[np.min(x), np.max(x)])
# plx.show()
def check_tuning():
adjustment = []
cents_off = []
for i in range(60, 80, 2):
freq = sample_frequency_at_voltage(note_to_voltage(i))
cents = 1200 * np.log2(note_to_freq(i) / freq)
print(
"midi={}, target={:2.1f} hz, observed={:2.1f} hz, cents off={:2.1f} cents".format(
i, note_to_freq(i), freq, cents
)
)
cents_off.append(cents)
cents_off_mean = np.mean(cents_off)
cents_off_sd = np.std(cents_off)
print("mean cents off: {0:+} +/- {0:+}".format(cents_off_mean, cents_off_sd))
def sample_frequency_at_voltage(voltage):
set_voltage(voltage)
time.sleep(1.0)
freq = get_frequency_analysis()
# print("{:2.2f} hz at {:2.2f} volt".format(freq, voltage))
return freq
def get_frequency_analysis():
cmd = "arecord -d 1 -f cd -t wav -D sysdefault:CARD=1 /tmp/1s.wav"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
if b"Recording WAVE" not in output:
raise output
# cmd = "sox /tmp/1s.wav -n stat -freq"
cmd = "aubio pitch -m schmitt -H 1024 /tmp/1s.wav"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
with open("/tmp/1s.dat", "wb") as f:
f.write(output)
freq = analyze_aubio()
return freq
def analyze_aubio():
gathered_freqs = []
with open("/tmp/1s.dat", "r") as f:
linenum = 0
for line in f:
linenum += 1
if linenum < 5:
continue
s = line.split()
if len(s) != 2:
continue
freq = float(s[1])
if freq > 100:
gathered_freqs.append(freq)
if len(gathered_freqs) == 0:
return -1
avg = np.median(gathered_freqs)
return avg
def analyze_sox():
previous_amp = 0
previous_freq = 0
gathering = -1
gathered_freqs = []
gathered_amps = []
known_frequencies = {}
known_powers = {}
with open("/tmp/1s.dat") as f:
for line in f:
line = line.strip()
if ":" in line:
continue
nums = line.split()
if len(nums) > 2:
continue
amp = float(nums[1])
freq = float(nums[0])
if amp > 10 and amp > previous_amp:
gathering = 0
if gathering == 0:
gathered_amps = []
gathered_freqs = []
gathered_freqs.append(previous_freq)
gathered_amps.append(previous_amp)
if gathering > -1:
gathering += 1
gathered_freqs.append(freq)
gathered_amps.append(amp)
if gathering == 3:
gathering = -1
freq_power = np.sum(gathered_amps)
freq_average = (
np.sum(np.multiply(gathered_amps, gathered_freqs)) / freq_power
)
found = False
for f in known_frequencies:
if freq_average < f * 0.92 or freq_average > f * 1.08:
continue
found = True
known_frequencies[f].append(freq_average)
known_powers[f].append(freq_power)
if not found:
known_frequencies[freq_average] = [freq_average]
known_powers[freq_average] = [freq_power]
previous_freq = freq
previous_amp = amp
freq_and_power = {}
for f in known_frequencies:
freq_and_power[np.mean(known_frequencies[f])] = np.mean(known_powers[f])
for i, v in enumerate(
sorted(freq_and_power.items(), key=lambda x: x[1], reverse=True)
):
if i == 1:
return v[0]
return -1
#
# midi listeners
#
def midi(name):
global keys_on
logger.info("listening on '{}'", name)
with mido.open_input(name) as inport:
name = name.split()
if len(name) > 2:
name = " ".join(name[:2])
else:
name = " ".join(name)
name = name.lower()
for msg in inport:
if msg.type == "note_on":
logger.info(f"[{name}] {msg.type} {msg.note} {msg.velocity}")
set_voltage(note_to_voltage(msg.note))
keys_on += 1
elif msg.type == "note_off":
keys_on -= 1
if keys_on == 0:
set_voltage(0)
def listen_for_midi():
inputs = mido.get_input_names()
for name in inputs:
t = threading.Thread(target=midi, args=(name,))
t.daemon = True
t.start()
while True:
time.sleep(10)
#
# cli
#
def handler(signal_received, frame):
try:
set_voltage(0)
except:
pass
logger.info("exiting")
sys.exit(0)
@click.command()
@click.option("--vdd", help="set the rail-to-rail voltage", default=5.2)
@click.option("--tune", help="activate tuning", is_flag=True, default=False)
@click.option("--play", help="initialize playing", is_flag=True, default=False)
@click.option("--adj", help="adjust voltage", default=0.0)
@click.option("--do", help="runs a function (debugging)", is_flag=True, default=False)
@click.option(
"--noinit",
help="do not intiialize mcp4725 (debugging)",
is_flag=True,
default=False,
)
def gorun(tune, play, vdd, noinit, adj, do):
signal(SIGINT, handler)
global rail_to_rail_vdd
global voltage_adjustment
rail_to_rail_vdd = vdd
voltage_adjustment = adj
if not noinit:
init_mcp4725()
if tune:
do_tuning()
check_tuning()
if play:
load_tuning()
listen_for_midi()
if __name__ == "__main__":
logger.remove()
logger.add(
sys.stderr,
format="<green>{time:HH:mm:ss}</green> (<cyan>{function}:{line}</cyan>) - {message}",
)
print(
"""
███╗ ███╗██╗██████╗ ██╗ ██████╗ ██████╗██╗ ██╗
████╗ ████║██║██╔══██╗██║ ╚════██╗ ██╔════╝██║ ██║
██╔████╔██║██║██║ ██║██║ █████╔╝ ██║ ██║ ██║
██║╚██╔╝██║██║██║ ██║██║ ██╔═══╝ ██║ ╚██╗ ██╔╝
██║ ╚═╝ ██║██║██████╔╝██║ ███████╗ ╚██████╗ ╚████╔╝
╚═╝ ╚═╝╚═╝╚═════╝ ╚═╝ ╚══════╝ ╚═════╝ ╚═══╝
version v0.2.0 (github.com/schollz/midi2cv)
convert any incoming midi signal into a control voltage
from your raspberry pi.
"""
)
gorun()