pitch_shifted_spectogram.py
1.1 KB
import numpy as np
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Load the audio file
audio_file = "./sounds/s4.mp3"
audio, sr = librosa.load(audio_file, sr=None)
# Compute the original Mel spectrogram
mel_spec_orig = librosa.feature.melspectrogram(y=audio, sr=sr)
# Pitch shift parameters
pitch_shift_factor = 2.0
# Apply pitch shifting
audio_pitch_shifted = librosa.effects.pitch_shift(y=audio, sr=sr, n_steps=pitch_shift_factor, bins_per_octave=1)
mel_spec_pitch_shifted = librosa.feature.melspectrogram(y=audio_pitch_shifted, sr=sr)
# Display the original Mel spectrogram
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
librosa.display.specshow(librosa.power_to_db(mel_spec_orig, ref=np.max), sr=sr, x_axis='time', y_axis='mel')
plt.title('Original Mel Spectrogram')
#plt.colorbar(format='%+2.0f dB')
# Display the pitch-shifted Mel spectrogram
plt.subplot(1, 2, 2)
librosa.display.specshow(librosa.power_to_db(mel_spec_pitch_shifted, ref=np.max), sr=sr, x_axis='time', y_axis='mel')
plt.title('Pitch-Shifted Mel Spectrogram')
#plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.show()