This commit is contained in:
Bruno Rybársky 2022-02-20 17:34:22 +01:00
commit 6fbbbbc8d4
4 changed files with 95 additions and 0 deletions

1
.gitignore vendored Normal file

@ -0,0 +1 @@
song.npy

17
visualiser/importer.py Normal file

@ -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)

66
visualiser/player.py Normal file

@ -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)

11
visualiser/plotter.py Normal file

@ -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()