File size: 1,813 Bytes
1857b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pause classifier — inverts VAD speech segments into silence gaps and classifies them.

Pause types:
    - micro (< 0.25s): ignored, not worth reporting
    - natural (0.25s – 1.0s): healthy, expected pauses
    - hesitation (1.0s – 2.5s): may indicate uncertainty
    - lost_train_of_thought (> 2.5s): significant interruption
"""

from .vad_service import PAUSE_MICRO_MAX_S, PAUSE_NATURAL_MAX_S, PAUSE_HESITATION_MAX_S


def speech_segments_to_pauses(speech_segments, total_duration_s):
    """speech_segments: [(start_s, end_s), ...], sorted.
    Returns the gaps between/around them as raw pause candidates."""
    pauses = []
    cursor = 0.0
    for start, end in speech_segments:
        if start > cursor:
            pauses.append((cursor, start))
        cursor = end
    if total_duration_s > cursor:
        pauses.append((cursor, total_duration_s))
    return pauses


def classify_pause(duration_s: float) -> str | None:
    """Returns None for micro-pauses (not worth reporting), else a pause_type."""
    if duration_s < PAUSE_MICRO_MAX_S:
        return None
    elif duration_s < PAUSE_NATURAL_MAX_S:
        return "natural"
    elif duration_s < PAUSE_HESITATION_MAX_S:
        return "hesitation"
    return "lost_train_of_thought"


def get_classified_pauses(speech_segments, total_duration_s):
    """Returns list of classified pause events (excluding micro-pauses)."""
    events = []
    for start, end in speech_segments_to_pauses(speech_segments, total_duration_s):
        pause_type = classify_pause(end - start)
        if pause_type is not None:
            events.append({
                "start": round(start, 2),
                "end": round(end, 2),
                "duration": round(end - start, 2),
                "type": pause_type,
            })
    return events