There was an opportunity to perform signal processing for high resolution sound sources a while ago. At that time, I searched various Python libraries that can read 24-bit wav, so I made a memo.
There is scipy.io.wavfile
as a library to read wav in Python,
This only supports wav with the number of quantization bits = {8,16,32,64,96,128}.
In other words, 24-bit wav cannot be read.
The most common high-resolution audio source in the street is 24-bit, so
This is a problem, so I tried to find out how to read 24bit wav.
--Do your best with the standard wave module
(I haven't implemented it, so I'm sorry if I make a mistake n)
It should be good if the bytes
object obtained byreadframes ()
is properly converted and stored in numpy.ndarray.
However, unpack processing seems to be a little troublesome because it is necessary to store a byte string in units of 24 bits in a 32-bit type.
scikits.audiolab http://cournape.github.io/audiolab/ A relatively old libsndfile wrapper. You can read and write wav in MATLAB-like notation.
test_audiolab.py
from scikits.audiolab import wavread
data, fs, fmt = wavread(fn)
--Since it is a wrapper of libsndfile, it can handle formats that support libsndfile other than wav (flac etc. are also possible)
--Installation is troublesome because it depends on libsndfile --The update has stopped since 2010 --So naturally Python3 is not supported
wavio https://github.com/WarrenWeckesser/wavio A relatively new library. (It seems that it is implemented internally using wave) It is possible to read and write wav of any number of fs / quantization bits including 24 bits.
test_wavio.py
import wavio
w = wavio.read(fn_in)
fs = w.rate
bit = 8 * w.sampwidth
data = w.data.T
data = data / float( 2**(bit-1) ) # -1.0 to 1.Normalize to 0
--Python3 compatible --Pure-python (no dependency on external lib) --API is also relatively easy to understand
--Only wav is supported
Personally, I feel that wavio
is the current optimal solution.
It has the drawback that it can only be used with wav, but at worst it can be converted with sox or ffmpeg.
If you don't want to increase the dependency on non-standard libraries, implement it gently with wave.
Recommended Posts