7

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?

Graham
  • 7,431
  • 18
  • 59
  • 84
Saha Reno
  • 71
  • 1
  • 2
  • 2
    Questions on Stack Overflow should be as much as possible self-contained so that can be useful to future readers and include a [mcve] exemplifying the issue. – user2314737 Feb 11 '18 at 09:34

2 Answers2

17

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.

Alioth
  • 171
  • 1
  • 3
1

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)
Ali Tavana
  • 358
  • 2
  • 5