marquesafonso commited on
Commit
f2c6f1c
·
verified ·
1 Parent(s): 26e9031

move to gif animation + transparent background

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -9,16 +9,16 @@ import tempfile
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
- FPS = 2
13
  fig, ax = plt.subplots()
14
  line, = ax.plot([], [], lw=2)
15
- window_length = int(window_seconds * sr / FPS)
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
- fig.set_facecolor("black")
22
 
23
  def init():
24
  ax.set_xlim(0, window_seconds)
@@ -27,7 +27,7 @@ def extract_waveform_animation(audio_file, window_seconds=5):
27
 
28
  def update(frame):
29
  # Get current window
30
- start = frame * sr // FPS
31
  end = start + window_length
32
  window = y[start:end]
33
 
@@ -40,9 +40,9 @@ def extract_waveform_animation(audio_file, window_seconds=5):
40
 
41
  total_frames = int(duration) * FPS
42
  ani = FuncAnimation(fig, update, frames=range(total_frames),
43
- init_func=init, interval=window_seconds // FPS, blit=False)
44
 
45
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
46
  ani.save(tmpfile.name, writer='ffmpeg', fps=FPS)
47
  video_path = tmpfile.name
48
 
@@ -55,7 +55,7 @@ iface = gr.Interface(
55
  gr.Audio(type="filepath"),
56
  gr.Slider(1, 10, value=5, step=1, label="Window Size (seconds)")
57
  ],
58
- outputs=gr.Video(),
59
  description="Scroll through audio waveform with a moving window."
60
  )
61
 
 
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
+ FPS = 1
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
+ fig.set_facecolor("#00FFFF00")
22
 
23
  def init():
24
  ax.set_xlim(0, window_seconds)
 
27
 
28
  def update(frame):
29
  # Get current window
30
+ start = frame * sr
31
  end = start + window_length
32
  window = y[start:end]
33
 
 
40
 
41
  total_frames = int(duration) * FPS
42
  ani = FuncAnimation(fig, update, frames=range(total_frames),
43
+ init_func=init, interval=window_seconds, blit=False)
44
 
45
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.gif') as tmpfile:
46
  ani.save(tmpfile.name, writer='ffmpeg', fps=FPS)
47
  video_path = tmpfile.name
48
 
 
55
  gr.Audio(type="filepath"),
56
  gr.Slider(1, 10, value=5, step=1, label="Window Size (seconds)")
57
  ],
58
+ outputs=gr.Image(),
59
  description="Scroll through audio waveform with a moving window."
60
  )
61