File size: 9,763 Bytes
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cad88
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
268
269
270
271
272
273
274
275
"""
CLI command for voice denoising.

Removes silence and background noise from audio files.
"""

import json
import logging
import sys
from pathlib import Path
from typing import Optional

import click
from rich.console import Console
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeRemainingColumn

from src.lib.audio_io import AudioIOError, write_audio
from src.services.voice_denoising import VoiceDenoisingService

logger = logging.getLogger(__name__)
console = Console()


@click.command()
@click.argument("input_file", type=click.Path(exists=True, path_type=Path))
@click.option(
    "--output",
    "-o",
    type=click.Path(path_type=Path),
    help="Output file path (default: input_denoised.m4a)",
)
@click.option(
    "--vad-threshold",
    type=click.FloatRange(0.0, 1.0),
    default=0.5,
    help="Voice activity detection threshold (0.0-1.0, default: 0.5). Higher = more aggressive.",
)
@click.option(
    "--silence-threshold",
    type=float,
    default=1.5,
    help="Maximum silence duration to remove in seconds (default: 1.5).",
)
@click.option(
    "--min-duration",
    type=float,
    default=0.5,
    help="Minimum voice segment duration in seconds (default: 0.5).",
)
@click.option(
    "--crossfade",
    type=int,
    default=75,
    help="Crossfade duration between segments in milliseconds (default: 75).",
)
@click.option(
    "--silence",
    type=int,
    default=150,
    help="Silence duration between segments in milliseconds (default: 150).",
)
@click.option(
    "--output-format",
    type=click.Choice(["m4a", "wav"], case_sensitive=False),
    default="m4a",
    help="Output audio format (default: m4a).",
)
@click.option(
    "--report",
    type=click.Path(path_type=Path),
    help="Path to save denoising report JSON (default: same directory as output).",
)
def denoise(
    input_file: Path,
    output: Optional[Path],
    vad_threshold: float,
    silence_threshold: float,
    min_duration: float,
    crossfade: int,
    silence: int,
    output_format: str,
    report: Optional[Path],
):
    """
    Remove silence and background noise from audio file.

    This command uses voice activity detection (VAD) to identify voice segments,
    removes background noise, and concatenates the voice segments with smooth
    transitions.

    Examples:

        # Basic denoising with defaults
        voice-tools denoise noisy_audio.m4a

        # Custom output path
        voice-tools denoise noisy_audio.m4a --output clean_audio.m4a

        # Aggressive noise removal
        voice-tools denoise noisy_audio.m4a --vad-threshold 0.7 --silence-threshold 1.0

        # Keep more audio (less aggressive)
        voice-tools denoise noisy_audio.m4a --vad-threshold 0.3 --silence-threshold 3.0
    """
    console.print("\n[bold cyan]Voice Tools - Voice Denoising[/bold cyan]\n")

    # Validate input file
    if not input_file.exists():
        console.print(f"[red]Error: Input file not found: {input_file}[/red]", file=sys.stderr)
        sys.exit(1)

    # Determine output path
    if output is None:
        output = input_file.parent / f"{input_file.stem}_denoised.{output_format}"
    else:
        # Ensure output has correct extension
        if output.suffix.lower().lstrip(".") != output_format.lower():
            output = output.with_suffix(f".{output_format}")

    # Determine report path
    if report is None:
        report = output.parent / "denoising_report.json"

    try:
        # Initialize service
        with Progress(
            SpinnerColumn(),
            TextColumn("[progress.description]{task.description}"),
            transient=True,
            console=console,
        ) as progress:
            progress.add_task(description="Initializing voice denoising models...", total=None)

            try:
                service = VoiceDenoisingService(vad_threshold=vad_threshold)
            except Exception as e:
                console.print(
                    f"[red]Error: Failed to initialize models: {e}[/red]", file=sys.stderr
                )
                sys.exit(3)

        console.print("[green]✓[/green] Models loaded\n")

        # Log configuration
        logger.info(f"Denoising {input_file}")
        logger.info(f"VAD threshold: {vad_threshold}, Silence threshold: {silence_threshold}s")
        logger.info(
            f"Min duration: {min_duration}s, Crossfade: {crossfade}ms, Silence: {silence}ms"
        )

        # Process audio
        with Progress(
            SpinnerColumn(),
            TextColumn("[progress.description]{task.description}"),
            BarColumn(),
            TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
            TimeRemainingColumn(),
            console=console,
        ) as progress:
            task = progress.add_task(description="Processing audio...", total=100)

            # Progress callback
            def progress_callback(stage: str, current: float, total: float):
                # 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_progress = int(current * 100)
                else:
                    # Integer format: convert to percentage
                    display_progress = int((current / total) * 100) if total > 0 else 0

                progress.update(
                    task, description=f"[cyan]{stage}[/cyan]", completed=display_progress
                )

            # Step 1: Read and denoise (70%)
            progress.update(task, description="[cyan]Reading audio and detecting voice...[/cyan]")

            try:
                denoised_audio, denoise_report = service.denoise_audio(
                    str(input_file),
                    silence_threshold=silence_threshold,
                    min_segment_duration=min_duration,
                    crossfade_ms=crossfade,
                    silence_ms=silence,
                    progress_callback=progress_callback,
                )

                # Check if result is an error report
                if denoised_audio is None and denoise_report.get("status") == "failed":
                    error_type = denoise_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"[{color}]Error ({error_type}): {denoise_report['error']}[/{color}]",
                        file=sys.stderr,
                    )
                    sys.exit(2)
            except Exception as e:
                console.print(f"[red]Error: Unexpected error: {e}[/red]", file=sys.stderr)
                logger.exception("Unexpected error")
                sys.exit(3)

            progress.update(task, advance=70)

            # Check if any audio was kept
            if len(denoised_audio) == 0:
                console.print(
                    "\n[yellow]Warning: No voice segments detected[/yellow]\n"
                    "  Try lowering the VAD threshold (--vad-threshold) or\n"
                    "  increasing the silence threshold (--silence-threshold)\n",
                    file=sys.stderr,
                )
                sys.exit(3)

            # Step 2: Write output (20%)
            progress.update(task, description="[cyan]Writing output file...[/cyan]")

            try:
                write_audio(str(output), denoised_audio, 16000)
            except Exception as e:
                console.print(f"[red]Error: Failed to write output: {e}[/red]", file=sys.stderr)
                logger.exception("Write error")
                sys.exit(4)

            progress.update(task, advance=20)

            # Step 3: Write report (10%)
            progress.update(task, description="[cyan]Generating report...[/cyan]")

            try:
                report_data = {
                    **denoise_report,
                    "output_file": str(output),
                    "report_file": str(report),
                }

                with open(report, "w") as f:
                    json.dump(report_data, f, indent=2)
            except Exception as e:
                console.print(f"[yellow]Warning: Failed to write report: {e}[/yellow]")
                logger.warning(f"Report write failed: {e}")

            progress.update(task, advance=10)

        # Display results
        console.print("\n[green]✓ Denoising complete![/green]\n")
        console.print(f"[cyan]Output:[/cyan] {output}")
        console.print(f"[cyan]Report:[/cyan] {report}\n")

        # Display statistics
        console.print("[bold]Statistics:[/bold]")
        console.print(f"  Original duration: {denoise_report['original_duration']:.1f}s")
        console.print(f"  Output duration:   {denoise_report['output_duration']:.1f}s")
        console.print(f"  Compression ratio: {denoise_report['compression_ratio']:.1%}")
        console.print(f"  Segments kept:     {denoise_report['segments_kept']}")
        console.print(f"  Segments removed:  {denoise_report['segments_removed']}\n")

        sys.exit(0)

    except KeyboardInterrupt:
        console.print("\n[yellow]Interrupted by user[/yellow]")
        sys.exit(130)
    except Exception as e:
        console.print(f"[red]Unexpected error: {e}[/red]", file=sys.stderr)
        logger.exception("Unexpected error")
        sys.exit(1)