TheStinger commited on
Commit
d0b2fc8
1 Parent(s): b253fed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -6,7 +6,7 @@ import soundfile as sf
6
 
7
  def create_spectrogram_and_get_info(audio_file):
8
  # Read the audio data from the file
9
- audio_data, sample_rate = sf.read(audio_file.name)
10
 
11
  # Flatten the audio data if it's not mono
12
  if len(audio_data.shape) > 1:
@@ -19,7 +19,9 @@ def create_spectrogram_and_get_info(audio_file):
19
  plt.savefig('spectrogram.png')
20
 
21
  # Get the audio file info
22
- audio_info = sf.info(audio_file.name)
 
 
23
 
24
  # Create a table with the audio file info
25
  info_table = f"""
@@ -28,15 +30,15 @@ def create_spectrogram_and_get_info(audio_file):
28
  | Durata | {audio_info.duration} secondi |
29
  | Campioni al secondo | {audio_info.samplerate} Hz |
30
  | Canali | {audio_info.channels} |
31
- | Bitrate | {audio_info.samplerate * audio_info.channels * audio_info.subtype.itemsize * 8} bit/s |
32
- | Estensione | {os.path.splitext(audio_file.name)[1]} |
33
  """
34
 
35
  # Return the PNG file of the spectrogram and the info table
36
  return info_table, 'spectrogram.png'
37
 
38
  # Create the Gradio interface
39
- iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="numpy"), outputs=["markdown", "image"])
40
  iface.launch()
41
 
42
 
 
6
 
7
  def create_spectrogram_and_get_info(audio_file):
8
  # Read the audio data from the file
9
+ audio_data, sample_rate = sf.read(audio_file)
10
 
11
  # Flatten the audio data if it's not mono
12
  if len(audio_data.shape) > 1:
 
19
  plt.savefig('spectrogram.png')
20
 
21
  # Get the audio file info
22
+ audio_info = sf.info(audio_file)
23
+
24
+ bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
25
 
26
  # Create a table with the audio file info
27
  info_table = f"""
 
30
  | Durata | {audio_info.duration} secondi |
31
  | Campioni al secondo | {audio_info.samplerate} Hz |
32
  | Canali | {audio_info.channels} |
33
+ | Bitrate | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
34
+ | Estensione | {os.path.splitext(audio_file)[1]} |
35
  """
36
 
37
  # Return the PNG file of the spectrogram and the info table
38
  return info_table, 'spectrogram.png'
39
 
40
  # Create the Gradio interface
41
+ iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="filepath"), outputs=["markdown", "image"])
42
  iface.launch()
43
 
44