2024-05-30 05:13:51 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
track_count = gr.State(1)
|
|
|
|
add_track_btn = gr.Button("Add Track")
|
|
|
|
|
2024-05-31 05:15:35 +08:00
|
|
|
add_track_btn.click(lambda count: count + 1, track_count, track_count)
|
|
|
|
|
2024-05-30 05:13:51 +08:00
|
|
|
@gr.render(inputs=track_count)
|
|
|
|
def render_tracks(count):
|
2024-05-31 05:15:35 +08:00
|
|
|
audios = []
|
|
|
|
volumes = []
|
2024-05-30 05:13:51 +08:00
|
|
|
with gr.Row():
|
|
|
|
for i in range(count):
|
2024-05-31 05:15:35 +08:00
|
|
|
with gr.Column(variant="panel", min_width=200):
|
|
|
|
gr.Textbox(placeholder="Track Name", key=f"name-{i}", show_label=False)
|
2024-05-30 05:13:51 +08:00
|
|
|
track_audio = gr.Audio(label=f"Track {i}", key=f"track-{i}")
|
2024-05-31 05:15:35 +08:00
|
|
|
track_volume = gr.Slider(0, 100, value=100, label="Volume", key=f"volume-{i}")
|
|
|
|
audios.append(track_audio)
|
|
|
|
volumes.append(track_volume)
|
|
|
|
|
|
|
|
def merge(data):
|
|
|
|
sr, output = None, None
|
|
|
|
for audio, volume in zip(audios, volumes):
|
|
|
|
sr, audio_val = data[audio]
|
|
|
|
volume_val = data[volume]
|
|
|
|
final_track = audio_val * (volume_val / 100)
|
|
|
|
if output is None:
|
|
|
|
output = final_track
|
|
|
|
else:
|
|
|
|
min_shape = tuple(min(s1, s2) for s1, s2 in zip(output.shape, final_track.shape))
|
|
|
|
trimmed_output = output[:min_shape[0], ...][:, :min_shape[1], ...] if output.ndim > 1 else output[:min_shape[0]]
|
2024-06-26 18:05:32 +08:00
|
|
|
trimmed_final = final_track[:min_shape[0], ...][:, :min_shape[1], ...] if final_track.ndim > 1 else final_track[:min_shape[0]]
|
2024-05-31 05:15:35 +08:00
|
|
|
output += trimmed_output + trimmed_final
|
|
|
|
return (sr, output)
|
2024-07-30 13:08:51 +08:00
|
|
|
|
2024-05-31 05:15:35 +08:00
|
|
|
merge_btn.click(merge, set(audios + volumes), output_audio)
|
2024-05-30 05:13:51 +08:00
|
|
|
|
2024-05-31 05:15:35 +08:00
|
|
|
merge_btn = gr.Button("Merge Tracks")
|
2024-06-05 05:19:30 +08:00
|
|
|
output_audio = gr.Audio(label="Output", interactive=False)
|
2024-07-30 13:08:51 +08:00
|
|
|
|
2024-05-30 05:13:51 +08:00
|
|
|
if __name__ == "__main__":
|
2024-06-26 18:05:32 +08:00
|
|
|
demo.launch()
|