Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
+
from scipy.signal import convolve
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
# Reverb function using convolution with a simple impulse response
|
| 8 |
+
def add_reverb(audio_segment, decay=0.5, delay=200):
|
| 9 |
+
# Convert AudioSegment to numpy array
|
| 10 |
+
samples = np.array(audio_segment.get_array_of_samples())
|
| 11 |
+
|
| 12 |
+
# Create a simple reverb impulse response
|
| 13 |
+
impulse_response = np.zeros(delay * 10)
|
| 14 |
+
impulse_response[::delay] = decay ** np.arange(len(impulse_response) // delay)
|
| 15 |
+
|
| 16 |
+
# Apply convolution for reverb
|
| 17 |
+
processed_samples = convolve(samples, impulse_response, mode='full')[:len(samples)]
|
| 18 |
+
processed_samples = np.int16(processed_samples / np.max(np.abs(processed_samples)) * 32767)
|
| 19 |
+
|
| 20 |
+
# Convert numpy array back to AudioSegment
|
| 21 |
+
processed_audio = AudioSegment(
|
| 22 |
+
processed_samples.tobytes(),
|
| 23 |
+
frame_rate=audio_segment.frame_rate,
|
| 24 |
+
sample_width=audio_segment.sample_width,
|
| 25 |
+
channels=audio_segment.channels
|
| 26 |
+
)
|
| 27 |
+
return processed_audio
|
| 28 |
+
|
| 29 |
+
# Wrapper function to apply selected effect
|
| 30 |
+
def apply_effect(audio_file, effect, reverb_decay, reverb_delay):
|
| 31 |
+
audio = AudioSegment.from_file(audio_file)
|
| 32 |
+
|
| 33 |
+
if effect == "Reverb":
|
| 34 |
+
audio = add_reverb(audio, decay=reverb_decay, delay=int(reverb_delay))
|
| 35 |
+
|
| 36 |
+
# Save the modified audio to a temporary file
|
| 37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 38 |
+
audio.export(temp_file.name, format="wav")
|
| 39 |
+
return temp_file.name
|
| 40 |
+
|
| 41 |
+
# Gradio Interface
|
| 42 |
+
def build_ui():
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("### Audio Effects with Gradio")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
audio_input = gr.Audio(source="upload", type="filepath", label="Upload Audio")
|
| 48 |
+
audio_output = gr.Audio(label="Processed Audio")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
effect = gr.Radio(["Reverb"], label="Choose Effect")
|
| 52 |
+
reverb_decay = gr.Slider(0.1, 1.0, value=0.5, step=0.1, label="Reverb Decay")
|
| 53 |
+
reverb_delay = gr.Slider(50, 500, value=200, step=10, label="Reverb Delay (ms)")
|
| 54 |
+
|
| 55 |
+
apply_button = gr.Button("Apply Effect")
|
| 56 |
+
|
| 57 |
+
apply_button.click(
|
| 58 |
+
fn=apply_effect,
|
| 59 |
+
inputs=[audio_input, effect, reverb_decay, reverb_delay],
|
| 60 |
+
outputs=[audio_output]
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return demo
|
| 64 |
+
|
| 65 |
+
# Launch Gradio App
|
| 66 |
+
demo = build_ui()
|
| 67 |
+
demo.launch()
|