Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import librosa
|
3 |
+
import numpy as np
|
4 |
+
import soundfile as sf
|
5 |
+
|
6 |
+
def reverse_segments(audio_path):
|
7 |
+
# Load audio and detect BPM
|
8 |
+
y, sr = librosa.load(audio_path, sr=None)
|
9 |
+
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
|
10 |
+
|
11 |
+
# Reverse the entire audio
|
12 |
+
y_reversed = y[::-1]
|
13 |
+
|
14 |
+
# Split into beat segments
|
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
|
19 |
+
processed_audio = np.concatenate([segment[::-1] for segment in segments])
|
20 |
+
|
21 |
+
# Save the output
|
22 |
+
output_path = "output.wav"
|
23 |
+
sf.write(output_path, processed_audio, sr)
|
24 |
+
|
25 |
+
return output_path
|
26 |
+
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=reverse_segments,
|
29 |
+
inputs=gr.Audio(type="filepath"),
|
30 |
+
outputs=gr.Audio(),
|
31 |
+
title="Beat-Reversed Music",
|
32 |
+
description="Reverses the entire song, then splits it by detected beats and plays each segment forward."
|
33 |
+
)
|
34 |
+
|
35 |
+
iface.launch()
|