Open In App

Getting started with PySoundFile

Improve
Improve
Like Article
Like
Save
Share
Report

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

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('noise.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




# importing 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:



Last Updated : 20 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads