import gradio as gr import librosa import librosa.display import matplotlib.pyplot as plt def create_spectrogram_and_get_info(audio_file): # Read the audio data from the file audio_data, sample_rate = librosa.load(audio_file) # Compute the mel-scaled spectrogram spectrogram = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate) # Convert the power spectrogram to decibel (dB) units spectrogram_db = librosa.power_to_db(spectrogram, ref=np.max) # Display the spectrogram 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() # Save the spectrogram to a PNG file plt.savefig('spectrogram.png') # Get the audio file info audio_info = sf.info(audio_file) bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0) # Create a table with the audio file info 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 the PNG file of the spectrogram and the info table return info_table, 'spectrogram.png'