High resolution acoustic signal processing (1) --How to read 24-bit wav file with Python

Overview

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.

Candidate

--Do your best with the standard wave module

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)

advantage

--Since it is a wrapper of libsndfile, it can handle formats that support libsndfile other than wav (flac etc. are also possible)

Disadvantage

--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

advantage

--Python3 compatible --Pure-python (no dependency on external lib) --API is also relatively easy to understand

Disadvantage

--Only wav is supported

So which one should I use after all?

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

High resolution acoustic signal processing (1) --How to read 24-bit wav file with Python
How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
Acoustic signal processing with Python
How to read an Excel file (.xlsx) with Pandas [Python]
How to do multi-core parallel processing with python
How to measure mp3 file playback time with python
How to convert JSON file to CSV file with Python Pandas
[Python] How to deal with pandas read_html read error
How to switch the configuration file to be read by Python
[Python Kivy] How to create an exe file with pyinstaller
Python: How to use async with
[Python] Write to csv file with Python
Output to csv file with Python
MP3 to WAV conversion with Python
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to make a command to read the configuration file with pyramid
[ROS2] How to play a bag file with python format launch
Upload & download wav file to X server (X-Server) by FTP with Python
[Python] How to convert db file to csv
Change Python 64bit environment to 32bit environment with Anaconda
How to read pydoc on python interpreter
Read CSV file with python (Download & parse CSV file)
How to convert Python to an exe file
How to work with BigQuery in Python
Let's read the RINEX file with Python ①
How to do portmanteau test with python
How to display python Japanese with lolipop
[Python] How to read a csv file (read_csv method of pandas module)
How to enter Japanese with Python curses
Read CSV file with Python and convert it to DataFrame as it is
[Python] How to deal with module errors
How to read problem data with paiza
How to install python3 with docker centos
3. Natural language processing with Python 1-2. How to create a corpus: Aozora Bunko
[Automation] Read mail (msg file) with Python
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
How to drop Google Docs in one folder in a .txt file with python
How to upload with Heroku, Flask, Python, Git (4)
[REAPER] How to play with Reascript in Python
Strategy on how to monetize with Python Java
How to achieve time wait processing with wxpython
How to create a JSON file in Python
[Python] How to draw multiple graphs with Matplotlib
Convert svg file to png / ico with Python
How to crop an image with Python + OpenCV
Read table data in PDF file with Python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to read an array with Python's ConfigParser
How to specify attributes with Mock of python
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
Wav file generation from numeric text with python
Read line by line from a file with Python
[Python] How to handle Japanese characters with openCV
I want to write to a file with Python
Read wav files with only Python standard packages
How to read a file in a different directory
Materials to read when getting started with Python