Spaces:
Running
Running
test plotly & html
Browse files- .gitignore +1 -0
- app.py +62 -26
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.idea/
|
app.py
CHANGED
@@ -5,35 +5,71 @@ import librosa
|
|
5 |
import librosa.display
|
6 |
import base64
|
7 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def plot_stft(audio_file):
|
|
|
10 |
# Load audio file
|
11 |
-
|
12 |
-
|
13 |
-
# Compute
|
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 |
# Gradio interface
|
39 |
demo = gr.Interface(fn=plot_stft,
|
|
|
5 |
import librosa.display
|
6 |
import base64
|
7 |
from io import BytesIO
|
8 |
+
import soundfile as sf
|
9 |
+
import numpy as np
|
10 |
+
import IPython.display as ipd
|
11 |
+
import plotly.graph_objects as go
|
12 |
+
import plotly
|
13 |
+
from scipy.signal import csd
|
14 |
+
from scipy.ndimage import interpolation
|
15 |
+
from plotly import tools
|
16 |
+
import scipy.signal as ssig
|
17 |
+
import matplotlib.pyplot as plt
|
18 |
+
import librosa
|
19 |
+
import librosa.display
|
20 |
+
|
21 |
|
22 |
def plot_stft(audio_file):
|
23 |
+
|
24 |
# Load audio file
|
25 |
+
audio, sampling_rate = librosa.load(audio_file)
|
26 |
+
|
27 |
+
# Compute stft
|
28 |
+
freq, frames, stft = ssig.stft(audio,
|
29 |
+
sampling_rate,
|
30 |
+
window='hann',
|
31 |
+
nperseg=512,
|
32 |
+
noverlap=412,
|
33 |
+
nfft=1024,
|
34 |
+
return_onesided=True,
|
35 |
+
boundary='zeros',
|
36 |
+
padded=True,
|
37 |
+
axis=- 1)
|
38 |
+
|
39 |
+
|
40 |
+
spectrogram = go.Heatmap(z=librosa.amplitude_to_db(np.abs(stft), ref=np.max),
|
41 |
+
x=frames,
|
42 |
+
y=freq,
|
43 |
+
colorscale='Viridis')
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
fig = go.Figure(spectrogram)
|
48 |
+
|
49 |
+
|
50 |
+
fig.update_layout(
|
51 |
+
#width=300,
|
52 |
+
#height=500,
|
53 |
+
font=dict(family='Latin Modern Roman', size=18),
|
54 |
+
xaxis=dict(
|
55 |
+
title='Time (seconds)',
|
56 |
+
titlefont=dict(family='Latin Modern Roman',
|
57 |
+
size=18)),
|
58 |
+
yaxis=dict(
|
59 |
+
title='Frequency (Hz)',
|
60 |
+
titlefont=dict(family='Latin Modern Roman',
|
61 |
+
size=18)),
|
62 |
+
margin=dict(l=0, r=0, t=0, b=0),
|
63 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
64 |
+
plot_bgcolor='rgba(0,0,0,0)')
|
65 |
+
|
66 |
+
fig.update_traces(colorbar_thickness=8, selector=dict(type='heatmap'))
|
67 |
+
fig.update_traces(showscale=True, showlegend=False, visible = True)
|
68 |
+
fig.update_xaxes(visible=True, showgrid=False)
|
69 |
+
fig.update_yaxes(visible=True, showgrid=False)
|
70 |
+
plotly.offline.plot(fig, filename='stft.html', config={'displaylogo': False, 'modeBarButtonsToRemove': ['toImage','zoomIn', 'zoomOut','resetScale']})
|
71 |
+
|
72 |
+
return 'stft.html'
|
73 |
|
74 |
# Gradio interface
|
75 |
demo = gr.Interface(fn=plot_stft,
|