import numpy as np
= '/kaggle/input/c2piano/C2.wav' filepath
Let’s discover some ways in which we can manipulate sound using a Python and a couple awesome open source projects.
Loading A Sound
import IPython
IPython.display.Audio(filepath)
Generating Sound
import librosa
import soundfile as sf
= librosa.load(filepath, sr=16000) # y is a numpy array of the wav file, sr = sample rate
y, sr = librosa.effects.pitch_shift(y, sr, n_steps=2) # shifted by 4 half steps
y_shifted 'shifted.wav', y_shifted, sr, subtype='PCM_24') # write to shifted.wav using soundfile
sf.write(= '/kaggle/working/shifted.wav'
shifted_path IPython.display.Audio(shifted_path)
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:5: FutureWarning: Pass sr=16000 as keyword args. From version 0.10 passing these as positional arguments will result in an error
"""
Visualizing Sound
import librosa.display # https://librosa.org/doc/main/troubleshooting.html
=sr) librosa.display.waveshow(y, sr
<librosa.display.AdaptiveWaveplot at 0x7faed03dce90>
import matplotlib.pyplot as plt
# https://librosa.org/doc/main/generated/librosa.display.specshow.html#librosa.display.specshow
= librosa.load(filepath, duration=15)
y, sr = plt.subplots(nrows=2, ncols=1, sharex=True)
fig, ax = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
D = librosa.display.specshow(D, y_axis='linear', x_axis='time',
img =sr, ax=ax[0])
sr0].set(title='Linear-frequency power spectrogram')
ax[0].label_outer()
ax[= 1024
hop_length = librosa.amplitude_to_db(np.abs(librosa.stft(y, hop_length=hop_length)),
D =np.max)
ref='log', sr=sr, hop_length=hop_length,
librosa.display.specshow(D, y_axis='time', ax=ax[1])
x_axis1].set(title='Log-frequency power spectrogram')
ax[1].label_outer()
ax[=ax, format="%+2.f dB") fig.colorbar(img, ax
<matplotlib.colorbar.Colorbar at 0x7faed02e0110>
🎶🎹 This Python notebook can be copied in my equivalent Kaggle post 🎶🎹