@@ -9,12 +9,14 @@ import {
99import { AAC_DELIVERY_TRUE_PEAK_DBFS } from "./audioPadTrim.js" ;
1010import { measureTruePeakDbfs } from "./audioTruePeak.js" ;
1111
12- // The worklet's 4x detector can land up to 1.7 dB over its ceiling, hence 2 dB under the -1 dBTP limit.
13- const LIMIT_CEILING_DBTP = AAC_DELIVERY_TRUE_PEAK_DBFS - 2 ;
14- const LIMIT_CHAIN = {
12+ // Each pass lowers the ceiling by what the last encode measured over it; past -3 dBTP the whole-mix cut takes over.
13+ const LIMIT_PASSES = 3 ;
14+ const LOWEST_CEILING_DBTP = AAC_DELIVERY_TRUE_PEAK_DBFS - 2 ;
15+ const PASS_HEADROOM_DB = 0.05 ;
16+ const limitChain = ( ceiling : number ) => ( {
1517 version : 1 as const ,
16- nodes : [ { type : "truepeak" , id : "delivery" , params : { ceiling : LIMIT_CEILING_DBTP } } ] ,
17- } ;
18+ nodes : [ { type : "truepeak" , id : "delivery" , params : { ceiling } } ] ,
19+ } ) ;
1820
1921interface DeliveryLimitDeps {
2022 measure : typeof measureTruePeakDbfs ;
@@ -45,30 +47,45 @@ export async function limitDeliveryPeaks(
4547) : Promise < { success : boolean ; error ?: string ; failureReason ?: "external_interruption" } > {
4648 const { audioPath, sourcePath, signal, timeoutMs } = input ;
4749 if ( ! deps . sourceExists ( sourcePath ) ) return { success : true } ;
48- const peak = await deps . measure ( audioPath , { timeoutMs, signal } ) ;
50+ let peak = await deps . measure ( audioPath , { timeoutMs, signal } ) ;
4951 if ( peak <= AAC_DELIVERY_TRUE_PEAK_DBFS ) return { success : true } ;
5052
51- const limitedPath = join ( dirname ( sourcePath ) , "audio-source-limited.wav" ) ;
52- try {
53- await deps . applyChain ( sourcePath , LIMIT_CHAIN , limitedPath , { trackId : "delivery" , signal } ) ;
54- } catch ( error ) {
55- if ( signal ?. aborted ) throw error ;
56- input . warn ?.( "[audio] true-peak limit failed, the whole-mix cut applies" , {
57- error : error instanceof Error ? error . message : String ( error ) ,
53+ const alternating = [ "a" , "b" ] . map ( ( k ) => join ( dirname ( sourcePath ) , `audio-source-limited-${ k } .wav` ) ) ;
54+ let encodedFrom : string | undefined ;
55+ let ceiling = AAC_DELIVERY_TRUE_PEAK_DBFS ;
56+ for ( let pass = 0 ; pass < LIMIT_PASSES ; pass ++ ) {
57+ const limitedPath = alternating [ pass % 2 ] ! ;
58+ try {
59+ await deps . applyChain ( sourcePath , limitChain ( ceiling ) , limitedPath , {
60+ trackId : "delivery" ,
61+ signal,
62+ } ) ;
63+ } catch ( error ) {
64+ if ( signal ?. aborted ) throw error ;
65+ input . warn ?.( "[audio] true-peak limit failed, the whole-mix cut applies" , {
66+ error : error instanceof Error ? error . message : String ( error ) ,
67+ } ) ;
68+ break ;
69+ }
70+ const encoded = await deps . encode ( limitedPath , audioPath , input . targetDurationSeconds , {
71+ signal,
72+ timeout : timeoutMs ,
5873 } ) ;
59- return { success : true } ;
74+ if ( ! encoded . success ) {
75+ return {
76+ success : false ,
77+ error : `[audioDeliveryLimit] ${ describeFfmpegFailure ( encoded , timeoutMs ) } ` ,
78+ failureReason : encoded . failureReason ,
79+ } ;
80+ }
81+ encodedFrom = limitedPath ;
82+ peak = await deps . measure ( audioPath , { timeoutMs, signal } ) ;
83+ if ( peak <= AAC_DELIVERY_TRUE_PEAK_DBFS || ceiling <= LOWEST_CEILING_DBTP ) break ;
84+ ceiling = Math . max (
85+ ceiling - ( peak - AAC_DELIVERY_TRUE_PEAK_DBFS ) - PASS_HEADROOM_DB ,
86+ LOWEST_CEILING_DBTP ,
87+ ) ;
6088 }
61- const encoded = await deps . encode ( limitedPath , audioPath , input . targetDurationSeconds , {
62- signal,
63- timeout : timeoutMs ,
64- } ) ;
65- if ( ! encoded . success ) {
66- return {
67- success : false ,
68- error : `[audioDeliveryLimit] ${ describeFfmpegFailure ( encoded , timeoutMs ) } ` ,
69- failureReason : encoded . failureReason ,
70- } ;
71- }
72- deps . rename ( limitedPath , sourcePath , { copyFallback : true } ) ;
89+ if ( encodedFrom ) deps . rename ( encodedFrom , sourcePath , { copyFallback : true } ) ;
7390 return { success : true } ;
7491}
0 commit comments