From bbe4569444a06dcb691b967f25e6ff9253623bb5 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 30 May 2015 20:53:57 +0200 Subject: Import the third version of the python spectrum visualizer --- visualizer/visualizer.py | 79 ++++++++++++++++++++++++------------------------ 1 file 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 @@ #!/usr/bin/python3 import serial -import matplotlib -import matplotlib.pyplot as plt -import matplotlib.animation as animation -from collections import OrderedDict +import json +from sys import stdout + -nb_bins = 128 baud_rate = 115200 port = "/dev/ttyACM0" -bin_separator = ' ' -val_separator = ':' - -def recv(link, nb_val): - valid = False - while not valid: +def recv(link): + while True: line = link.readline().rstrip() - + try: line = line.decode("utf-8") - except TypeError: - valid = False + except UnicodeDecodeError: + continue + + try: + data = json.loads(line) + except ValueError: continue - raw_bins = line.split(bin_separator) - valid = len(raw_bins) == nb_bins - - if not valid: + try: + spectrum = data["spectrum"] + fundamental_bin = data["fundamental_bin"] + fundamental_freq = data["fundamental_freq"] + midi_note = data["midi_note"] + except ValueError: continue - - bins = OrderedDict() - for b in raw_bins: - values = b.split(val_separator) - valid = len(values) == 2 - if not valid: - continue - - bins[int(values[0])] = int(values[1]) - - return bins + + 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 1: - data = recv(link, nb_bins) - x, y = list(data.keys()), list(data.values()) - - plt.cla() - plt.hist(y, bins=x) - plt.draw() - plt.show(block=False) - - - + 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)) -- cgit v1.2.3