Movatterモバイル変換


[0]ホーム

URL:


Open In App

In this article, we will explore the way of visualizing sounds waves using Python and Matplotlib.

Modules Needed

 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 usepip3 instead ofpip or you might create a virtual environment and run the above command.

Approach

  • Import matplotlib, Numpy, wave, and sys module.
  • Open the audio file using thewave.open() method.
  • Read all frames of the opened sound wave usingreadframes() function.
  • Store the frame rate in a variable using thegetframrate() 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.
 

Python3
# importsimportmatplotlib.pyplotaspltimportnumpyasnpimportwave,sys# shows the sound wavesdefvisualize(path:str):# reading the audio fileraw=wave.open(path)# reads all the frames# -1 indicates all or max framessignal=raw.readframes(-1)signal=np.frombuffer(signal,dtype="int16")# gets the frame ratef_rate=raw.getframerate()# to Plot the x-axis in seconds# you need get the frame rate# and divide by size of your signal# to create a Time Vector# spaced linearly with the size# of the audio filetime=np.linspace(0,# startlen(signal)/f_rate,num=len(signal))# using matplotlib to plot# creates a new figureplt.figure(1)# title of the plotplt.title("Sound Wave")# label of x-axisplt.xlabel("Time")# actual plottingplt.plot(time,signal)# shows the plot# in new windowplt.show()# you can also save# the plot using# plt.savefig('filename')if__name__=="__main__":# gets the command line Valuepath=sys.argv[1]visualize(path)

Output:

plotting sound in python

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 awav file. If you have some other file type then you can useffmpeg 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 the link https://file-examples.com/wp-content/uploads/2017/11/file_example_WAV_1MG.wav", 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 issoundwave.py and the name of the audio file issample_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.


Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp