在使用Librosa进行人声分离的情况下,可以单独绘制人声和背景音乐,但我想从人声部分提取音频,人声部分的频谱位于名为“S_foreground”的变量中(请访问上面的链接进行演示)。如何获得前景(人声)音频?
2 回答
您可能已经注意到,S_foreground
来自S_full
which 来自一个名为 的函数magphase
。根据有关此功能的文档,它可以
将复值频谱图 D 分成其幅度 (S) 和相位 (P) 分量,使得 D = S * P。
由于magphase
in 采用的实际参数
S_full, phase = librosa.magphase(librosa.stft(y))
是stft(y)
,这是 的短时傅立叶变换y
,初始ndarray
,我认为你需要做的是计算一个新的D
:
D_foreground = S_foreground * phase
并将其扔给逆 stft 函数 ( librosa.istft
):
y_foreground = librosa.istft(D_foreground)
之后,您可以使用输出功能:
librosa.output.write_wav(output_file_path, y_foreground, sr)
老实说,我对这些理论上的东西并不熟悉(我使用这种方法的输出质量差可能就是一个证明),但以上是我对如何导出音频的猜测。事实证明,保真度很差(至少在我的情况下),所以如果你真的关心音频质量,你可能想尝试其他一些软件。
@Alioth 的答案是有效的,除了:
librosa.output.write_wav(output_file_path, y_foreground, sr)
不推荐使用 librosa 中的输出方法,因此替代解决方案可能是声音文件:
import soundfile as sf
sf.write('your_output_path.wav', y_foreground, sr)