DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on

4

Recover background audio with librosa and saving it with soundfile

In my previous article I separated the vocals from the instruments but didn't save it. This article is aimed towards saving the audio from IPython.Audio in IPython. The full spectrum, foreground spectrum and background spectrum from my previous article looked like this: Audio spectrum of the track

The other goal I tied myself to was to play the audio from the instruments only which I'll also show in this article.

To save the audio, we use soundfile's write() function and get the audio data, sample rate and save it as .wav

import soundfile as sf

sf.write('Instruments.wav', x_background, sr, subtype='PCM_24')
y, sr = librosa.load('Instruments.wav')
ipd.Audio('Instruments.wav')
Enter fullscreen mode Exit fullscreen mode

In recovering the background audio or instrument audio from the masked spectrum, we only use the istft() function from librosa.

# Recover the background audio from the masked spectrogram
x_background = librosa.istft(S_background * phase)
# playback audio
ipd.Audio(data=x_background[90*sr:110*sr], rate=sr)
Enter fullscreen mode Exit fullscreen mode

That's it.

To view code from the Jupyter notebook you can head over to this article

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more