Diferentes módulos de Python para leer wav:
Existen al menos las siguientes bibliotecas para leer archivos de audio wave:
El ejemplo más simple:
Este es un ejemplo simple con SoundFile:
import soundfile as sf
data, samplerate = sf.read('existing_file.wav')
Formato de la salida:
Advertencia, los datos no siempre están en el mismo formato, eso depende de la biblioteca. Por ejemplo:
from scikits import audiolab
from scipy.io import wavfile
from sys import argv
for filepath in argv[1:]:
x, fs, nb_bits = audiolab.wavread(filepath)
print('Reading with scikits.audiolab.wavread:', x)
fs, x = wavfile.read(filepath)
print('Reading with scipy.io.wavfile.read:', x)
Salida:
Reading with scikits.audiolab.wavread: [ 0. 0. 0. ..., -0.00097656 -0.00079346 -0.00097656]
Reading with scipy.io.wavfile.read: [ 0 0 0 ..., -32 -26 -32]
SoundFile y Audiolab devuelven flota entre -1 y 1 (como hace matab, esa es la convención para las señales de audio). Scipy y wave devuelven enteros, que puede convertir en flotantes de acuerdo con la cantidad de bits de codificación, por ejemplo:
from scipy.io.wavfile import read as wavread
samplerate, x = wavread(audiofilename)
if x.dtype == 'int16':
nb_bits = 16
elif x.dtype == 'int32':
nb_bits = 32
max_nb_bit = float(2 ** (nb_bits - 1))
samples = x / (max_nb_bit + 1)