|
import gradio as gr |
|
import librosa |
|
import librosa.display |
|
import matplotlib.pyplot as plt |
|
|
|
def create_spectrogram_and_get_info(audio_file): |
|
|
|
audio_data, sample_rate = librosa.load(audio_file) |
|
|
|
|
|
spectrogram = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate) |
|
|
|
|
|
spectrogram_db = librosa.power_to_db(spectrogram, ref=np.max) |
|
|
|
|
|
plt.figure(figsize=(10, 4)) |
|
librosa.display.specshow(spectrogram_db, x_axis='time', y_axis='mel', sr=sample_rate, fmax=8000) |
|
plt.colorbar(format='%+2.0f dB') |
|
plt.title('Mel spectrogram') |
|
plt.tight_layout() |
|
|
|
|
|
plt.savefig('spectrogram.png') |
|
|
|
|
|
audio_info = sf.info(audio_file) |
|
|
|
bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0) |
|
|
|
|
|
info_table = f""" |
|
| Informazione | Valore | |
|
| --- | --- | |
|
| Durata | {audio_info.duration} secondi | |
|
| Campioni al secondo | {audio_info.samplerate} Hz | |
|
| Canali | {audio_info.channels} | |
|
| Bitrate | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s | |
|
| Estensione | {os.path.splitext(audio_file)[1]} | |
|
""" |
|
|
|
|
|
return info_table, 'spectrogram.png' |
|
|