summaryrefslogtreecommitdiff
path: root/visualizer/visualizer.py
blob: cc856eb5bc4ef50b2b7ed8213658222f7df02ded (plain)
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
#!/usr/bin/python3

import serial
import json
from sys import stdout


baud_rate = 115200
port = "/dev/ttyACM0"


def recv(link):
	while True:
		line = link.readline().rstrip()

		try:
			line = line.decode("utf-8")
		except UnicodeDecodeError:
			continue

		try:
			data = json.loads(line)
		except ValueError:
			continue

		try:
			spectrum = data["spectrum"]
			fundamental_bin = data["fundamental_bin"]
			fundamental_freq = data["fundamental_freq"]
			midi_note = data["midi_note"]
		except ValueError:
			continue

		return spectrum, fundamental_bin, fundamental_freq, midi_note


def show_spectrum(data):
	for line in range(50, 0, -1):
		for ampl in data:

			if ampl >= line:
				stdout.write('█')
			else:
				stdout.write(' ')
			
		stdout.write("\n")
	
	stdout.write("\n")
	stdout.flush()


if __name__ == '__main__':
	
	link = serial.Serial(port, baud_rate)
	
	while True:
		spectrum, fundamental_bin, fundamental_freq, midi_note = recv(link)
		show_spectrum(spectrum)
		print("fundamental: bin %d -> %f Hz -> note %d" % (fundamental_bin, fundamental_freq, midi_note))