TheStinger commited on
Commit
a0110cc
1 Parent(s): 57f4714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -1,24 +1,23 @@
1
  import gradio as gr
2
- import librosa
3
- import librosa.display
4
  import matplotlib.pyplot as plt
 
 
 
5
 
6
  def create_spectrogram_and_get_info(audio_file):
7
- # Read the audio data from the file
8
- audio_data, sample_rate = librosa.load(audio_file)
9
 
10
- # Compute the mel-scaled spectrogram
11
- spectrogram = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate)
 
 
 
12
 
13
- # Convert the power spectrogram to decibel (dB) units
14
- spectrogram_db = librosa.power_to_db(spectrogram, ref=np.max)
15
 
16
- # Display the spectrogram
17
- plt.figure(figsize=(10, 4))
18
- librosa.display.specshow(spectrogram_db, x_axis='time', y_axis='mel', sr=sample_rate, fmax=8000)
19
- plt.colorbar(format='%+2.0f dB')
20
- plt.title('Mel spectrogram')
21
- plt.tight_layout()
22
 
23
  # Save the spectrogram to a PNG file
24
  plt.savefig('spectrogram.png')
@@ -41,3 +40,8 @@ def create_spectrogram_and_get_info(audio_file):
41
 
42
  # Return the PNG file of the spectrogram and the info table
43
  return info_table, 'spectrogram.png'
 
 
 
 
 
 
1
  import gradio as gr
 
 
2
  import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ import os
5
+ import soundfile as sf
6
 
7
  def create_spectrogram_and_get_info(audio_file):
 
 
8
 
9
+ # Clear figure in case it has data in it
10
+ plt.clf()
11
+
12
+ # Read the audio data from the file
13
+ audio_data, sample_rate = sf.read(audio_file)
14
 
15
+ # Flatten the audio data if it's not mono
16
+ audio_data = audio_data.flatten() if len(audio_data.shape) > 1 else audio_data
17
 
18
+ # Create the spectrogram
19
+ plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
20
+ cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude')
 
 
 
21
 
22
  # Save the spectrogram to a PNG file
23
  plt.savefig('spectrogram.png')
 
40
 
41
  # Return the PNG file of the spectrogram and the info table
42
  return info_table, 'spectrogram.png'
43
+
44
+ # Create the Gradio interface
45
+ iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="filepath"), outputs=["markdown", "image"])
46
+ iface.launch()
47
+