File size: 1,562 Bytes
42e7707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6993bfb
42e7707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import librosa
import numpy as np
import soundfile as sf
import tempfile

def change_pitch(audio_file, pitch_shift):
    # Load the audio file
    y, sr = librosa.load(audio_file, sr=None)
    
    # Apply pitch shifting
    y_shifted = librosa.effects.pitch_shift(y=y, sr=sr, n_steps=pitch_shift)
    
    # Save original and shifted audio for playback
    original_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
    shifted_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
    
    sf.write(original_path, y, sr)
    sf.write(shifted_path, y_shifted, sr)
    
    return original_path, shifted_path

with gr.Blocks() as demo:
    gr.Markdown("## 🎧 Audio Pitch Shifter")
    gr.Markdown("Upload an audio file, choose how many semitones to shift the pitch, and listen to the result.")
    
    with gr.Row():
        audio_input = gr.Audio(type="filepath", label="Upload Audio")
        pitch_slider = gr.Slider(-12, 12, value=0, step=0.1, label="Pitch Shift (semitones)")
        submit_btn = gr.Button("Apply Pitch Shift")
    
    with gr.Row():
        original_audio = gr.Audio(label="Original Audio", interactive=False)
        shifted_audio = gr.Audio(label="Pitch-Shifted Audio", interactive=False)
    
    submit_btn.click(
        fn=change_pitch,
        inputs=[audio_input, pitch_slider],
        outputs=[original_audio, shifted_audio]
    )

if __name__ == "__main__":
    # Fix: Add share=True to create a shareable link when localhost isn't accessible
    demo.launch(share=True)