braidio 0.0.78__tar.gz → 0.0.79__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. {braidio-0.0.78 → braidio-0.0.79}/PKG-INFO +2 -2
  2. braidio-0.0.79/braidio/leveling.py +115 -0
  3. {braidio-0.0.78 → braidio-0.0.79}/braidio/multivoice.py +5 -27
  4. {braidio-0.0.78 → braidio-0.0.79}/braidio/render.py +3 -27
  5. {braidio-0.0.78 → braidio-0.0.79}/pyproject.toml +2 -2
  6. braidio-0.0.79/tests/test_leveling.py +82 -0
  7. {braidio-0.0.78 → braidio-0.0.79}/tests/test_tts_api_key.py +2 -2
  8. {braidio-0.0.78 → braidio-0.0.79}/.editorconfig +0 -0
  9. {braidio-0.0.78 → braidio-0.0.79}/.gitattributes +0 -0
  10. {braidio-0.0.78 → braidio-0.0.79}/.gitignore +0 -0
  11. {braidio-0.0.78 → braidio-0.0.79}/LICENSE +0 -0
  12. {braidio-0.0.78 → braidio-0.0.79}/README.md +0 -0
  13. {braidio-0.0.78 → braidio-0.0.79}/braidio/__init__.py +0 -0
  14. {braidio-0.0.78 → braidio-0.0.79}/braidio/_paths.py +0 -0
  15. {braidio-0.0.78 → braidio-0.0.79}/braidio/bodies/__init__.py +0 -0
  16. {braidio-0.0.78 → braidio-0.0.79}/braidio/bodies/_domain.py +0 -0
  17. {braidio-0.0.78 → braidio-0.0.79}/braidio/bodies/_render_nodes.py +0 -0
  18. {braidio-0.0.78 → braidio-0.0.79}/braidio/bodies/_tiers.py +0 -0
  19. {braidio-0.0.78 → braidio-0.0.79}/braidio/bodies/_video.py +0 -0
  20. {braidio-0.0.78 → braidio-0.0.79}/braidio/captions.py +0 -0
  21. {braidio-0.0.78 → braidio-0.0.79}/braidio/compose.py +0 -0
  22. {braidio-0.0.78 → braidio-0.0.79}/braidio/conversation.py +0 -0
  23. {braidio-0.0.78 → braidio-0.0.79}/braidio/cost.py +0 -0
  24. {braidio-0.0.78 → braidio-0.0.79}/braidio/data/skills/braidio/SKILL.md +0 -0
  25. {braidio-0.0.78 → braidio-0.0.79}/braidio/data/skills/braidio-commentary-video/SKILL.md +0 -0
  26. {braidio-0.0.78 → braidio-0.0.79}/braidio/defaults.py +0 -0
  27. {braidio-0.0.78 → braidio-0.0.79}/braidio/delivery.py +0 -0
  28. {braidio-0.0.78 → braidio-0.0.79}/braidio/downloads.py +0 -0
  29. {braidio-0.0.78 → braidio-0.0.79}/braidio/formats.py +0 -0
  30. {braidio-0.0.78 → braidio-0.0.79}/braidio/genre.py +0 -0
  31. {braidio-0.0.78 → braidio-0.0.79}/braidio/importing/__init__.py +0 -0
  32. {braidio-0.0.78 → braidio-0.0.79}/braidio/importing/__main__.py +0 -0
  33. {braidio-0.0.78 → braidio-0.0.79}/braidio/importing/_catalog.py +0 -0
  34. {braidio-0.0.78 → braidio-0.0.79}/braidio/importing/_manifest.py +0 -0
  35. {braidio-0.0.78 → braidio-0.0.79}/braidio/importing/_writer.py +0 -0
  36. {braidio-0.0.78 → braidio-0.0.79}/braidio/kinds.py +0 -0
  37. {braidio-0.0.78 → braidio-0.0.79}/braidio/markdown_script.py +0 -0
  38. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/__init__.py +0 -0
  39. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/_docs.py +0 -0
  40. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/_guide.py +0 -0
  41. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/_helpers.py +0 -0
  42. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/_media.py +0 -0
  43. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/credentials.py +0 -0
  44. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/metering.py +0 -0
  45. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/tools.py +0 -0
  46. {braidio-0.0.78 → braidio-0.0.79}/braidio/mcp/workspace.py +0 -0
  47. {braidio-0.0.78 → braidio-0.0.79}/braidio/music.py +0 -0
  48. {braidio-0.0.78 → braidio-0.0.79}/braidio/pacing.py +0 -0
  49. {braidio-0.0.78 → braidio-0.0.79}/braidio/project.py +0 -0
  50. {braidio-0.0.78 → braidio-0.0.79}/braidio/provenance.py +0 -0
  51. {braidio-0.0.78 → braidio-0.0.79}/braidio/relevance.py +0 -0
  52. {braidio-0.0.78 → braidio-0.0.79}/braidio/rights.py +0 -0
  53. {braidio-0.0.78 → braidio-0.0.79}/braidio/script.py +0 -0
  54. {braidio-0.0.78 → braidio-0.0.79}/braidio/sources.py +0 -0
  55. {braidio-0.0.78 → braidio-0.0.79}/braidio/structure.py +0 -0
  56. {braidio-0.0.78 → braidio-0.0.79}/braidio/style.py +0 -0
  57. {braidio-0.0.78 → braidio-0.0.79}/braidio/textprep.py +0 -0
  58. {braidio-0.0.78 → braidio-0.0.79}/braidio/timeline.py +0 -0
  59. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/__init__.py +0 -0
  60. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_asset_refs.py +0 -0
  61. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_common.py +0 -0
  62. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_dialogue.py +0 -0
  63. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_episode.py +0 -0
  64. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_ingest.py +0 -0
  65. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_narration.py +0 -0
  66. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_segment.py +0 -0
  67. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_video_cut.py +0 -0
  68. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_video_panels.py +0 -0
  69. {braidio-0.0.78 → braidio-0.0.79}/braidio/transforms/_voice.py +0 -0
  70. {braidio-0.0.78 → braidio-0.0.79}/braidio/tts.py +0 -0
  71. {braidio-0.0.78 → braidio-0.0.79}/braidio/video.py +0 -0
  72. {braidio-0.0.78 → braidio-0.0.79}/braidio/voice_library.py +0 -0
  73. {braidio-0.0.78 → braidio-0.0.79}/braidio/weave.py +0 -0
  74. {braidio-0.0.78 → braidio-0.0.79}/braidio/weave_config.py +0 -0
  75. {braidio-0.0.78 → braidio-0.0.79}/conftest.py +0 -0
  76. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/Extraction and nw-App Design.md +0 -0
  77. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/research/commentary-formats-and-styles.md +0 -0
  78. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/research/conversational-vs-narration-tts.md +0 -0
  79. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/research/notebooklm-and-conversational-pacing.md +0 -0
  80. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/README.md +0 -0
  81. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/anti-platitude-checklist.md +0 -0
  82. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/commentary-voice-guide.md +0 -0
  83. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/_style-groups.md +0 -0
  84. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/amanda-dobbins.md +0 -0
  85. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/david-sims.md +0 -0
  86. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/griffin-newman.md +0 -0
  87. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/helen-molesworth.md +0 -0
  88. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/hrishikesh-hirway.md +0 -0
  89. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/jason-mantzoukas.md +0 -0
  90. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/john-mcwhorter.md +0 -0
  91. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/jon-caramanica.md +0 -0
  92. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/june-diane-raphael.md +0 -0
  93. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/karina-longworth.md +0 -0
  94. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/katy-hessel.md +0 -0
  95. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/louis-theroux.md +0 -0
  96. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/mallory-rubin.md +0 -0
  97. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/melvyn-bragg.md +0 -0
  98. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/michael-barbaro.md +0 -0
  99. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/nate-sloan-charlie-harding.md +0 -0
  100. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/paul-scheer.md +0 -0
  101. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/sean-fennessey.md +0 -0
  102. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/talk-art.md +0 -0
  103. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/style/voices/yasi-salek.md +0 -0
  104. {braidio-0.0.78 → braidio-0.0.79}/misc/docs/youtube_ingest_credentials.md +0 -0
  105. {braidio-0.0.78 → braidio-0.0.79}/tests/_audio.py +0 -0
  106. {braidio-0.0.78 → braidio-0.0.79}/tests/test_body_schema_stability.py +0 -0
  107. {braidio-0.0.78 → braidio-0.0.79}/tests/test_conversation.py +0 -0
  108. {braidio-0.0.78 → braidio-0.0.79}/tests/test_cost.py +0 -0
  109. {braidio-0.0.78 → braidio-0.0.79}/tests/test_credits_card.py +0 -0
  110. {braidio-0.0.78 → braidio-0.0.79}/tests/test_defaults.py +0 -0
  111. {braidio-0.0.78 → braidio-0.0.79}/tests/test_delivery.py +0 -0
  112. {braidio-0.0.78 → braidio-0.0.79}/tests/test_downloads.py +0 -0
  113. {braidio-0.0.78 → braidio-0.0.79}/tests/test_expressiveness.py +0 -0
  114. {braidio-0.0.78 → braidio-0.0.79}/tests/test_formats.py +0 -0
  115. {braidio-0.0.78 → braidio-0.0.79}/tests/test_genre_placement.py +0 -0
  116. {braidio-0.0.78 → braidio-0.0.79}/tests/test_importing.py +0 -0
  117. {braidio-0.0.78 → braidio-0.0.79}/tests/test_markdown_script.py +0 -0
  118. {braidio-0.0.78 → braidio-0.0.79}/tests/test_mcp.py +0 -0
  119. {braidio-0.0.78 → braidio-0.0.79}/tests/test_mcp_caller_key.py +0 -0
  120. {braidio-0.0.78 → braidio-0.0.79}/tests/test_media_auth.py +0 -0
  121. {braidio-0.0.78 → braidio-0.0.79}/tests/test_multivoice.py +0 -0
  122. {braidio-0.0.78 → braidio-0.0.79}/tests/test_music.py +0 -0
  123. {braidio-0.0.78 → braidio-0.0.79}/tests/test_namespaced_source.py +0 -0
  124. {braidio-0.0.78 → braidio-0.0.79}/tests/test_nw_app.py +0 -0
  125. {braidio-0.0.78 → braidio-0.0.79}/tests/test_pacing.py +0 -0
  126. {braidio-0.0.78 → braidio-0.0.79}/tests/test_packaging.py +0 -0
  127. {braidio-0.0.78 → braidio-0.0.79}/tests/test_render_settings.py +0 -0
  128. {braidio-0.0.78 → braidio-0.0.79}/tests/test_script.py +0 -0
  129. {braidio-0.0.78 → braidio-0.0.79}/tests/test_sources.py +0 -0
  130. {braidio-0.0.78 → braidio-0.0.79}/tests/test_structure.py +0 -0
  131. {braidio-0.0.78 → braidio-0.0.79}/tests/test_textprep_style.py +0 -0
  132. {braidio-0.0.78 → braidio-0.0.79}/tests/test_timeline.py +0 -0
  133. {braidio-0.0.78 → braidio-0.0.79}/tests/test_transforms.py +0 -0
  134. {braidio-0.0.78 → braidio-0.0.79}/tests/test_transforms_dialogue.py +0 -0
  135. {braidio-0.0.78 → braidio-0.0.79}/tests/test_transforms_episode_members.py +0 -0
  136. {braidio-0.0.78 → braidio-0.0.79}/tests/test_transforms_rights.py +0 -0
  137. {braidio-0.0.78 → braidio-0.0.79}/tests/test_transforms_secrets.py +0 -0
  138. {braidio-0.0.78 → braidio-0.0.79}/tests/test_transforms_structure.py +0 -0
  139. {braidio-0.0.78 → braidio-0.0.79}/tests/test_turn_overlap.py +0 -0
  140. {braidio-0.0.78 → braidio-0.0.79}/tests/test_video.py +0 -0
  141. {braidio-0.0.78 → braidio-0.0.79}/tests/test_video_bodies.py +0 -0
  142. {braidio-0.0.78 → braidio-0.0.79}/tests/test_video_footage.py +0 -0
  143. {braidio-0.0.78 → braidio-0.0.79}/tests/test_video_transforms.py +0 -0
  144. {braidio-0.0.78 → braidio-0.0.79}/tests/test_voice_library.py +0 -0
  145. {braidio-0.0.78 → braidio-0.0.79}/tests/test_weave.py +0 -0
  146. {braidio-0.0.78 → braidio-0.0.79}/tests/test_weave_config.py +0 -0
  147. {braidio-0.0.78 → braidio-0.0.79}/tests/test_wire_descriptions.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: braidio
3
- Version: 0.0.78
3
+ Version: 0.0.79
4
4
  Summary: Weave narration with extracted media segments into audiovisual productions
5
5
  Project-URL: Homepage, https://github.com/thorwhalen/braidio
6
6
  Project-URL: Repository, https://github.com/thorwhalen/braidio
@@ -26,7 +26,7 @@ Classifier: Topic :: Multimedia :: Video
26
26
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
27
  Requires-Python: >=3.10
28
28
  Requires-Dist: elevenlabs
29
- Requires-Dist: mixing[media]>=0.0.28
29
+ Requires-Dist: mixing[media]>=0.0.77
30
30
  Provides-Extra: dev
31
31
  Requires-Dist: build; extra == 'dev'
32
32
  Requires-Dist: pytest-cov>=4.0; extra == 'dev'
@@ -0,0 +1,115 @@
1
+ """Level one spoken part to a loudness target with a single measured gain.
2
+
3
+ Every narration take, dialogue exchange and source clip is levelled before it
4
+ is woven, so a quiet clip sits beside a loud take. ffmpeg's one-pass
5
+ ``loudnorm`` would do that *dynamically*: it rides the gain through the part,
6
+ flattening a voice's own dynamics (a whisper, an emphasis) and pumping quiet
7
+ passages. :func:`level_part` instead measures the part once and applies one
8
+ gain, then a true-peak limiter holds the ceiling. The measurement and the
9
+ limiter are :mod:`mixing.loudness` (the fleet's one loudness component);
10
+ this module only decodes, applies and encodes.
11
+
12
+ >>> peak_ceiling(DEFAULT_TRUE_PEAK_DB) # doctest: +ELLIPSIS
13
+ 0.84...
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import subprocess
19
+ from contextlib import closing
20
+ from pathlib import Path
21
+
22
+ import numpy as np
23
+ from mixing.decode import decode_float_chunks
24
+ from mixing.loudness import level_group, limit_peaks
25
+ from mixing.util import ffmpeg_exe
26
+
27
+ #: Integrated loudness a spoken part is levelled to, in LUFS.
28
+ DEFAULT_TARGET_LUFS = -16.0
29
+ #: True-peak ceiling of a levelled part, in dBTP.
30
+ DEFAULT_TRUE_PEAK_DB = -1.5
31
+ #: Sample rate every levelled part is written at, so the weave concatenates
32
+ #: parts of one rate whatever each source was.
33
+ OUTPUT_SAMPLE_RATE = 44100
34
+
35
+ _PCM_FORMAT = "f32le" # how float samples travel to the encoder
36
+ _STDERR_TAIL_CHARS = 800 # how much of ffmpeg's complaint an error carries
37
+
38
+
39
+ def peak_ceiling(true_peak_db: float) -> float:
40
+ """The linear ceiling (full scale 1.0) of a ``true_peak_db`` dBTP limit."""
41
+ return 10.0 ** (true_peak_db / 20.0)
42
+
43
+
44
+ def _decode(src: Path) -> tuple[np.ndarray, int]:
45
+ rate, chunks = decode_float_chunks(src)
46
+ with closing(chunks):
47
+ parts = list(chunks)
48
+ return (np.concatenate(parts) if parts else np.zeros((0, 1), np.float32)), rate
49
+
50
+
51
+ def _encode(samples: np.ndarray, rate: int, dst: Path, *, sample_rate: int) -> None:
52
+ proc = subprocess.run(
53
+ [
54
+ ffmpeg_exe(),
55
+ "-y",
56
+ "-f",
57
+ _PCM_FORMAT,
58
+ "-ar",
59
+ str(rate),
60
+ "-ac",
61
+ str(samples.shape[1]),
62
+ "-i",
63
+ "-",
64
+ "-ar",
65
+ str(sample_rate), # the resample, as its own step
66
+ str(dst),
67
+ ],
68
+ input=np.ascontiguousarray(samples, dtype="<f4").tobytes(),
69
+ capture_output=True,
70
+ )
71
+ if proc.returncode != 0:
72
+ tail = proc.stderr.decode(errors="replace").strip()[-_STDERR_TAIL_CHARS:]
73
+ raise RuntimeError(f"ffmpeg could not write {dst.name}: {tail}")
74
+
75
+
76
+ def level_part(
77
+ src: str | Path,
78
+ dst: str | Path,
79
+ *,
80
+ target_lufs: float = DEFAULT_TARGET_LUFS,
81
+ true_peak_db: float = DEFAULT_TRUE_PEAK_DB,
82
+ sample_rate: int = OUTPUT_SAMPLE_RATE,
83
+ ) -> Path:
84
+ """Write ``src`` to ``dst`` at ``target_lufs`` with one gain, true-peak limited.
85
+
86
+ The gain is :func:`mixing.loudness.level_group` over the whole part (so the
87
+ part keeps its own dynamics; silence gets none), the limiter
88
+ :func:`mixing.loudness.limit_peaks` at ``true_peak_db``. Gains are applied
89
+ in float, so nothing clips before the limiter. ``dst`` is written at
90
+ ``sample_rate``, in the format its suffix names.
91
+
92
+ Bounds worth knowing:
93
+
94
+ - The gain is capped at ``mixing.loudness.MAX_GAIN_DB`` (with a warning):
95
+ a part far below the target comes out quieter than it, rather than as
96
+ amplified noise (``loudnorm`` would have pulled it all the way up).
97
+ - The ceiling holds on the samples handed to the encoder; a lossy ``dst``
98
+ (mp3) can read a few tenths of a dB over it. A levelled part is an
99
+ intermediate: the final master limits again.
100
+ - The whole part is decoded into memory (float, plus the limiter's copy):
101
+ fine for spoken parts and clips of minutes, roughly 1 GB for 10 minutes
102
+ of 48 kHz stereo. A streaming file-level leveller belongs in ``mixing``.
103
+ """
104
+ src, dst = Path(src), Path(dst)
105
+ dst.parent.mkdir(parents=True, exist_ok=True)
106
+ samples, rate = _decode(src)
107
+ gain_db, _ = level_group([samples], rate, target_lufs=target_lufs)
108
+ leveled = limit_peaks(
109
+ samples * 10.0 ** (gain_db / 20.0),
110
+ ceiling=peak_ceiling(true_peak_db),
111
+ true_peak=True,
112
+ fs=rate,
113
+ )
114
+ _encode(leveled, rate, dst, sample_rate=sample_rate)
115
+ return dst
@@ -18,13 +18,13 @@ from __future__ import annotations
18
18
 
19
19
  import random
20
20
  import re
21
- import shutil
22
21
  import subprocess
23
22
  from dataclasses import dataclass
24
23
  from pathlib import Path
25
24
 
26
25
  from mixing import concatenate_audio
27
26
 
27
+ from braidio.leveling import DEFAULT_TARGET_LUFS, level_part
28
28
  from braidio.tts import narrate
29
29
 
30
30
  _MARKUP_RE = re.compile(r"<[^>]*>|\[[^\]]*\]")
@@ -122,30 +122,6 @@ def group_turns(
122
122
  return turns
123
123
 
124
124
 
125
- def _loudnorm(
126
- src: Path, dst: Path, *, target_lufs: float = -16.0, true_peak: float = -1.5
127
- ) -> Path:
128
- if shutil.which("ffmpeg") is None:
129
- raise RuntimeError("ffmpeg not found on PATH.")
130
- dst.parent.mkdir(parents=True, exist_ok=True)
131
- subprocess.run(
132
- [
133
- "ffmpeg",
134
- "-y",
135
- "-i",
136
- str(src),
137
- "-af",
138
- f"loudnorm=I={target_lufs}:TP={true_peak}:LRA=11",
139
- "-ar",
140
- "44100",
141
- str(dst),
142
- ],
143
- check=True,
144
- capture_output=True,
145
- )
146
- return dst
147
-
148
-
149
125
  def render_multivoice(
150
126
  segments: list[str],
151
127
  pool: list[Voice],
@@ -163,7 +139,7 @@ def render_multivoice(
163
139
  speed_jitter: float = 0.04,
164
140
  crossfade_s: float = 0.1,
165
141
  gap_s: float = 0.0,
166
- target_lufs: float = -16.0,
142
+ target_lufs: float = DEFAULT_TARGET_LUFS,
167
143
  ) -> list[tuple[Voice, str]]:
168
144
  """Render ``segments`` cycling ``pool`` → ``out_path``.
169
145
 
@@ -198,7 +174,9 @@ def render_multivoice(
198
174
  model_id=model_id,
199
175
  voice_settings={**settings, "speed": speed},
200
176
  )
201
- parts.append(_loudnorm(raw, work / f"norm{i:03d}.mp3", target_lufs=target_lufs))
177
+ parts.append(
178
+ level_part(raw, work / f"norm{i:03d}.mp3", target_lufs=target_lufs)
179
+ )
202
180
  out = Path(out_path)
203
181
  out.parent.mkdir(parents=True, exist_ok=True)
204
182
  if gap_s > 0:
@@ -33,6 +33,7 @@ from braidio.rights import (
33
33
  RightsPolicy,
34
34
  plan_production,
35
35
  )
36
+ from braidio.leveling import DEFAULT_TARGET_LUFS, level_part
36
37
  from braidio.script import Script
37
38
  from braidio.sources import SegmentSource
38
39
  from braidio.structure import DEFAULT_STRUCTURE, prepare_pause, prepare_sting
@@ -40,10 +41,6 @@ from braidio.tts import narrate
40
41
  from braidio.weave import TimelineItem, extract_padded, weave_timeline
41
42
  from braidio.weave_config import WeaveConfig, speaker_key
42
43
 
43
- _DEFAULT_LUFS = -16.0
44
- _TRUE_PEAK = -1.5
45
- _LOUDNESS_RANGE = 11 # loudnorm LRA target for a spoken part
46
-
47
44
 
48
45
  def _require_ffmpeg() -> None:
49
46
  if shutil.which("ffmpeg") is None:
@@ -215,27 +212,6 @@ def _end_tail(path: Path, *, fade_s: float, silence_s: float) -> None:
215
212
  pass # not a real/probeable audio file — leave it untailed
216
213
 
217
214
 
218
- def _loudnorm(src: Path, dst: Path, *, target_lufs: float = _DEFAULT_LUFS) -> Path:
219
- _require_ffmpeg()
220
- dst.parent.mkdir(parents=True, exist_ok=True)
221
- subprocess.run(
222
- [
223
- "ffmpeg",
224
- "-y",
225
- "-i",
226
- str(src),
227
- "-af",
228
- f"loudnorm=I={target_lufs}:TP={_TRUE_PEAK}:LRA={_LOUDNESS_RANGE}",
229
- "-ar",
230
- "44100",
231
- str(dst),
232
- ],
233
- check=True,
234
- capture_output=True,
235
- )
236
- return dst
237
-
238
-
239
215
  def render_production(
240
216
  script: Script,
241
217
  *,
@@ -303,7 +279,7 @@ def render_production(
303
279
  """
304
280
  publishable = rights.publishable_clip_rights if rights else PUBLISHABLE_CLIP_RIGHTS
305
281
  plan = plan_production(script, profile, publishable_clip_rights=publishable)
306
- target_lufs = config.target_lufs if config is not None else _DEFAULT_LUFS
282
+ target_lufs = config.target_lufs if config is not None else DEFAULT_TARGET_LUFS
307
283
  duck_db = config.duck_db if config is not None else -15.0
308
284
  structure = structure if structure is not None else DEFAULT_STRUCTURE
309
285
 
@@ -439,7 +415,7 @@ def render_production(
439
415
  min_len_s=clip_min_len,
440
416
  )
441
417
  parts.append(
442
- _loudnorm(raw, work / f"part{i:02d}.mp3", target_lufs=target_lufs)
418
+ level_part(raw, work / f"part{i:02d}.mp3", target_lufs=target_lufs)
443
419
  if normalize
444
420
  else raw
445
421
  )
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "braidio"
9
- version = "0.0.78"
9
+ version = "0.0.79"
10
10
  description = "Weave narration with extracted media segments into audiovisual productions"
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.10"
@@ -48,7 +48,7 @@ authors = [
48
48
  # mixing>=0.0.28: narrate() calls text_to_speech(return_cache_status=...) (added
49
49
  # there) so a mixing-cache hit reports cost_usd_actual=$0 (braidio#8).
50
50
  dependencies = [
51
- "mixing[media]>=0.0.28",
51
+ "mixing[media]>=0.0.77",
52
52
  "elevenlabs",
53
53
  ]
54
54
 
@@ -0,0 +1,82 @@
1
+ """``braidio.leveling.level_part``: one measured gain per spoken part, true-peak limited.
2
+
3
+ These measure the written file, not the call: a levelling that rode the gain
4
+ through the part (one-pass ``loudnorm``, what this replaced) passes a
5
+ "reaches the target" check while failing the "keeps its dynamics" one.
6
+ """
7
+
8
+ import shutil
9
+
10
+ import numpy as np
11
+ import pytest
12
+ from scipy.io import wavfile
13
+
14
+ from braidio.leveling import DEFAULT_TRUE_PEAK_DB, OUTPUT_SAMPLE_RATE, level_part
15
+
16
+ pytestmark = pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="needs ffmpeg")
17
+
18
+ _RATE = 48000
19
+
20
+
21
+ def _tone(seconds, amplitude, *, freq=440.0, rate=_RATE):
22
+ t = np.arange(int(seconds * rate)) / rate
23
+ return (amplitude * np.sin(2 * np.pi * freq * t)).astype(np.float32)
24
+
25
+
26
+ def _write(path, samples, rate=_RATE):
27
+ wavfile.write(path, rate, samples)
28
+ return path
29
+
30
+
31
+ def _read(path):
32
+ from mixing.decode import decode_float_chunks
33
+
34
+ rate, chunks = decode_float_chunks(path)
35
+ return np.concatenate(list(chunks)), rate
36
+
37
+
38
+ def _lufs(samples, rate):
39
+ from mixing.loudness import integrated_lufs
40
+
41
+ return integrated_lufs(samples, rate)
42
+
43
+
44
+ def test_a_quiet_part_reaches_the_target(tmp_path):
45
+ src = _write(tmp_path / "quiet.wav", _tone(3.0, 0.02))
46
+ out, rate = _read(level_part(src, tmp_path / "out.wav", target_lufs=-20.0))
47
+ assert _lufs(out, rate) == pytest.approx(-20.0, abs=0.3)
48
+
49
+
50
+ def test_one_gain_keeps_the_parts_own_dynamics(tmp_path):
51
+ # a whisper then an emphasis, 14 dB apart: one gain keeps them 14 dB apart,
52
+ # where a dynamic normaliser pulls them together
53
+ whisper, emphasis = _tone(4.0, 0.02), _tone(4.0, 0.1)
54
+ src = _write(tmp_path / "dyn.wav", np.concatenate([whisper, emphasis]))
55
+ out, rate = _read(level_part(src, tmp_path / "out.wav", target_lufs=-20.0))
56
+ half = len(out) // 2
57
+ before = _lufs(emphasis[:, None], _RATE) - _lufs(whisper[:, None], _RATE)
58
+ after = _lufs(out[half:], rate) - _lufs(out[:half], rate)
59
+ assert after == pytest.approx(before, abs=0.5)
60
+
61
+
62
+ def test_the_true_peak_ceiling_holds_when_the_gain_would_clip(tmp_path):
63
+ from mixing.loudness import true_peak_db
64
+
65
+ # quiet body with one sharp peak: the gain to the target pushes the peak past 0 dBFS
66
+ body = _tone(3.0, 0.01)
67
+ body[_RATE : _RATE + 200] = 0.5
68
+ src = _write(tmp_path / "peaky.wav", body)
69
+ out, rate = _read(level_part(src, tmp_path / "out.wav", target_lufs=-14.0))
70
+ assert true_peak_db(out, rate) <= DEFAULT_TRUE_PEAK_DB + 0.3
71
+
72
+
73
+ def test_every_part_is_written_at_one_rate(tmp_path):
74
+ src = _write(tmp_path / "lo.wav", _tone(2.0, 0.1, rate=22050), rate=22050)
75
+ _, rate = _read(level_part(src, tmp_path / "out.wav"))
76
+ assert rate == OUTPUT_SAMPLE_RATE
77
+
78
+
79
+ def test_silence_stays_silent(tmp_path):
80
+ src = _write(tmp_path / "silent.wav", np.zeros(_RATE * 2, np.float32))
81
+ out, _ = _read(level_part(src, tmp_path / "out.mp3"))
82
+ assert np.abs(out).max() < 1e-3
@@ -80,7 +80,7 @@ def test_render_multivoice_threads_api_key(tmp_path, monkeypatch):
80
80
  return Path(out_path)
81
81
 
82
82
  monkeypatch.setattr(mv, "narrate", fake_narrate)
83
- monkeypatch.setattr(mv, "_loudnorm", lambda src, dst, **kw: src)
83
+ monkeypatch.setattr(mv, "level_part", lambda src, dst, **kw: src)
84
84
  monkeypatch.setattr(
85
85
  mv, "concatenate_audio", lambda *a, output, **kw: Path(output).write_bytes(b"x")
86
86
  )
@@ -125,7 +125,7 @@ def _patch_render_boundaries(monkeypatch, tmp_path):
125
125
 
126
126
  monkeypatch.setattr(render_mod, "narrate", fake_narrate)
127
127
  monkeypatch.setattr(render_mod, "render_dialogue", fake_render_dialogue)
128
- monkeypatch.setattr(render_mod, "_loudnorm", lambda src, dst, **kw: src)
128
+ monkeypatch.setattr(render_mod, "level_part", lambda src, dst, **kw: src)
129
129
  monkeypatch.setattr(
130
130
  render_mod,
131
131
  "concatenate_audio",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes