File size: 2,581 Bytes
b31f4bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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()