Spaces:
Runtime error
Runtime error
File size: 2,726 Bytes
4908947 a27bf5b dd329e3 4908947 848bd00 4908947 a27bf5b dd329e3 4908947 a27bf5b 4908947 a27bf5b dd329e3 a27bf5b 39e2ba0 a27bf5b 4908947 a27bf5b 4908947 a27bf5b 4908947 39e2ba0 a27bf5b 4908947 848bd00 4908947 39e2ba0 a27bf5b 4908947 7b2659c ceea695 39e2ba0 a27bf5b 4908947 fee787d 7b2659c 4908947 80acfa0 4908947 dd329e3 4908947 |
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 |
import gradio as gr
import numpy as np
import librosa
# Function to load audio using librosa
def load_audio(file):
# Load the audio file and return the audio array and sample rate
audio, sr = librosa.load(file, sr=None)
return audio, sr
# Function to get a relevant audio segment based on onset detection
def get_segment(audio, sr):
# Calculate onset envelope and detect onsets
onset_env = librosa.onset.onset_strength(y=audio, sr=sr)
onset_frames = librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, backtrack=True)
# If no onsets are detected, return a segment from the beginning
if len(onset_frames) == 0:
return audio[:2048], sr # Return the first segment of 1 second
# Calculate energy over time
energy = np.array([np.sum(np.abs(audio[i:i + 2048]**2)) for i in range(0, len(audio), 2048)])
# Threshold to consider a segment musically relevant
energy_threshold = np.mean(energy) + np.std(energy)
# Find onsets that exceed the energy threshold
relevant_onsets = [f for f in onset_frames if f < len(energy) and energy[f] > energy_threshold]
# If no relevant onsets are found, fall back to the first detected onset
start_frame = relevant_onsets[0] if relevant_onsets else onset_frames[0]
start_sample = librosa.frames_to_samples(start_frame)
# Define a segment length in samples (1 second)
segment_length = sr # 1 second segment
end_sample = min(start_sample + segment_length, len(audio))
# Return the selected segment
return audio[start_sample:end_sample], sr
# Function to extend music by adding silence
def extend_music(file, added_minutes):
audio, sr = load_audio(file)
# Get a relevant segment from the audio
segment, sr = get_segment(audio, sr)
# Calculate the number of samples to add based on the duration in minutes
additional_samples = int(added_minutes * 60 * sr)
extended_audio = np.concatenate([segment, np.zeros(additional_samples)]) # Append zeros for silence
# Normalize audio to the range of [-1, 1]
extended_audio = extended_audio / np.max(np.abs(extended_audio)) # Avoid overflow
return extended_audio.astype(np.float32), sr # Return audio and sample rate
# Gradio UI setup
with gr.Blocks() as app:
gr.Markdown("# Audio Extender")
audio_input = gr.Audio(type="filepath", label="Upload Audio File")
added_minutes = gr.Slider(minimum=0, maximum=10, label="Additional Minutes")
audio_output = gr.Audio(type="numpy", label="Extended Audio")
submit_button = gr.Button("Extend Audio")
submit_button.click(extend_music, inputs=[audio_input, added_minutes], outputs=audio_output)
# Launch the app
app.launch()
|