tuning-alterer / app.py
vericudebuget's picture
Create app.py
b31f4bc verified
# app.py
import gradio as gr
from audio_tuner import change_audio_tuning
import os
def tuning_interface(audio_input, initial_tuning, desired_tuning):
"""
A wrapper function to connect the core logic to the Gradio interface.
Args:
audio_input (str): The temporary filepath of the uploaded audio provided by Gradio.
initial_tuning (float): The initial tuning frequency from the number input.
desired_tuning (float): The desired tuning frequency from the number input.
Returns:
(int, numpy.ndarray) or None: The processed audio data or None if an error occurs.
"""
if audio_input is None:
raise gr.Error("Please upload an audio file first.")
try:
# The first argument from the gr.Audio input is the filepath
return change_audio_tuning(audio_input, initial_tuning, desired_tuning)
except ValueError as e:
# Display errors gracefully in the Gradio interface
raise gr.Error(str(e))
except Exception as e:
raise gr.Error(f"An unexpected error occurred: {e}")
# --- Define the Gradio Interface ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# Audio Tuning Speed Adjuster
Upload an audio file, set its original tuning frequency (e.g., A4 = 440Hz),
and then set your desired tuning frequency (e.g., A4 = 432Hz).
The app will slow down or speed up the audio to match the desired tuning without digital pitch correction.
"""
)
with gr.Row():
with gr.Column():
audio_in = gr.Audio(type="filepath", label="Input Audio")
gr.Markdown("### Tuning Parameters")
initial_hz = gr.Number(label="Initial Tuning (Hz)", value=440)
desired_hz = gr.Number(label="Desired Tuning (Hz)", value=432)
submit_btn = gr.Button("Apply Tuning", variant="primary")
with gr.Column():
audio_out = gr.Audio(label="Tuned Audio", type="numpy")
submit_btn.click(
fn=tuning_interface,
inputs=[audio_in, initial_hz, desired_hz],
outputs=audio_out
)
gr.Examples(
examples=[
[None, 440, 432],
[None, 440, 528],
[None, 432, 444],
],
inputs=[audio_in, initial_hz, desired_hz],
outputs=audio_out,
fn=tuning_interface,
cache_examples=False # Set to True if you have example audio files
)
# --- Run the App ---
if __name__ == "__main__":
demo.launch()