|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import os |
|
import soundfile as sf |
|
|
|
def create_spectrogram_and_get_info(audio_file): |
|
|
|
plt.clf() |
|
|
|
|
|
audio_data, sample_rate = sf.read(audio_file) |
|
|
|
|
|
audio_data = audio_data.flatten() if len(audio_data.shape) > 1 else audio_data |
|
|
|
|
|
plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided', |
|
cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude') |
|
|
|
|
|
plt.savefig('spectrogram.png') |
|
|
|
|
|
audio_info = sf.info(audio_file) |
|
|
|
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0) |
|
|
|
|
|
minutes, seconds = divmod(audio_info.duration, 60) |
|
seconds, milliseconds = divmod(seconds, 1) |
|
milliseconds *= 1000 |
|
|
|
|
|
bitrate = audio_info.samplerate * audio_info.channels * bit_depth / 8 / 1024 / 1024 |
|
|
|
|
|
info_table = f""" |
|
# Ilaria Audio Analyzer π |
|
|
|
Audio Analyzer Software by Ilaria, Help me on Ko-Fi |
|
|
|
Special thanks to Alex Murkoff for helping me coding it! |
|
|
|
Need help with AI? Join AI Hub! |
|
|
|
| Information | Value | |
|
| --- | --- | |
|
| Duration | {int(minutes)} minutes - {int(seconds)} seconds - {int(milliseconds)} milliseconds | |
|
| Samples per second | {audio_info.samplerate} Hz | |
|
| Audio Channels | {audio_info.channels} | |
|
| Bitrate | {bitrate:.2f} Mb/s | |
|
| Extension | {os.path.splitext(audio_file)[1]} | |
|
""" |
|
|
|
|
|
return info_table, 'spectrogram.png' |
|
|
|
|
|
iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="filepath"), outputs=["markdown", "image"]) |
|
iface.launch() |
|
|