Skip to main content

PySox Examples

PySox How To Examples in Python

PySox is a Python library that provides a simple interface to the SoX (Sound eXchange) command-line utility. SoX is a cross-platform command-line utility for sound processing.

In this tutorial, we will explore various examples of how to use the PySox library in Python. We will cover different functionalities such as audio file manipulation, audio effects, and audio format conversion.

Prerequisites

Before we begin, make sure you have installed the PySox library. You can install it using pip:

pip install pysox

Example 1: Reading an Audio File

To start, let's learn how to read an audio file using PySox. We will use the sox.file_info function to get information about the audio file.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Get information about the audio file
info = tfm.file_info('input.wav')
print(info)

Expected Output:

{
'channels': 2,
'sample_rate': 44100,
'bit_depth': 16,
'duration': 3.456
}

In this example, we created a sox.Transformer object and used the file_info method to retrieve information about the audio file 'input.wav'. The output provides details such as the number of channels, sample rate, bit depth, and duration of the audio file.

Example 2: Changing Audio Format

Next, let's see how to change the audio format of an audio file. We will use the sox.build function to specify the desired output format.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Set the input and output formats
tfm.set_input_format(file_type='wav')
tfm.set_output_format(file_type='mp3')

# Convert the audio file
tfm.build('input.wav', 'output.mp3')

In this example, we created a sox.Transformer object and used the set_input_format and set_output_format methods to specify the input and output formats, respectively. We then used the build method to convert the audio file 'input.wav' to the MP3 format and save it as 'output.mp3'.

Example 3: Applying Audio Effects

PySox allows us to apply various audio effects to modify the audio file. Let's see how to add echo effect to an audio file.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Add an echo effect
tfm.echo()

# Apply the effect to the audio file
tfm.build('input.wav', 'output.wav')

In this example, we added an echo effect to the audio file by calling the echo method on the sox.Transformer object. The build method is then used to apply the effect to the audio file 'input.wav' and save the modified file as 'output.wav'.

Example 4: Changing Playback Speed

We can also change the playback speed of an audio file using PySox. Let's see how to increase the playback speed by 1.5 times.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Increase the playback speed by 1.5 times
tfm.speed(factor=1.5)

# Apply the speed change to the audio file
tfm.build('input.wav', 'output.wav')

In this example, we used the speed method to increase the playback speed of the audio file by a factor of 1.5. The modified audio file is saved as 'output.wav'.

Example 5: Applying Fade In and Fade Out

PySox allows us to apply fade in and fade out effects to an audio file. Let's see how to add a fade in effect for the first 2 seconds and a fade out effect for the last 2 seconds.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Add a fade in effect for the first 2 seconds
tfm.fade(fade_in_len=2)

# Add a fade out effect for the last 2 seconds
tfm.fade(fade_out_len=2)

# Apply the effects to the audio file
tfm.build('input.wav', 'output.wav')

In this example, we added a fade in effect for the first 2 seconds and a fade out effect for the last 2 seconds of the audio file. The modified audio file is saved as 'output.wav'.

Example 6: Concatenating Audio Files

PySox allows us to concatenate multiple audio files into a single audio file. Let's see how to concatenate two audio files 'input1.wav' and 'input2.wav'.

import sox

# Create a SoX combiner object
comb = sox.Combiner()

# Concatenate two audio files
comb.build(['input1.wav', 'input2.wav'], 'output.wav', 'concatenate')

In this example, we created a sox.Combiner object and used the build method to concatenate the audio files 'input1.wav' and 'input2.wav'. The concatenated audio file is saved as 'output.wav'.

Example 7: Splitting an Audio File

We can also split an audio file into multiple segments using PySox. Let's see how to split an audio file 'input.wav' into two segments, each of 5 seconds.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Split the audio file into two segments of 5 seconds each
tfm.build('input.wav', 'output.wav', splice={'start': 0, 'duration': 5}, splice={'start': 5, 'duration': 5})

In this example, we used the build method to split the audio file 'input.wav' into two segments. The first segment starts at 0 seconds and has a duration of 5 seconds, while the second segment starts at 5 seconds and also has a duration of 5 seconds. The split audio segments are saved as 'output.wav'.

Example 8: Applying Volume Adjustment

PySox allows us to adjust the volume of an audio file. Let's see how to increase the volume of an audio file by 10 decibels.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Increase the volume by 10 decibels
tfm.vol(10)

# Apply the volume adjustment to the audio file
tfm.build('input.wav', 'output.wav')

In this example, we used the vol method to increase the volume of the audio file by 10 decibels. The modified audio file is saved as 'output.wav'.

Example 9: Applying High-pass Filter

We can also apply a high-pass filter to an audio file using PySox. Let's see how to apply a high-pass filter with a cutoff frequency of 1000 Hz.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Apply a high-pass filter with a cutoff frequency of 1000 Hz
tfm.highpass(1000)

# Apply the filter to the audio file
tfm.build('input.wav', 'output.wav')

In this example, we used the highpass method to apply a high-pass filter with a cutoff frequency of 1000 Hz to the audio file. The modified audio file is saved as 'output.wav'.

Example 10: Combining Multiple Effects

PySox allows us to combine multiple effects and apply them to an audio file. Let's see how to combine the fade in, fade out, and echo effects.

import sox

# Create a SoX transformer object
tfm = sox.Transformer()

# Add a fade in effect for the first 2 seconds
tfm.fade(fade_in_len=2)

# Add a fade out effect for the last 2 seconds
tfm.fade(fade_out_len=2)

# Add an echo effect
tfm.echo()

# Apply the effects to the audio file
tfm.build('input.wav', 'output.wav')

In this example, we combined the fade in, fade out, and echo effects by calling the corresponding methods on the sox.Transformer object. The modified audio file is saved as 'output.wav'.

These are just a few examples of what you can do with the PySox library. Explore the PySox documentation for more functionalities and options.