File size: 9,009 Bytes
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cad88
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e1515
cb39c05
95e1515
 
 
 
 
 
 
 
 
 
cb39c05
95e1515
cb39c05
95e1515
 
 
 
 
 
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e1515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
CLI command for speaker extraction

Extracts specific speaker from audio using reference clip.
"""

import json
import sys
from pathlib import Path

import click
from rich.console import Console
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
from rich.table import Table

from src.services.speaker_extraction import SpeakerExtractionService

console = Console()


def _display_results(report: dict, output_path: Path):
    """Display extraction results in a formatted table"""
    console.print()
    console.print("[bold green]✓ Extraction Complete[/bold green]")
    console.print()

    # Summary table
    summary_table = Table(title="Extraction Summary", show_header=True)
    summary_table.add_column("Metric", style="cyan")
    summary_table.add_column("Value", style="white")

    summary_table.add_row("Segments found", str(report["segments_found"]))
    summary_table.add_row("Segments included", str(report["segments_included"]))
    summary_table.add_row("Total duration", f"{report['total_duration_seconds']:.1f}s")
    summary_table.add_row("Average confidence", f"{report['average_confidence']:.3f}")
    summary_table.add_row("Processing time", f"{report['processing_time_seconds']:.1f}s")

    if report.get("low_confidence_segments", 0) > 0:
        summary_table.add_row(
            "Low confidence segments", str(report["low_confidence_segments"]), style="yellow"
        )

    console.print(summary_table)
    console.print()

    # Output files
    if report.get("output_file"):
        console.print(f"[bold]Output:[/bold] {report['output_file']}")

    # Write report JSON
    report_file = output_path.parent / "extraction_report.json"
    with open(report_file, "w") as f:
        json.dump(report, f, indent=2)

    console.print(f"[bold]Report:[/bold] {report_file}")
    console.print()


@click.command()
@click.argument("reference_clip", type=click.Path(exists=True, path_type=Path))
@click.argument("target_file", type=click.Path(exists=True, path_type=Path))
@click.option(
    "--output",
    "-o",
    type=click.Path(path_type=Path),
    default="extracted_speaker.m4a",
    help="Output file path (or directory if --no-concatenate)",
)
@click.option(
    "--threshold",
    type=float,
    default=0.40,
    help="Speaker matching threshold (0.0-1.0, lower is stricter)",
)
@click.option(
    "--min-confidence",
    type=float,
    default=0.30,
    help="Minimum confidence for including segments (0.0-1.0)",
)
@click.option(
    "--concatenate/--no-concatenate",
    default=True,
    help="Concatenate matching segments into single file",
)
@click.option(
    "--silence",
    type=int,
    default=150,
    help="Silence duration between concatenated segments (milliseconds)",
)
@click.option(
    "--crossfade",
    type=int,
    default=75,
    help="Crossfade duration for smooth transitions (milliseconds)",
)
@click.option("--sample-rate", type=int, default=44100, help="Output sample rate in Hz")
@click.option("--bitrate", type=str, default="192k", help="Output bitrate (e.g., 192k, 256k)")
def extract_speaker(
    reference_clip,
    target_file,
    output,
    threshold,
    min_confidence,
    concatenate,
    silence,
    crossfade,
    sample_rate,
    bitrate,
):
    """
    Extract specific speaker from audio using reference clip.

    REFERENCE_CLIP: Path to audio file containing reference speaker's voice (3+ seconds)

    TARGET_FILE: Path to audio file to extract speaker from

    Examples:

      # Basic extraction with default settings
      voice-tools extract-speaker reference.m4a interview.m4a

      # Strict matching with custom output
      voice-tools extract-speaker ref.m4a target.m4a \\
          --threshold 0.30 --output alice_voice.m4a

      # Export to separate segment files
      voice-tools extract-speaker ref.m4a podcast.m4a \\
          --no-concatenate --output ./alice_segments/
    """
    console.print()
    console.print("[bold]Voice Tools - Speaker Extraction[/bold]")
    console.print()

    try:
        # Validate threshold range
        if not 0.0 <= threshold <= 1.0:
            console.print(
                "[bold red]Error:[/bold red] Threshold must be between 0.0 and 1.0", style="red"
            )
            sys.exit(1)

        if not 0.0 <= min_confidence <= 1.0:
            console.print(
                "[bold red]Error:[/bold red] Min confidence must be between 0.0 and 1.0",
                style="red",
            )
            sys.exit(1)

        # Initialize service
        console.print("Initializing speaker extraction models...")
        service = SpeakerExtractionService()
        console.print("[green]✓[/green] Models loaded")
        console.print()

        # Validate reference clip
        is_valid, message = service.validate_reference_clip(str(reference_clip))
        if not is_valid:
            console.print(f"[bold red]Error:[/bold red] {message}", style="red")
            sys.exit(4)  # Exit code 4 for reference clip issues

        if message and "warning" in message.lower():
            console.print(f"[yellow]Warning:[/yellow] {message}")
            console.print()

        # Progress tracking
        current_task = None

        with Progress(
            SpinnerColumn(),
            TextColumn("[progress.description]{task.description}"),
            BarColumn(),
            TextColumn("{task.completed}/{task.total}"),
            "•",
            TimeElapsedColumn(),
            console=console,
            transient=False,
        ) as prog:

            def progress_callback(stage: str, current: float, total: float):
                nonlocal current_task
                # Interpret float-based (0.0-1.0) vs integer-based formats
                if total == 1.0:
                    # Float format: current is 0.0-1.0, scale to 100 for display
                    display_total = 100
                    display_current = int(current * 100)
                else:
                    # Integer format: use as-is
                    display_total = int(total)
                    display_current = int(current)

                if current_task is None:
                    current_task = prog.add_task(stage, total=display_total)
                else:
                    prog.update(
                        current_task,
                        description=stage,
                        completed=display_current,
                        total=display_total,
                    )

            # Perform extraction
            report = service.extract_and_export(
                reference_clip=str(reference_clip),
                target_file=str(target_file),
                output_path=str(output),
                threshold=threshold,
                min_confidence=min_confidence,
                concatenate=concatenate,
                silence_duration_ms=silence,
                crossfade_duration_ms=crossfade,
                sample_rate=sample_rate,
                bitrate=bitrate,
                progress_callback=progress_callback,
            )

        # Check if result is an error report
        if report.get("status") == "failed":
            error_type = report.get("error_type", "processing")
            # Color-code by error type
            color_map = {
                "audio_io": "red",
                "processing": "red",
                "validation": "yellow",
                "ssl": "magenta",
                "model_loading": "magenta",
            }
            color = color_map.get(error_type, "red")
            console.print(
                f"[bold {color}]Error ({error_type}):[/bold {color}] {report['error']}", style=color
            )
            sys.exit(2)

        # Check if any segments were found
        if report["segments_included"] == 0:
            console.print()
            console.print(
                "[yellow]Warning:[/yellow] No segments matched reference speaker", style="yellow"
            )
            console.print(
                f"  Try lowering the threshold (current: {threshold:.2f}) for more permissive matching",
                style="dim",
            )
            sys.exit(3)  # Exit code 3 for no matches

        # Display results
        _display_results(report, output)

        # Show low confidence warning
        if report.get("low_confidence_segments", 0) > 0:
            console.print(
                f"[yellow]Note:[/yellow] {report['low_confidence_segments']} segment(s) "
                f"have confidence close to threshold. Consider raising threshold for stricter matching.",
                style="dim",
            )
            console.print()

    except Exception as e:
        console.print(f"[bold red]Error:[/bold red] Unexpected error: {e}", style="red")
        console.print()
        console.print("[dim]Stack trace:[/dim]")
        import traceback

        console.print(traceback.format_exc(), style="dim")
        sys.exit(3)  # Exit code 3 for processing error