Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}"
|
24 |
+
|
25 |
+
# If the audio is stereo, convert to mono by averaging channels
|
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 |
+
|
32 |
+
if simulate_rotation:
|
33 |
+
# Compute a time-varying angle for a full cycle (2π) at the desired rotation speed.
|
34 |
+
angle = 2 * np.pi * rotation_speed * t
|
35 |
+
# Constant power panning: left uses cosine, right uses sine.
|
36 |
+
left = np.cos(angle) * audio
|
37 |
+
right = np.sin(angle) * audio
|
38 |
+
else:
|
39 |
+
# If rotation is not enabled, duplicate the audio to both channels.
|
40 |
+
left = audio
|
41 |
+
right = audio
|
42 |
+
|
43 |
+
# Combine the channels into a stereo signal.
|
44 |
+
binaural_audio = np.stack((left, right), axis=-1)
|
45 |
+
|
46 |
+
# Normalize to prevent clipping.
|
47 |
+
max_val = np.max(np.abs(binaural_audio))
|
48 |
+
if max_val > 0:
|
49 |
+
binaural_audio = binaural_audio / max_val
|
50 |
+
|
51 |
+
# Save the output to a WAV file.
|
52 |
+
output_file = "output_binaural.wav"
|
53 |
+
try:
|
54 |
+
sf.write(output_file, binaural_audio, sr)
|
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="""
|
62 |
+
/* Custom CSS to enhance spacing and font styling */
|
63 |
+
.title { font-size: 2.5em; font-weight: bold; text-align: center; margin-bottom: 0.5em; }
|
64 |
+
.subtitle { font-size: 1.2em; text-align: center; margin-bottom: 1em; }
|
65 |
+
.footer { text-align: center; font-size: 0.9em; margin-top: 2em; color: #555; }
|
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"):
|
73 |
+
with gr.Row():
|
74 |
+
input_audio = gr.Audio(source="upload", 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, 1.0, value=0.1, step=0.01, label="Rotation Speed (Hz)")
|
78 |
+
convert_button = gr.Button("Convert Audio")
|
79 |
+
with gr.Row():
|
80 |
+
output_audio = gr.Audio(type="file", label="Binaural Audio Output")
|
81 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
82 |
+
|
83 |
+
convert_button.click(
|
84 |
+
fn=binauralize,
|
85 |
+
inputs=[input_audio, simulate_rotation, rotation_speed],
|
86 |
+
outputs=[output_audio, status_text]
|
87 |
+
)
|
88 |
+
|
89 |
+
with gr.Tab("Instructions"):
|
90 |
+
gr.Markdown("""
|
91 |
+
### How to Use SonicOrbit
|
92 |
+
1. **Upload Audio:**
|
93 |
+
Upload a mono or stereo audio file. If you upload a stereo file, it will be converted to mono by averaging the channels.
|
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). A higher value rotates the audio field faster.
|
98 |
+
4. **Convert Audio:**
|
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!
|
102 |
+
""")
|
103 |
+
|
104 |
+
gr.Markdown("<div class='footer'>© 2025 SonicOrbit. All rights reserved.</div>")
|
105 |
+
|
106 |
+
if __name__ == "__main__":
|
107 |
+
demo.launch()
|