Các mô-đun Python khác nhau để đọc wav:
Có ít nhất các thư viện sau để đọc các tệp âm thanh sóng:
Ví dụ đơn giản nhất:
Đây là một ví dụ đơn giản với SoundFile:
import soundfile as sf
data, samplerate = sf.read('existing_file.wav')
Định dạng của đầu ra:
Cảnh báo, dữ liệu không phải lúc nào cũng ở cùng một định dạng, điều đó phụ thuộc vào thư viện. Ví dụ:
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)
Đầu ra:
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]
Trả về SoundFile và Audiolab nổi giữa -1 và 1 (như matab, đó là quy ước cho tín hiệu âm thanh). Các số nguyên trả về dạng sóng và dạng sóng mà bạn có thể chuyển đổi thành số nổi theo số bit mã hóa, ví dụ:
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)