File size: 1,375 Bytes
81e781d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0de5606
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
import librosa
import matplotlib.pyplot as plt
import pandas as pd
from utils import word_by_word_table
import shutil
import os


def compare_audio(tts_path, user_audio_path):
    if isinstance(tts_path, str):
        y_tts, sr_tts = librosa.load(tts_path, sr=None)
    elif isinstance(tts_path, tuple):
        sr_tts, y_tts = tts_path
    else:
        raise ValueError("Invalid gTTS input type")
    if user_audio_path is None:
        return None
    if isinstance(user_audio_path, str):
        y_user, sr_user = librosa.load(user_audio_path, sr=None)
    elif isinstance(user_audio_path, tuple):
        sr_user, y_user = user_audio_path
    else:
        raise ValueError("Invalid user audio input type")
    min_len = min(len(y_tts), len(y_user))
    y_tts, y_user = y_tts[:min_len], y_user[:min_len]
    fig, ax = plt.subplots(3, 1, figsize=(10, 7))
    ax[0].plot(y_tts)
    ax[0].set_title("Reference (gTTS) Audio")
    ax[1].plot(y_user)
    ax[1].set_title("Your Recorded Audio")
    ax[2].plot(y_tts - y_user, color="red")
    ax[2].set_title("Difference (Reference - Recorded)")
    plt.tight_layout()
    return fig



def compare_both(tts_path, user_audio_path):
    fig = compare_audio(tts_path, user_audio_path)
    table = word_by_word_table(tts_path, user_audio_path)
    return fig, table


def reset_all():
    return ("", None, None, None, None, None)