Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,21 @@ import librosa
|
|
| 3 |
import numpy as np
|
| 4 |
import soundfile as sf
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
# Load audio
|
| 8 |
y, sr = librosa.load(audio_path, sr=None)
|
| 9 |
-
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
beat_frames = librosa.frames_to_samples(beats)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
segments = [y_reversed[beat_frames[i]:beat_frames[i+1]] for i in range(len(beat_frames)-1)]
|
| 17 |
|
| 18 |
# Reverse each segment back to forward
|
|
@@ -25,11 +30,11 @@ def reverse_segments(audio_path):
|
|
| 25 |
return output_path
|
| 26 |
|
| 27 |
iface = gr.Interface(
|
| 28 |
-
fn=
|
| 29 |
inputs=gr.Audio(type="filepath"),
|
| 30 |
outputs=gr.Audio(),
|
| 31 |
-
title="Beat-Reversed Music",
|
| 32 |
-
description="
|
| 33 |
)
|
| 34 |
|
| 35 |
iface.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import soundfile as sf
|
| 5 |
|
| 6 |
+
def reverse_segments_advanced(audio_path):
|
| 7 |
+
# Load audio with high precision
|
| 8 |
y, sr = librosa.load(audio_path, sr=None)
|
|
|
|
| 9 |
|
| 10 |
+
# Advanced BPM detection
|
| 11 |
+
onset_env = librosa.onset.onset_strength(y=y, sr=sr, aggregate=np.median)
|
| 12 |
+
tempo, beats = librosa.beat.beat_track(y=y, sr=sr, onset_envelope=onset_env, trim=False)
|
| 13 |
|
| 14 |
+
# Convert beats to exact sample positions
|
| 15 |
beat_frames = librosa.frames_to_samples(beats)
|
| 16 |
+
|
| 17 |
+
# Reverse the entire audio first
|
| 18 |
+
y_reversed = y[::-1]
|
| 19 |
+
|
| 20 |
+
# Cut into beat-aligned segments
|
| 21 |
segments = [y_reversed[beat_frames[i]:beat_frames[i+1]] for i in range(len(beat_frames)-1)]
|
| 22 |
|
| 23 |
# Reverse each segment back to forward
|
|
|
|
| 30 |
return output_path
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
+
fn=reverse_segments_advanced,
|
| 34 |
inputs=gr.Audio(type="filepath"),
|
| 35 |
outputs=gr.Audio(),
|
| 36 |
+
title="High-Precision Beat-Reversed Music",
|
| 37 |
+
description="More accurate BPM detection for reversing music and playing beat-aligned segments forward."
|
| 38 |
)
|
| 39 |
|
| 40 |
iface.launch()
|