remove axis + refactor total frames
Browse files
app.py
CHANGED
|
@@ -2,52 +2,57 @@ import gradio as gr
|
|
| 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
|
| 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
|
|
|
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return line,
|
| 32 |
|
| 33 |
-
total_frames = int(duration)
|
| 34 |
-
ani = FuncAnimation(fig, update, 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
|
| 39 |
video_path = tmpfile.name
|
| 40 |
|
| 41 |
-
plt.close(fig)
|
| 42 |
return video_path
|
| 43 |
|
| 44 |
-
# Modified interface with
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=extract_waveform_animation,
|
| 47 |
inputs=[
|
| 48 |
gr.Audio(type="filepath"),
|
| 49 |
-
gr.Slider(1, 10, value=
|
| 50 |
-
gr.ColorPicker(label="Background Color")
|
| 51 |
],
|
| 52 |
outputs=gr.Video(),
|
| 53 |
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 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 |
+
# Initialize with first window
|
| 18 |
+
first_window = y[:window_length]
|
| 19 |
+
x_vals = np.linspace(0, duration, num=len(y))
|
| 20 |
ax.set_axis_off()
|
|
|
|
| 21 |
|
| 22 |
def init():
|
| 23 |
ax.set_xlim(0, window_seconds)
|
| 24 |
+
ax.set_ylim(np.min(y), np.max(y)) # Reduced max for visibility
|
| 25 |
return line,
|
| 26 |
|
| 27 |
def update(frame):
|
| 28 |
+
# Get current window
|
| 29 |
start = frame * sr
|
| 30 |
end = start + window_length
|
| 31 |
window = y[start:end]
|
|
|
|
| 32 |
|
| 33 |
+
# Update x and y limits
|
| 34 |
+
ax.set_xlim(frame, frame + window_seconds)
|
| 35 |
+
|
| 36 |
+
# Update line data
|
| 37 |
+
line.set_data(x_vals[start:end], window)
|
| 38 |
return line,
|
| 39 |
|
| 40 |
+
total_frames = int(duration)
|
| 41 |
+
ani = FuncAnimation(fig, update, frames=range(total_frames),
|
| 42 |
+
init_func=init, interval=7, blit=False)
|
| 43 |
|
| 44 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
|
| 45 |
+
ani.save(tmpfile.name, writer='ffmpeg', fps=1)
|
| 46 |
video_path = tmpfile.name
|
| 47 |
|
|
|
|
| 48 |
return video_path
|
| 49 |
|
| 50 |
+
# Modified interface with window controls
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=extract_waveform_animation,
|
| 53 |
inputs=[
|
| 54 |
gr.Audio(type="filepath"),
|
| 55 |
+
gr.Slider(1, 10, value=5, step=1, label="Window Size (seconds)")
|
|
|
|
| 56 |
],
|
| 57 |
outputs=gr.Video(),
|
| 58 |
description="Scroll through audio waveform with a moving window."
|