File size: 2,821 Bytes
95e1515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Progress Reporting Utilities

Provides standardized progress callback types and stage name constants
for consistent progress reporting across all Voice Tools services.
"""

from typing import Callable

# Standard progress callback signature
# Parameters: (stage: str, current: float, total: float)
# - stage: User-friendly description of current processing stage
# - current: Current progress value (0.0-1.0 for modern format)
# - total: Total progress value (1.0 for percentage, >1.0 for legacy format)
ProgressCallback = Callable[[str, float, float], None]


# Standardized stage names for Speaker Separation
SPEAKER_SEPARATION_STAGES = {
    "segmentation": "Detecting speech segments",
    "embeddings": "Computing speaker embeddings",
    "discrete_diarization": "Identifying speakers",
    "speaker_counting": "Counting speakers",
}

# Standardized stage names for Voice Denoising
VOICE_DENOISING_STAGES = [
    "Detecting voice activity",  # VAD processing
    "Reducing noise",  # Noise reduction
    "Concatenating segments",  # Segment assembly
]

# Standardized stage names for Speaker Extraction
SPEAKER_EXTRACTION_STAGES = [
    "Loading reference audio",  # Reference processing
    "Computing embeddings",  # Embedding generation
    "Matching voice segments",  # Similarity comparison
    "Extracting segments",  # Audio extraction
]


def interpret_progress(current: float, total: float) -> float:
    """
    Convert progress callback parameters to 0.0-1.0 percentage.

    Handles both modern format (total=1.0, current is percentage)
    and legacy format (total>1.0, current is count).

    Args:
        current: Current progress value
        total: Total progress value

    Returns:
        float: Progress percentage between 0.0 and 1.0
    """
    if total == 1.0:
        # Modern format: current is already a percentage
        return max(0.0, min(1.0, current))
    elif total > 0:
        # Legacy format: calculate percentage
        return max(0.0, min(1.0, current / total))
    else:
        return 0.0


def safe_progress_callback(
    callback: ProgressCallback | None, stage: str, current: float, total: float
) -> None:
    """
    Safely invoke progress callback with exception handling.

    Progress reporting is non-critical, so exceptions are caught
    and logged rather than propagated.

    Args:
        callback: Optional progress callback function
        stage: Stage description
        current: Current progress
        total: Total progress
    """
    if callback:
        try:
            callback(stage, current, total)
        except Exception as e:
            import logging

            logger = logging.getLogger(__name__)
            logger.warning(f"Progress callback failed: {e}")
            # Continue processing - progress reporting is non-critical