TheStinger commited on
Commit
d3097eb
1 Parent(s): 498396a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -58
app.py CHANGED
@@ -4,44 +4,16 @@ import numpy as np
4
  import os
5
  import soundfile as sf
6
 
7
- def main():
8
- # Gradio Interface
9
- with gr.Blocks() as app:
10
- gr.Markdown(
11
- """
12
- # <div align="center"> Ilaria Audio Analyzer 💖 (BETA) </div>
13
- Audio Analyzer Software by Ilaria, Help me on [Ko-Fi](https://ko-fi.com/ilariaowo)\n
14
- Special thanks to [Alex Murkoff](https://github.com/alexlnkp) for helping me coding it!
15
-
16
- Need help with AI? [Join AI Hub!](https://discord.gg/aihub)
17
- """
18
- )
19
-
20
- with gr.Row():
21
- with gr.Column():
22
- audio_input = gr.Audio(type='filepath')
23
- create_spec_butt = gr.Button(value='Create Spectrogram And Get Info', variant='primary')
24
- with gr.Column():
25
- output_markdown = gr.Markdown(value="", visible=True)
26
- image_output = gr.Image(type='filepath', interactive=False)
27
-
28
- create_spec_butt.click(fn=create_spectrogram_and_get_info, inputs=[audio_input], outputs=[output_markdown, image_output])
29
- audio_input.change(fn=lambda: ({"value": "", "__type__": "update"},
30
- {"value": "", "__type__": "update"}),
31
- inputs=[], outputs=[image_output, output_markdown])
32
-
33
- app.queue(max_size=1022).launch(share=True)
34
-
35
  def create_spectrogram_and_get_info(audio_file):
 
36
  # Clear figure in case it has data in it
37
  plt.clf()
38
 
39
  # Read the audio data from the file
40
  audio_data, sample_rate = sf.read(audio_file)
41
 
42
- # Convert to mono if it's not mono
43
- if len(audio_data.shape) > 1:
44
- audio_data = np.mean(audio_data, axis=1)
45
 
46
  # Create the spectrogram
47
  plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
@@ -52,38 +24,23 @@ def create_spectrogram_and_get_info(audio_file):
52
 
53
  # Get the audio file info
54
  audio_info = sf.info(audio_file)
55
-
56
  bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
57
 
58
- # Convert duration to minutes, seconds, and milliseconds
59
- minutes, seconds = divmod(audio_info.duration, 60)
60
- seconds, milliseconds = divmod(seconds, 1)
61
- milliseconds *= 1000 # convert from seconds to milliseconds
62
-
63
- # Convert bitrate to mb/s
64
- bitrate = audio_info.samplerate * audio_info.channels * bit_depth / 8 / 1024 / 1024
65
-
66
- # Calculate speed in kbps
67
- speed_in_kbps = audio_info.samplerate * bit_depth / 1000
68
-
69
  # Create a table with the audio file info
70
  info_table = f"""
71
- <center>
72
-
73
- | Information | Value |
74
- | :---: | :---: |
75
- | File Name | {os.path.basename(audio_file)} |
76
- | Duration | {int(minutes)} minutes - {int(seconds)} seconds - {int(milliseconds)} milliseconds |
77
- | Bitrate | {speed_in_kbps} kbp/s |
78
- | Audio Channels | {audio_info.channels} |
79
- | Samples per second | {audio_info.samplerate} Hz |
80
- | Bit per second | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
81
-
82
- </center>
83
  """
84
-
85
  # Return the PNG file of the spectrogram and the info table
86
- return {"value": info_table, "__type__": "update"}, 'spectrogram.png'
87
 
88
  # Create the Gradio interface
89
- main()
 
 
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',
 
24
 
25
  # Get the audio file info
26
  audio_info = sf.info(audio_file)
27
+
28
  bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
29
 
 
 
 
 
 
 
 
 
 
 
 
30
  # Create a table with the audio file info
31
  info_table = f"""
32
+ | Informazione | Valore |
33
+ | --- | --- |
34
+ | Durata | {audio_info.duration} secondi |
35
+ | Campioni al secondo | {audio_info.samplerate} Hz |
36
+ | Canali | {audio_info.channels} |
37
+ | Bitrate | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
38
+ | Estensione | {os.path.splitext(audio_file)[1]} |
 
 
 
 
 
39
  """
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()