From 6fbbbbc8d427c09d362a4de3ffa64148973cfd6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Sun, 20 Feb 2022 17:34:22 +0100 Subject: [PATCH] Init --- .gitignore | 1 + visualiser/importer.py | 17 +++++++++++ visualiser/player.py | 66 ++++++++++++++++++++++++++++++++++++++++++ visualiser/plotter.py | 11 +++++++ 4 files changed, 95 insertions(+) create mode 100644 .gitignore create mode 100644 visualiser/importer.py create mode 100644 visualiser/player.py create mode 100644 visualiser/plotter.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80c214c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +song.npy \ No newline at end of file diff --git a/visualiser/importer.py b/visualiser/importer.py new file mode 100644 index 0000000..944f48e --- /dev/null +++ b/visualiser/importer.py @@ -0,0 +1,17 @@ +import wave +import numpy + +# Read file to get buffer use a 16000 hz wav +ifile = wave.open("/home/bruno/song.wav") +samples = ifile.getnframes() +audio = ifile.readframes(samples) + +# Convert buffer to float32 using NumPy +audio_as_np_int16 = numpy.frombuffer(audio, dtype=numpy.int16) +audio_as_np_float32 = audio_as_np_int16.astype(numpy.float32) + +# Normalise float32 array so that values are between -1.0 and +1.0 +max_int16 = 2**15 +audio_normalised = audio_as_np_float32 / max_int16 +with open('song.npy', 'wb') as f: + numpy.save(f, audio_normalised) \ No newline at end of file diff --git a/visualiser/player.py b/visualiser/player.py new file mode 100644 index 0000000..f0bf446 --- /dev/null +++ b/visualiser/player.py @@ -0,0 +1,66 @@ +#import modules +import numpy as np +import time +import math +import pyaudio +import pygame as pg + +#load song.npy +data = np.load('song.npy') + +surface = pg.display.set_mode((800, 600)) + +progress = 0 + +average = 0 + +# define callback (2) +def callback(in_data, frame_count, time_info, status): + #read frames from data + global progress + global average + #print(frame_count) + #read frames from data + frames = data[progress:progress+frame_count] + #average half of the frames + framesx = [] + for i in range(0, len(frames), 80): + average = np.average(frames[i:i+2]) + framesx.append(average) + #print(len(framesx)) + for tmp in framesx: + #remap tmp from -1 to 1 to 0 to 255 + tmp = (tmp + 1) * 127.5 + #convert to int + tmx, _ = math.modf(tmp) + tmp = tmx * 255 + #set treshold + #print(tmp) + #create color + if tmp > 250: + xtmp = 255 + else: + xtmp = 0 + color = (xtmp, xtmp, xtmp) + #set screen to color + surface.fill(color) + #update progress + pg.display.flip() + #increment progress + progress += frame_count + #return frames + return (frames, pyaudio.paContinue) +#initialise pyaudio +pg.init() +p = pyaudio.PyAudio() +#play the song from data +stream = p.open(format=p.get_format_from_width(data.dtype.itemsize), + channels=1, + rate=16000, + output=True, + stream_callback=callback) + +stream.start_stream() + +while True: + time.sleep(0.1) \ No newline at end of file diff --git a/visualiser/plotter.py b/visualiser/plotter.py new file mode 100644 index 0000000..7343697 --- /dev/null +++ b/visualiser/plotter.py @@ -0,0 +1,11 @@ +# import numpy +import matplotlib.pyplot as plt +import numpy as np + +#load the saved data +data = np.load('song.npy') +#remap tmp from -1 to 1 to 0 to 255 +tmp = (data + 1) * 127.5 +#plot the data +plt.plot(tmp) +plt.show() \ No newline at end of file