Brief overview about some of the main python libraries which promote input and output of digital audio files.
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.display import Audio
import numpy as np
from _utils import *
from scipy.io import wavfile
%%time
try:
fs, x_scipy = wavfile.read('../_data/A4_cello01.wav')
x_scipy = x_scipy.astype(np.float32)
x_scipy /= np.abs(x_scipy).max(axis=0)
except:
print('Can not load audio!')
Wall time: 9.97 ms
audiovis(x_scipy, fs)
audio stereo
Audio(x_scipy.min(axis=1), rate=fs)
summary_audio(x_scipy)
L R min: -0.9645 -0.9377 1st Quar: -0.2134 -0.1654 median: -0.0060 -0.0115 mean: -0.0017 -0.0016 3rd Quar: 0.1955 0.1379 max: 1.0000 1.0000 sigma: 0.3149 0.2663
%%time
spectrogram(x_scipy, fs=fs, ws=2048)
audio stereo
Wall time: 871 ms
#wavfile.write('_output/audio_scipy.wav', fs, x_scipy)
Reading and writing audio files using pySoundFile.
Install by command line:
>>> conda install -c conda-forge pysoundfile
or
>>> pip install pysoundfile
import soundfile
%%time
try:
x_soundfile, fs = soundfile.read('../_data/A4_cello01.wav')
except:
print('Can not load audio!')
Wall time: 5.98 ms
audiovis(x_soundfile, fs)
audio stereo
Audio(x_soundfile.min(axis=1), rate=fs)
summary_audio(x_soundfile)
L R min: -0.2648 -0.2215 1st Quar: -0.0586 -0.0391 median: -0.0016 -0.0027 mean: -0.0005 -0.0004 3rd Quar: 0.0537 0.0326 max: 0.2745 0.2362 sigma: 0.0865 0.0629
%%time
spectrogram(x_soundfile)
audio stereo
Wall time: 842 ms
#soundfile.write('_output/audio_soundfile.wav', x_soundfile, fs)
import librosa
%%time
try:
x_librosa, fs = librosa.load('../_data/A4_cello01.wav', mono=False)
x_librosa = x_librosa.T
except:
print('Can not load audio!')
Wall time: 495 ms
audiovis(x_librosa, fs)
audio stereo
Audio(x_librosa.min(axis=1), rate=fs)
summary_audio(x_librosa)
L R min: -0.2633 -0.2182 1st Quar: -0.0585 -0.0390 median: -0.0017 -0.0028 mean: -0.0005 -0.0004 3rd Quar: 0.0536 0.0326 max: 0.2729 0.2319 sigma: 0.0865 0.0629
%%time
spectrogram(x_librosa)
audio stereo
Wall time: 643 ms
#librosa.output.write_wav('_output/audio_librosa.wav', x_librosa, fs)