I have identified two worthy ways to do this.
Method 1: use the wavefile module
Use this method if you don't mind installing some additional libraries that included a bit of clutter on my Mac, but it was easy on my Ubuntu server.
https://github.com/vokimon/python-wavefile
import wavefile # returns the contents of the wav file as a double precision float array def wav_to_floats(filename = 'file1.wav'): w = wavefile.load(filename) return w[1][0] signal = wav_to_floats(sys.argv[1]) print "read "+str(len(signal))+" frames" print "in the range "+str(min(signal))+" to "+str(min(signal))
Method 2: use the wave module
Use this method if you need less problems installing modules.
It reads a wav file from the file system and converts it to a float in the range -1 to 1. It works with 16-bit files, and if they are> 1 channel, they will alternate the samples in the same way as they are found in the file. For other bit depths, change the "h" in the argument to struct.unpack according to the table at the bottom of this page:
https://docs.python.org/2/library/struct.html
It will not work for 24-bit files, since there is no data type that is 24 bits, so there is no way to tell struct.unpack what to do.
import wave import struct import sys def wav_to_floats(wave_file): w = wave.open(wave_file) astr = w.readframes(w.getnframes())
yeeking
source share