Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,123 +1,75 @@
|
|
| 1 |
-
stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]
|
| 2 |
-
original_filename_base = os.path.basename(audio_file_path).rsplit('.', 1)[0]
|
| 3 |
-
stable_input_path = f"stable_input_{original_filename_base}.wav"
|
| 4 |
-
shutil.copy(audio_file_path, stable_input_path)
|
| 5 |
-
|
| 6 |
-
model_arg = "--two-stems=vocals" if "2 Stems" in stem_choice else ""
|
| 7 |
-
output_dir = "separated"
|
| 8 |
-
if os.path.exists(output_dir): shutil.rmtree(output_dir)
|
| 9 |
-
|
| 10 |
-
command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""
|
| 11 |
-
progress(0.2, desc="Running Demucs (this can take a minute)...")
|
| 12 |
-
|
| 13 |
-
process = await asyncio.create_subprocess_shell(
|
| 14 |
-
command,
|
| 15 |
-
stdout=asyncio.subprocess.PIPE,
|
| 16 |
-
stderr=asyncio.subprocess.PIPE)
|
| 17 |
-
|
| 18 |
-
stdout, stderr = await process.communicate()
|
| 19 |
-
|
| 20 |
-
if process.returncode != 0:
|
| 21 |
-
raise gr.Error(f"Demucs failed to run. Error: {stderr.decode()[:500]}")
|
| 22 |
-
|
| 23 |
-
progress(0.8, desc="Locating separated stem files...")
|
| 24 |
-
stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]
|
| 25 |
-
model_folder_name = next(os.walk(output_dir))[1][0]
|
| 26 |
-
stems_path = os.path.join(output_dir, model_folder_name, stable_filename_base)
|
| 27 |
-
|
| 28 |
-
if not os.path.exists(stems_path):
|
| 29 |
-
raise gr.Error(f"Demucs finished, but the output directory was not found!")
|
| 30 |
-
|
| 31 |
-
vocals_path = os.path.join(stems_path, "vocals.wav") if os.path.exists(os.path.join(stems_path, "vocals.wav")) else None
|
| 32 |
-
drums_path = os.path.join(stems_path, "drums.wav") if os.path.exists(os.path.join(stems_path, "drums.wav")) else None
|
| 33 |
-
bass_path = os.path.join(stems_path, "bass.wav") if os.path.exists(os.path.join(stems_path, "bass.wav")) else None
|
| 34 |
-
other_filename = "no_vocals.wav" if "2 Stems" in stem_choice else "other.wav"
|
| 35 |
-
other_path = os.path.join(stems_path, other_filename) if os.path.exists(os.path.join(stems_path, other_filename)) else None
|
| 36 |
-
|
| 37 |
-
os.remove(stable_input_path)
|
| 38 |
-
return vocals_path, drums_path, bass_path, other_path
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"An error occurred: {e}")
|
| 41 |
-
raise gr.Error(str(e))
|
| 42 |
-
|
| 43 |
-
def visualize_slices(stem_audio_data, progress=gr.Progress(track_tqdm=True)):
|
| 44 |
-
if stem_audio_data is None:
|
| 45 |
-
gr.Warning("This stem is empty. Cannot visualize.")
|
| 46 |
-
return None, None, None
|
| 47 |
-
|
| 48 |
-
sample_rate, y_int = stem_audio_data
|
| 49 |
-
y = librosa.util.buf_to_float(y_int)
|
| 50 |
-
|
| 51 |
-
progress(0.3, desc="Finding transients...")
|
| 52 |
-
onset_frames = librosa.onset.onset_detect(y=librosa.to_mono(y.T) if y.ndim > 1 else y, sr=sample_rate, wait=1, pre_avg=1, post_avg=1, post_max=1, delta=0.05)
|
| 53 |
-
onset_times = librosa.frames_to_time(onset_frames, sr=sample_rate)
|
| 54 |
-
|
| 55 |
-
progress(0.7, desc="Generating waveform plot...")
|
| 56 |
-
fig, ax = plt.subplots(figsize=(10, 3))
|
| 57 |
-
fig.patch.set_facecolor('#1f2937')
|
| 58 |
-
ax.set_facecolor('#111827')
|
| 59 |
-
librosa.display.waveshow(y, sr=sample_rate, ax=ax, color='#32f6ff', alpha=0.7)
|
| 60 |
-
for t in onset_times:
|
| 61 |
-
ax.axvline(x=t, color='#ff3b3b', linestyle='--', linewidth=1)
|
| 62 |
-
ax.tick_params(colors='gray'); ax.xaxis.label.set_color('gray'); ax.yaxis.label.set_color('gray')
|
| 63 |
-
ax.set_xlabel("Time (s)"); ax.set_ylabel("Amplitude"); ax.set_title("Detected Slices", color='white')
|
| 64 |
-
plt.tight_layout()
|
| 65 |
-
|
| 66 |
-
progress(1, desc="Done!")
|
| 67 |
-
return fig, onset_times, stem_audio_data
|
| 68 |
-
|
| 69 |
-
def preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):
|
| 70 |
-
if active_stem_audio is None or onset_times is None: return None
|
| 71 |
-
sample_rate, y = active_stem_audio; clicked_time = evt.index[0]
|
| 72 |
-
start_time = 0; end_time = len(y) / sample_rate
|
| 73 |
-
for i, t in enumerate(onset_times):
|
| 74 |
-
if t > clicked_time:
|
| 75 |
-
end_time = t; break
|
| 76 |
-
start_time = t
|
| 77 |
-
start_sample = librosa.time_to_samples(start_time, sr=sample_rate)
|
| 78 |
-
end_sample = librosa.time_to_samples(end_time, sr=sample_rate)
|
| 79 |
-
sliced_audio = y[start_sample:end_sample]
|
| 80 |
-
return (sample_rate, sliced_audio)
|
| 81 |
-
|
| 82 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
|
| 83 |
gr.Markdown("# 🎵 Loop Architect")
|
| 84 |
onset_times_state = gr.State(value=None)
|
| 85 |
active_stem_state = gr.State(value=None)
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=1):
|
| 89 |
gr.Markdown("### 1. Separate Stems")
|
| 90 |
audio_input = gr.Audio(type="filepath", label="Upload a Track")
|
| 91 |
stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")
|
| 92 |
submit_button = gr.Button("Separate Stems")
|
| 93 |
-
|
| 94 |
with gr.Column(scale=2):
|
| 95 |
with gr.Accordion("Separated Stems", open=True):
|
| 96 |
-
with gr.Row():
|
| 97 |
-
vocals_output = gr.Audio(label="Vocals", scale=
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
gr.Markdown("### Slice Editor")
|
| 110 |
-
|
|
|
|
| 111 |
preview_player = gr.Audio(label="Slice Preview")
|
| 112 |
|
| 113 |
-
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 114 |
stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 115 |
-
|
| 116 |
-
slice_vocals_btn.click(fn=visualize_slices, inputs=vocals_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 117 |
-
slice_drums_btn.click(fn=visualize_slices, inputs=drums_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 118 |
-
slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 119 |
-
slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 120 |
-
|
| 121 |
slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
|
| 2 |
gr.Markdown("# 🎵 Loop Architect")
|
| 3 |
onset_times_state = gr.State(value=None)
|
| 4 |
active_stem_state = gr.State(value=None)
|
| 5 |
+
vocals_bar_times_state = gr.State(value=None)
|
| 6 |
+
drums_bar_times_state = gr.State(value=None)
|
| 7 |
+
bass_bar_times_state = gr.State(value=None)
|
| 8 |
+
other_bar_times_state = gr.State(value=None)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
with gr.Row():
|
| 12 |
with gr.Column(scale=1):
|
| 13 |
gr.Markdown("### 1. Separate Stems")
|
| 14 |
audio_input = gr.Audio(type="filepath", label="Upload a Track")
|
| 15 |
stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")
|
| 16 |
submit_button = gr.Button("Separate Stems")
|
| 17 |
+
|
| 18 |
with gr.Column(scale=2):
|
| 19 |
with gr.Accordion("Separated Stems", open=True):
|
| 20 |
+
with gr.Row():
|
| 21 |
+
vocals_output = gr.Audio(label="Vocals", scale=2)
|
| 22 |
+
with gr.Column(scale=1):
|
| 23 |
+
slice_vocals_btn = gr.Button("Visualize Slices")
|
| 24 |
+
vocals_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")
|
| 25 |
+
create_vocals_loop_btn = gr.Button("Create Loop")
|
| 26 |
+
vocals_loop_output = gr.Audio(label="Vocals Loop", visible=False, scale=2)
|
| 27 |
+
vocals_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 28 |
+
with gr.Row():
|
| 29 |
+
drums_output = gr.Audio(label="Drums", scale=2)
|
| 30 |
+
with gr.Column(scale=1):
|
| 31 |
+
slice_drums_btn = gr.Button("Visualize Slices")
|
| 32 |
+
drums_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")
|
| 33 |
+
create_drums_loop_btn = gr.Button("Create Loop")
|
| 34 |
+
drums_loop_output = gr.Audio(label="Drums Loop", visible=False, scale=2)
|
| 35 |
+
drums_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 36 |
+
with gr.Row():
|
| 37 |
+
bass_output = gr.Audio(label="Bass", scale=2)
|
| 38 |
+
with gr.Column(scale=1):
|
| 39 |
+
slice_bass_btn = gr.Button("Visualize Slices")
|
| 40 |
+
bass_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")
|
| 41 |
+
create_bass_loop_btn = gr.Button("Create Loop")
|
| 42 |
+
bass_loop_output = gr.Audio(label="Bass Loop", visible=False, scale=2)
|
| 43 |
+
bass_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 44 |
+
with gr.Row():
|
| 45 |
+
other_output = gr.Audio(label="Other / Instrumental", scale=2)
|
| 46 |
+
with gr.Column(scale=1):
|
| 47 |
+
slice_other_btn = gr.Button("Visualize Slices")
|
| 48 |
+
other_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")
|
| 49 |
+
create_other_loop_btn = gr.Button("Create Loop")
|
| 50 |
+
other_loop_output = gr.Audio(label="Other Loop", visible=False, scale=2)
|
| 51 |
+
other_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
gr.Markdown("### Slice Editor")
|
| 55 |
+
sensitivity_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="Onset Sensitivity")
|
| 56 |
+
slice_plot = gr.Image(label="Click a region on the waveform to preview a slice")
|
| 57 |
preview_player = gr.Audio(label="Slice Preview")
|
| 58 |
|
| 59 |
+
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output, vocals_bar_times_state, drums_bar_times_state, bass_bar_times_state, other_bar_times_state])
|
| 60 |
stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 61 |
+
|
| 62 |
+
slice_vocals_btn.click(fn=visualize_slices, inputs=[vocals_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 63 |
+
slice_drums_btn.click(fn=visualize_slices, inputs=[drums_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 64 |
+
slice_bass_btn.click(fn=visualize_slices, inputs=[bass_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 65 |
+
slice_other_btn.click(fn=visualize_slices, inputs=[other_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 66 |
+
|
| 67 |
slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
|
| 68 |
|
| 69 |
+
create_vocals_loop_btn.click(fn=create_loop, inputs=[vocals_output, vocals_bar_times_state, vocals_loop_length], outputs=[vocals_loop_output, vocals_loop_download_btn])
|
| 70 |
+
create_drums_loop_btn.click(fn=create_loop, inputs=[drums_output, drums_bar_times_state, drums_loop_length], outputs=[drums_loop_output, drums_loop_download_btn])
|
| 71 |
+
create_bass_loop_btn.click(fn=create_loop, inputs=[bass_output, bass_bar_times_state, bass_loop_length], outputs=[bass_loop_output, bass_loop_download_btn])
|
| 72 |
+
create_other_loop_btn.click(fn=create_loop, inputs=[other_output, other_bar_times_state, other_loop_length], outputs=[other_loop_output, other_loop_download_btn])
|
| 73 |
+
|
| 74 |
+
|
| 75 |
demo.launch()
|