feat(ui): Implement interactive preview for sound sources
Browse filesThis commit introduces a live audio preview system, allowing users to instantly audition their selected SoundFont or 8-bit Synthesizer settings without running a full render. This significantly improves the sound design workflow.
535 unmodified lines5365375385395405411929 unmodified lines24712472247324742475247624772478247924802481305 unmodified lines278727882789279027912792535 unmodified linesprint("No notes found in this MIDI.")def scale_instrument_velocity(instrument, scale=0.8):for note in instrument.notes:note.velocity = max(1, min(127, int(note.velocity * scale)))1929 unmodified linesvalue=RENDER_TYPE_DESCRIPTIONS["Render as-is"], # Set initial valueelem_classes="description-box" # Optional: for CSS styling)# --- SoundFont Bank with 8-bit option ---soundfont_bank = gr.Dropdown([SYNTH_8_BIT_LABEL] + list(soundfonts_dict.keys()),label="SoundFont / Synthesizer",value=list(soundfonts_dict.keys())[0] if soundfonts_dict else SYNTH_8_BIT_LABEL)render_sample_rate = gr.Radio(["16000", "32000", "44100"],label="Audio Sample Rate",305 unmodified linesinputs=s8bit_echo_sustain,outputs=echo_sustain_settings)# Launch the Gradio appapp.queue().launch(inbrowser=True, debug=True)535 unmodified lines5365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686691929 unmodified lines25992600260126022603260426052606260726082609261026112612261326142615261626172618305 unmodified lines292429252926292729282929293029312932293329342935535 unmodified linesprint("No notes found in this MIDI.")def preview_sound_source(sound_source_name: str, *args):"""Generates a short audio preview for either a selected SoundFont or the8-bit Synthesizer, using the Super Mario Bros. theme as a test melody.This function acts as a router:- If a SoundFont is selected, it uses FluidSynth.- If the 8-bit Synthesizer is selected, it uses the internal `synthesize_8bit_style`function, capturing the current UI settings for an accurate preview.Args:sound_source_name (str): The name of the SoundFont or the 8-bit synth label.*args: Captures all current UI settings, which are passed to build anAppParameters object for the 8-bit synth preview.Returns:A Gradio-compatible audio tuple (sample_rate, numpy_array)."""srate = 44100 # Use a standard sample rate for all previews.# 1. Create a MIDI object in memory.preview_midi = pretty_midi.PrettyMIDI()# Use a lead instrument. Program 81 (Lead 2, sawtooth) is a good, bright default.instrument = pretty_midi.Instrument(program=81, is_drum=False, name="Preview Lead")# 2. Define the melody: Super Mario Bros. theme intro# - tempo: A brisk 200 BPM, so each 0.15s step is a 16th note.# - notes: A list of tuples (pitch, duration_in_steps)tempo = 200.0time_per_step = 60.0 / tempo / 2 # 16th note duration at this tempo# (Pitch, Duration in steps)# MIDI Pitch 60 = C4 (Middle C)melody_data = [(76, 1), (76, 2), (76, 2), (72, 1), (76, 2), # E E E C E(79, 4), (67, 4) # G G(low)]current_time = 0.0for pitch, duration_steps in melody_data:start_time = current_timeend_time = start_time + (duration_steps * time_per_step)# Add a tiny gap between notes to ensure they re-trigger clearlynote_end_time = end_time - 0.01note = pretty_midi.Note(velocity=120, # Use a high velocity for a bright, clear soundpitch=pitch,start=start_time,end=note_end_time)instrument.notes.append(note)current_time = end_timepreview_midi.instruments.append(instrument)# --- ROUTING LOGIC: Decide which synthesizer to use ---# CASE 1: 8-bit Synthesizer Previewif sound_source_name == SYNTH_8_BIT_LABEL:print("Generating preview for: 8-bit Synthesizer")try:# Create a temporary AppParameters object from the current UI settingsparams = AppParameters(**dict(zip(ALL_PARAM_KEYS, args)))# Use the internal synthesizer to render the preview MIDIaudio_waveform = synthesize_8bit_style(midi_data=preview_midi, fs=srate, params=params)# Normalize and prepare for Gradiopeak_val = np.max(np.abs(audio_waveform))if peak_val > 0:audio_waveform /= peak_val# The synth returns (channels, samples), Gradio needs (samples, channels)audio_out = (audio_waveform.T * 32767).astype(np.int16)print("8-bit preview generated successfully.")return (srate, audio_out)except Exception as e:print(f"An error occurred during 8-bit preview generation: {e}")return None# CASE 2: SoundFont Previewelse:soundfont_path = soundfonts_dict.get(sound_source_name)if not soundfont_path or not os.path.exists(soundfont_path):print(f"Preview failed: SoundFont file not found at '{soundfont_path}'")raise gr.Error(f"Could not find the SoundFont file for '{sound_source_name}'.")try:print(f"Generating preview for: {sound_source_name}")# Convert the in-memory MIDI object to a binary stream.midi_io = io.BytesIO()preview_midi.write(midi_io)midi_data = midi_io.getvalue()# Use the existing rendering function to generate the audio.# Ensure the output is a tuple (sample_rate, numpy_array)audio_out = midi_to_colab_audio(midi_data,soundfont_path=soundfont_path,sample_rate=srate,output_for_gradio=True)# Ensure the returned value is exactly what Gradio expects.# The function `midi_to_colab_audio` should return a NumPy array.# We must wrap it in a tuple with the sample rate.if isinstance(audio_out, np.ndarray):print("SoundFont preview generated successfully.")return (srate, audio_out)else:# If the rendering function fails, it might return something else.# We handle this to prevent the Gradio error.print("Preview failed: Rendering function did not return valid audio data.")return Noneexcept Exception as e:# Catch any other errors, including from FluidSynth, and report them.print(f"An error occurred during SoundFont preview generation: {e}")# It's better to return None than to crash the UI.# The error will be visible in the console.return Nonedef scale_instrument_velocity(instrument, scale=0.8):for note in instrument.notes:note.velocity = max(1, min(127, int(note.velocity * scale)))1929 unmodified linesvalue=RENDER_TYPE_DESCRIPTIONS["Render as-is"], # Set initial valueelem_classes="description-box" # Optional: for CSS styling)# --- SoundFont Bank with Preview Button ---with gr.Row(elem_id="soundfont_selector_row"):soundfont_bank = gr.Dropdown([SYNTH_8_BIT_LABEL] + list(soundfonts_dict.keys()),label="SoundFont / Synthesizer",value=list(soundfonts_dict.keys())[0] if soundfonts_dict else SYNTH_8_BIT_LABEL,scale=4 # Give the dropdown more space)# The preview button, with a speaker icon for clarity.preview_sf_button = gr.Button("🔊 Preview", scale=1)# This audio player is dedicated to playing the preview clips.# It's not interactive, as it's for output only.preview_sf_player = gr.Audio(label="SoundFont Preview", interactive=False, show_label=False)render_sample_rate = gr.Radio(["16000", "32000", "44100"],label="Audio Sample Rate",305 unmodified linesinputs=s8bit_echo_sustain,outputs=echo_sustain_settings)# --- Event listener for the unified sound source preview button ---preview_sf_button.click(fn=preview_sound_source,inputs=[soundfont_bank] + all_settings_components,outputs=[preview_sf_player])# Launch the Gradio appapp.queue().launch(inbrowser=True, debug=True)
app.py+148-5