File size: 1,470 Bytes
de60a6a
ef9fce3
 
458da1c
eb0bc41
77121d6
 
ef9fce3
 
 
 
77121d6
ef9fce3
 
77121d6
ef9fce3
 
 
 
 
 
6af81aa
77121d6
458da1c
6af81aa
77121d6
d0b2fc8
 
 
5a86410
77121d6
5a86410
 
 
 
 
 
d0b2fc8
 
5a86410
41bada8
77121d6
41bada8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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'