In case of vocal separation using Librosa, the vocal and background music can be plotted separately but I want to extract the audio from vocal part and the spectrum of vocal part is located in a variable named 'S_foreground' (please visit the above link for demonstration). How can I get the foreground (vocal) audio?
librosa uses soundfile and audioread for reading audio. As of v0. 7, librosa uses soundfile by default, and falls back on audioread only when dealing with codecs unsupported by soundfile (notably, MP3, and some variants of WAV). For a list of codecs supported by soundfile , see the libsndfile documentation.
You may have noticed that S_foreground
comes from S_full
which comes from a function called magphase
. According to the document about this function, it can
Separate a complex-valued spectrogram D into its magnitude (S) and phase (P) components, so that D = S * P.
Since the actual parameter taken by magphase
in
S_full, phase = librosa.magphase(librosa.stft(y))
is stft(y)
, which is the Short-Time Fourier Transform of y
, the initial ndarray
, I reckon what you need to do is to calculate a new D
:
D_foreground = S_foreground * phase
And throw it to the Inverse stft function (librosa.istft
):
y_foreground = librosa.istft(D_foreground)
After that, you can use the output function:
librosa.output.write_wav(output_file_path, y_foreground, sr)
To be honest, I am not familiar with these theoretical things (my poor output quality using this method might be a proof), but above is my guess on how you should export your audio. It turns out that the fidelity is very poor (at least in my case), so you might want to try some other software out if you really care about the audio quality.
the answer of @Alioth is working except:
librosa.output.write_wav(output_file_path, y_foreground, sr)
which the output method in librosa is deprecated, so the alternative solution could be the soundfile:
import soundfile as sf
sf.write('your_output_path.wav', y_foreground, sr)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With