SonicOrbit / app.py
Bils's picture
Update app.py
1a17445 verified
raw
history blame
6.78 kB
import numpy as np
import soundfile as sf
import gradio as gr
import math
def binauralize(audio_file, simulate_rotation, rotation_speed):
"""
Simulate a binaural (stereo) effect by applying a dynamic panning effect
to an input audio file. No HRIR files are required.
Parameters:
audio_file (str): Path to input audio file (mono or stereo).
simulate_rotation (bool): If True, apply a dynamic rotation (panning) effect.
rotation_speed (float): Speed of the rotation effect (in Hz).
Returns:
output_file (str): Path to the output stereo audio file.
status (str): Status message.
"""
try:
audio, sr = sf.read(audio_file)
except Exception as e:
return None, f"Error reading input audio file: {e}"
# Convert to mono if needed.
if audio.ndim > 1:
audio = np.mean(audio, axis=1)
t = np.arange(len(audio)) / sr
if simulate_rotation:
# Compute a time-varying angle for a full cycle (2π) at the desired rotation speed.
angle = 2 * np.pi * rotation_speed * t
left = np.cos(angle) * audio
right = np.sin(angle) * audio
else:
left = audio
right = audio
binaural_audio = np.stack((left, right), axis=-1)
# Normalize to prevent clipping.
max_val = np.max(np.abs(binaural_audio))
if max_val > 0:
binaural_audio = binaural_audio / max_val
output_file = "output_binaural.wav"
try:
sf.write(output_file, binaural_audio, sr)
except Exception as e:
return None, f"Error writing output audio file: {e}"
return output_file, "Binaural conversion complete!"
def simulate_map(audio_file, listener_x, listener_y):
"""
Process an input audio file and simulate binaural panning based on the listener's position.
The source is fixed at (0,0). Listener coordinates (listener_x, listener_y) determine the angle.
Also applies optional distance attenuation.
"""
try:
audio, sr = sf.read(audio_file)
except Exception as e:
return None, f"Error reading input audio file: {e}"
if audio.ndim > 1:
audio = np.mean(audio, axis=1)
# Compute the angle (in radians) between the listener position and the source at (0,0)
# Here, we use atan2(listener_x, listener_y) so that a positive X (to the right) yields a positive angle.
theta_rad = math.atan2(listener_x, listener_y)
theta_deg = theta_rad * 180 / math.pi
# Clamp theta to [-90, 90] degrees (for panning purposes)
theta_deg = max(-90, min(90, theta_deg))
# Map theta from [-90, 90] to a panning parameter p in radians:
# When theta_deg = -90, p = 0 (full left); theta_deg = 0, p = 45° in radians; theta_deg = 90, p = 90° (full right)
p = (theta_deg + 90) * math.pi / 360
left_gain = math.cos(p)
right_gain = math.sin(p)
# Optional distance attenuation: the further away the listener, the lower the volume.
distance = math.sqrt(listener_x**2 + listener_y**2)
attenuation = 1 / (1 + distance)
left = audio * left_gain * attenuation
right = audio * right_gain * attenuation
binaural_audio = np.stack((left, right), axis=-1)
max_val = np.max(np.abs(binaural_audio))
if max_val > 0:
binaural_audio = binaural_audio / max_val
output_file = "output_map.wav"
try:
sf.write(output_file, binaural_audio, sr)
except Exception as e:
return None, f"Error writing output audio file: {e}"
return output_file, f"Listener: ({listener_x}, {listener_y}), Angle: {theta_deg:.1f}°"
# Create an enhanced UI using Gradio Blocks and Tabs.
with gr.Blocks(title="SonicOrbit", css="""
.title { font-size: 2.5em; font-weight: bold; text-align: center; margin-bottom: 0.5em; }
.subtitle { font-size: 1.2em; text-align: center; margin-bottom: 1em; }
.footer { text-align: center; font-size: 0.9em; margin-top: 2em; color: #555; }
.tab-description { margin: 10px; font-size: 1em; }
""") as demo:
gr.Markdown("<div class='title'>SonicOrbit</div>")
gr.Markdown("<div class='subtitle'>Binaural 360 Audio Converter & Interactive Map</div>")
with gr.Tabs():
with gr.Tab("Converter"):
with gr.Row():
input_audio = gr.Audio(type="filepath", label="Upload Audio (Mono or Stereo)")
with gr.Row():
simulate_rotation = gr.Checkbox(label="Simulate Rotation", value=True)
rotation_speed = gr.Slider(0.01, 1.0, value=0.1, step=0.01, label="Rotation Speed (Hz)")
convert_button = gr.Button("Convert Audio")
with gr.Row():
output_audio = gr.Audio(type="filepath", label="Binaural Audio Output")
status_text = gr.Textbox(label="Status", interactive=False)
convert_button.click(
fn=binauralize,
inputs=[input_audio, simulate_rotation, rotation_speed],
outputs=[output_audio, status_text]
)
with gr.Tab("Interactive Map"):
gr.Markdown("<div class='tab-description'>Move the listener around the source (fixed at (0,0)) using the sliders. The binaural panning will update based on the listener's position and distance.</div>")
with gr.Row():
map_audio = gr.Audio(type="filepath", label="Upload Audio (Mono or Stereo)")
with gr.Row():
listener_x = gr.Slider(-10, 10, value=0, step=0.1, label="Listener X Position")
listener_y = gr.Slider(-10, 10, value=1, step=0.1, label="Listener Y Position")
map_button = gr.Button("Update Listener Position")
with gr.Row():
map_output = gr.Audio(type="filepath", label="Binaural Map Output")
map_status = gr.Textbox(label="Status", interactive=False)
map_button.click(
fn=simulate_map,
inputs=[map_audio, listener_x, listener_y],
outputs=[map_output, map_status]
)
gr.Markdown("""
<div class='footer'>
© 2025 SonicOrbit. All rights reserved.<br>
Created with ❤️ by <a href="https://bilsimaging.com" target="_blank" style="color: #88aaff;">bilsimaging.com</a>
and this <a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2FSonicOrbit" target="_blank">
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2FSonicOrbit&countColor=%23263759" alt="visitor badge" /></a>
</div>
""")
if __name__ == "__main__":
demo.launch()