Audio Modules¶
convert_audio_to_wav ¶
convert_audio_to_wav(
input_path,
*,
output_path=None,
output_dir=None,
sample_rate=16000,
bit_depth=16,
channels=1,
overwrite_existing=False
)
Convert any FFmpeg-readable audio/video file to a linear PCM WAV.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_path
|
str | Path
|
Source media file (audio or video container). FFmpeg must be able to read it. |
required |
output_path
|
str | Path | None
|
Target WAV path. If None, defaults to
|
None
|
sample_rate
|
int
|
Desired sample rate (Hz). |
16000
|
bit_depth
|
(16, 24, 32)
|
Output PCM bit depth; maps to |
16,24,32
|
channels
|
int | None
|
If provided, set number of output channels (e.g., 1=mono, 2=stereo). If None, keep original channel count. |
1
|
overwrite_existing
|
bool
|
Overwrite |
False
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written WAV file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If |
RuntimeError
|
If FFmpeg/FFprobe are missing or the conversion fails. |
Notes
- Video inputs are supported: the audio stream is extracted and converted.
- For multi-channel sources and
channels is None
, channel layout is preserved. - We run FFmpeg with
-nostdin
to avoid TTY issues in pipelines.
Source code in src\taters\audio\convert_to_wav.py
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 |
|
Thin CLI shim for the vendored Whisper diarization wrapper.
This module exists so you can run:
python -m taters.audio.diarize_with_thirdparty --audio_path ...
It simply delegates to the real implementation in
taters/audio/diarizer/whisper_diar_wrapper.py
. :contentReference[oaicite:0]{index=0}
Extract all audio streams from a video/container into standalone WAV files.
This utility probes the container with ffprobe
, lists audio streams (with
index and tags), and then maps each stream with ffmpeg
to a separate PCM WAV.
It is useful for multi-track recordings (e.g., Zoom, OBS, ProRes with stems).
:contentReference[oaicite:1]{index=1}
split_audio_streams_to_wav ¶
split_audio_streams_to_wav(
input_path,
output_dir=None,
sample_rate=48000,
bit_depth=16,
overwrite=True,
)
Extract each audio stream in a container to its own WAV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_path
|
str | PathLike
|
Video or audio container readable by FFmpeg. |
required |
output_dir
|
str | PathLike | None
|
Destination directory. If None, defaults to |
None
|
sample_rate
|
int
|
Target sample rate for the output WAVs (Hz). |
48000
|
bit_depth
|
(16, 24, 32)
|
Output PCM bit depth (little-endian). |
16,24,32
|
overwrite
|
bool
|
If True, overwrite existing files. If False and a target exists,
raises :class: |
True
|
Returns:
Type | Description |
---|---|
list[str]
|
Absolute paths to the created WAVs. |
Behavior
- Output file names are constructed from the input base name and stream
metadata:
<stem>_a<index>[_<lang>][_<title>].wav
with safe slugs. - Uses
-map 0:a:<N>
to select the N-th audio stream in the container. - Runs FFmpeg with
-nostdin
and quiet loglevel to avoid TTY lockups.
Examples:
>>> split_audio_streams_to_wav("session.mp4")
['.../audio/session_a0_eng.wav', '.../audio/session_a1_eng.wav']
Source code in src\taters\audio\extract_wav_from_video.py
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 |
|
High-level, environment-safe wrapper for exporting Whisper encoder embeddings.
This module provides a single entry point, :func:extract_whisper_embeddings
,
which (by default) launches a subprocess to extract embeddings using a dedicated
worker module. The subprocess approach avoids CUDA/Torch collisions with other
parts of your pipeline.
Two modes are supported:
1) Transcript-driven mode
Pass transcript_csv
to compute one embedding vector per transcript row
(e.g., per diarized segment). The output is a CSV with columns
start_time,end_time,speaker,e0..e{D-1}
.
2) General-audio mode
Omit transcript_csv
to analyze the raw WAV. You can segment by fixed
windows or by non-silent regions; optionally aggregate to a single mean row.
extract_whisper_embeddings ¶
extract_whisper_embeddings(
*,
source_wav,
transcript_csv=None,
time_unit="auto",
strategy="windows",
window_s=30.0,
hop_s=15.0,
min_seg_s=1.0,
top_db=30.0,
aggregate="none",
output_dir=None,
overwrite_existing=False,
model_name="base",
device="auto",
compute_type="float16",
run_in_subprocess=True,
extra_env=None,
verbose=True,
extractor_module="taters.audio.extract_whisper_embeddings_subproc"
)
Export Whisper encoder embeddings to a CSV file, using a subprocess by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_wav
|
str | Path
|
Path to the input WAV. Must be readable by |
required |
transcript_csv
|
str | Path | None
|
If provided, enables transcript-driven mode. The CSV is expected to contain timestamp columns and (optionally) a speaker column. A row is emitted per transcript segment. |
None
|
time_unit
|
('auto', 'ms', 's', 'samples')
|
How to interpret timestamps in |
"auto","ms","s","samples"
|
strategy
|
('windows', 'nonsilent')
|
General-audio mode only. "windows" uses fixed sized windows with overlap; "nonsilent" uses an energy-based splitter (librosa.effects.split). |
"windows","nonsilent"
|
window_s
|
float
|
General-audio mode only. Window length and hop (seconds). |
30.0, 15.0
|
hop_s
|
float
|
General-audio mode only. Window length and hop (seconds). |
30.0, 15.0
|
min_seg_s
|
float
|
General-audio mode only. Skip segments shorter than this many seconds. |
1.0
|
top_db
|
float
|
General-audio mode only ("nonsilent"). Threshold (dB) below reference to consider as silence. Smaller → more segments; larger → fewer. |
30.0
|
aggregate
|
('none', 'mean')
|
General-audio mode only. If "mean", a single pooled row is written covering the entire file; otherwise one row per segment. |
"none","mean"
|
output_dir
|
str | Path | None
|
Directory for the output CSV. If None, defaults to
|
None
|
model_name
|
str
|
Model identifier passed through to the worker (e.g., "tiny", "base", "small", "large-v3" or a local CTranslate2 model directory). |
"base"
|
device
|
('auto', 'cuda', 'cpu')
|
Runtime device. If "cpu", environment variables are set to disable CUDA in the child process. |
"auto","cuda","cpu"
|
compute_type
|
str
|
CTranslate2 compute type (e.g., "float16", "int8", "float32"); passed to the worker module. |
"float16"
|
run_in_subprocess
|
bool
|
If True (recommended), runs extraction in a separate Python process to isolate Torch/CUDA state from the parent process. |
True
|
extra_env
|
dict | None
|
Additional environment variables to inject into the child process. |
None
|
verbose
|
bool
|
If True, print the launched command and the child's stdout. |
True
|
extractor_module
|
str
|
Dotted module path whose |
"chopshop.audio.extract_whisper_embeddings_subproc"
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written embeddings CSV. Pattern:
|
Notes
- The subprocess writes and exits. The parent returns once the file exists.
- If
transcript_csv
is supplied, the worker runs in transcript mode; otherwise general-audio mode is used with the given segmentation strategy. - Failures in the child process are re-raised with the captured stdout/stderr to ease debugging.
Examples:
Transcript per-segment embeddings:
>>> extract_whisper_embeddings(
... source_wav="audio/session.wav",
... transcript_csv="transcripts/session.csv",
... time_unit="ms",
... model_name="small",
... device="cuda",
... )
Whole-file mean embedding:
>>> extract_whisper_embeddings(
... source_wav="audio/session.wav",
... strategy="nonsilent",
... aggregate="mean",
... output_dir="features/whisper-embeddings",
... )
Source code in src\taters\audio\extract_whisper_embeddings.py
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 |
|
Subprocess worker that computes Whisper encoder embeddings.
This module is meant to be executed with python -m ...
by the wrapper in
extract_whisper_embeddings.py
. It avoids importing heavyweight torch
packages in the parent process and keeps CUDA state isolated.
Two entry functions implement I/O and shape-handling:
- :func:
export_segment_embeddings_csv
— transcript-driven, one vector per row. - :func:
export_audio_embeddings_csv
— general WAVs; segmentation + optional pooling.
Both functions use faster-whisper
(CTranslate2 backend) and WhisperFeatureExtractor
to produce encoder features, then pool the encoder outputs into fixed-length vectors.
export_audio_embeddings_csv ¶
export_audio_embeddings_csv(
source_wav,
output_dir=None,
*,
config=EmbedConfig(),
sr=16000,
strategy="windows",
window_s=30.0,
hop_s=15.0,
min_seg_s=1.0,
top_db=30.0,
apply_l2_normalization=False,
aggregate="none"
)
Compute Whisper encoder embeddings for an arbitrary WAV (no transcript).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_wav
|
str | Path
|
Input audio (any format |
required |
output_dir
|
str | Path | None
|
Directory for the output CSV. Defaults to the WAV's parent if None. |
None
|
config
|
(EmbedConfig, keyword - only)
|
Model/device/compute configuration. |
EmbedConfig()
|
sr
|
int
|
Resample rate used by the feature extractor. |
16000
|
strategy
|
('windows', 'nonsilent')
|
|
"windows","nonsilent"
|
window_s
|
float
|
Window length and hop size (seconds). Used by both strategies. |
30.0
|
hop_s
|
float
|
Window length and hop size (seconds). Used by both strategies. |
30.0
|
min_seg_s
|
float
|
Discard segments shorter than this length (seconds). |
1.0
|
top_db
|
float
|
Silence threshold for "nonsilent". Higher → fewer segments. |
30.0
|
aggregate
|
('none', 'mean')
|
If "mean", write a single pooled vector over the whole file. |
"none","mean"
|
Returns:
Type | Description |
---|---|
Path
|
CSV path: |
Notes
- When
aggregate="none"
, rows arestart_time,end_time,SEGMENT_i,e0..
. - When
aggregate="mean"
, a single row0.000,<dur>,GLOBAL_MEAN,e0..
is written.
Source code in src\taters\audio\extract_whisper_embeddings_subproc.py
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 |
|
export_segment_embeddings_csv ¶
export_segment_embeddings_csv(
transcript_csv,
source_wav,
output_dir=None,
*,
config=EmbedConfig(),
start_col="start_time",
end_col="end_time",
speaker_col="speaker",
apply_l2_normalization=False,
sr=16000
)
Compute Whisper encoder embeddings for each transcript segment and write a CSV.
Expected transcript columns (auto-resolved with fallbacks): - start_time (or: start, from, t0, start_ms, start_sec) - end_time (or: end, to, t1, end_ms, end_sec) - speaker (optional; fallbacks include speaker_label, spk, speaker_id, ...)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transcript_csv
|
str | Path
|
CSV with segment timings (and optionally speaker labels). |
required |
source_wav
|
str | Path
|
Audio file to slice. Will be resampled to |
required |
output_dir
|
str | Path | None
|
Directory for the output CSV. If None, defaults to the WAV's parent. |
None
|
config
|
(EmbedConfig, keyword - only)
|
Configuration for model name, device, compute type, and time unit. |
EmbedConfig()
|
start_col
|
str
|
Column name hints. The function will fall back to common aliases if the exact names are not present. |
'start_time'
|
end_col
|
str
|
Column name hints. The function will fall back to common aliases if the exact names are not present. |
'start_time'
|
speaker_col
|
str
|
Column name hints. The function will fall back to common aliases if the exact names are not present. |
'start_time'
|
sr
|
int
|
Sample rate for feature extraction (audio is resampled as needed). |
16000
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written CSV: |
Behavior
- Attempts to infer time units ("s", "ms", "samples") when config.time_unit == "auto".
- Skips invalid or tiny segments (< 2 samples after rounding).
- Pools encoder outputs to a fixed-length vector (mean over time).
- Writes header even if no valid segments remain (empty payload).
See Also
export_audio_embeddings_csv : transcript-free embeddings.
Source code in src\taters\audio\extract_whisper_embeddings_subproc.py
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 |
|
make_speaker_wavs_from_csv ¶
make_speaker_wavs_from_csv(
source_wav,
transcript_csv_path,
output_dir=None,
*,
overwrite_existing=False,
start_col="start_time",
end_col="end_time",
speaker_col="speaker",
time_unit="ms",
silence_ms=1000,
pre_silence_ms=None,
post_silence_ms=None,
sr=16000,
mono=True,
min_dur_ms=50,
merge_consecutive=True
)
Concatenate speaker-specific segments into per-speaker WAV files.
If merge_consecutive=True
(default), adjacent transcript rows with the same
speaker are merged into a single, longer segment spanning from the first
start to the last end — including any silence between those turns. If you
need the strict per-row behavior, set merge_consecutive=False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_wav
|
str | Path
|
Path to the source WAV. |
required |
transcript_csv_path
|
str | Path
|
CSV with timing and speaker columns (e.g., diarization output). |
required |
output_dir
|
str | Path | None
|
Where to write the per-speaker files. If None, defaults to
|
None
|
start_col
|
str
|
Column names in the transcript CSV. |
'start_time'
|
end_col
|
str
|
Column names in the transcript CSV. |
'start_time'
|
speaker_col
|
str
|
Column names in the transcript CSV. |
'start_time'
|
time_unit
|
('ms', 's')
|
Units for start/end columns. |
"ms","s"
|
silence_ms
|
int
|
If |
1000
|
pre_silence_ms
|
int | None
|
Explicit padding (ms) before/after each segment; overrides |
None
|
post_silence_ms
|
int | None
|
Explicit padding (ms) before/after each segment; overrides |
None
|
sr
|
int | None
|
Resample output to this rate. If None, keep original rate. |
16000
|
mono
|
bool
|
Downmix to mono if True. |
True
|
min_dur_ms
|
int
|
Skip segments shorter than this duration (ms). |
50
|
merge_consecutive
|
bool
|
Merge back-to-back turns for the same speaker into one segment span (including any inter-turn silence). If False, emit one clip per row. |
True
|
Returns:
Type | Description |
---|---|
dict[str, Path]
|
Mapping from friendly speaker label → output WAV path. |
Behavior
- Input speaker labels are sanitized for filenames but a more readable label (without path-hostile characters) is preserved for naming.
- Segments are sorted by start time per speaker before concatenation.
- If a speaker ends up with zero valid segments, no file is written.
Examples:
>>> make_speaker_wavs_from_csv(
... source_wav="audio/session.wav",
... transcript_csv_path="transcripts/session.csv",
... time_unit="ms",
... silence_ms=0, # no padding
... sr=16000,
... mono=True,
... )
Source code in src\taters\audio\split_wav_by_speaker.py
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 |
|