diff options
author | Pacien TRAN-GIRARD | 2015-05-30 20:53:57 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-05-30 20:53:57 +0200 |
commit | bbe4569444a06dcb691b967f25e6ff9253623bb5 (patch) | |
tree | 4ad9b57a225a584e600b063d6959b154c9a393d0 | |
parent | e1dad3dc1f70f5e6d360e5516a799481ea29ff2d (diff) | |
download | museduino-bbe4569444a06dcb691b967f25e6ff9253623bb5.tar.gz |
Import the third version of the python spectrum visualizer
-rwxr-xr-x | visualizer/visualizer.py | 79 |
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 | ||
3 | import serial | 3 | import serial |
4 | import matplotlib | 4 | import json |
5 | import matplotlib.pyplot as plt | 5 | from sys import stdout |
6 | import matplotlib.animation as animation | 6 | |
7 | from collections import OrderedDict | ||
8 | 7 | ||
9 | nb_bins = 128 | ||
10 | baud_rate = 115200 | 8 | baud_rate = 115200 |
11 | port = "/dev/ttyACM0" | 9 | port = "/dev/ttyACM0" |
12 | 10 | ||
13 | bin_separator = ' ' | ||
14 | val_separator = ':' | ||
15 | |||
16 | 11 | ||
17 | def recv(link, nb_val): | 12 | def 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 | 37 | def 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 | ||
46 | if __name__ == '__main__': | 52 | if __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 | |||