音频读写


1. librosa.load

wav, sr = librosa.load(path, sr=22050)
wav <- [-1, 1]

https://librosa.org/doc/latest/generated/librosa.load.html

2. scipy.io.wavfile

samplerate, data = wavfile.read(wav_fname)
data <- 原数据类型

scipy.io.wavfile.write(filename, rate, data)
scipy.io.wavfile.write("xx.wav", 16000, (wav * np.iinfo(np.int16).max).astype(np.int16))
按data的原数据类型保存

https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html

https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html

3. soundfile

import soundfile as sf
data, samplerate = sf.read('xx.wav')
data <- [-1, 1]

sf.write('xx.wav', data, samplerate, subtype=None)
默认按'PCM_16'保存

https://pysoundfile.readthedocs.io/en/latest/index.html#soundfile.read

https://pysoundfile.readthedocs.io/en/latest/index.html#soundfile.write

4. Ipython

import IPython.display as ipd
ipd.Audio("xx.wav")
audio = [2, 5, 9, 33, 15] 或 [0.1, -0.22, 0.3, 0.2, -0.2]
ipd.Audio(audio, rate=sampling_rate)