File size: 1,035 Bytes
e6dfae0
435c886
cdc9708
435c886
e6dfae0
435c886
cdc9708
435c886
 
cdc9708
435c886
 
 
cdc9708
 
 
435c886
cdc9708
 
 
 
 
 
 
435c886
cdc9708
 
 
 
 
 
 
 
 
 
435c886
 
 
 
cdc9708
e6dfae0
 
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
import gradio as gr
import numpy as np
import plotly.graph_objects as go
import librosa

def plot_stft(audio_file):
    # Load the audio file
    y, sr = librosa.load(audio_file)
    
    # Compute the STFT
    D = librosa.stft(y)
    S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
    
    # Generate time and frequency axes
    times = librosa.times_like(S_db)
    freqs = librosa.fft_frequencies(sr=sr)
    
    # Create Plotly figure
    fig = go.Figure(data=go.Heatmap(
        z=S_db,
        x=times,
        y=freqs,
        colorscale='Viridis'
    ))
    
    # Update layout for better visualization
    fig.update_layout(
        title="STFT (Short-Time Fourier Transform)",
        xaxis_title="Time (s)",
        yaxis_title="Frequency (Hz)",
        yaxis_type="log"
    )
    
    # Return the HTML representation of the plot
    return fig.to_html()

# Gradio interface
demo = gr.Interface(fn=plot_stft, 
                    inputs=gr.Audio(type="filepath"), 
                    outputs="html")

demo.launch()