File size: 1,529 Bytes
de60a6a
458da1c
a0110cc
 
 
eb0bc41
9a8d907
d3097eb
a0110cc
 
 
 
 
77121d6
d3097eb
 
77121d6
a0110cc
 
9a8d907
6af81aa
77121d6
458da1c
6af81aa
77121d6
d0b2fc8
d3097eb
d0b2fc8
5a86410
77121d6
5a86410
d3097eb
 
 
 
 
 
 
5a86410
d3097eb
ed72842
d3097eb
a0110cc
 
d3097eb
 
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
45
46
47
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):

    # Clear figure in case it has data in it
    plt.clf()
    
    # Read the audio data from the file
    audio_data, sample_rate = sf.read(audio_file)

    # Flatten the audio data if it's not mono
    audio_data = audio_data.flatten() if len(audio_data.shape) > 1 else audio_data

    # Create the spectrogram
    plt.specgram(audio_data, Fs=sample_rate / 1, NFFT=4096, sides='onesided',
                 cmap="Reds_r", scale_by_freq=True, scale='dB', mode='magnitude')

    # 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'

# Create the Gradio interface
iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="filepath"), outputs=["markdown", "image"])
iface.launch()