https://github.com/melizalab/py-ewave
Advantages:
Disadvantages:
Installation:
python3 -m pip install ewave
import ewave
with ewave.open('data/test_wav_pcm16.wav') as w:
print("samplerate = {0.sampling_rate} Hz, length = {0.nframes} samples, "
"channels = {0.nchannels}, dtype = {0.dtype!r}".format(w))
data = w.read()
samplerate = 44100 Hz, length = 15 samples, channels = 7, dtype = dtype('int16')
data
memmap([[ 32767, 28086, 23405, 18724, 14043, 9362, 4681], [ 29522, 17511, 5208, -4166, -8756, -8435, -4681], [ 20430, -6250, -21087, -16870, -3125, 5837, 4681], [ 7291, -25305, -14593, 11674, 12652, -2083, -4681], [ -7291, -25305, 14593, 11674, -12652, -2083, 4681], [-20430, -6250, 21087, -16870, 3125, 5837, -4681], [-29522, 17511, -5208, -4166, 8756, -8435, 4681], [-32767, 28086, -23405, 18724, -14043, 9362, -4681], [-29522, 17511, -5208, -4166, 8756, -8435, 4681], [-20430, -6250, 21087, -16870, 3125, 5837, -4681], [ -7291, -25305, 14593, 11674, -12652, -2083, 4681], [ 7291, -25305, -14593, 11674, 12652, -2083, -4681], [ 20430, -6250, -21087, -16870, -3125, 5837, 4681], [ 29522, 17511, 5208, -4166, -8756, -8435, -4681], [ 32767, 28086, 23405, 18724, 14043, 9362, 4681]], dtype=int16)
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.plot(data);
Obviously, the returned samples have the dtype
'int16'
(as stored in the file).
To be able to do something useful, we'll have to convert it to floating point and normalize it to a range from -1 to 1.
Luckily, there is a function especially made for this:
np.set_printoptions(precision=4)
ewave.rescale(data, 'float32')
array([[ 1. , 0.8571, 0.7143, 0.5714, 0.4286, 0.2857, 0.1429], [ 0.9009, 0.5344, 0.1589, -0.1271, -0.2672, -0.2574, -0.1429], [ 0.6235, -0.1907, -0.6435, -0.5148, -0.0954, 0.1781, 0.1429], [ 0.2225, -0.7722, -0.4453, 0.3563, 0.3861, -0.0636, -0.1429], [-0.2225, -0.7722, 0.4453, 0.3563, -0.3861, -0.0636, 0.1429], [-0.6235, -0.1907, 0.6435, -0.5148, 0.0954, 0.1781, -0.1429], [-0.9009, 0.5344, -0.1589, -0.1271, 0.2672, -0.2574, 0.1429], [-1. , 0.8571, -0.7143, 0.5714, -0.4286, 0.2857, -0.1429], [-0.9009, 0.5344, -0.1589, -0.1271, 0.2672, -0.2574, 0.1429], [-0.6235, -0.1907, 0.6435, -0.5148, 0.0954, 0.1781, -0.1429], [-0.2225, -0.7722, 0.4453, 0.3563, -0.3861, -0.0636, 0.1429], [ 0.2225, -0.7722, -0.4453, 0.3563, 0.3861, -0.0636, -0.1429], [ 0.6235, -0.1907, -0.6435, -0.5148, -0.0954, 0.1781, 0.1429], [ 0.9009, 0.5344, 0.1589, -0.1271, -0.2672, -0.2574, -0.1429], [ 1. , 0.8571, 0.7143, 0.5714, 0.4286, 0.2857, 0.1429]], dtype=float32)
Note: until version 1.0.4 this was broken, but now it works (see https://github.com/melizalab/py-ewave/issues/4).
Files with floating point data can be used, WAVEX is supported:
with ewave.open('data/test_wav_float32.wav') as w:
data = w.read()
plt.plot(data);
Looking good!
with ewave.open('data/test_wavex_pcm16.wav') as w:
data = w.read()
plt.plot(data);
with ewave.open('data/test_wavex_float32.wav') as w:
data = w.read()
plt.plot(data);
Opening a 24-bit PCM file fails with a not very verbose error message:
import traceback
try:
ewave.open('data/test_wav_pcm24.wav')
except:
traceback.print_exc()
else:
print("It works (unexpectedly)!")
Traceback (most recent call last): File "<ipython-input-10-5697a06b57c7>", line 3, in <module> ewave.open('data/test_wav_pcm24.wav') File "/home/mg/.local/lib/python3.7/site-packages/ewave.py", line 91, in __init__ self._load_header() File "/home/mg/.local/lib/python3.7/site-packages/ewave.py", line 271, in _load_header chunk.skip() File "/usr/lib/python3.7/chunk.py", line 160, in skip self.file.seek(n, 1) File "/usr/lib/python3.7/chunk.py", line 113, in seek raise RuntimeError RuntimeError
# TODO!
print("ewave:", ewave.__version__)
import numpy, IPython, sys
print("NumPy: {}; IPython: {}".format(numpy.__version__, IPython.__version__))
print("Python interpreter:")
print(sys.version)
ewave: 1.0.4 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]