In this article, we will explore the way of visualizing sounds waves using Python and Matplotlib.
1. Matplotlib: Install Matplotlib using the below command:
pip install matplotlib
2. Numpy: Numpy gets installed automatically installed with Matplotlib. Although, if you face any import error, use the below command to install Numpy
pip install numpy
Note: If you are on Linux like me, then you might need to use pip3 instead of pip or you might create a virtual environment and run the above command.
- Import matplotlib, Numpy, wave, and sys module.
- Open the audio file using the wave.open() method.
- Read all frames of the opened sound wave using readframes() function.
- Store the frame rate in a variable using the getframrate() function.
- Finally, plot the x-axis in seconds using frame rate.
- Use the matplotlib.figure() function to plot the derived graph
- Use labels as per the requirement.
Below is the implementation.
So, we are done with coding, now it’s the moment of truth. Let’s check if it actually works or not. You can try out any audio file but make sure that it has to be a wav file. If you have some other file type then you can use ffmpeg to convert it to wav file. If you want then feel free to download the audio file we will be using. You can download it using this link, but do try out other files too.
To run the code, you need to pass the path of the audio file in the command line. To do that type the following in your terminal:
python soundwave.py sample_audio.wav
It is important to note that name of the Python file is soundwave.py and the name of the audio file is sample_audio.wav. You need to change these according to your system. Now, a new window should have popped up and should be seeing a sound wave plot. If you have used my audio, then your plot should look something like this.
- Plotting the Growth Curve of Coronavirus in various Countries using Python
- Python Bokeh - Plotting Horizontal Bar Graphs
- Python Bokeh - Plotting Vertical Bar Graphs
- Python | Visualize graphs generated in NetworkX using Matplotlib
- 3D Wireframe plotting in Python using Matplotlib
- Python | Matplotlib Sub plotting using object oriented API
- Python | Matplotlib Graph plotting using object oriented API
- 3D Contour Plotting in Python using Matplotlib
- Plotting Histogram in Python using Matplotlib
- 3D Surface plotting in Python using Matplotlib
- 3D Scatter Plotting in Python using Matplotlib
- Plotting cross-spectral density in Python using Matplotlib
- Three-dimensional Plotting in Python using Matplotlib
- Different plotting using pandas and matplotlib
- Plotting graph For IRIS Dataset Using Seaborn And Matplotlib
- Python | Plot different graphs using plotly and cufflinks
- Operations on Graph and Special Graphs using Networkx module | Python
- Plot Live Graphs using Python Dash and Plotly
- Histogram Plotting and stretching in Python (without using inbuilt function)
- Corona Virus cases of various countries - Using Python PyQt
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.