Skip to content
Related Articles

Related Articles

Improve Article

Getting started with PySoundFile

  • Last Updated : 24 Oct, 2020

PySoundFile is a Python module used for reading and writing audio files,  see an audio file as NumPy array including of pitches and all. This module can read the audio file i.e. it extracts the NumPy array from audio(.wav files) and able to write it too

Installation: Run the following pip command:

pip install PySoundFile 

PySoundFile supports all the format that libsndfile supports, for example WAV, FLAC, OGG and MAT files

Here’s the link of the audio file used in the examples link

Reading audio file:



Audio files can be read using the read() function.

Syntax: read(file, frames=-1, start=0, stop=None, fill_value=None, samplerate=None, channels=None, subtype=None)

Parameters: 

  • file: path of the audio file
  • frames: This argument is used to specify number of frames to read, by default it has -1 value indicating whole file to read
  • start, stop: To specify start and end to read audio file, By default whole file is read
  • fill_value: If there is less data left in the file than requested, the rest of the frames are filled with fill_value. If no fill_value is specified, the smaller array is returned

Returns: 2 values

  • data: numpy array ( length should be 44,100 elements) If returned is 1-D numpy array then audio read is mono channeled
  • samplerate: sample rate (or “sampling rate”) defines how many times per second a sound is sampled.  Technically , it is the frequency of samples used in a digital recording. The standard sample rate used for audio CDs is 44.1 kilohertz for this file it is same too

Supported file formats: WAV ,AIFF, AU, PAF, SVX, NIST, VOC, IRCAM, W64, MAT4, MAT5, PVF, XI, HTK, SDS, AVR, WAVEX, SD2, FLAC, CAF, WVE, OGG, MPC2K, RF64     

Example: We will be reading the following file: 

Python3




# import the module
import soundfile as sf
  
# read the file
data, samplerate = sf.read('noice.wav')
  
# display the data
print(data)
print("---------------------")
print("Sample Rate is ", samplerate)
print("---------------------")
print("Done")

Output:



Writing a file: We can write a file using the write() function.

Syntax: write(file, data, samplerate, subtype=None, endian=None, format=None, closefd=True)

Parameters: 

  • file: path of the output file
  • data: data to be written
  • samplerate: sample rate of the audio data

Returns: Nothing

To write an audio file, we need NumPy array (i.e. data) as sample rate has standard value, it opens the file in wb mode i.e. if the same name exists then the program will rewrite it  

Example: We will be reading the following file:

Python3




# imporintg the module
import soundfile as sf
  
# reading the file
data, samplerate = sf.read('Sample.wav')
  
# writing the file
sf.write('writing_file_output.wav', data, samplerate)
  
# You may compare both audio, link is given in the output

Output: This is the output file:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :