Effort free video editing!
Yield once in the audio render iterator
WyattBlue7c6b99faf218
diff --git a/src/render/audio.nim b/src/render/audio.nimindex 06cd10c9..1c4b5911 100644--- a/src/render/audio.nim+++ b/src/render/audio.nim@@ -1319,13 +1319,16 @@ proc makeAudioFrames*(fmt: AVSampleFormat, timeline: ptr v3, frameSize: int, layfor getter in samples.values:getter.close()+ var gainLinear = 1.0'f64+ var loudnormGraph: Graph+ var fltpPts = 0'i64+ let layoutDesc = $tl.layout+ let bufferArgs = &"sample_rate={sr}:sample_fmt=fltp:channel_layout={layoutDesc}:" &+ &"time_base=1/{sr}"+case norm.kindof nkNull:- let produce = newTimelineProducer()- for chunk in produce():- if stop != nil and stop[].load(): break- for fr in emitChunk(chunk):- yield fr+ discardof nkPeak:# Pass 1: stream the timeline to find the global peak.@@ -1342,23 +1345,10 @@ proc makeAudioFrames*(fmt: AVSampleFormat, timeline: ptr v3, frameSize: int, laylet peakAdjustment = norm.t - maxPeakLeveldebug &"current peak level: {maxPeakLevel}"debug &"peak adjustment: {peakAdjustment:.3f}dB"- let gainLinear = pow(10.0'f64, peakAdjustment / 20.0'f64)-- # Pass 2: stream the timeline again, applying the gain.- let produce2 = newTimelineProducer()- for chunk in produce2():- var c = chunk- if peakAdjustment != 0.0'f32:- for k in 0 ..< c.len:- c[k] = int16(max(-32768.0'f64, min(32767.0'f64, c[k].float64 * gainLinear)))- for fr in emitChunk(c):- yield fr+ if peakAdjustment != 0.0'f32:+ gainLinear = pow(10.0'f64, peakAdjustment / 20.0'f64)of nkEbu:- let layoutDesc = $tl.layout- let bufferArgs = &"sample_rate={sr}:sample_fmt=fltp:channel_layout={layoutDesc}:" &- &"time_base=1/{sr}"-# First pass: stream chunks through loudnorm to measure loudness.let capture = beginLoudnormCapture()let firstPass = &"i={norm.i}:lra={norm.lra}:tp={norm.tp}:offset={norm.gain}:print_format=json"@@ -1368,7 +1358,6 @@ proc makeAudioFrames*(fmt: AVSampleFormat, timeline: ptr v3, frameSize: int, laylet analysisSink = analysisGraph.add("abuffersink")analysisGraph.linkNodes(analysisSrc, analysisFilter, analysisSink).configure()- var fltpPts = 0'i64block:let produce = newTimelineProducer()for chunk in produce():@@ -1410,40 +1399,55 @@ proc makeAudioFrames*(fmt: AVSampleFormat, timeline: ptr v3, frameSize: int, lay&"measured_i={measuredI}:measured_lra={measuredLRA}:measured_tp={measuredTP}:" &&"measured_thresh={measuredThresh}:print_format=none"debug &"EBU norm: {secondPass}"- var loudnormGraph = newGraph()+ loudnormGraph = newGraph()let bufferSrc = loudnormGraph.add("abuffer", bufferArgs)let loudnormFilter = loudnormGraph.add("loudnorm", secondPass)let aformat = loudnormGraph.add("aformat", &"sample_rates={sr}")let bufferSink = loudnormGraph.add("abuffersink")loudnormGraph.linkNodes(bufferSrc, loudnormFilter, aformat,bufferSink).configure()-fltpPts = 0- block:- let produce = newTimelineProducer()- for chunk in produce():- var f = chunkToFltpFrame(chunk, tl.layout, sr, fltpPts)- loudnormGraph.push(f)- av_frame_free(addr f)- while true:- let outF = loudnormGraph.pullTransient()- if outF == nil:- break- rechunkBuf.add loudnormOutToChunk(outF, targetChannels)- for fr in drainRechunk(false):- yield fr- loudnormGraph.flush()++ proc pullLoudnorm() =while true:let outF = loudnormGraph.pullTransient()if outF == nil:breakrechunkBuf.add loudnormOutToChunk(outF, targetChannels)- loudnormGraph.cleanup()-- # Pad with silence if loudnorm produced fewer samples than the timeline.- let pending = rechunkBuf.len div targetChannels- if ebuEmitted + pending < totalSamples:- rechunkBuf.add newSeq[int16](- (totalSamples - ebuEmitted - pending) * targetChannels)- for fr in drainRechunk(true):++ # One pass over the timeline for every kind; the step after the last chunk+ # flushes loudnorm.+ let produce = newTimelineProducer()+ var done = false+ while not done:+ var frames: seq[(ptr AVFrame, int64)]+ var chunk = produce()+ if finished(produce) or (norm.kind == nkNull and stop != nil and stop[].load()):+ done = true+ if norm.kind == nkEbu:+ loudnormGraph.flush()+ pullLoudnorm()+ loudnormGraph.cleanup()+ # Pad with silence if loudnorm produced fewer samples than the timeline.+ let pending = rechunkBuf.len div targetChannels+ if ebuEmitted + pending < totalSamples:+ rechunkBuf.add newSeq[int16](+ (totalSamples - ebuEmitted - pending) * targetChannels)+ frames = drainRechunk(true)+ else:+ case norm.kind+ of nkNull:+ frames = emitChunk(chunk)+ of nkPeak:+ if gainLinear != 1.0'f64:+ for k in 0 ..< chunk.len:+ chunk[k] = int16(max(-32768.0'f64, min(32767.0'f64, chunk[k].float64 * gainLinear)))+ frames = emitChunk(chunk)+ of nkEbu:+ var f = chunkToFltpFrame(chunk, tl.layout, sr, fltpPts)+ loudnormGraph.push(f)+ av_frame_free(addr f)+ pullLoudnorm()+ frames = drainRechunk(false)+ for fr in frames:yield fr