The module audioread
can be found there: https://github.com/beetbox/audioread
This isn't really an option because it has the same disadvantages as the wave module (and more) and it has to be installed manually (but at least it's available as Debian package).
Advantages:
Disadvantages:
Installation:
python3 -m pip install audioread
I don't recommend the module audioread
, but you can have a look how it is supposed to work, anyway.
First, let's enable inline plotting and load the NumPy stuff:
import matplotlib.pyplot as plt
import numpy as np
Following code should read a 16-bit WAV file.
To convert it to floating point values and to normalize to a range from -1 to 1, we're using the function pcm2float()
, located in the file utility.py.
import audioread
import utility
temp = bytearray()
with audioread.audio_open('data/test_wav_pcm16.wav') as f:
framerate = f.samplerate
duration = f.duration
nchannels = f.channels
print("sampling rate = {framerate} Hz, duration = {duration} seconds, channels = {nchannels}".format(**locals()))
# chunk size can be specified with 'block_samples' (default 1024):
for chunk in f.read_data():
temp.extend(chunk)
sig = np.frombuffer(temp, dtype='<i2').reshape(-1, nchannels)
normalized = utility.pcm2float(sig, np.float32)
plt.plot(normalized);
sampling rate = 44100 Hz, duration = 0.0003401360544217687 seconds, channels = 7
TODO
import sys, numpy, IPython
print("Versions: NumPy = {}; IPython = {}".format(numpy.__version__, IPython.__version__))
print("Python interpreter:")
print(sys.version)
print("audioread version =", audioread.__version__)
Versions: NumPy = 1.16.2; IPython = 7.5.0.dev Python interpreter: 3.7.4 (default, Jul 11 2019, 10:43:21) [GCC 8.3.0] audioread version = 2.1.8