File size: 857 Bytes
fe506fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from resemblyzer import VoiceEncoder, preprocess_wav
import numpy as np
import os

encoder = VoiceEncoder()


def verify_voices(audio_path1, audio_path2, threshold=0.75):
    """
    Compares two audio files and returns whether they belong to the same speaker.
    """
    try:
        audio1 = preprocess_wav(audio_path1)
        audio2 = preprocess_wav(audio_path2)

        embedding1 = encoder.embed_utterance(audio1)
        embedding2 = encoder.embed_utterance(audio2)

        similarity = np.dot(embedding1, embedding2) / (
            np.linalg.norm(embedding1) * np.linalg.norm(embedding2)
        )

        match = similarity > threshold

        return {
            "match": match,
            "similarity": round(float(similarity), 4),
            "threshold": threshold,
        }
    except Exception as e:
        return {"error": str(e)}