使用libros分离音频前景并转换回音频流

2024-10-04 03:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试隔离音频流的前景,然后使用librosa将其保存为独立的音频流。你知道吗

从这个看起来relevant example开始。你知道吗

我有完整的、前景的和背景的数据,就像在S_fullS_foregroundS_background中的例子那样,但是我不确定如何使用这些数据作为音频。你知道吗

我试图使用librosa.istft(...)来转换它们,然后使用soundfile.write(...)将其保存为.wav文件,但我只剩下一个大小大致合适但无法使用的文件(?)数据。你知道吗

有人能给我举个例子吗?你知道吗

谢谢。你知道吗


Tags: 文件数据example音频full例子writebackground
1条回答
网友
1楼 · 发布于 2024-10-04 03:23:20

把最简单的例子放在一起, 具有原始采样率的istft()实际上起作用。你知道吗

我会在某处找到我的虫子。 这是工作代码

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) 

相关问题 更多 >