jhauret commited on
Commit
cd352f8
1 Parent(s): 954dbdf

test plotly & html

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. 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
- y, sr = librosa.load(audio_file)
12
-
13
- # Compute the Short-Time Fourier Transform (STFT)
14
- D = librosa.stft(y)
15
- S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
16
-
17
- # Plot the STFT
18
- plt.figure(figsize=(10, 6))
19
- librosa.display.specshow(S_db, sr=sr, x_axis='time', y_axis='log')
20
- plt.colorbar(format='%+2.0f dB')
21
- plt.title('STFT (Short-Time Fourier Transform)')
22
-
23
- # Save the plot to a BytesIO object
24
- buf = BytesIO()
25
- plt.savefig(buf, format='png')
26
- plt.close()
27
- buf.seek(0)
28
-
29
- # Encode the image as base64
30
- image_base64 = base64.b64encode(buf.getvalue()).decode('utf-8')
31
- buf.close()
32
-
33
- # Create an HTML img tag with the base64 encoded image
34
- html_img = f'<img src="data:image/png;base64,{image_base64}" alt="STFT plot"/>'
35
-
36
- return html_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,