Spaces:
Sleeping
Sleeping
ziqiangao commited on
Commit ·
62ee3db
1
Parent(s): a310a15
try it on here
Browse files
app.py
CHANGED
|
@@ -1,7 +1,75 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
from scipy import signal
|
| 5 |
|
| 6 |
+
def extract_phantom_center_test(input_file, output_format, rdf=0.99999):
|
| 7 |
+
output_file = f"other.{output_format}"
|
| 8 |
+
output_center_file = f"center.{output_format}"
|
| 9 |
+
data, samplerate = sf.read(input_file)
|
| 10 |
+
|
| 11 |
+
if data.ndim != 2 or data.shape[1] != 2:
|
| 12 |
+
raise ValueError("A stereo file is required (2 channels)")
|
| 13 |
|
| 14 |
+
left = data[:, 0]
|
| 15 |
+
right = data[:, 1]
|
| 16 |
+
mono = np.mean(data, axis=1)
|
| 17 |
+
|
| 18 |
+
nperseg = samplerate # window size
|
| 19 |
+
noverlap = nperseg // 2 # overlap
|
| 20 |
+
|
| 21 |
+
f, t, Z_left = signal.stft(left, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 22 |
+
f, t, Z_right = signal.stft(right, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 23 |
+
f, t, Z_mono = signal.stft(mono, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 24 |
+
|
| 25 |
+
Z_common_left = np.minimum(np.abs(Z_left), np.abs(Z_right)) * np.exp(1j * np.angle(Z_mono))
|
| 26 |
+
Z_common_right = np.minimum(np.abs(Z_left), np.abs(Z_right)) * np.exp(1j * np.angle(Z_mono))
|
| 27 |
+
|
| 28 |
+
reduction_factor = rdf
|
| 29 |
+
|
| 30 |
+
Z_new_left = Z_left - Z_common_left * reduction_factor
|
| 31 |
+
Z_new_right = Z_right - Z_common_right * reduction_factor
|
| 32 |
+
|
| 33 |
+
_, new_left = signal.istft(Z_new_left, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 34 |
+
_, new_right = signal.istft(Z_new_right, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 35 |
+
|
| 36 |
+
_, common_signal_left = signal.istft(Z_common_left, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 37 |
+
_, common_signal_right = signal.istft(Z_common_right, fs=samplerate, nperseg=nperseg, noverlap=noverlap)
|
| 38 |
+
|
| 39 |
+
new_left = new_left[:len(left)]
|
| 40 |
+
new_right = new_right[:len(right)]
|
| 41 |
+
common_signal_left = common_signal_left[:len(left)]
|
| 42 |
+
common_signal_right = common_signal_right[:len(right)]
|
| 43 |
+
|
| 44 |
+
peak = max(np.max(np.abs(new_left)), np.max(np.abs(new_right)))
|
| 45 |
+
if peak > 1.0:
|
| 46 |
+
new_left /= peak
|
| 47 |
+
new_right /= peak
|
| 48 |
+
|
| 49 |
+
sf.write(output_file, np.column_stack((new_left, new_right)), samplerate)
|
| 50 |
+
|
| 51 |
+
sf.write(output_center_file, np.column_stack((common_signal_left, common_signal_right)), samplerate)
|
| 52 |
+
|
| 53 |
+
return output_file, output_center_file
|
| 54 |
+
|
| 55 |
+
with gr.Blocks(title="Phantom Center Extraction", theme=gr.themes.Soft()) as demo:
|
| 56 |
+
gr.Markdown("# Phantom Center Extraction")
|
| 57 |
+
|
| 58 |
+
input_audio = gr.Audio(label="Upload stereo audio", type="filepath")
|
| 59 |
+
reduction_f = gr.Slider(0.1, 1.0, value=0.99999, step=0.00001, label="Reduction Factor (rdf)", interactive=True)
|
| 60 |
+
output_format = gr.Dropdown(choices=["flac", "wav"], value="flac", label="Export format")
|
| 61 |
+
|
| 62 |
+
extract_btn = gr.Button("Separate")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
side_audio = gr.Audio(label="Other audio", type="filepath", interactive=False)
|
| 66 |
+
center_audio = gr.Audio(label="Phantom center audio", type="filepath", interactive=False)
|
| 67 |
+
|
| 68 |
+
extract_btn.click(
|
| 69 |
+
fn=extract_phantom_center_test,
|
| 70 |
+
inputs=[input_audio, output_format, reduction_f],
|
| 71 |
+
outputs=[side_audio, center_audio]
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch(server_name="0.0.0.0")
|