Waveform display of audio in Python

Introduction

A memo when writing the code to display the waveform of the voice. It is easy to use librosa.display.waveplot of the library called librosa, but since an error occurs when the number of data in the audio file is large, the method of displaying using matplotlib.pyplot is also described.

How to use librosa.display.waveplot

The easiest way.

code

import librosa

def make_waveform_librosa(filename):
    y, sr = librosa.load(filename)
    librosa.display.waveplot(y, sr=sr)

make_waveform_librosa("test.mp3")

result

image.png However, if the number of data in the audio file to be read is large, OverflowError: Exceeded cell block limit may occur and the waveform may not be displayed properly.

How to use matplotlib.pyplot

Since the chunk size of the agg running on the back end of matplotlib can be changed, it is possible to handle even when the number of data in the audio file is large.

code

import matplotlib.pyplot as plt
import matplotlib
import librosa

def make_waveform_pyplot(filename):
    y, sr = librosa.load(filename)
    totaltime = len(y)/sr
    time_array = np.arange(0, totaltime, 1/sr)
    mpl.rcParams['agg.path.chunksize'] = 100000
    fig, ax = plt.subplots()
    formatter = mpl.ticker.FuncFormatter(lambda s, x: time.strftime('%M:%S', time.gmtime(s)))
    ax.xaxis.set_major_formatter(formatter)
    ax.set_xlim(0, totaltime)
    ax.set_xlabel("Time")
    ax.plot(time_array, y)
    plt.show()

make_waveform_pyplot("test.mp3")

The chunk size was written as follows when referring to here, so I set it to 100000 as appropriate.

### Agg rendering
### Warning: experimental, 2008/10/10
#agg.path.chunksize : 0           # 0 to disable; values in the range
                                  # 10000 to 100000 can improve speed slightly
                                  # and prevent an Agg rendering failure
                                  # when plotting very large data sets,
                                  # especially if they are very gappy.
                                  # It may cause minor artifacts, though.
                                  # A value of 20000 is probably a good
                                  # starting point.

result

image.png I was able to output the same thing as librosa.display.waveplot.

in conclusion

Since librosa.display.waveplot also uses matplotlib internally, I think that it will be possible to display it by playing with chunksize in the same way, but I think that it is necessary to play with the code of the library directly.

Recommended Posts

Waveform display of audio in Python
Display a list of alphabets in Python 3
Waveform envelope in Python
Draw mp3 waveform in Python
Display UTM-30LX data in Python
Equivalence of objects in Python
Implementation of quicksort in Python
Display a histogram of image brightness values in python
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Display characters like AA in python
[ev3dev × Python] Display, audio, LED control
Separate display of Python graphs (memo)
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Display LaTeX notation formulas in Python, matplotlib
[Python, Julia] 3D display in Jupyter-Mayavi library
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
Comparison of Japanese conversion module in Python3
Display Python 3 in the browser with MAMP
Summary of various for statements in Python
The result of installing python in Anaconda
How to display multiplication table in python
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
Non-logical operator usage of or in python
Display pyopengl in a browser (python + eel)
In search of the fastest FizzBuzz in Python
Practical example of Hexagonal Architecture in Python
How to display Hello world in python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Get rid of DICOM images in Python
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Introduction of Python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Discord in Python
DCI in Python
quicksort in python
nCr in python