marquesafonso commited on
Commit
05629f7
·
verified ·
1 Parent(s): 5d9f56e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -6,47 +6,56 @@ import io
6
  import librosa
7
  import tempfile
8
 
9
- def extract_waveform_animation(audio_file):
10
- # Load audio file
11
  y, sr = librosa.load(audio_file, sr=None)
12
  duration = librosa.get_duration(y=y, sr=sr)
13
 
14
- # Create a figure and axis for the animation
15
  fig, ax = plt.subplots()
16
  line, = ax.plot([], [], lw=2)
17
- ax.set_xlim(0, duration)
18
- ax.set_ylim(np.min(y), np.max(y))
 
 
 
19
 
20
- # Function to initialize the animation
21
  def init():
22
- line.set_data([], [])
 
23
  return line,
24
-
25
- # Function to update the animation frame
26
  def update(frame):
 
27
  start = frame * sr
28
- end = min(start + sr, len(y))
29
- line.set_data(np.linspace(0, duration, num=len(y[:end])), y[:end])
 
 
 
 
 
 
30
  return line,
 
 
 
 
31
 
32
- # Create the animation
33
- ani = FuncAnimation(fig, update, frames=np.arange(0, int(duration)), init_func=init, interval=7, blit=False)
34
-
35
- # Save the animation to a temporary file
36
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
37
  ani.save(tmpfile.name, writer='ffmpeg', fps=1)
38
  video_path = tmpfile.name
39
 
40
  return video_path
41
 
42
- # Define the Gradio interface
43
  iface = gr.Interface(
44
  fn=extract_waveform_animation,
45
- inputs=gr.Audio(type="filepath"),
 
 
 
46
  outputs=gr.Video(),
47
- description="Upload an audio file to extract a video animation from its waveform."
48
  )
49
 
50
- # Launch the app
51
  if __name__ == "__main__":
52
  iface.launch()
 
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
 
 
21
  def init():
22
+ ax.set_xlim(0, window_seconds)
23
+ ax.set_ylim(np.min(y), np.max(y)/10) # 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
+ # Update x and y limits
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=range(total_frames),
41
+ init_func=init, interval=7, blit=False)
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 window controls
50
  iface = gr.Interface(
51
  fn=extract_waveform_animation,
52
+ inputs=[
53
+ gr.Audio(type="filepath"),
54
+ gr.Slider(1, 10, value=5, label="Window Size (seconds)")
55
+ ],
56
  outputs=gr.Video(),
57
+ description="Scroll through audio waveform with a moving window."
58
  )
59
 
 
60
  if __name__ == "__main__":
61
  iface.launch()