File size: 1,879 Bytes
4cc54d7
 
 
 
7ccd379
4cc54d7
2f40a85
4cc54d7
7ccd379
4cc54d7
1dc0f0f
80a9f21
4cc54d7
7ccd379
6d6ab1e
05629f7
7ccd379
 
 
6c001bc
bdb2fa6
4cc54d7
 
05629f7
7ccd379
4cc54d7
05629f7
4cc54d7
7ccd379
80a9f21
05629f7
 
 
7ccd379
 
 
 
 
4cc54d7
05629f7
3754c59
7ccd379
fd8083a
4cc54d7
2f40a85
46972b6
2f40a85
4cc54d7
2f40a85
4cc54d7
7ccd379
4cc54d7
 
05629f7
 
7ccd379
05629f7
b3e87b5
05629f7
4cc54d7
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import io
import librosa
import tempfile

def extract_waveform_animation(audio_file, window_seconds=5):
    y, sr = librosa.load(audio_file, sr=None)
    duration = librosa.get_duration(y=y, sr=sr)
    FPS = 2
    fig, ax = plt.subplots()
    line, = ax.plot([], [], lw=2)
    window_length = int(window_seconds * sr / FPS)
    
    # Initialize with first window
    first_window = y[:window_length]
    x_vals = np.linspace(0, duration, num=len(y))
    ax.set_axis_off()
    fig.set_facecolor("black")
    
    def init():
        ax.set_xlim(0, window_seconds)
        ax.set_ylim(np.min(y), np.max(y))  # Reduced max for visibility
        return line,

    def update(frame):
        # Get current window
        start = frame * sr // FPS
        end = start + window_length
        window = y[start:end]
        
        # Update x and y limits
        ax.set_xlim(frame, frame + window_seconds)
        
        # Update line data
        line.set_data(x_vals[start:end], window)
        return line,

    total_frames = int(duration) * FPS
    ani = FuncAnimation(fig, update, frames=range(total_frames), 
                       init_func=init, interval=window_seconds, blit=False)
    
    with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
        ani.save(tmpfile.name, writer='ffmpeg', fps=FPS)
        video_path = tmpfile.name
    
    return video_path

# Modified interface with window controls
iface = gr.Interface(
    fn=extract_waveform_animation,
    inputs=[
        gr.Audio(type="filepath"),
        gr.Slider(1, 10, value=5, step=1, label="Window Size (seconds)")
    ],
    outputs=gr.Video(),
    description="Scroll through audio waveform with a moving window."
)

if __name__ == "__main__":
    iface.launch()