Spaces:
Runtime error
Runtime error
File size: 2,436 Bytes
ba8d59d bc570b2 97c41eb ba8d59d bc570b2 ba8d59d bc570b2 ba8d59d bc570b2 97c41eb ba8d59d bc570b2 ba8d59d bc570b2 ba8d59d |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import pesq
import numpy as np
import soundfile as sf
import gradio as gr
def calculate_pesq(reference_audio, degraded_audio):
# Extract audio data and sample rates from Gradio's Audio widget output
ref_signal, ref_sample_rate = reference_audio[0], reference_audio[1]
deg_signal, deg_sample_rate = degraded_audio[0], degraded_audio[1]
# Convert audio data to the same sample rate if needed
if ref_sample_rate != deg_sample_rate:
from scipy.signal import resample
min_length = min(len(ref_signal), len(deg_signal))
ref_signal = resample(ref_signal[:min_length], len(ref_signal))
deg_signal = resample(deg_signal[:min_length], len(deg_signal))
pesq_score = pesq.pesq(ref_signal, deg_signal, ref_sample_rate)
return pesq_score
def calculate_snr(reference_audio, degraded_audio):
ref_signal, _ = reference_audio
deg_signal, _ = degraded_audio
noise = ref_signal - deg_signal
snr = 10 * np.log10(np.sum(ref_signal**2) / np.sum(noise**2))
return snr
def calculate_audio_difference(reference_audio, degraded_audio):
ref_signal, ref_sample_rate = reference_audio[0], reference_audio[1]
deg_signal, deg_sample_rate = degraded_audio[0], degraded_audio[1]
if ref_sample_rate != deg_sample_rate:
from scipy.signal import resample
min_length = min(len(ref_signal), len(deg_signal))
ref_signal = resample(ref_signal[:min_length], len(ref_signal))
deg_signal = resample(deg_signal[:min_length], len(deg_signal))
diff_count = np.count_nonzero(ref_signal != deg_signal)
total_samples = len(ref_signal)
difference_percentage = (diff_count / total_samples) * 100
return difference_percentage
def audio_quality_interface(reference_audio, degraded_audio):
pesq_score = calculate_pesq(reference_audio, degraded_audio)
snr = calculate_snr(reference_audio, degraded_audio)
difference_percentage = calculate_audio_difference(reference_audio, degraded_audio)
result = f"Risultati:\n\nPunteggio PESQ: {pesq_score:.2f}\nSNR: {snr:.2f}\nPercentuale di differenza: {difference_percentage:.2f}%"
return result
iface = gr.Interface(fn=audio_quality_interface,
inputs=[gr.Audio(label="Audio di riferimento"), gr.Audio(label="Audio degradato")],
outputs=gr.Textbox(),
layout="vertical",
live=True)
iface.launch()
|