Basic pitch shift using stream methods in sounddevice module for python

The sounddevice module in Python provides a convenient way to manipulate audio streams. In this article, we will explore different ways to perform a basic pitch shift using stream methods in the sounddevice module.

Option 1: Changing the playback speed

One way to achieve a pitch shift is by changing the playback speed of the audio stream. By increasing or decreasing the speed, we can effectively change the pitch of the audio. Here’s how you can do it:

import sounddevice as sd

def pitch_shift(stream, factor):
    sd.play(stream, samplerate=int(stream.samplerate * factor))

# Example usage
stream = sd.read('audio.wav')
pitch_shift(stream, 1.5)  # Increase pitch by 50%

This approach works by adjusting the sample rate of the audio stream. However, it may introduce artifacts and distortions in the audio due to the change in speed.

Option 2: Time stretching

Another way to achieve a pitch shift is by using time stretching algorithms. Time stretching allows us to change the duration of the audio without affecting the pitch. Here’s an example:

import sounddevice as sd
import librosa

def pitch_shift(stream, factor):
    y = stream.flatten()
    y_stretch = librosa.effects.time_stretch(y, factor)
    sd.play(y_stretch, samplerate=stream.samplerate)

# Example usage
stream = sd.read('audio.wav')
pitch_shift(stream, 0.8)  # Decrease pitch by 20%

This approach uses the librosa library to perform time stretching on the audio stream. It provides better quality pitch shifting compared to changing the playback speed.

Option 3: Harmonic/Percussive separation

Harmonic/Percussive separation is a technique used in audio signal processing to separate the harmonic and percussive components of a sound. By manipulating these components separately, we can achieve a pitch shift effect. Here’s an example:

import sounddevice as sd
import librosa

def pitch_shift(stream, factor):
    y = stream.flatten()
    y_harmonic, y_percussive = librosa.effects.hpss(y)
    y_harmonic_shift = librosa.effects.pitch_shift(y_harmonic, stream.samplerate, n_steps=factor)
    y_shifted = y_harmonic_shift + y_percussive
    sd.play(y_shifted, samplerate=stream.samplerate)

# Example usage
stream = sd.read('audio.wav')
pitch_shift(stream, 2)  # Increase pitch by 2 semitones

This approach separates the harmonic and percussive components of the audio stream using the librosa library. It then applies pitch shifting only to the harmonic component and combines it with the original percussive component. This method provides the best quality pitch shifting with minimal artifacts.

After exploring these different options, it is clear that Option 3, which utilizes harmonic/percussive separation, provides the best quality pitch shifting with minimal artifacts. It allows for more control over the pitch shift effect and produces more natural-sounding results. However, the choice of method ultimately depends on the specific requirements and constraints of your project.

Rate this post

3 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents