Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,28 @@
|
|
1 |
import numpy as np
|
2 |
import soundfile as sf
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
def binauralize(audio_file, simulate_rotation, rotation_speed):
|
6 |
"""
|
7 |
Simulate a binaural (stereo) effect by applying a dynamic panning effect
|
8 |
to an input audio file. No HRIR files are required.
|
|
|
|
|
|
|
9 |
|
10 |
Parameters:
|
11 |
audio_file (str): Path to input audio file (mono or stereo).
|
12 |
simulate_rotation (bool): If True, apply a dynamic rotation (panning) effect.
|
13 |
rotation_speed (float): Speed of the rotation effect (in Hz).
|
|
|
14 |
|
15 |
Returns:
|
16 |
output_file (str): Path to the output stereo audio file.
|
17 |
status (str): Status message.
|
18 |
"""
|
19 |
try:
|
20 |
-
# Load input audio file
|
21 |
audio, sr = sf.read(audio_file)
|
22 |
except Exception as e:
|
23 |
return None, f"Error reading input audio file: {e}"
|
@@ -26,6 +31,19 @@ def binauralize(audio_file, simulate_rotation, rotation_speed):
|
|
26 |
if audio.ndim > 1:
|
27 |
audio = np.mean(audio, axis=1)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Create a time vector for the audio length
|
30 |
t = np.arange(len(audio)) / sr
|
31 |
|
@@ -55,7 +73,7 @@ def binauralize(audio_file, simulate_rotation, rotation_speed):
|
|
55 |
except Exception as e:
|
56 |
return None, f"Error writing output audio file: {e}"
|
57 |
|
58 |
-
return output_file, "Binaural conversion complete!"
|
59 |
|
60 |
# Create an enhanced UI using Gradio Blocks and Tabs.
|
61 |
with gr.Blocks(title="SonicOrbit", css="""
|
@@ -66,7 +84,7 @@ with gr.Blocks(title="SonicOrbit", css="""
|
|
66 |
""") as demo:
|
67 |
|
68 |
gr.Markdown("<div class='title'>SonicOrbit</div>")
|
69 |
-
gr.Markdown("<div class='subtitle'>Binaural 360 Audio Converter with Dynamic Rotation</div>")
|
70 |
|
71 |
with gr.Tabs():
|
72 |
with gr.Tab("Converter"):
|
@@ -74,7 +92,8 @@ with gr.Blocks(title="SonicOrbit", css="""
|
|
74 |
input_audio = gr.Audio(type="filepath", label="Upload Audio (Mono or Stereo)")
|
75 |
with gr.Row():
|
76 |
simulate_rotation = gr.Checkbox(label="Simulate Rotation", value=True)
|
77 |
-
rotation_speed = gr.Slider(0.01,
|
|
|
78 |
convert_button = gr.Button("Convert Audio")
|
79 |
with gr.Row():
|
80 |
output_audio = gr.Audio(type="filepath", label="Binaural Audio Output")
|
@@ -82,7 +101,7 @@ with gr.Blocks(title="SonicOrbit", css="""
|
|
82 |
|
83 |
convert_button.click(
|
84 |
fn=binauralize,
|
85 |
-
inputs=[input_audio, simulate_rotation, rotation_speed],
|
86 |
outputs=[output_audio, status_text]
|
87 |
)
|
88 |
|
@@ -94,8 +113,10 @@ with gr.Blocks(title="SonicOrbit", css="""
|
|
94 |
2. **Simulate Rotation:**
|
95 |
Enable this option to apply a dynamic panning effect that simulates a rotating sound source.
|
96 |
3. **Rotation Speed:**
|
97 |
-
Adjust the slider to set the speed of the rotation effect (in Hertz).
|
98 |
-
4. **
|
|
|
|
|
99 |
Click the **Convert Audio** button to process your audio file. The output is a binaural (stereo) audio file with the simulated 360° effect.
|
100 |
|
101 |
Enjoy your immersive 3D audio experience!
|
|
|
1 |
import numpy as np
|
2 |
import soundfile as sf
|
3 |
import gradio as gr
|
4 |
+
import librosa
|
5 |
|
6 |
+
def binauralize(audio_file, simulate_rotation, rotation_speed, auto_rotation):
|
7 |
"""
|
8 |
Simulate a binaural (stereo) effect by applying a dynamic panning effect
|
9 |
to an input audio file. No HRIR files are required.
|
10 |
+
|
11 |
+
If auto_rotation is enabled, AI beat detection (via librosa) is used to
|
12 |
+
determine the rotation speed based on the tempo of the audio.
|
13 |
|
14 |
Parameters:
|
15 |
audio_file (str): Path to input audio file (mono or stereo).
|
16 |
simulate_rotation (bool): If True, apply a dynamic rotation (panning) effect.
|
17 |
rotation_speed (float): Speed of the rotation effect (in Hz).
|
18 |
+
auto_rotation (bool): If True, auto-detect rotation speed using AI beat detection.
|
19 |
|
20 |
Returns:
|
21 |
output_file (str): Path to the output stereo audio file.
|
22 |
status (str): Status message.
|
23 |
"""
|
24 |
try:
|
25 |
+
# Load input audio file using soundfile
|
26 |
audio, sr = sf.read(audio_file)
|
27 |
except Exception as e:
|
28 |
return None, f"Error reading input audio file: {e}"
|
|
|
31 |
if audio.ndim > 1:
|
32 |
audio = np.mean(audio, axis=1)
|
33 |
|
34 |
+
# If auto_rotation is enabled, use librosa to detect tempo and adjust rotation speed.
|
35 |
+
if auto_rotation:
|
36 |
+
try:
|
37 |
+
# librosa expects float32 input.
|
38 |
+
audio_float = audio.astype(np.float32)
|
39 |
+
tempo, _ = librosa.beat.beat_track(y=audio_float, sr=sr)
|
40 |
+
rotation_speed = tempo / 60.0 # Convert BPM to Hz (approx.)
|
41 |
+
status_msg = f"Auto rotation enabled: Detected tempo = {tempo:.1f} BPM, setting rotation speed = {rotation_speed:.3f} Hz."
|
42 |
+
except Exception as e:
|
43 |
+
status_msg = f"Auto rotation failed, using user provided rotation speed. Error: {e}"
|
44 |
+
else:
|
45 |
+
status_msg = "Using user provided rotation speed."
|
46 |
+
|
47 |
# Create a time vector for the audio length
|
48 |
t = np.arange(len(audio)) / sr
|
49 |
|
|
|
73 |
except Exception as e:
|
74 |
return None, f"Error writing output audio file: {e}"
|
75 |
|
76 |
+
return output_file, f"Binaural conversion complete! {status_msg}"
|
77 |
|
78 |
# Create an enhanced UI using Gradio Blocks and Tabs.
|
79 |
with gr.Blocks(title="SonicOrbit", css="""
|
|
|
84 |
""") as demo:
|
85 |
|
86 |
gr.Markdown("<div class='title'>SonicOrbit</div>")
|
87 |
+
gr.Markdown("<div class='subtitle'>Binaural 360 Audio Converter with Dynamic Rotation & AI Beat Detection</div>")
|
88 |
|
89 |
with gr.Tabs():
|
90 |
with gr.Tab("Converter"):
|
|
|
92 |
input_audio = gr.Audio(type="filepath", label="Upload Audio (Mono or Stereo)")
|
93 |
with gr.Row():
|
94 |
simulate_rotation = gr.Checkbox(label="Simulate Rotation", value=True)
|
95 |
+
rotation_speed = gr.Slider(0.01, 5.0, value=0.1, step=0.01, label="Rotation Speed (Hz)")
|
96 |
+
auto_rotation = gr.Checkbox(label="Auto Detect Rotation Speed (AI)", value=False)
|
97 |
convert_button = gr.Button("Convert Audio")
|
98 |
with gr.Row():
|
99 |
output_audio = gr.Audio(type="filepath", label="Binaural Audio Output")
|
|
|
101 |
|
102 |
convert_button.click(
|
103 |
fn=binauralize,
|
104 |
+
inputs=[input_audio, simulate_rotation, rotation_speed, auto_rotation],
|
105 |
outputs=[output_audio, status_text]
|
106 |
)
|
107 |
|
|
|
113 |
2. **Simulate Rotation:**
|
114 |
Enable this option to apply a dynamic panning effect that simulates a rotating sound source.
|
115 |
3. **Rotation Speed:**
|
116 |
+
Adjust the slider to set the speed of the rotation effect (in Hertz).
|
117 |
+
4. **Auto Detect Rotation Speed (AI):**
|
118 |
+
Enable this option to let SonicOrbit analyze your audio and automatically set the rotation speed based on the detected tempo.
|
119 |
+
5. **Convert Audio:**
|
120 |
Click the **Convert Audio** button to process your audio file. The output is a binaural (stereo) audio file with the simulated 360° effect.
|
121 |
|
122 |
Enjoy your immersive 3D audio experience!
|