我正在尝试隔离音频流的前景,然后使用librosa将其另存为独立音频流。
从看似relevant example开始。
我像在S_full
,S_foreground
和S_background
中的示例一样,将全部的前景和背景数据隔离开了,但是我不确定如何使用这些数据作为音频。
[我试图使用librosa.istft(...)进行转换,然后使用.wav
将其另存为soundfile.write(...)
文件,但剩下的文件大小大致正确,但数据不可用(?)。
任何人都可以形容或指出我的例子吗?
谢谢。
将最小的例子放在一起,istft()具有原始采样率实际上是有效的。
我会在某个地方找到我的错误。FWIW这是工作代码
import numpy as np
import librosa
from librosa import display
import soundfile
import matplotlib.pyplot as plt
y, sr = librosa.load('audio/rb-testspeech.mp3', duration=5)
S_full, phase = librosa.magphase(librosa.stft(y))
S_filter = librosa.decompose.nn_filter(S_full,
aggregate=np.median,
metric='cosine',
width=int(librosa.time_to_frames(2, sr=sr)))
S_filter = np.minimum(S_full, S_filter)
margin_i, margin_v = 2, 10
power = 2
mask_v = librosa.util.softmask(S_full - S_filter,
margin_v * S_filter,
power=power)
S_foreground = mask_v * S_full
full = librosa.amplitude_to_db(S_full, ref=np.max)
librosa.display.specshow(full, y_axis='log', sr=sr)
plt.title('Full spectrum')
plt.colorbar()
plt.tight_layout()
plt.show()
print("y({}): {}".format(len(y),y))
print("sr: {}".format(sr))
full_audio = librosa.istft(S_full)
foreground_audio = librosa.istft(S_foreground)
print("full({}): {}".format(len(full_audio), full_audio))
soundfile.write('orig.WAV', y, sr)
soundfile.write('full.WAV', full_audio, sr)
soundfile.write('foreground.WAV', foreground_audio, sr)