Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,71 +4,48 @@ 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 |
-
|
| 26 |
audio, sr = sf.read(audio_file)
|
| 27 |
except Exception as e:
|
| 28 |
return None, f"Error reading input audio file: {e}"
|
| 29 |
|
| 30 |
-
# If the audio is stereo, convert to mono by averaging channels
|
| 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 |
-
|
| 38 |
audio_float = audio.astype(np.float32)
|
| 39 |
tempo, _ = librosa.beat.beat_track(y=audio_float, sr=sr)
|
| 40 |
-
|
| 41 |
tempo = float(tempo)
|
| 42 |
-
rotation_speed = tempo / 60.0
|
| 43 |
status_msg = f"Auto rotation enabled: Detected tempo = {tempo:.1f} BPM, setting rotation speed = {rotation_speed:.3f} Hz."
|
| 44 |
except Exception as e:
|
| 45 |
status_msg = f"Auto rotation failed, using user provided rotation speed. Error: {e}"
|
| 46 |
else:
|
| 47 |
status_msg = "Using user provided rotation speed."
|
| 48 |
|
| 49 |
-
|
| 50 |
t = np.arange(len(audio)) / sr
|
| 51 |
|
| 52 |
if simulate_rotation:
|
| 53 |
-
|
| 54 |
angle = 2 * np.pi * rotation_speed * t
|
| 55 |
-
# Constant power panning: left uses cosine, right uses sine.
|
| 56 |
left = np.cos(angle) * audio
|
| 57 |
right = np.sin(angle) * audio
|
| 58 |
else:
|
| 59 |
-
# If rotation is not enabled, duplicate the audio to both channels.
|
| 60 |
left = audio
|
| 61 |
right = audio
|
| 62 |
|
| 63 |
-
# Combine the channels into a stereo signal.
|
| 64 |
binaural_audio = np.stack((left, right), axis=-1)
|
| 65 |
|
| 66 |
-
# Normalize to prevent clipping.
|
| 67 |
max_val = np.max(np.abs(binaural_audio))
|
| 68 |
if max_val > 0:
|
| 69 |
binaural_audio = binaural_audio / max_val
|
| 70 |
|
| 71 |
-
# Save the output to a WAV file.
|
| 72 |
output_file = "output_binaural.wav"
|
| 73 |
try:
|
| 74 |
sf.write(output_file, binaural_audio, sr)
|
|
@@ -77,7 +54,6 @@ def binauralize(audio_file, simulate_rotation, rotation_speed, auto_rotation):
|
|
| 77 |
|
| 78 |
return output_file, f"Binaural conversion complete! {status_msg}"
|
| 79 |
|
| 80 |
-
# Create an enhanced UI using Gradio Blocks and Tabs.
|
| 81 |
with gr.Blocks(title="SonicOrbit", css="""
|
| 82 |
/* Custom CSS to enhance spacing and font styling */
|
| 83 |
.title { font-size: 2.5em; font-weight: bold; text-align: center; margin-bottom: 0.5em; }
|
|
|
|
| 4 |
import librosa
|
| 5 |
|
| 6 |
def binauralize(audio_file, simulate_rotation, rotation_speed, auto_rotation):
|
| 7 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
+
|
| 10 |
audio, sr = sf.read(audio_file)
|
| 11 |
except Exception as e:
|
| 12 |
return None, f"Error reading input audio file: {e}"
|
| 13 |
|
|
|
|
| 14 |
if audio.ndim > 1:
|
| 15 |
audio = np.mean(audio, axis=1)
|
| 16 |
|
|
|
|
| 17 |
if auto_rotation:
|
| 18 |
try:
|
| 19 |
+
|
| 20 |
audio_float = audio.astype(np.float32)
|
| 21 |
tempo, _ = librosa.beat.beat_track(y=audio_float, sr=sr)
|
| 22 |
+
|
| 23 |
tempo = float(tempo)
|
| 24 |
+
rotation_speed = tempo / 60.0
|
| 25 |
status_msg = f"Auto rotation enabled: Detected tempo = {tempo:.1f} BPM, setting rotation speed = {rotation_speed:.3f} Hz."
|
| 26 |
except Exception as e:
|
| 27 |
status_msg = f"Auto rotation failed, using user provided rotation speed. Error: {e}"
|
| 28 |
else:
|
| 29 |
status_msg = "Using user provided rotation speed."
|
| 30 |
|
| 31 |
+
|
| 32 |
t = np.arange(len(audio)) / sr
|
| 33 |
|
| 34 |
if simulate_rotation:
|
| 35 |
+
|
| 36 |
angle = 2 * np.pi * rotation_speed * t
|
|
|
|
| 37 |
left = np.cos(angle) * audio
|
| 38 |
right = np.sin(angle) * audio
|
| 39 |
else:
|
|
|
|
| 40 |
left = audio
|
| 41 |
right = audio
|
| 42 |
|
|
|
|
| 43 |
binaural_audio = np.stack((left, right), axis=-1)
|
| 44 |
|
|
|
|
| 45 |
max_val = np.max(np.abs(binaural_audio))
|
| 46 |
if max_val > 0:
|
| 47 |
binaural_audio = binaural_audio / max_val
|
| 48 |
|
|
|
|
| 49 |
output_file = "output_binaural.wav"
|
| 50 |
try:
|
| 51 |
sf.write(output_file, binaural_audio, sr)
|
|
|
|
| 54 |
|
| 55 |
return output_file, f"Binaural conversion complete! {status_msg}"
|
| 56 |
|
|
|
|
| 57 |
with gr.Blocks(title="SonicOrbit", css="""
|
| 58 |
/* Custom CSS to enhance spacing and font styling */
|
| 59 |
.title { font-size: 2.5em; font-weight: bold; text-align: center; margin-bottom: 0.5em; }
|