summaryrefslogtreecommitdiff
path: root/visualizer
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-05-30 20:53:57 +0200
committerPacien TRAN-GIRARD2015-05-30 20:53:57 +0200
commitbbe4569444a06dcb691b967f25e6ff9253623bb5 (patch)
tree4ad9b57a225a584e600b063d6959b154c9a393d0 /visualizer
parente1dad3dc1f70f5e6d360e5516a799481ea29ff2d (diff)
downloadmuseduino-bbe4569444a06dcb691b967f25e6ff9253623bb5.tar.gz
Import the third version of the python spectrum visualizer
Diffstat (limited to 'visualizer')
-rwxr-xr-xvisualizer/visualizer.py79
1 files changed, 39 insertions, 40 deletions
diff --git a/visualizer/visualizer.py b/visualizer/visualizer.py
index 872dd9d..cc856eb 100755
--- a/visualizer/visualizer.py
+++ b/visualizer/visualizer.py
@@ -1,60 +1,59 @@
1#!/usr/bin/python3 1#!/usr/bin/python3
2 2
3import serial 3import serial
4import matplotlib 4import json
5import matplotlib.pyplot as plt 5from sys import stdout
6import matplotlib.animation as animation 6
7from collections import OrderedDict
8 7
9nb_bins = 128
10baud_rate = 115200 8baud_rate = 115200
11port = "/dev/ttyACM0" 9port = "/dev/ttyACM0"
12 10
13bin_separator = ' '
14val_separator = ':'
15
16 11
17def recv(link, nb_val): 12def recv(link):
18 valid = False 13 while True:
19 while not valid:
20 line = link.readline().rstrip() 14 line = link.readline().rstrip()
21 15
22 try: 16 try:
23 line = line.decode("utf-8") 17 line = line.decode("utf-8")
24 except TypeError: 18 except UnicodeDecodeError:
25 valid = False 19 continue
20
21 try:
22 data = json.loads(line)
23 except ValueError:
26 continue 24 continue
27 25
28 raw_bins = line.split(bin_separator) 26 try:
29 valid = len(raw_bins) == nb_bins 27 spectrum = data["spectrum"]
30 28 fundamental_bin = data["fundamental_bin"]
31 if not valid: 29 fundamental_freq = data["fundamental_freq"]
30 midi_note = data["midi_note"]
31 except ValueError:
32 continue 32 continue
33 33
34 bins = OrderedDict() 34 return spectrum, fundamental_bin, fundamental_freq, midi_note
35 for b in raw_bins: 35
36 values = b.split(val_separator) 36
37 valid = len(values) == 2 37def show_spectrum(data):
38 if not valid: 38 for line in range(50, 0, -1):
39 continue 39 for ampl in data:
40 40
41 bins[int(values[0])] = int(values[1]) 41 if ampl >= line:
42 42 stdout.write('█')
43 return bins 43 else:
44 stdout.write(' ')
45
46 stdout.write("\n")
44 47
48 stdout.write("\n")
49 stdout.flush()
50
45 51
46if __name__ == '__main__': 52if __name__ == '__main__':
47 53
48 link = serial.Serial(port, baud_rate) 54 link = serial.Serial(port, baud_rate)
49 55
50 while 1: 56 while True:
51 data = recv(link, nb_bins) 57 spectrum, fundamental_bin, fundamental_freq, midi_note = recv(link)
52 x, y = list(data.keys()), list(data.values()) 58 show_spectrum(spectrum)
53 59 print("fundamental: bin %d -> %f Hz -> note %d" % (fundamental_bin, fundamental_freq, midi_note))
54 plt.cla()
55 plt.hist(y, bins=x)
56 plt.draw()
57 plt.show(block=False)
58
59
60