Mix Common
src.xil_pipeline.mix_common
Shared multi-track mixing utilities for the audio pipeline.
Provides timeline construction and per-layer audio building used by XILP003 (automated two-pass mix) and XILP005 (DAW layer export). Both stages classify stems by direction_type from the parsed script JSON, then build foreground (dialogue/SFX) and background (ambience/ music) layers independently before combining.
Module Attributes
BACKGROUND_DIRECTION_TYPES: direction_type values routed to the background layer rather than the foreground timeline. AMBIENCE_LEVEL_DB: Default dB reduction applied to ambience overlays in the automated mix (Option A). 0 for DAW export (Option C). MUSIC_LEVEL_DB: Default dB reduction applied to music overlays in the automated mix. 0 for DAW export.
BACKGROUND_DIRECTION_TYPES
module-attribute
BACKGROUND_DIRECTION_TYPES: frozenset[str] = frozenset({'AMBIENCE', 'MUSIC', 'VINTAGE FILTER', 'FILM AUDIO', 'SPEAKERPHONE', 'PHONE FILTER'})
SPAN_DIRECTION_TREATMENTS
module-attribute
SPAN_DIRECTION_TREATMENTS: dict[str, str] = {'VINTAGE FILTER': 'vintage', 'FILM AUDIO': 'film', 'SPEAKERPHONE': 'speakerphone', 'PHONE FILTER': 'phone'}
FILTER_REGISTRY
module-attribute
FILTER_REGISTRY: dict[str, str] = {'phone': 'apply_phone_filter', 'vintage': 'apply_vintage_filter', 'film': 'apply_film_filter', 'speakerphone': 'apply_speakerphone_filter'}
StemPlan
dataclass
Resolved metadata for a single audio stem file.
Attributes:
-
seq(int) –Sequence number extracted from the stem filename.
-
filepath(str) –Absolute or relative path to the MP3 stem file.
-
direction_type(str | None) –Parsed direction category for this entry (
"SFX","MUSIC","AMBIENCE","BEAT"), orNonefor dialogue stems. -
entry_type(str | None) –Parsed entry classification (
"dialogue","direction", etc.), orNoneif not in index. -
foreground_override(bool) –When
True, forces the stem into the foreground timeline even ifdirection_typewould normally route it to the background (e.g. preamble intro music that must play sequentially, not as an overlay).
Source code in src/xil_pipeline/mix_common.py
__init__
__init__(seq: int, filepath: str, direction_type: str | None, entry_type: str | None, text: str | None = None, scene: str | None = None, foreground_override: bool = False, volume_percentage: float | None = None, ramp_in_seconds: float | None = None, ramp_out_seconds: float | None = None, play_duration: float | None = None, tts_model: str | None = None, pre_trimmed: bool = False, loop: bool = True) -> None
extract_seq
Extract the sequence number from a stem filename.
Stems are named {seq:03d}_{section}[-{scene}]_{speaker}.mp3.
Legacy preamble stems used an n prefix (n{abs(seq):03d}_...mp3)
and are still parsed for backward compatibility.
Parameters:
-
filepath(str) –Path like
stems/S01E01/001_preamble_tina.mp3orstems/S01E01/003_cold-open_adam.mp3.
Returns:
-
int–Integer sequence number (e.g.
"003"→3).
Source code in src/xil_pipeline/mix_common.py
load_entries_index
Load a parsed script JSON and return a {seq: entry} index.
Parameters:
-
parsed_path(str) –Path to the parsed script JSON produced by XILP001.
Returns:
Source code in src/xil_pipeline/mix_common.py
collect_stem_plans
collect_stem_plans(stems_dir: str, entries_index: dict[int, dict], sfx_config: SfxConfiguration | None = None) -> list[StemPlan]
Collect and classify all MP3 stems in a stems directory.
Uses the entries index to look up each stem's direction_type
and entry_type by sequence number. Stems whose seq is not in
the index are logged as stale and skipped.
Parameters:
-
stems_dir(str) –Directory containing episode stem MP3 files.
-
entries_index(dict[int, dict]) –{seq: entry}mapping from :func:load_entries_index. -
sfx_config(SfxConfiguration | None, default:None) –Optional :class:
~models.SfxConfiguration; when provided, resolves per-effect or category-default volume/ramp values into MUSIC and AMBIENCE plans.
Returns:
Source code in src/xil_pipeline/mix_common.py
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 | |
apply_phone_filter
Apply the mobile-call treatment to an audio segment.
Steep 300 Hz / 3.4 kHz skirts, an earpiece presence lift, hard AGC and a real GSM 06.10 round-trip, landing ~1 dB under an untreated stem.
This was a pure-pydub one-liner until the measurements showed what it was actually doing: pydub's filters are single-pole, so the old chain left 80 Hz only 11 dB down and 8 kHz only 9 dB down — a slightly muffled voice rather than a phone. Rendered through ffmpeg it is 37 dB and 46 dB respectively.
Requires ffmpeg, like the film and speakerphone treatments; without it the stem passes through untreated with a warning, and losing only the GSM encoder costs the codec grit rather than the whole effect.
Parameters:
-
segment(AudioSegment) –Input audio segment to filter.
Returns:
-
AudioSegment–Filtered audio segment.
Source code in src/xil_pipeline/mix_common.py
apply_vintage_filter
Apply a vintage (1960s-era) audio filter to an audio segment.
Rolls off high frequencies above 5 kHz (1960s tape/broadcast ceiling) and low rumble below 150 Hz, then reduces volume by 3 dB to simulate the compressed, mid-forward quality of aged tape recording.
Parameters:
-
segment(AudioSegment) –Input audio segment to filter.
Returns:
-
AudioSegment–Filtered audio segment.
Source code in src/xil_pipeline/mix_common.py
apply_film_filter
Apply the Film Audio treatment to an audio segment.
Warm and reflective, in the register of a 1990s indie film print: full-bodied
low end, soft top, gentle levelling, tape-style saturation and a faint pink
noise grain. Deliberately distinct from :func:apply_vintage_filter, which
is darker, mono and un-saturated.
Implemented as an ffmpeg filter graph (see :mod:xil_pipeline.audio_fx);
returns the segment unchanged if ffmpeg is unavailable.
Parameters:
-
segment(AudioSegment) –Input audio segment to filter.
Returns:
-
AudioSegment–Filtered audio segment, the same length as the input.
Source code in src/xil_pipeline/mix_common.py
apply_speakerphone_filter
Apply the speakerphone treatment to an audio segment.
Narrow-band (steep 350 Hz / 3.4 kHz skirts) with hard AGC, odd-harmonic
saturation and a short tabletop slap. Much more pronounced than
:func:apply_phone_filter, which is a gentle single-pole band-limit.
Implemented as an ffmpeg filter graph (see :mod:xil_pipeline.audio_fx);
returns the segment unchanged if ffmpeg is unavailable.
Parameters:
-
segment(AudioSegment) –Input audio segment to filter.
Returns:
-
AudioSegment–Filtered audio segment, the same length as the input.
Source code in src/xil_pipeline/mix_common.py
build_foreground
build_foreground(stem_plans: list[StemPlan], cast_config: dict, gap_ms: int = 600, vintage_scenes: list[str] | None = None) -> tuple[AudioSegment, dict[int, int]]
Build the foreground audio track and a full-episode timeline.
Iterates stems in sequence order. Foreground stems (dialogue, SFX, BEAT) are concatenated with silence gaps and their positions are recorded in the timeline. Background stems (AMBIENCE, MUSIC) are recorded in the timeline at the current foreground cursor position but do not advance it — they are overlaid at that cue point in a separate background pass.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
cast_config(dict) –{speaker_key: {"pan": float, "filter": str | bool | None}}for per-speaker audio effects.filteracceptsFalse/None(no filter),True/"phone"(phone filter),"vintage", or a comma-separated combination such as"vintage,phone". -
gap_ms(int, default:600) –Silence inserted between foreground stems in ms.
-
vintage_scenes(list[str] | None, default:None) –Optional list of scene labels (e.g.
["scene-3", "scene-4"]) whose dialogue stems receive an additional vintage filter pass. Applied after the per-speaker filter chain.
Returns:
-
AudioSegment–Tuple of
(foreground_audio, timeline)wheretimeline -
dict[int, int]–maps sequence numbers to millisecond offsets within the
-
tuple[AudioSegment, dict[int, int]]–foreground track.
Source code in src/xil_pipeline/mix_common.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 | |
build_ambience_layer
build_ambience_layer(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int, level_db: float = AMBIENCE_LEVEL_DB) -> AudioSegment
Build the ambience background layer.
Each AMBIENCE stem is looped from its cue point to the start of
the next background cue (AMBIENCE or MUSIC) or the end of the
track, whichever comes first. The level_db parameter controls
ducking; use 0 for DAW layer export so the producer controls
levels in-DAW.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
timeline(dict[int, int]) –Cue-point timestamps from :func:
build_foreground. -
total_ms(int) –Total foreground track length in milliseconds.
-
level_db(float, default:AMBIENCE_LEVEL_DB) –Volume adjustment applied to the clip before looping. Negative values duck the ambience below dialogue.
Returns:
-
AudioSegment–Tuple of
(layer, labels)where layer is a full-length -
AudioSegment–class:
~pydub.AudioSegmentwith ambience looped at each cue -
AudioSegment–point, and labels is a list of
(start_sec, end_sec, text) -
AudioSegment–tuples spanning each looped region.
Source code in src/xil_pipeline/mix_common.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | |
build_vintage_filter_layer
build_vintage_filter_layer(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int, level_db: float = 0) -> tuple[AudioSegment, list[tuple]]
Build the vintage filter crackle layer.
Loops the crackle source between each VINTAGE FILTER ENGAGES
marker and the next VINTAGE FILTER DISENGAGES marker (or track
end if no DISENGAGES follows). Use level_db=0 for DAW export so
the producer controls levels in-DAW.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
timeline(dict[int, int]) –Cue-point timestamps from :func:
build_foreground. -
total_ms(int) –Total foreground track length in milliseconds.
-
level_db(float, default:0) –Volume adjustment applied to each looped region.
Returns:
-
AudioSegment–Tuple of
(layer, labels)where layer is a full-length -
list[tuple]–class:
~pydub.AudioSegmentwith crackle looped at each active -
tuple[AudioSegment, list[tuple]]–span, and labels is a list of
(start_sec, end_sec, text) -
tuple[AudioSegment, list[tuple]]–tuples for each looped region.
Source code in src/xil_pipeline/mix_common.py
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 | |
build_music_layer
build_music_layer(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int, level_db: float = MUSIC_LEVEL_DB, include_foreground_override: bool = False) -> AudioSegment
Build the music/sting background layer.
Each MUSIC stem is overlaid at its cue point without looping.
Use level_db=0 for DAW layer export so levels are set in-DAW.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
timeline(dict[int, int]) –Cue-point timestamps from :func:
build_foreground. -
total_ms(int) –Total foreground track length in milliseconds.
-
level_db(float, default:MUSIC_LEVEL_DB) –Volume adjustment applied before overlaying.
-
include_foreground_override(bool, default:False) –When
True, preamble/postamble MUSIC stems (foreground_override=True) are placed at their timeline position in this layer. Used by DAW export so the operator can see and mix them; set toFalse(default) for the integrated mix where they play sequentially via :func:build_foregroundinstead.
Returns:
-
AudioSegment–Tuple of
(layer, labels)where layer is a full-length -
AudioSegment–class:
~pydub.AudioSegmentwith music stings overlaid at -
AudioSegment–their cue positions, and labels is a list of
-
AudioSegment–(start_sec, end_sec, text)tuples for each sting.
Source code in src/xil_pipeline/mix_common.py
build_dialogue_layer
build_dialogue_layer(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int, cast_config: dict, vintage_scenes: list[str] | None = None) -> tuple
Build an isolated dialogue layer for DAW export.
Places only dialogue stems (entry_type == "dialogue") at their
foreground timeline positions in a full-length silent segment.
Filter and pan effects are applied per speaker as configured.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
timeline(dict[int, int]) –Cue-point timestamps from :func:
build_foreground. -
total_ms(int) –Total track length in milliseconds.
-
cast_config(dict) –Per-speaker audio settings.
-
vintage_scenes(list[str] | None, default:None) –Optional list of scene labels whose dialogue stems receive an additional vintage filter pass (same as :func:
build_foreground).
Returns:
-
tuple–Tuple of
(layer, labels)where layer is a full-length -
tuple–class:
~pydub.AudioSegmentwith dialogue stems at their -
tuple–timeline positions, and labels is a list of
-
tuple–(start_sec, end_sec, speaker)tuples for Audacity label export.
Source code in src/xil_pipeline/mix_common.py
build_foreground_timeline_only
build_foreground_timeline_only(stem_plans: list[StemPlan], gap_ms: int = 600) -> tuple[int, dict[int, int]]
Build a foreground timeline without decoding audio.
Lightweight variant of :func:build_foreground that reads MP3
durations via mutagen header inspection instead of loading full
audio via pydub. Enables --dry-run --timeline without
expensive audio decoding.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
gap_ms(int, default:600) –Silence gap between foreground stems in ms.
Returns:
-
int–Tuple of
(total_ms, timeline)wheretimelinemaps -
dict[int, int]–sequence numbers to millisecond offsets.
Source code in src/xil_pipeline/mix_common.py
compute_dialogue_labels
Compute dialogue label tuples without loading audio.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list.
-
timeline(dict[int, int]) –Cue-point timestamps from a foreground build.
Returns:
-
list[tuple]–List of 7-element tuples
(start_s, end_s, speaker, None, None, None, snippet) -
list[tuple]–where snippet is the first 5 words of the dialogue text (or
Noneif no -
list[tuple]–text is available). Positions [3]–[5] are
None(dialogue has no ramp or -
list[tuple]–play_duration); position [6] carries the snippet for the HTML tooltip.
Source code in src/xil_pipeline/mix_common.py
compute_ambience_labels
compute_ambience_labels(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int) -> list[tuple[float, float, str]]
Compute ambience label tuples without loading audio.
Uses the same boundary logic as :func:build_ambience_layer.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list.
-
timeline(dict[int, int]) –Cue-point timestamps.
-
total_ms(int) –Total episode duration in ms.
Returns:
Source code in src/xil_pipeline/mix_common.py
compute_vintage_filter_labels
compute_vintage_filter_labels(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int) -> list[tuple[float, float, str]]
Compute vintage filter label tuples without loading audio.
Uses the same boundary logic as :func:build_vintage_filter_layer.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list.
-
timeline(dict[int, int]) –Cue-point timestamps.
-
total_ms(int) –Total episode duration in ms.
Returns:
Source code in src/xil_pipeline/mix_common.py
compute_music_labels
compute_music_labels(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int, include_foreground_override: bool = False) -> list[tuple[float, float, str]]
Compute music label tuples without loading audio.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list.
-
timeline(dict[int, int]) –Cue-point timestamps.
-
total_ms(int) –Total episode duration in ms.
-
include_foreground_override(bool, default:False) –When
True, include preamble/ postamble MUSIC stems. Mirror of the same flag on :func:build_music_layer.
Returns:
Source code in src/xil_pipeline/mix_common.py
compute_sfx_labels
compute_sfx_labels(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int) -> list[tuple[float, float, str]]
Compute SFX/BEAT label tuples without loading audio.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list.
-
timeline(dict[int, int]) –Cue-point timestamps.
-
total_ms(int) –Total episode duration in ms.
Returns:
Source code in src/xil_pipeline/mix_common.py
derive_structure_bands
derive_structure_bands(entries_index: dict[int, dict], timeline: dict[int, int], total_ms: int, key: str = 'section') -> list[tuple[float, float, str]]
Derive contiguous script-structure bands (sections or scenes) for the timeline.
Groups consecutive parsed entries sharing the same entry[key] value into
one band spanning from the first placed entry of the group to the start of
the next group (the last band runs to total_ms).
Section and scene header entries carry no stem, so they never appear in
timeline; boundaries therefore come from the first entry of each group
that does have a cue point. Entries whose value is None (e.g. preamble
lines have no scene) produce no band — a legitimate gap.
Parameters:
-
entries_index(dict[int, dict]) –Parsed entries keyed by seq (see :func:
load_entries_index). -
timeline(dict[int, int]) –Cue-point timestamps in ms, keyed by seq.
-
total_ms(int) –Total episode duration in ms, used to close the final band.
-
key(str, default:'section') –Entry field to band on —
"section"or"scene".
Returns:
Source code in src/xil_pipeline/mix_common.py
build_sfx_layer
build_sfx_layer(stem_plans: list[StemPlan], timeline: dict[int, int], total_ms: int) -> AudioSegment
Build an isolated SFX layer for DAW export.
Places only one-shot SFX and BEAT stems (direction_type in
("SFX", "BEAT")) at their foreground timeline positions.
Parameters:
-
stem_plans(list[StemPlan]) –Classified stem list from :func:
collect_stem_plans. -
timeline(dict[int, int]) –Cue-point timestamps from :func:
build_foreground. -
total_ms(int) –Total track length in milliseconds.
Returns:
-
AudioSegment–Tuple of
(layer, labels)where layer is a full-length -
AudioSegment–class:
~pydub.AudioSegmentwith SFX stems at their timeline -
AudioSegment–positions, and labels is a list of
(start_sec, end_sec, text) -
AudioSegment–tuples for each one-shot effect.