Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,8 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import librosa
|
| 4 |
import soundfile as sf
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
# Process audio file or recording
|
| 8 |
def process_audio(audio_input, sample_rate=44100):
|
|
@@ -28,12 +29,12 @@ def process_audio(audio_input, sample_rate=44100):
|
|
| 28 |
"volume": float(np.mean(np.abs(audio_data)) * 100)
|
| 29 |
}
|
| 30 |
|
| 31 |
-
# Save audio to a temporary file
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
return vis_data,
|
| 37 |
|
| 38 |
# Gradio interface function
|
| 39 |
def audio_visualizer(audio_file, audio_record):
|
|
@@ -90,6 +91,10 @@ visualizer_html = """
|
|
| 90 |
visualizer.querySelectorAll('.bar').forEach(bar => bar.remove());
|
| 91 |
bars = [];
|
| 92 |
const containerWidth = visualizer.clientWidth;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
const barWidth = (containerWidth / barCount) - barSpacing;
|
| 94 |
for (let i = 0; i < barCount; i++) {
|
| 95 |
const bar = document.createElement('div');
|
|
@@ -102,20 +107,37 @@ visualizer_html = """
|
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
|
|
|
| 105 |
createBars();
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
// Function to update visualization
|
| 108 |
function updateVisualization(data) {
|
| 109 |
-
if (!data || !data.frequencies)
|
|
|
|
|
|
|
|
|
|
| 110 |
const { frequencies } = data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
const freqCount = Math.min(frequencies.length, barCount);
|
| 112 |
for (let i = 0; i < freqCount; i++) {
|
| 113 |
-
const height = (frequencies[i] /
|
| 114 |
bars[i].style.height = `${height}px`;
|
| 115 |
}
|
| 116 |
const wavePoints = frequencies.map((f, i) => [
|
| 117 |
(i / frequencies.length) * visualizer.clientWidth,
|
| 118 |
-
visualizer.clientHeight * (1 - f /
|
| 119 |
]);
|
| 120 |
let wavePath = `path('M0 ${visualizer.clientHeight / 2} `;
|
| 121 |
wavePoints.forEach(([x, y]) => {
|
|
@@ -143,7 +165,10 @@ visualizer_html = """
|
|
| 143 |
updateVisualization(data);
|
| 144 |
} catch (e) {
|
| 145 |
console.error('Error parsing visualization data:', e);
|
|
|
|
| 146 |
}
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
}, 500);
|
| 149 |
});
|
|
@@ -179,9 +204,9 @@ with gr.Blocks() as demo:
|
|
| 179 |
outputs=[vis_output, audio_output, vis_data_output]
|
| 180 |
)
|
| 181 |
clear.click(
|
| 182 |
-
fn=lambda: (None, None),
|
| 183 |
inputs=[],
|
| 184 |
-
outputs=[audio_file, audio_record]
|
| 185 |
)
|
| 186 |
|
| 187 |
# Launch Gradio app
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import librosa
|
| 4 |
import soundfile as sf
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# Process audio file or recording
|
| 9 |
def process_audio(audio_input, sample_rate=44100):
|
|
|
|
| 29 |
"volume": float(np.mean(np.abs(audio_data)) * 100)
|
| 30 |
}
|
| 31 |
|
| 32 |
+
# Save audio to a temporary file on disk
|
| 33 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 34 |
+
sf.write(temp_file.name, audio_data, sr, format='wav')
|
| 35 |
+
temp_file_path = temp_file.name
|
| 36 |
|
| 37 |
+
return vis_data, temp_file_path
|
| 38 |
|
| 39 |
# Gradio interface function
|
| 40 |
def audio_visualizer(audio_file, audio_record):
|
|
|
|
| 91 |
visualizer.querySelectorAll('.bar').forEach(bar => bar.remove());
|
| 92 |
bars = [];
|
| 93 |
const containerWidth = visualizer.clientWidth;
|
| 94 |
+
if (containerWidth === 0) {
|
| 95 |
+
console.warn('Visualizer container width is 0, retrying on resize');
|
| 96 |
+
return;
|
| 97 |
+
}
|
| 98 |
const barWidth = (containerWidth / barCount) - barSpacing;
|
| 99 |
for (let i = 0; i < barCount; i++) {
|
| 100 |
const bar = document.createElement('div');
|
|
|
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
+
// Initial creation of bars
|
| 111 |
createBars();
|
| 112 |
|
| 113 |
+
// Reset visualization
|
| 114 |
+
function resetVisualization() {
|
| 115 |
+
bars.forEach(bar => {
|
| 116 |
+
bar.style.height = '0px';
|
| 117 |
+
});
|
| 118 |
+
audioWave.style.clipPath = `path('M0 ${visualizer.clientHeight / 2} L${visualizer.clientWidth} ${visualizer.clientHeight / 2} Z')`;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
// Function to update visualization
|
| 122 |
function updateVisualization(data) {
|
| 123 |
+
if (!data || !data.frequencies || data.frequencies.length === 0) {
|
| 124 |
+
resetVisualization();
|
| 125 |
+
return;
|
| 126 |
+
}
|
| 127 |
const { frequencies } = data;
|
| 128 |
+
const maxFreq = Math.max(...frequencies);
|
| 129 |
+
if (maxFreq === 0) {
|
| 130 |
+
resetVisualization();
|
| 131 |
+
return;
|
| 132 |
+
}
|
| 133 |
const freqCount = Math.min(frequencies.length, barCount);
|
| 134 |
for (let i = 0; i < freqCount; i++) {
|
| 135 |
+
const height = (frequencies[i] / maxFreq) * visualizer.clientHeight * 0.8;
|
| 136 |
bars[i].style.height = `${height}px`;
|
| 137 |
}
|
| 138 |
const wavePoints = frequencies.map((f, i) => [
|
| 139 |
(i / frequencies.length) * visualizer.clientWidth,
|
| 140 |
+
visualizer.clientHeight * (1 - (f / maxFreq))
|
| 141 |
]);
|
| 142 |
let wavePath = `path('M0 ${visualizer.clientHeight / 2} `;
|
| 143 |
wavePoints.forEach(([x, y]) => {
|
|
|
|
| 165 |
updateVisualization(data);
|
| 166 |
} catch (e) {
|
| 167 |
console.error('Error parsing visualization data:', e);
|
| 168 |
+
resetVisualization();
|
| 169 |
}
|
| 170 |
+
} else {
|
| 171 |
+
resetVisualization();
|
| 172 |
}
|
| 173 |
}, 500);
|
| 174 |
});
|
|
|
|
| 204 |
outputs=[vis_output, audio_output, vis_data_output]
|
| 205 |
)
|
| 206 |
clear.click(
|
| 207 |
+
fn=lambda: (None, None, None),
|
| 208 |
inputs=[],
|
| 209 |
+
outputs=[audio_file, audio_record, vis_data_output]
|
| 210 |
)
|
| 211 |
|
| 212 |
# Launch Gradio app
|