-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandcuts.py
More file actions
225 lines (171 loc) · 7 KB
/
Copy pathrandcuts.py
File metadata and controls
225 lines (171 loc) · 7 KB
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
"""Random audio clip concatenation tool."""
from __future__ import annotations
import argparse
import random
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, List, Sequence, Tuple
from pydub import AudioSegment
@dataclass
class ClipMetadata:
"""Metadata describing a generated audio clip."""
index: int
duration_ms: int
start_ms: int
fade_ms: int
@dataclass
class RandomCutsResult:
"""Result of generating a random set of audio clips."""
source: AudioSegment
concatenated: AudioSegment
metadata: List[ClipMetadata]
def export(self, output_path: Path | str) -> Path:
"""Export the concatenated audio to ``output_path``.
Args:
output_path: Destination file path. The extension determines the
export format.
Returns:
The path the audio was exported to.
Raises:
ValueError: If ``output_path`` lacks a file extension.
"""
path = Path(output_path)
if not path.suffix:
raise ValueError("Output path must include a file extension to infer format.")
export_format = path.suffix.lstrip(".").lower()
path.parent.mkdir(parents=True, exist_ok=True)
self.concatenated.export(path, format=export_format)
return path
class AudioProcessingError(RuntimeError):
"""Raised when audio loading or processing fails."""
def _convert_durations_to_ms(durations: Sequence[float]) -> List[int]:
"""Convert durations in seconds to integer milliseconds with validation."""
durations_ms: List[int] = []
for idx, dur in enumerate(durations):
if dur <= 0:
raise ValueError(f"Duration at position {idx} must be greater than 0 (got {dur!r}).")
dur_ms = int(round(dur * 1000))
if dur_ms <= 0:
raise ValueError(
f"Duration at position {idx} is too small after rounding; must be at least 1 ms (got {dur!r})."
)
durations_ms.append(dur_ms)
return durations_ms
def build_random_clips_concatenation(
source: AudioSegment,
durations: Sequence[float],
rng: random.Random,
) -> Tuple[AudioSegment, List[ClipMetadata]]:
"""Construct a concatenated audio segment composed of random clips.
Args:
source: The source audio segment to slice clips from.
durations: Durations of each desired clip, in seconds.
rng: Random number generator used to pick start positions.
Returns:
A tuple of the concatenated audio segment and metadata describing each clip.
Raises:
ValueError: If any duration is invalid relative to the source.
"""
src_ms = len(source)
durations_ms = _convert_durations_to_ms(durations)
clips: List[AudioSegment] = []
metadata: List[ClipMetadata] = []
for idx, dur_ms in enumerate(durations_ms):
if dur_ms > src_ms:
raise ValueError(
f"Duration at position {idx} ({dur_ms} ms) exceeds source length ({src_ms} ms)."
)
max_start = src_ms - dur_ms
start_ms = rng.randint(0, max_start) if max_start > 0 else 0
clip = source[start_ms : start_ms + dur_ms]
fade_ms = min(300, dur_ms // 2)
if fade_ms > 0:
clip = clip.fade_in(fade_ms).fade_out(fade_ms)
clips.append(clip)
metadata.append(ClipMetadata(index=idx, duration_ms=dur_ms, start_ms=start_ms, fade_ms=fade_ms))
if not clips:
concatenated = AudioSegment.silent(duration=0, frame_rate=source.frame_rate)
concatenated = concatenated.set_channels(source.channels)
else:
concatenated = clips[0]
for clip in clips[1:]:
concatenated += clip
return concatenated, metadata
def parse_args(argv: Iterable[str] | None = None) -> argparse.Namespace:
"""Parse command-line arguments for the CLI tool."""
parser = argparse.ArgumentParser(description="Create a random concatenation of audio clips.")
parser.add_argument("src_path", type=Path, help="Path to the source audio file.")
parser.add_argument(
"durations",
metavar="duration",
type=str,
nargs="+",
help="Durations in seconds for each clip.",
)
parser.add_argument("--seed", type=int, default=None, help="Seed for random start positions.")
parser.add_argument(
"--out",
type=Path,
default=Path("out.wav"),
help="Output audio path (format inferred from extension).",
)
return parser.parse_args(argv)
def load_audio(path: Path) -> AudioSegment:
"""Load an audio file, raising an informative error on failure."""
if not path.exists():
raise AudioProcessingError(f"Source file does not exist: {path}")
try:
return AudioSegment.from_file(path)
except Exception as exc: # pragma: no cover - pydub raises many exception types
raise AudioProcessingError(f"Failed to decode audio file '{path}': {exc}") from exc
def generate_random_cuts(
src_path: Path | str,
durations: Sequence[float | int | str],
*,
seed: int | None = None,
output_path: Path | str | None = None,
) -> RandomCutsResult:
"""Generate a random concatenation of clips from ``src_path``.
Args:
src_path: Path to the source audio file.
durations: Durations of each clip in seconds. Values are converted to
floats, so integers and strings representing numbers are accepted.
seed: Optional seed used to initialise the random number generator.
output_path: Optional destination path to export the concatenated
audio. When ``None`` the audio is not written to disk.
Returns:
``RandomCutsResult`` containing the original audio segment, the
concatenated output, and metadata for each generated clip.
"""
try:
duration_values = [float(value) for value in durations]
except (TypeError, ValueError) as exc:
raise ValueError(f"All durations must be numeric values: {exc}") from exc
rng = random.Random(seed)
source = load_audio(Path(src_path))
concatenated, metadata = build_random_clips_concatenation(source, duration_values, rng)
result = RandomCutsResult(source=source, concatenated=concatenated, metadata=metadata)
if output_path is not None:
result.export(output_path)
return result
def main(argv: Iterable[str] | None = None) -> None:
"""Entry point for the randcuts CLI."""
args = parse_args(argv)
result = generate_random_cuts(
args.src_path,
args.durations,
seed=args.seed,
output_path=args.out,
)
source = result.source
src_ms = len(source)
print(
f"Loaded {args.src_path} ({src_ms} ms, {source.channels} ch, {source.frame_rate} Hz)"
)
for clip_meta in result.metadata:
print(
f"Clip {clip_meta.index}: dur={clip_meta.duration_ms} ms | start={clip_meta.start_ms} ms | fade={clip_meta.fade_ms} ms"
)
print(f"Exported concatenated audio to {args.out}")
if __name__ == "__main__":
main()