@@ -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 ;
@@ -48,27 +50,61 @@ export async function limitDeliveryPeaks(
4850 const 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" ) ;
53+ const alternating = [ "a" , "b" ] . map ( ( k ) =>
54+ join ( dirname ( sourcePath ) , `audio-source-limited-${ k } .wav` ) ,
55+ ) ;
56+ let encodedFrom : string | undefined ;
57+ let ceiling = AAC_DELIVERY_TRUE_PEAK_DBFS ;
58+ for ( let pass = 0 ; pass < LIMIT_PASSES ; pass ++ ) {
59+ const limitedPath = alternating [ pass % 2 ] ! ;
60+ const result = await limitPass ( input , deps , ceiling , limitedPath ) ;
61+ if ( "encodeFailure" in result ) return result . encodeFailure ;
62+ if ( "limitFailed" in result ) break ;
63+ encodedFrom = limitedPath ;
64+ if ( result . peak <= AAC_DELIVERY_TRUE_PEAK_DBFS || ceiling <= LOWEST_CEILING_DBTP ) break ;
65+ ceiling = Math . max (
66+ ceiling - ( result . peak - AAC_DELIVERY_TRUE_PEAK_DBFS ) - PASS_HEADROOM_DB ,
67+ LOWEST_CEILING_DBTP ,
68+ ) ;
69+ }
70+ if ( encodedFrom ) deps . rename ( encodedFrom , sourcePath , { copyFallback : true } ) ;
71+ return { success : true } ;
72+ }
73+
74+ type DeliveryLimitResult = Awaited < ReturnType < typeof limitDeliveryPeaks > > ;
75+
76+ /** One pass: the original mix limited at `ceiling` into `limitedPath`, encoded to the delivery file, and measured. */
77+ async function limitPass (
78+ input : Parameters < typeof limitDeliveryPeaks > [ 0 ] ,
79+ deps : DeliveryLimitDeps ,
80+ ceiling : number ,
81+ limitedPath : string ,
82+ ) : Promise < { peak : number } | { limitFailed : true } | { encodeFailure : DeliveryLimitResult } > {
83+ const { audioPath, sourcePath, signal, timeoutMs } = input ;
5284 try {
53- await deps . applyChain ( sourcePath , LIMIT_CHAIN , limitedPath , { trackId : "delivery" , signal } ) ;
85+ await deps . applyChain ( sourcePath , limitChain ( ceiling ) , limitedPath , {
86+ trackId : "delivery" ,
87+ signal,
88+ } ) ;
5489 } catch ( error ) {
5590 if ( signal ?. aborted ) throw error ;
5691 input . warn ?.( "[audio] true-peak limit failed, the whole-mix cut applies" , {
5792 error : error instanceof Error ? error . message : String ( error ) ,
5893 } ) ;
59- return { success : true } ;
94+ return { limitFailed : true } ;
6095 }
6196 const encoded = await deps . encode ( limitedPath , audioPath , input . targetDurationSeconds , {
6297 signal,
6398 timeout : timeoutMs ,
6499 } ) ;
65100 if ( ! encoded . success ) {
66101 return {
67- success : false ,
68- error : `[audioDeliveryLimit] ${ describeFfmpegFailure ( encoded , timeoutMs ) } ` ,
69- failureReason : encoded . failureReason ,
102+ encodeFailure : {
103+ success : false ,
104+ error : `[audioDeliveryLimit] ${ describeFfmpegFailure ( encoded , timeoutMs ) } ` ,
105+ failureReason : encoded . failureReason ,
106+ } ,
70107 } ;
71108 }
72- deps . rename ( limitedPath , sourcePath , { copyFallback : true } ) ;
73- return { success : true } ;
109+ return { peak : await deps . measure ( audioPath , { timeoutMs, signal } ) } ;
74110}
0 commit comments