audio_experiment/visualiser/importer.py
2022-02-20 17:34:22 +01:00

17 lines
778 B
Python

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)