remove axis; add param for background colour
Browse files
app.py
CHANGED
@@ -2,56 +2,52 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
from matplotlib.animation import FuncAnimation
|
5 |
-
import io
|
6 |
import librosa
|
7 |
import tempfile
|
8 |
|
9 |
-
def extract_waveform_animation(audio_file, window_seconds=5):
|
10 |
y, sr = librosa.load(audio_file, sr=None)
|
11 |
duration = librosa.get_duration(y=y, sr=sr)
|
|
|
12 |
|
13 |
fig, ax = plt.subplots()
|
14 |
-
line, = ax.plot([], [], lw=2)
|
15 |
-
window_length = int(window_seconds * sr)
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
|
21 |
def init():
|
22 |
ax.set_xlim(0, window_seconds)
|
23 |
-
ax.set_ylim(np.min(y), np.max(y)) # Reduced max for visibility
|
24 |
return line,
|
25 |
|
26 |
def update(frame):
|
27 |
-
# Get current window
|
28 |
start = frame * sr
|
29 |
end = start + window_length
|
30 |
window = y[start:end]
|
|
|
31 |
|
32 |
-
|
33 |
-
ax.set_xlim(frame, frame + window_seconds)
|
34 |
-
|
35 |
-
# Update line data
|
36 |
-
line.set_data(x_vals[start:end], window)
|
37 |
return line,
|
38 |
|
39 |
-
total_frames = int(duration) - window_seconds
|
40 |
-
ani = FuncAnimation(fig, update, frames=
|
41 |
-
init_func=init,
|
42 |
|
43 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
|
44 |
-
ani.save(tmpfile.name, writer='ffmpeg', fps=1)
|
45 |
video_path = tmpfile.name
|
46 |
|
|
|
47 |
return video_path
|
48 |
|
49 |
-
# Modified interface with
|
50 |
iface = gr.Interface(
|
51 |
fn=extract_waveform_animation,
|
52 |
inputs=[
|
53 |
gr.Audio(type="filepath"),
|
54 |
-
gr.Slider(1, 10, value=
|
|
|
55 |
],
|
56 |
outputs=gr.Video(),
|
57 |
description="Scroll through audio waveform with a moving window."
|
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
from matplotlib.animation import FuncAnimation
|
|
|
5 |
import librosa
|
6 |
import tempfile
|
7 |
|
8 |
+
def extract_waveform_animation(audio_file, window_seconds=5, bg_color='black'):
|
9 |
y, sr = librosa.load(audio_file, sr=None)
|
10 |
duration = librosa.get_duration(y=y, sr=sr)
|
11 |
+
window_length = int(window_seconds * sr)
|
12 |
|
13 |
fig, ax = plt.subplots()
|
14 |
+
line, = ax.plot([], [], lw=2, color='white')
|
|
|
15 |
|
16 |
+
# Remove all axes
|
17 |
+
ax.set_axis_off()
|
18 |
+
ax.set_facecolor(bg_color)
|
19 |
|
20 |
def init():
|
21 |
ax.set_xlim(0, window_seconds)
|
|
|
22 |
return line,
|
23 |
|
24 |
def update(frame):
|
|
|
25 |
start = frame * sr
|
26 |
end = start + window_length
|
27 |
window = y[start:end]
|
28 |
+
x_vals = np.linspace(frame, frame + window_seconds, num=len(window))
|
29 |
|
30 |
+
line.set_data(x_vals, window)
|
|
|
|
|
|
|
|
|
31 |
return line,
|
32 |
|
33 |
+
total_frames = int(duration) - window_seconds + 1
|
34 |
+
ani = FuncAnimation(fig, update, frames=np.arange(total_frames),
|
35 |
+
init_func=init, blit=False)
|
36 |
|
37 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
|
38 |
+
ani.save(tmpfile.name, writer='ffmpeg', fps=1, dpi=200)
|
39 |
video_path = tmpfile.name
|
40 |
|
41 |
+
plt.close(fig)
|
42 |
return video_path
|
43 |
|
44 |
+
# Modified interface with updated parameters
|
45 |
iface = gr.Interface(
|
46 |
fn=extract_waveform_animation,
|
47 |
inputs=[
|
48 |
gr.Audio(type="filepath"),
|
49 |
+
gr.Slider(1, 10, value=2, step=1, label="Window Size (seconds)"),
|
50 |
+
gr.ColorPicker(label="Background Color")
|
51 |
],
|
52 |
outputs=gr.Video(),
|
53 |
description="Scroll through audio waveform with a moving window."
|