File size: 2,652 Bytes
0ad74ed
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: audio_mixer"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", "    track_count = gr.State(1)\n", "    add_track_btn = gr.Button(\"Add Track\")\n", "\n", "    add_track_btn.click(lambda count: count + 1, track_count, track_count)\n", "\n", "    @gr.render(inputs=track_count)\n", "    def render_tracks(count):\n", "        audios = []\n", "        volumes = []\n", "        with gr.Row():\n", "            for i in range(count):\n", "                with gr.Column(variant=\"panel\", min_width=200):\n", "                    gr.Textbox(placeholder=\"Track Name\", key=f\"name-{i}\", show_label=False)\n", "                    track_audio = gr.Audio(label=f\"Track {i}\", key=f\"track-{i}\")\n", "                    track_volume = gr.Slider(0, 100, value=100, label=\"Volume\", key=f\"volume-{i}\")\n", "                    audios.append(track_audio)\n", "                    volumes.append(track_volume)\n", "\n", "            def merge(data):\n", "                sr, output = None, None\n", "                for audio, volume in zip(audios, volumes):\n", "                    sr, audio_val = data[audio]\n", "                    volume_val = data[volume]\n", "                    final_track = audio_val * (volume_val / 100)\n", "                    if output is None:\n", "                        output = final_track\n", "                    else:\n", "                        min_shape = tuple(min(s1, s2) for s1, s2 in zip(output.shape, final_track.shape))\n", "                        trimmed_output = output[:min_shape[0], ...][:, :min_shape[1], ...] if output.ndim > 1 else output[:min_shape[0]]\n", "                        trimmed_final = final_track[:min_shape[0], ...][:, :min_shape[1], ...] if final_track.ndim > 1 else final_track[:min_shape[0]]\n", "                        output += trimmed_output + trimmed_final\n", "                return (sr, output)\n", "\n", "            merge_btn.click(merge, set(audios + volumes), output_audio)\n", "\n", "    merge_btn = gr.Button(\"Merge Tracks\")\n", "    output_audio = gr.Audio(label=\"Output\", interactive=False)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}