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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
import os
from pathlib import Path
from typing import List, Optional, Tuple, Union

import librosa
import numpy as np
import soundfile as sf

try:
    from .audio_info import validate_audio_path
except ImportError:
    from audio_info import validate_audio_path


def cut_audio(
    audio_path: str,
    start_time: float,
    end_time: float,
    output_path: Optional[str] = None,
    output_format: str = "wav",
) -> str:
    """
    Cut a segment from an audio file between specified start and end times.

    Args:
        audio_path: Path to input audio file or URL
        start_time: Start time in seconds
        end_time: End time in seconds
        output_path: Optional output directory (default: None, uses current directory)
        output_format: Output format ('wav' or 'mp3', default: 'wav')

    Returns:
        Path to the cut audio file

    Raises:
        ValueError: If start_time >= end_time or times are out of range
        FileNotFoundError: If audio file doesn't exist
    """
    try:
        # Validate audio path
        validated_path = validate_audio_path(audio_path)

        # Load audio
        y, sr = librosa.load(validated_path, sr=None, mono=False)

        # Get audio duration
        duration = len(y) / sr if y.ndim == 1 else len(y[0]) / sr

        if start_time >= end_time:
            raise ValueError(
                f"Start time ({start_time}s) must be less than end time ({end_time}s)"
            )

        if start_time < 0:
            raise ValueError(f"Start time ({start_time}s) cannot be negative")

        if end_time > duration:
            raise ValueError(
                f"End time ({end_time}s) exceeds audio duration ({duration:.2f}s)"
            )

        # Convert time to sample indices
        start_sample = int(start_time * sr)
        end_sample = int(end_time * sr)

        # Cut the audio segment
        if y.ndim == 1:
            # Mono audio
            y_cut = y[start_sample:end_sample]
        else:
            # Multi-channel audio
            y_cut = y[:, start_sample:end_sample]

        # Generate output filename
        if not output_path:
            output_path = "."
        os.makedirs(output_path, exist_ok=True)

        original_filename = Path(validated_path).stem
        output_filename = f"{original_filename}_cut_{start_time:.1f}s_to_{end_time:.1f}s.{output_format.lower()}"
        output_file_path = os.path.join(output_path, output_filename)

        # Save the cut audio
        if y_cut.ndim == 2:
            y_cut = y_cut.T  # Transpose for soundfile

        if output_format.lower() == "mp3":
            # For MP3, use ffmpeg through subprocess
            import tempfile
            import subprocess

            with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
                sf.write(temp_wav.name, y_cut, sr)

                cmd = [
                    "ffmpeg",
                    "-y",
                    "-i",
                    temp_wav.name,
                    "-c:a",
                    "libmp3lame",
                    "-b:a",
                    "192k",
                    output_file_path,
                ]
                subprocess.run(cmd, capture_output=True, check=True)
                os.unlink(temp_wav.name)
        else:
            sf.write(output_file_path, y_cut, sr)

        return output_file_path

    except Exception as e:
        raise RuntimeError(f"Error cutting audio: {str(e)}")


def mute_time_windows(
    audio_path: str,
    mute_windows: List[Tuple[float, float]],
    output_path: Optional[str] = None,
    output_format: str = "wav",
    fade_duration: float = 0.1,
) -> str:
    """
    Mute specific time windows in an audio file.

    Args:
        audio_path: Path to input audio file or URL
        mute_windows: List of (start_time, end_time) tuples in seconds
        output_path: Optional output directory (default: None, uses current directory)
        output_format: Output format ('wav' or 'mp3', default: 'wav')
        fade_duration: Fade in/out duration in seconds for smooth transitions (default: 0.1s)

    Returns:
        Path to the processed audio file with muted sections

    Raises:
        ValueError: If mute windows are invalid or overlapping
    """
    try:
        # Validate audio path
        validated_path = validate_audio_path(audio_path)

        # Load audio
        y, sr = librosa.load(validated_path, sr=None, mono=False)

        # Get audio duration
        duration = len(y) / sr if y.ndim == 1 else len(y[0]) / sr

        # Validate and sort mute windows
        sorted_windows = sorted(mute_windows, key=lambda x: x[0])

        for i, (start, end) in enumerate(sorted_windows):
            if start >= end:
                raise ValueError(
                    f"Window {i}: start time ({start}s) must be less than end time ({end}s)"
                )
            if start < 0 or end > duration:
                raise ValueError(
                    f"Window {i}: time range ({start}s-{end}s) outside audio duration (0-{duration:.2f}s)"
                )

            # Check for overlaps
            if i > 0:
                prev_start, prev_end = sorted_windows[i - 1]
                if start < prev_end:
                    raise ValueError(f"Window {i} overlaps with previous window")

        # Create a copy of the audio for processing
        y_processed = y.copy()

        # Apply muting with fade in/out
        for start_time, end_time in sorted_windows:
            start_sample = int(start_time * sr)
            end_sample = int(end_time * sr)
            fade_samples = int(fade_duration * sr)

            if y_processed.ndim == 1:
                # Mono audio
                # Apply fade out before mute
                fade_start = max(0, start_sample - fade_samples)
                if fade_start < start_sample:
                    fade_out = np.linspace(1, 0, start_sample - fade_start)
                    y_processed[fade_start:start_sample] *= fade_out

                # Apply mute
                y_processed[start_sample:end_sample] = 0

                # Apply fade in after mute
                fade_end = min(len(y_processed), end_sample + fade_samples)
                if end_sample < fade_end:
                    fade_in = np.linspace(0, 1, fade_end - end_sample)
                    y_processed[end_sample:fade_end] *= fade_in
            else:
                # Multi-channel audio
                # Apply fade out before mute
                fade_start = max(0, start_sample - fade_samples)
                if fade_start < start_sample:
                    fade_out = np.linspace(1, 0, start_sample - fade_start)
                    y_processed[:, fade_start:start_sample] *= fade_out[np.newaxis, :]

                # Apply mute
                y_processed[:, start_sample:end_sample] = 0

                # Apply fade in after mute
                fade_end = min(y_processed.shape[1], end_sample + fade_samples)
                if end_sample < fade_end:
                    fade_in = np.linspace(0, 1, fade_end - end_sample)
                    y_processed[:, end_sample:fade_end] *= fade_in[np.newaxis, :]

        # Generate output filename
        if not output_path:
            output_path = "."
        os.makedirs(output_path, exist_ok=True)

        original_filename = Path(validated_path).stem
        windows_str = "_".join([f"{s:.1f}-{e:.1f}" for s, e in sorted_windows[:3]])
        if len(sorted_windows) > 3:
            windows_str += f"_and_{len(sorted_windows) - 3}_more"

        output_filename = (
            f"{original_filename}_muted_{windows_str}.{output_format.lower()}"
        )
        output_file_path = os.path.join(output_path, output_filename)

        # Save the processed audio
        if y_processed.ndim == 2:
            y_processed = y_processed.T  # Transpose for soundfile

        if output_format.lower() == "mp3":
            # For MP3, use ffmpeg through subprocess
            import tempfile
            import subprocess

            with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
                sf.write(temp_wav.name, y_processed, sr)

                cmd = [
                    "ffmpeg",
                    "-y",
                    "-i",
                    temp_wav.name,
                    "-c:a",
                    "libmp3lame",
                    "-b:a",
                    "192k",
                    output_file_path,
                ]
                subprocess.run(cmd, capture_output=True, check=True)
                os.unlink(temp_wav.name)
        else:
            sf.write(output_file_path, y_processed, sr)

        return output_file_path

    except Exception as e:
        raise RuntimeError(f"Error muting audio windows: {str(e)}")


def extract_segments(
    audio_path: str,
    segments: List[Tuple[float, float]],
    output_path: Optional[str] = None,
    output_format: str = "wav",
    join_segments: bool = False,
) -> Union[str, List[str]]:
    """
    Extract multiple segments from an audio file.

    Args:
        audio_path: Path to input audio file or URL
        segments: List of (start_time, end_time) tuples in seconds
        output_path: Optional output directory (default: None, uses current directory)
        output_format: Output format ('wav' or 'mp3', default: 'wav')
        join_segments: If True, join all segments into one file; if False, save separately

    Returns:
        If join_segments=True: Path to joined audio file
        If join_segments=False: List of paths to individual segment files

    Raises:
        ValueError: If segments are invalid
    """
    try:
        # Validate audio path
        validated_path = validate_audio_path(audio_path)

        # Load audio
        y, sr = librosa.load(validated_path, sr=None, mono=False)

        # Get audio duration
        duration = len(y) / sr if y.ndim == 1 else len(y[0]) / sr

        # Validate segments
        for i, (start, end) in enumerate(segments):
            if start >= end:
                raise ValueError(
                    f"Segment {i}: start time ({start}s) must be less than end time ({end}s)"
                )
            if start < 0 or end > duration:
                raise ValueError(
                    f"Segment {i}: time range ({start}s-{end}s) outside audio duration"
                )

        if not output_path:
            output_path = "."
        os.makedirs(output_path, exist_ok=True)

        original_filename = Path(validated_path).stem

        if join_segments:
            # Join all segments into one file
            segments_audio = []

            for start_time, end_time in segments:
                start_sample = int(start_time * sr)
                end_sample = int(end_time * sr)

                if y.ndim == 1:
                    segment = y[start_sample:end_sample]
                else:
                    segment = y[:, start_sample:end_sample]

                segments_audio.append(segment)

            # Concatenate all segments
            if y.ndim == 1:
                y_joined = np.concatenate(segments_audio)
            else:
                y_joined = np.concatenate(segments_audio, axis=1)

            # Save joined audio
            output_filename = (
                f"{original_filename}_segments_joined.{output_format.lower()}"
            )
            output_file_path = os.path.join(output_path, output_filename)

            if y_joined.ndim == 2:
                y_joined = y_joined.T

            if output_format.lower() == "mp3":
                import tempfile
                import subprocess

                with tempfile.NamedTemporaryFile(
                    suffix=".wav", delete=False
                ) as temp_wav:
                    sf.write(temp_wav.name, y_joined, sr)

                    cmd = [
                        "ffmpeg",
                        "-y",
                        "-i",
                        temp_wav.name,
                        "-c:a",
                        "libmp3lame",
                        "-b:a",
                        "192k",
                        output_file_path,
                    ]
                    subprocess.run(cmd, capture_output=True, check=True)
                    os.unlink(temp_wav.name)
            else:
                sf.write(output_file_path, y_joined, sr)

            return output_file_path
        else:
            # Save segments separately
            segment_files = []

            for i, (start_time, end_time) in enumerate(segments):
                start_sample = int(start_time * sr)
                end_sample = int(end_time * sr)

                if y.ndim == 1:
                    segment = y[start_sample:end_sample]
                else:
                    segment = y[:, start_sample:end_sample]

                output_filename = f"{original_filename}_segment_{i + 1}_{start_time:.1f}s_to_{end_time:.1f}s.{output_format.lower()}"
                output_file_path = os.path.join(output_path, output_filename)

                if segment.ndim == 2:
                    segment = segment.T

                if output_format.lower() == "mp3":
                    import tempfile
                    import subprocess

                    with tempfile.NamedTemporaryFile(
                        suffix=".wav", delete=False
                    ) as temp_wav:
                        sf.write(temp_wav.name, segment, sr)

                        cmd = [
                            "ffmpeg",
                            "-y",
                            "-i",
                            temp_wav.name,
                            "-c:a",
                            "libmp3lame",
                            "-b:a",
                            "192k",
                            output_file_path,
                        ]
                        subprocess.run(cmd, capture_output=True, check=True)
                        os.unlink(temp_wav.name)
                else:
                    sf.write(output_file_path, segment, sr)

                segment_files.append(output_file_path)

            return segment_files

    except Exception as e:
        raise RuntimeError(f"Error extracting segments: {str(e)}")


def trim_audio(
    audio_path: str,
    trim_start: Optional[float] = None,
    trim_end: Optional[float] = None,
    output_path: Optional[str] = None,
    output_format: str = "wav",
) -> str:
    """
    Trim audio from the beginning and/or end.

    Args:
        audio_path: Path to input audio file or URL
        trim_start: Amount to trim from start in seconds (None = no trim from start)
        trim_end: Amount to trim from end in seconds (None = no trim from end)
        output_path: Optional output directory (default: None, uses current directory)
        output_format: Output format ('wav' or 'mp3', default: 'wav')

    Returns:
        Path to the trimmed audio file

    Raises:
        ValueError: If trim amounts are invalid or exceed audio duration
    """
    try:
        # Validate audio path
        validated_path = validate_audio_path(audio_path)

        # Load audio
        y, sr = librosa.load(validated_path, sr=None, mono=False)

        # Get audio duration
        duration = len(y) / sr if y.ndim == 1 else len(y[0]) / sr

        # Validate trim amounts
        if trim_start is not None and trim_start < 0:
            raise ValueError("Trim start amount cannot be negative")

        if trim_end is not None and trim_end < 0:
            raise ValueError("Trim end amount cannot be negative")

        if trim_start is None:
            trim_start = 0.0
        if trim_end is None:
            trim_end = 0.0

        total_trim = trim_start + trim_end
        if total_trim >= duration:
            raise ValueError(
                f"Total trim ({total_trim}s) exceeds or equals audio duration ({duration:.2f}s)"
            )

        # Calculate trim boundaries
        start_sample = int(trim_start * sr)
        if trim_end > 0:
            end_sample = int((duration - trim_end) * sr)
        else:
            end_sample = len(y) if y.ndim == 1 else y.shape[1]

        # Trim the audio
        if y.ndim == 1:
            y_trimmed = y[start_sample:end_sample]
        else:
            y_trimmed = y[:, start_sample:end_sample]

        # Generate output filename
        if not output_path:
            output_path = "."
        os.makedirs(output_path, exist_ok=True)

        original_filename = Path(validated_path).stem
        trim_parts = []
        if trim_start > 0:
            trim_parts.append(f"start_{trim_start:.1f}s")
        if trim_end > 0:
            trim_parts.append(f"end_{trim_end:.1f}s")

        trim_str = "_".join(trim_parts) if trim_parts else "trimmed"
        output_filename = f"{original_filename}_{trim_str}.{output_format.lower()}"
        output_file_path = os.path.join(output_path, output_filename)

        # Save the trimmed audio
        if y_trimmed.ndim == 2:
            y_trimmed = y_trimmed.T  # Transpose for soundfile

        if output_format.lower() == "mp3":
            # For MP3, use ffmpeg through subprocess
            import tempfile
            import subprocess

            with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
                sf.write(temp_wav.name, y_trimmed, sr)

                cmd = [
                    "ffmpeg",
                    "-y",
                    "-i",
                    temp_wav.name,
                    "-c:a",
                    "libmp3lame",
                    "-b:a",
                    "192k",
                    output_file_path,
                ]
                subprocess.run(cmd, capture_output=True, check=True)
                os.unlink(temp_wav.name)
        else:
            sf.write(output_file_path, y_trimmed, sr)

        return output_file_path

    except Exception as e:
        raise RuntimeError(f"Error trimming audio: {str(e)}")


if __name__ == "__main__":
    import argparse
    import json

    parser = argparse.ArgumentParser(description="Audio cutting and editing tools")
    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    # Cut audio
    cut_parser = subparsers.add_parser("cut", help="Cut audio segment")
    cut_parser.add_argument("audio", help="Path to audio file")
    cut_parser.add_argument("start", type=float, help="Start time in seconds")
    cut_parser.add_argument("end", type=float, help="End time in seconds")
    cut_parser.add_argument(
        "--format", default="wav", choices=["wav", "mp3"], help="Output format"
    )

    # Mute windows
    mute_parser = subparsers.add_parser("mute", help="Mute time windows")
    mute_parser.add_argument("audio", help="Path to audio file")
    mute_parser.add_argument("windows", help="JSON array of [start, end] pairs")
    mute_parser.add_argument(
        "--format", default="wav", choices=["wav", "mp3"], help="Output format"
    )

    # Extract segments
    extract_parser = subparsers.add_parser("extract", help="Extract segments")
    extract_parser.add_argument("audio", help="Path to audio file")
    extract_parser.add_argument("segments", help="JSON array of [start, end] pairs")
    extract_parser.add_argument(
        "--join", action="store_true", help="Join segments into one file"
    )
    extract_parser.add_argument(
        "--format", default="wav", choices=["wav", "mp3"], help="Output format"
    )

    # Trim audio
    trim_parser = subparsers.add_parser("trim", help="Trim audio from start/end")
    trim_parser.add_argument("audio", help="Path to audio file")
    trim_parser.add_argument(
        "--start", type=float, help="Trim amount from start in seconds"
    )
    trim_parser.add_argument(
        "--end", type=float, help="Trim amount from end in seconds"
    )
    trim_parser.add_argument(
        "--format", default="wav", choices=["wav", "mp3"], help="Output format"
    )

    args = parser.parse_args()

    try:
        if args.command == "cut":
            output = cut_audio(
                args.audio, args.start, args.end, output_format=args.format
            )
            print(f"Cut audio saved to: {output}")

        elif args.command == "mute":
            windows = json.loads(args.windows)
            output = mute_time_windows(args.audio, windows, output_format=args.format)
            print(f"Muted audio saved to: {output}")

        elif args.command == "extract":
            segments = json.loads(args.segments)
            result = extract_segments(
                args.audio, segments, join_segments=args.join, output_format=args.format
            )
            if args.join:
                print(f"Joined segments saved to: {result}")
            else:
                print("Extracted segments:")
                for i, segment_file in enumerate(result):
                    print(f"  {i + 1}. {segment_file}")

        elif args.command == "trim":
            output = trim_audio(
                args.audio, args.start, args.end, output_format=args.format
            )
            print(f"Trimmed audio saved to: {output}")

        else:
            parser.print_help()

    except Exception as e:
        print(f"Error: {e}")
        exit(1)