scipy.io.wavfile.read | Return sample rate of wave file
Hello, Welcome to this tutorial. Here we shall learn about how to return the sample rate of a wave file in Python.
Let’s first understand what is a wave file. WAV is a Waveform Audio FIle.
—>>Microsoft and IBM together created this wave file as a raw audio format.
To know more about wave files, click here
We need to import the SciPy module.
To learn more about the SciPy module,
Return the sample rate of a wave file
In Python, it is very easy to find out the sample rate of a wave file. This is because of the open-source library- SciPy which contains definitions to perform Scientific computations.
The Scipy has several sub-packages like constants, integrate, stats, signal, etc.
To know about the functionalities of these sub-packages, Click on the below links.
In our tutorial, we shall learn about the IO sub-package of SciPy.
The SciPy.io is the Input and Output package that affords a wide range of functions to work almost with different formats of files. Some of these formats are −
- Matlab
- IDL
- Matrix Market
- Wave, etc.
The sub-package contains the wave file module that just has functions for reading and writing sound files in the format with .wav extension. When you read a file, you get the sampling rate and an array of data.
Let’s understand it with the below example.
from scipy.io import wavfile fs, data = wavfile.read('./output/audio.wav')
In our code, we have imported the wavfile module. When we invoke the read function of wavfile module by providing the path of the file whose sample rate is to be known.
We will get the sample rate and the data in the form of an array as the output.
OUTPUT:
43100,([[-1, -2],
[ 1, 1],
[-4, -3],
...,
[ 4, -2],
[-4, 2],
[ 4, -1]],)
The 1st value is the sample rate followed by the data of the provided wave file.
Leave a Reply