diablofx commited on
Commit
ba7c3c9
1 Parent(s): 284c479

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -27
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import matplotlib.pyplot as plt
3
  import numpy as np
4
  import os
5
  import soundfile as sf
@@ -16,19 +15,15 @@ def main():
16
  with gr.Row():
17
  with gr.Column():
18
  audio_input = gr.Audio(type='filepath')
19
- create_spec_butt = gr.Button(value='Create Spectrogram And Get Info', variant='primary')
20
  with gr.Column():
21
  output_markdown = gr.Markdown(value="", visible=True)
22
- image_output = gr.Image(type='filepath', interactive=False)
23
 
24
- create_spec_butt.click(fn=create_spectrogram_and_get_info, inputs=[audio_input], outputs=[output_markdown, image_output])
25
 
26
  app.queue(max_size=1022).launch(share=True)
27
 
28
- def create_spectrogram_and_get_info(audio_file):
29
- # Clear figure in case it has data in it
30
- plt.clf()
31
-
32
  # Read the audio data from the file
33
  audio_data, sample_rate = sf.read(audio_file)
34
 
@@ -36,27 +31,15 @@ def create_spectrogram_and_get_info(audio_file):
36
  if len(audio_data.shape) > 1:
37
  audio_data = np.mean(audio_data, axis=1)
38
 
39
- # Create the spectrogram
40
- plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
41
- cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude', window=np.hanning(4096))
42
-
43
- # Save the spectrogram to a PNG file
44
- plt.savefig('spectrogram.png')
45
-
46
  # Get the audio file info
47
  audio_info = sf.info(audio_file)
48
 
49
  bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
50
 
51
- # Convert duration to minutes, seconds, and milliseconds
52
  minutes, seconds = divmod(audio_info.duration, 60)
53
- seconds, milliseconds = divmod(seconds, 1)
54
- milliseconds *= 1000 # convert from seconds to milliseconds
55
-
56
- # Convert bitrate to mb/s
57
- bitrate = audio_info.samplerate * audio_info.channels * bit_depth / 8 / 1024 / 1024
58
 
59
- # Calculate speed in kbps
60
  speed_in_kbps = audio_info.samplerate * bit_depth / 1000
61
 
62
  # Create a table with the audio file info
@@ -65,16 +48,15 @@ def create_spectrogram_and_get_info(audio_file):
65
  | Information | Value |
66
  | :---: | :---: |
67
  | File Name | {os.path.basename(audio_file)} |
68
- | Duration | {int(minutes)} minutes - {int(seconds)} seconds - {int(milliseconds)} milliseconds |
69
  | Bitrate | {speed_in_kbps} kbp/s |
70
  | Audio Channels | {audio_info.channels} |
71
  | Samples per second | {audio_info.samplerate} Hz |
72
- | Bit per second | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
73
 
74
  """
75
 
76
- # Return the PNG file of the spectrogram and the info table
77
- return info_table, 'spectrogram.png'
78
 
79
  # Create the Gradio interface
80
- main()
 
1
  import gradio as gr
 
2
  import numpy as np
3
  import os
4
  import soundfile as sf
 
15
  with gr.Row():
16
  with gr.Column():
17
  audio_input = gr.Audio(type='filepath')
18
+ create_info_butt = gr.Button(value='Get Audio File Info', variant='primary')
19
  with gr.Column():
20
  output_markdown = gr.Markdown(value="", visible=True)
 
21
 
22
+ create_info_butt.click(fn=get_audio_file_info, inputs=[audio_input], outputs=[output_markdown])
23
 
24
  app.queue(max_size=1022).launch(share=True)
25
 
26
+ def get_audio_file_info(audio_file):
 
 
 
27
  # Read the audio data from the file
28
  audio_data, sample_rate = sf.read(audio_file)
29
 
 
31
  if len(audio_data.shape) > 1:
32
  audio_data = np.mean(audio_data, axis=1)
33
 
 
 
 
 
 
 
 
34
  # Get the audio file info
35
  audio_info = sf.info(audio_file)
36
 
37
  bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
38
 
39
+ # Convert duration to minutes and seconds
40
  minutes, seconds = divmod(audio_info.duration, 60)
 
 
 
 
 
41
 
42
+ # Convert bitrate to kbps
43
  speed_in_kbps = audio_info.samplerate * bit_depth / 1000
44
 
45
  # Create a table with the audio file info
 
48
  | Information | Value |
49
  | :---: | :---: |
50
  | File Name | {os.path.basename(audio_file)} |
51
+ | Duration | {int(minutes)} minutes - {int(seconds)} seconds |
52
  | Bitrate | {speed_in_kbps} kbp/s |
53
  | Audio Channels | {audio_info.channels} |
54
  | Samples per second | {audio_info.samplerate} Hz |
 
55
 
56
  """
57
 
58
+ # Return the info table
59
+ return info_table
60
 
61
  # Create the Gradio interface
62
+ main()