Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,56 +4,46 @@ import librosa
|
|
| 4 |
|
| 5 |
# Function to load audio using librosa
|
| 6 |
def load_audio(file):
|
| 7 |
-
# Load the audio file and return the audio array and sample rate
|
| 8 |
audio, sr = librosa.load(file, sr=None)
|
| 9 |
return audio, sr
|
| 10 |
|
| 11 |
# Function to get a relevant audio segment based on onset detection
|
| 12 |
def get_segment(audio, sr):
|
| 13 |
-
# Calculate onset envelope and detect onsets
|
| 14 |
onset_env = librosa.onset.onset_strength(y=audio, sr=sr)
|
| 15 |
onset_frames = librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, backtrack=True)
|
| 16 |
|
| 17 |
-
# If no onsets are detected, return a segment from the beginning
|
| 18 |
if len(onset_frames) == 0:
|
| 19 |
-
return audio[:2048], sr # Return the first
|
| 20 |
|
| 21 |
# Calculate energy over time
|
| 22 |
energy = np.array([np.sum(np.abs(audio[i:i + 2048]**2)) for i in range(0, len(audio), 2048)])
|
| 23 |
|
| 24 |
-
# Threshold
|
| 25 |
energy_threshold = np.mean(energy) + np.std(energy)
|
| 26 |
-
|
| 27 |
-
# Find onsets that exceed the energy threshold
|
| 28 |
relevant_onsets = [f for f in onset_frames if f < len(energy) and energy[f] > energy_threshold]
|
| 29 |
|
| 30 |
-
# If no relevant onsets are found, fall back to the first detected onset
|
| 31 |
start_frame = relevant_onsets[0] if relevant_onsets else onset_frames[0]
|
| 32 |
start_sample = librosa.frames_to_samples(start_frame)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
segment_length = sr # 1 second segment
|
| 36 |
end_sample = min(start_sample + segment_length, len(audio))
|
| 37 |
|
| 38 |
-
# Return the selected segment
|
| 39 |
return audio[start_sample:end_sample], sr
|
| 40 |
|
| 41 |
# Function to extend music by adding silence
|
| 42 |
def extend_music(file, added_minutes):
|
| 43 |
audio, sr = load_audio(file)
|
| 44 |
-
|
| 45 |
-
# Get a relevant segment from the audio
|
| 46 |
segment, sr = get_segment(audio, sr)
|
| 47 |
|
| 48 |
-
# Calculate
|
| 49 |
additional_samples = int(added_minutes * 60 * sr)
|
| 50 |
-
extended_audio = np.concatenate([segment, np.zeros(additional_samples)]) #
|
| 51 |
|
| 52 |
# Normalize audio to the range of [-1, 1]
|
| 53 |
-
extended_audio = extended_audio / np.max(np.abs(extended_audio))
|
| 54 |
|
| 55 |
# Return the audio as a NumPy array and the sample rate as an integer
|
| 56 |
-
return extended_audio.astype(np.float32), sr
|
| 57 |
|
| 58 |
# Gradio UI setup
|
| 59 |
with gr.Blocks() as app:
|
|
@@ -66,5 +56,5 @@ with gr.Blocks() as app:
|
|
| 66 |
submit_button = gr.Button("Extend Audio")
|
| 67 |
submit_button.click(extend_music, inputs=[audio_input, added_minutes], outputs=audio_output)
|
| 68 |
|
| 69 |
-
# Launch the app
|
| 70 |
-
app.launch()
|
|
|
|
| 4 |
|
| 5 |
# Function to load audio using librosa
|
| 6 |
def load_audio(file):
|
|
|
|
| 7 |
audio, sr = librosa.load(file, sr=None)
|
| 8 |
return audio, sr
|
| 9 |
|
| 10 |
# Function to get a relevant audio segment based on onset detection
|
| 11 |
def get_segment(audio, sr):
|
|
|
|
| 12 |
onset_env = librosa.onset.onset_strength(y=audio, sr=sr)
|
| 13 |
onset_frames = librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, backtrack=True)
|
| 14 |
|
|
|
|
| 15 |
if len(onset_frames) == 0:
|
| 16 |
+
return audio[:2048], sr # Return the first 2048 samples if no onsets
|
| 17 |
|
| 18 |
# Calculate energy over time
|
| 19 |
energy = np.array([np.sum(np.abs(audio[i:i + 2048]**2)) for i in range(0, len(audio), 2048)])
|
| 20 |
|
| 21 |
+
# Threshold for musical relevance
|
| 22 |
energy_threshold = np.mean(energy) + np.std(energy)
|
|
|
|
|
|
|
| 23 |
relevant_onsets = [f for f in onset_frames if f < len(energy) and energy[f] > energy_threshold]
|
| 24 |
|
|
|
|
| 25 |
start_frame = relevant_onsets[0] if relevant_onsets else onset_frames[0]
|
| 26 |
start_sample = librosa.frames_to_samples(start_frame)
|
| 27 |
|
| 28 |
+
segment_length = sr # Length of segment in samples (1 second)
|
|
|
|
| 29 |
end_sample = min(start_sample + segment_length, len(audio))
|
| 30 |
|
|
|
|
| 31 |
return audio[start_sample:end_sample], sr
|
| 32 |
|
| 33 |
# Function to extend music by adding silence
|
| 34 |
def extend_music(file, added_minutes):
|
| 35 |
audio, sr = load_audio(file)
|
|
|
|
|
|
|
| 36 |
segment, sr = get_segment(audio, sr)
|
| 37 |
|
| 38 |
+
# Calculate additional samples
|
| 39 |
additional_samples = int(added_minutes * 60 * sr)
|
| 40 |
+
extended_audio = np.concatenate([segment, np.zeros(additional_samples)]) # Add silence
|
| 41 |
|
| 42 |
# Normalize audio to the range of [-1, 1]
|
| 43 |
+
extended_audio = extended_audio / np.max(np.abs(extended_audio)) if np.max(np.abs(extended_audio)) > 0 else extended_audio
|
| 44 |
|
| 45 |
# Return the audio as a NumPy array and the sample rate as an integer
|
| 46 |
+
return extended_audio.astype(np.float32), sr # Ensure it's a float32 NumPy array
|
| 47 |
|
| 48 |
# Gradio UI setup
|
| 49 |
with gr.Blocks() as app:
|
|
|
|
| 56 |
submit_button = gr.Button("Extend Audio")
|
| 57 |
submit_button.click(extend_music, inputs=[audio_input, added_minutes], outputs=audio_output)
|
| 58 |
|
| 59 |
+
# Launch the app with a public link
|
| 60 |
+
app.launch(share=True)
|