marquesafonso commited on
Commit
4cc54d7
·
1 Parent(s): a9b4659

add initial files

Browse files
Files changed (2) hide show
  1. app.py +55 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+
8
+ def extract_waveform_animation(audio_file, duration):
9
+ # Load audio file
10
+ y, sr = librosa.load(audio_file, sr=None)
11
+
12
+ # Determine the number of frames needed for the specified duration
13
+ num_frames = int(duration * sr)
14
+ y = y[:num_frames]
15
+
16
+ # Create a figure and axis for the animation
17
+ fig, ax = plt.subplots()
18
+ line, = ax.plot([], [], lw=2)
19
+ ax.set_xlim(0, duration)
20
+ ax.set_ylim(np.min(y), np.max(y))
21
+
22
+ # Function to initialize the animation
23
+ def init():
24
+ line.set_data([], [])
25
+ return line,
26
+
27
+ # Function to update the animation frame
28
+ def update(frame):
29
+ line.set_data(np.linspace(0, duration, num=len(y[:frame])), y[:frame])
30
+ return line,
31
+
32
+ # Create the animation
33
+ ani = FuncAnimation(fig, update, frames=np.arange(0, num_frames, sr // 10), init_func=init, blit=True)
34
+
35
+ # Save the animation to a buffer
36
+ buf = io.BytesIO()
37
+ ani.save(buf, format='gif')
38
+ buf.seek(0)
39
+
40
+ return buf
41
+
42
+ # Define the Gradio interface
43
+ iface = gr.Interface(
44
+ fn=extract_waveform_animation,
45
+ inputs=[
46
+ gr.Audio(source="upload", type="file"),
47
+ gr.Number(label="Duration (seconds)", value=5)
48
+ ],
49
+ outputs=gr.Image(type="file", format="gif"),
50
+ description="Upload an audio file to extract an animation from its waveform. Specify the duration of the animation."
51
+ )
52
+
53
+ # Launch the app
54
+ if __name__ == "__main__":
55
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ numpy
3
+ librosa
4
+ matplotlib