Sfx Common
src.xil_pipeline.sfx_common
Shared SFX library utilities.
Provides common functions for managing a shared SFX asset library and
generating episode-specific stems. Both XILU002_generate_SFX.py and
XILP002_producer.py delegate to this module to avoid code
duplication and to ensure that each unique sound effect is generated only
once into the shared SFX/ directory.
Module Attributes
SFX_DIR: Default path for the shared SFX asset library.
run_banner
Context manager that prints a start header and end trailer.
Usage:
Args:
script_name: Override the script name shown in the banner.
Defaults to ``os.path.basename(sys.argv[0])``.
Source code in src/xil_pipeline/sfx_common.py
slugify_effect_key
Convert direction text to a filesystem-safe slug.
Rules
- Lowercase the entire string.
- Replace
': '(colon-space) with'_'(category separator). - Replace remaining non-alphanumeric characters with
'-'. - Collapse multiple consecutive hyphens.
- Strip leading/trailing hyphens.
- Truncate to
_MAX_SLUG_LENchars; append an 8-char SHA-256 suffix when truncated to avoid collisions between long similar keys.
Examples:
>>> slugify_effect_key("BEAT")
'beat'
>>> slugify_effect_key("SFX: DOOR OPENS, BELL CHIMES")
'sfx_door-opens-bell-chimes'
Source code in src/xil_pipeline/sfx_common.py
file_nonempty
Return True if path exists and has a non-zero size.
Uses a single os.stat() call to avoid a TOCTOU race between
an existence check and a separate size check.
Parameters:
-
path(str) –Filesystem path to test.
Returns:
-
bool–Trueif the file exists andst_size > 0,Falseotherwise.
Source code in src/xil_pipeline/sfx_common.py
shared_sfx_path
Return the shared library file path for an effect key.
Parameters:
-
sfx_dir(str) –Base directory for shared SFX assets.
-
effect_key(str) –Direction text key (e.g.
'BEAT').
Returns:
-
str–Full path like
SFX/beat.mp3.
Source code in src/xil_pipeline/sfx_common.py
tag_mp3
tag_mp3(path: str, show: str = 'Sample Show', title: str | None = None, artist: str | None = None, lyrics: str | None = None, comments: str | None = None) -> None
Write ID3 metadata tags to an MP3 file.
Sets Album, Genre, and Year. Optionally sets Title, Artist, and Lyrics tags.
Parameters:
-
path(str) –Path to the MP3 file.
-
show(str, default:'Sample Show') –Album name (default
"Sample Show"). -
title(str | None, default:None) –Optional TIT2 title tag (e.g. the effect key or dialogue song label).
-
artist(str | None, default:None) –Optional TPE1 artist tag (e.g. the speaker's full name).
-
lyrics(str | None, default:None) –Optional USLT unsynchronised lyrics tag (full dialogue text).
Source code in src/xil_pipeline/sfx_common.py
tag_wav
tag_wav(path: str, show: str = 'Sample Show', title: str | None = None, artist: str | None = None) -> None
Write ID3 metadata tags to a WAV file.
Sets Album, Genre, and Year. Optionally sets Title and Artist.
Parameters:
-
path(str) –Path to the WAV file.
-
show(str, default:'Sample Show') –Album name (default
"Sample Show"). -
title(str | None, default:None) –Optional TIT2 title tag (e.g. the layer name).
-
artist(str | None, default:None) –Optional TPE1 artist tag.
Source code in src/xil_pipeline/sfx_common.py
ensure_shared_sfx
ensure_shared_sfx(effect_key: str, effect: SfxEntry, sfx_dir: str, defaults: dict, client=None, show: str = 'Sample Show') -> str
Ensure the shared SFX asset exists, generating if needed.
For type='silence' effects, generates silent audio locally via
pydub. For type='sfx' effects, calls the ElevenLabs Sound
Effects API via the provided client. In both cases, ID3 metadata
tags (Album, Genre, Year, Title) are written to the resulting MP3.
Parameters:
-
effect_key(str) –Direction text key.
-
effect(SfxEntry) –The
SfxEntrymodel instance. -
sfx_dir(str) –Shared SFX library directory.
-
defaults(dict) –Config-level defaults (e.g.
prompt_influence). -
client–ElevenLabs client instance. Required for
type='sfx'effects; may beNonefor silence-only generation. -
show(str, default:'Sample Show') –Show name for the Album ID3 tag.
Returns:
-
str–The path to the shared asset file.
Raises:
-
ValueError–If client is
Noneand the effect requires API generation.
Source code in src/xil_pipeline/sfx_common.py
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 | |
place_episode_stem
Copy a shared SFX asset to an episode stem location.
Parameters:
-
shared_path(str) –Path to the shared asset in
SFX/. -
stem_path(str) –Destination path in
stems/<TAG>/.
Returns:
Source code in src/xil_pipeline/sfx_common.py
load_sfx_entries
load_sfx_entries(script_json_path: str, sfx_json_path: str, max_duration: float | None = None, direction_types: set[str] | None = None, local_only: bool = False) -> list[dict]
Load direction entries matched against an SFX configuration.
Reads the parsed script and SFX config, returning only direction
entries whose text field has a matching key in the SFX effects
mapping.
Parameters:
-
script_json_path(str) –Path to the parsed script JSON.
-
sfx_json_path(str) –Path to the SFX configuration JSON.
-
max_duration(float | None, default:None) –If set, exclude effects with
duration_secondsexceeding this value. -
direction_types(set[str] | None, default:None) –If set, only include entries whose
direction_typeis in this set (e.g.{"SFX", "BEAT"}).Noneincludes all categories. -
local_only(bool, default:False) –If
True, skip effects that would require an API call — i.e.type == "sfx", nosourcefile, and not already present in the sharedSFX/directory. Silence entries and source-backed entries are always included.
Returns:
-
list[dict]–A list of SFX entry dicts with
seq,text,direction_type, -
list[dict]–stem_name,sfx_type,section, andscene.
Source code in src/xil_pipeline/sfx_common.py
generate_sfx
generate_sfx(sfx_entries: list[dict], sfx_config: dict, stems_dir: str, sfx_dir: str = SFX_DIR, client=None, start_from: int = 1) -> None
Generate SFX stems via a two-phase shared-library workflow.
Phase 1 — For each unique effect key, ensure the shared asset exists in sfx_dir (generate via API or silence if missing).
Phase 2 — For each script entry, copy the shared asset to the episode stems directory with the sequence-numbered filename.
Parameters:
-
sfx_entries(list[dict]) –SFX entry dicts from :func:
load_sfx_entries. -
sfx_config(dict) –Raw SFX config dict.
-
stems_dir(str) –Episode stems output directory.
-
sfx_dir(str, default:SFX_DIR) –Shared SFX library directory.
-
client–ElevenLabs client (needed for API effects).
-
start_from(int, default:1) –Only process entries with
seq >= start_from.
Source code in src/xil_pipeline/sfx_common.py
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 620 621 622 623 624 | |
dry_run_sfx
dry_run_sfx(sfx_entries: list[dict], sfx_config: dict, stems_dir: str, sfx_dir: str = SFX_DIR) -> None
Preview SFX generation showing status and credit estimates.
Each entry is classified as one of:
- EXISTS — episode stem already in stems/<TAG>/
- CACHED — shared asset in SFX/, will be copied (no API)
- NEW — needs API generation to SFX/, then copy
Parameters:
-
sfx_entries(list[dict]) –SFX entry dicts from :func:
load_sfx_entries. -
sfx_config(dict) –Raw SFX config dict.
-
stems_dir(str) –Episode stems directory.
-
sfx_dir(str, default:SFX_DIR) –Shared SFX library directory.
Source code in src/xil_pipeline/sfx_common.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 | |