Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -64,3 +64,89 @@ def process_audio_and_display_info(audio_file, interval):
|
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
main()
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
------------------
|
72 |
+
import gradio as gr
|
73 |
+
import matplotlib.pyplot as plt
|
74 |
+
import numpy as np
|
75 |
+
import os
|
76 |
+
import soundfile as sf
|
77 |
+
|
78 |
+
def main():
|
79 |
+
# Gradio Interface
|
80 |
+
with gr.Blocks() as app:
|
81 |
+
gr.Markdown(
|
82 |
+
"""
|
83 |
+
# <div align="center"> diablofx Audio Interval Cutter (BETA) </div>
|
84 |
+
Want to [support](https://ko-fi.com/diablofx) me? or [join AI HUB](https://discord.gg/aihub) for more help\n
|
85 |
+
"""
|
86 |
+
)
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column():
|
90 |
+
audio_input = gr.Audio(type='filepath')
|
91 |
+
create_spec_butt = gr.Button(value='Create Spectrogram And Get Info', variant='primary')
|
92 |
+
with gr.Column():
|
93 |
+
output_markdown = gr.Markdown(value="", visible=True)
|
94 |
+
image_output = gr.Image(type='filepath', interactive=False)
|
95 |
+
|
96 |
+
create_spec_butt.click(fn=create_spectrogram_and_get_info, inputs=[audio_input], outputs=[output_markdown, image_output])
|
97 |
+
|
98 |
+
app.queue(max_size=1022).launch(share=True)
|
99 |
+
|
100 |
+
def create_spectrogram_and_get_info(audio_file):
|
101 |
+
# Clear figure in case it has data in it
|
102 |
+
plt.clf()
|
103 |
+
|
104 |
+
# Read the audio data from the file
|
105 |
+
audio_data, sample_rate = sf.read(audio_file)
|
106 |
+
|
107 |
+
# Convert to mono if it's not mono
|
108 |
+
if len(audio_data.shape) > 1:
|
109 |
+
audio_data = np.mean(audio_data, axis=1)
|
110 |
+
|
111 |
+
# Create the spectrogram
|
112 |
+
plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
|
113 |
+
cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude', window=np.hanning(4096))
|
114 |
+
|
115 |
+
# Save the spectrogram to a PNG file
|
116 |
+
plt.savefig('spectrogram.png')
|
117 |
+
|
118 |
+
# Get the audio file info
|
119 |
+
audio_info = sf.info(audio_file)
|
120 |
+
|
121 |
+
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0)
|
122 |
+
|
123 |
+
# Convert duration to minutes, seconds, and milliseconds
|
124 |
+
minutes, seconds = divmod(audio_info.duration, 60)
|
125 |
+
seconds, milliseconds = divmod(seconds, 1)
|
126 |
+
milliseconds *= 1000 # convert from seconds to milliseconds
|
127 |
+
|
128 |
+
# Convert bitrate to mb/s
|
129 |
+
bitrate = audio_info.samplerate * audio_info.channels * bit_depth / 8 / 1024 / 1024
|
130 |
+
|
131 |
+
# Calculate speed in kbps
|
132 |
+
speed_in_kbps = audio_info.samplerate * bit_depth / 1000
|
133 |
+
|
134 |
+
# Create a table with the audio file info
|
135 |
+
info_table = f"""
|
136 |
+
|
137 |
+
| Information | Value |
|
138 |
+
| :---: | :---: |
|
139 |
+
| File Name | {os.path.basename(audio_file)} |
|
140 |
+
| Duration | {int(minutes)} minutes - {int(seconds)} seconds - {int(milliseconds)} milliseconds |
|
141 |
+
| Bitrate | {speed_in_kbps} kbp/s |
|
142 |
+
| Audio Channels | {audio_info.channels} |
|
143 |
+
| Samples per second | {audio_info.samplerate} Hz |
|
144 |
+
| Bit per second | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s |
|
145 |
+
|
146 |
+
"""
|
147 |
+
|
148 |
+
# Return the PNG file of the spectrogram and the info table
|
149 |
+
return info_table, 'spectrogram.png'
|
150 |
+
|
151 |
+
# Create the Gradio interface
|
152 |
+
main()
|