@@ -47,45 +47,64 @@ export async function limitDeliveryPeaks(
4747) : Promise < { success : boolean ; error ?: string ; failureReason ?: "external_interruption" } > {
4848 const { audioPath, sourcePath, signal, timeoutMs } = input ;
4949 if ( ! deps . sourceExists ( sourcePath ) ) return { success : true } ;
50- let peak = await deps . measure ( audioPath , { timeoutMs, signal } ) ;
50+ const peak = await deps . measure ( audioPath , { timeoutMs, signal } ) ;
5151 if ( peak <= AAC_DELIVERY_TRUE_PEAK_DBFS ) return { success : true } ;
5252
53- const alternating = [ "a" , "b" ] . map ( ( k ) => join ( dirname ( sourcePath ) , `audio-source-limited-${ k } .wav` ) ) ;
53+ const alternating = [ "a" , "b" ] . map ( ( k ) =>
54+ join ( dirname ( sourcePath ) , `audio-source-limited-${ k } .wav` ) ,
55+ ) ;
5456 let encodedFrom : string | undefined ;
5557 let ceiling = AAC_DELIVERY_TRUE_PEAK_DBFS ;
5658 for ( let pass = 0 ; pass < LIMIT_PASSES ; pass ++ ) {
5759 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 ,
73- } ) ;
74- if ( ! encoded . success ) {
75- return {
76- success : false ,
77- error : `[audioDeliveryLimit] ${ describeFfmpegFailure ( encoded , timeoutMs ) } ` ,
78- failureReason : encoded . failureReason ,
79- } ;
80- }
60+ const result = await limitPass ( input , deps , ceiling , limitedPath ) ;
61+ if ( "encodeFailure" in result ) return result . encodeFailure ;
62+ if ( "limitFailed" in result ) break ;
8163 encodedFrom = limitedPath ;
82- peak = await deps . measure ( audioPath , { timeoutMs, signal } ) ;
83- if ( peak <= AAC_DELIVERY_TRUE_PEAK_DBFS || ceiling <= LOWEST_CEILING_DBTP ) break ;
64+ if ( result . peak <= AAC_DELIVERY_TRUE_PEAK_DBFS || ceiling <= LOWEST_CEILING_DBTP ) break ;
8465 ceiling = Math . max (
85- ceiling - ( peak - AAC_DELIVERY_TRUE_PEAK_DBFS ) - PASS_HEADROOM_DB ,
66+ ceiling - ( result . peak - AAC_DELIVERY_TRUE_PEAK_DBFS ) - PASS_HEADROOM_DB ,
8667 LOWEST_CEILING_DBTP ,
8768 ) ;
8869 }
8970 if ( encodedFrom ) deps . rename ( encodedFrom , sourcePath , { copyFallback : true } ) ;
9071 return { success : true } ;
9172}
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 ;
84+ try {
85+ await deps . applyChain ( sourcePath , limitChain ( ceiling ) , limitedPath , {
86+ trackId : "delivery" ,
87+ signal,
88+ } ) ;
89+ } catch ( error ) {
90+ if ( signal ?. aborted ) throw error ;
91+ input . warn ?.( "[audio] true-peak limit failed, the whole-mix cut applies" , {
92+ error : error instanceof Error ? error . message : String ( error ) ,
93+ } ) ;
94+ return { limitFailed : true } ;
95+ }
96+ const encoded = await deps . encode ( limitedPath , audioPath , input . targetDurationSeconds , {
97+ signal,
98+ timeout : timeoutMs ,
99+ } ) ;
100+ if ( ! encoded . success ) {
101+ return {
102+ encodeFailure : {
103+ success : false ,
104+ error : `[audioDeliveryLimit] ${ describeFfmpegFailure ( encoded , timeoutMs ) } ` ,
105+ failureReason : encoded . failureReason ,
106+ } ,
107+ } ;
108+ }
109+ return { peak : await deps . measure ( audioPath , { timeoutMs, signal } ) } ;
110+ }
0 commit comments