Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

Agent: finish the sound of an editor project in one job: separate each clip's sound, remove...

Bowen Xue21fbe1340365
diff --git a/freevideo_engine/agent/errors.py b/freevideo_engine/agent/errors.py
index c9ef8ac..fd04e5f 100644
--- a/freevideo_engine/agent/errors.py
+++ b/freevideo_engine/agent/errors.py
@@ -126,6 +126,8 @@ ERRORS.update({
'Tell the user the model needs a network connection, then submit the job again with a new ref.'),
'stems_failed': (409, True, 'Splitting the sound did not finish.',
'Read the project, then submit the job again with a new ref; if it fails again, leave the sound as it is.'),
+ 'sound_failed': (409, True, 'The sound of the cut was not finished.',
+ 'Read the project, then submit the job again with a new ref; if it fails again, leave the sound as it is.'),
'voice_failed': (409, True, 'Changing the voice did not finish.',
'Read the project, then submit the job again with a new ref; if it fails again, leave the sound as it is.'),
'voice_model_missing': (409, False, 'The dubbing model is not ready on this machine.',
@@ -248,7 +250,7 @@ def recovery(code, *, kind=None, stage='request', resumable=True):
return 'Tell the user to free disk space, then submit the picture again with a new ref.'
if kind == 'music' and code == 'out_of_memory':
return 'Close other GPU applications, then submit the music job again with a new ref.'
- if kind in ('stems', 'voice', 'voice_lines') and code == 'out_of_memory':
+ if kind in ('stems', 'sound', 'finish_sound', 'voice', 'voice_lines') and code == 'out_of_memory':
return 'Close other GPU applications, then submit the job again with a new ref.'
if kind == 'image' and code == 'no_memory':
return 'Lower the picture dimensions or use fewer reference pictures, then submit with a new ref.'
diff --git a/freevideo_engine/agent/guide.py b/freevideo_engine/agent/guide.py
index d4c78e2..a9966e9 100644
--- a/freevideo_engine/agent/guide.py
+++ b/freevideo_engine/agent/guide.py
@@ -147,6 +147,7 @@ GUIDE = {
'POST /projects/{id}/generate reserves pending parts for clip_ids or a start/frames range and fills them as the returned job finishes. POST /projects/{id}/export renders the revision you read; wait for its job.',
'Music: POST /projects/{id}/music with the revision you read and a caption; wait for the job, then read the project: result.clip_ids are the new clips on a2. Run it after the cut is joined; a second run needs the earlier score removed from a2.',
'Split sound: POST /projects/{id}/stems with the revision you read and the clip_id of a sound clip; wait for the job. The clip\'s sound is split into dialogue, music and ambience on tracks a4, a5 and a6 over the clip\'s range, and the clip itself goes silent. result.parts lists each track with its name, clip_id, start, end and frames. Offered only where features lists projects.stems; a clip is split once, and deleting the three parts gives its sound back. To save the three tracks and the original mix as four wav files, POST /projects/{id}/stems/export with the revision and the split clip\'s clip_id; the answer lists folder and files at once.',
+ 'Finish the sound: after the cut is joined, POST /projects/{id}/finish_sound with the revision you read; wait for the job. In one job it splits each picture clip\'s sound where separation is offered and removes the separated music where it is a sustained score (shots, explosions and crowds filed there stay, at the ambience level), lays one score on a2 for the whole cut (music.caption composes it, music.source lays a file; music already on a2 stays), evens the dialogue, sets the music 16 LU under it, brings the cut to -18 LUFS with a -1 dBTP peak ceiling and fills digital silence with room tone. result gives loudness_lufs, true_peak_db, music_removed (per block: removed or kept, with the levels) and the steps in skipped; skipped.music is too_long for a cut over 600 s, and the rest still runs; then tell the user: The cut is over 10 minutes, so no music was composed. The other steps are done. To add music, run this again with a music file. Run again, only the clips split this time are judged and levelled.',
'Change a voice: POST /projects/{id}/voice/lines with the revision you read and a selection (clip_id, and start and end in seconds if only part) to write down the lines; read result.lines. Change the text of the lines to redo and POST /projects/{id}/voice/replace with the same selection and all lines (original_text unchanged); only the changed lines are spoken again, in the voice of the original, and the rest keep their sound. result.lines gives where each new line starts and ends; a line that cannot fit before the next one keeps its original voice with replaced false and warning too_long. POST /projects/{id}/voice/estimate with the lines answers at once with the seconds the changed lines take. Offered only where features lists projects.voice.',
'Subtitles: POST /projects/{id}/listen with mode transcribe (writes down what is said) or align (times the lines you give) and the revision you read; wait for the job and read result.lines, each with start and end frames. Then add them with a subtitles operation of cues {start, frames, text}; replace true swaps the whole track. With no speech model the job fails with no_speech_model: write the lines yourself.',
'The user sees one line under each picture clip: its note, or else the first line of its description. A description is generation text, often long and in English, so after you add, generate or change a clip, write a note (operation note) in the user\'s language saying what is in the picture: the subject first, then the setting, for example "Train through morning mist" (in Chinese "小火车穿过晨雾"). A short clip shows only the first few characters, about four in Chinese, and those must already make sense on their own. Do not write where the clip sits (opening, middle, ending, part 2): the order of the clips shows that. Do not write how far along it is: the clip already shows its progress. Keep it true when you change the clip again.',
@@ -383,6 +384,8 @@ CAPABILITIES_ZH = {
'剪辑拼好后先读取 revision,并删去该轨道上原有的配乐;首次使用会下载音乐模型(约 20 GB)',
('POST', '/projects/{id}/stems'): '把一个声音片段的声音分成人声、音乐和环境声,放在其下方的 a4、a5、a6 三条轨道上,原片段静音。'
'先读取 revision 与片段 ID,等待返回的任务完成,结果 parts 给出各轨道的名称、起止帧;仅在 features 含 projects.stems 时可用',
+ ('POST', '/projects/{id}/finish_sound'): '剪辑拼好后,用一个任务完成整条剪辑的声音:分离人声、音乐与环境声,在 a2 铺一段贯穿全片的配乐,'
+ '统一人声响度,整片调至 -18 LUFS,并以环境底噪填补无声处。先读取 revision,等待返回的任务完成',
('POST', '/projects/{id}/voice/lines'): '把选中声音片段里说的话逐句写下来,每句带起止秒数;先读取 revision 与片段 ID,等待返回的任务完成,结果 lines 给出各句;仅在 features 含 projects.voice 时可用',
('POST', '/projects/{id}/voice/replace'): '把改过文字的句子用原说话人的声音重新说一遍,放回原句的位置,没改的句子保持原声;提交 voice/lines 给出的全部句子,只改 text;结果 lines 给出每句新声的起止,放不进下一句之前的句子保留原声',
('POST', '/projects/{id}/voice/shorten'): '按目标秒数改短一句的文字;只返回文字,不保存也不配音',
diff --git a/freevideo_engine/agent/jobs.py b/freevideo_engine/agent/jobs.py
index cff93a7..2224c09 100644
--- a/freevideo_engine/agent/jobs.py
+++ b/freevideo_engine/agent/jobs.py
@@ -32,7 +32,7 @@ from ..system import windows
ACTIVE = ('waiting', 'queued', 'running', 'cancelling')
CAST_AGAIN = 'POST /freevideo/agent/v1/cast/%s/generate to make the reference pictures again.'
DONE = ('succeeded', 'failed', 'cancelled', 'interrupted')
-GPU_KINDS = ('vlm', 'enhance', 'image', 'music', 'stems', 'voice')
+GPU_KINDS = ('vlm', 'enhance', 'image', 'music', 'stems', 'finish_sound', 'voice')
IMAGE_ERRORS = errors.IMAGE_ERRORS
IMAGE_WAIT_ERRORS = ('busy', 'busy_video')
IMAGE_WAIT_NOTICES = {
@@ -1488,6 +1488,9 @@ class Jobs:
elif row['kind'] == 'stems':
from .stems import run_stems
result = run_stems(self, row)
+ elif row['kind'] == 'finish_sound':
+ from .sound import run_finish_sound
+ result = run_finish_sound(self, row)
elif row['kind'] == 'voice_lines':
from .voice import run_voice_lines
result = run_voice_lines(self, row)
diff --git a/freevideo_engine/agent/music.py b/freevideo_engine/agent/music.py
index 8a377c9..388a571 100644
--- a/freevideo_engine/agent/music.py
+++ b/freevideo_engine/agent/music.py
@@ -49,10 +49,10 @@ def run_music(jobs, row, *, poll=.5):
generate_seconds=numbers.get('generate_s'), load_seconds=numbers.get('load_s'))
-def _compose(jobs, row, service, out, frames, seed, quiet, phase, poll):
+def _compose(jobs, row, service, out, frames, seed, quiet, phase, poll, *, caption=None):
for attempt in range(PREEMPTIONS + 1):
try:
- return service.compose(out, row['spec']['caption'], frames, seed=seed, quiet=quiet, fps=comfy_editor.FPS,
+ return service.compose(out, row['spec']['caption'] if caption is None else caption, frames, seed=seed, quiet=quiet, fps=comfy_editor.FPS,
server=getattr(jobs.port, 'server', None), cancelled=lambda: bool(row.get('_cancel')),
phase=phase)
except service.Preempted:
diff --git a/freevideo_engine/agent/projects.py b/freevideo_engine/agent/projects.py
index 803cd88..d12a484 100644
--- a/freevideo_engine/agent/projects.py
+++ b/freevideo_engine/agent/projects.py
@@ -419,6 +419,24 @@ class Projects:
self._remember(ref, digest, result, client)
return result
+ def finish_sound(self, identity, value, *, jobs, ref=None, client=None):
+ """Queue one job that finishes the sound of the cut the caller read; a stale revision starts nothing."""
+ schema.validate(schema.project_schema('finish_sound'), value)
+ with self.listen_lock:
+ digest, replay = self._receipt('projects:finish_sound', dict(id=identity, body=value), ref, client)
+ if replay is not None:
+ return replay
+ stored = self._load(identity)['project']
+ if stored['revision'] != value['revision']:
+ raise ProjectError('project_changed', project=stored)
+ if not comfy_editor.duration(stored):
+ raise _invalid('/revision', 'a revision whose timeline has clips', value['revision'])
+ job = jobs.submit(dict(kind='finish_sound', editor_project=identity,
+ **{k: v for k, v in value.items() if k != 'ref'}), client=client)[0]
+ result = dict(job=jobs.view(job), jobs=[jobs.view(job)])
+ self._remember(ref, digest, result, client)
+ return result
+
def stems(self, identity, value, *, jobs, ref=None, client=None):
"""Queue a sound-splitting job for one sound clip of the revision the caller read."""
schema.validate(schema.project_schema('stems'), value)
diff --git a/freevideo_engine/agent/routes.py b/freevideo_engine/agent/routes.py
index 1df729c..dacb579 100644
--- a/freevideo_engine/agent/routes.py
+++ b/freevideo_engine/agent/routes.py
@@ -172,7 +172,7 @@ def submit_batch(jobs, body, client):
return jobs.submit_request(body, client=client)[0]
-JOB_OPERATIONS = ('generate', 'export', 'listen', 'music', 'stems', 'voice_lines', 'voice_replace')
+JOB_OPERATIONS = ('generate', 'export', 'listen', 'music', 'stems', 'finish_sound', 'voice_lines', 'voice_replace')
def stems_supported(machine):
@@ -203,7 +203,8 @@ def features(continuation, vlm, stems=False, voice=True):
'queue.shared': True, 'queue.busy': True, 'chain.auto': bool(continuation),
'enhance': bool(vlm.get('ready')), 'drama.film': True, 'images.edit': rules,
'images.layers': rules, 'images.heal': rules, 'images.render': rules, 'projects.read': True,
- **{'projects.' + name: rules for name in ('insert', 'split', 'move', 'trim', 'remove', 'export', 'generate', 'listen', 'music')},
+ **{'projects.' + name: rules for name in ('insert', 'split', 'move', 'trim', 'remove', 'export', 'generate', 'listen', 'music',
+ 'finish_sound')},
'projects.stems': bool(rules and stems), 'projects.voice': bool(rules and stems and voice)}
@@ -255,6 +256,7 @@ def capabilities(port, jobs):
'listen': 'Listen to the sound of an editing project for subtitle lines and times',
'music': 'Compose music for an editing project and lay it on its second sound track',
'stems': 'Split the sound of a clip into dialogue, music and ambience on sub-tracks',
+ 'finish_sound': 'Finish the sound of a joined cut: one score, even loudness, room tone',
'voice_lines': 'Write down the spoken lines of a sound clip with their start and end',
'voice': 'Say edited lines again in the original voice and put them back where the originals were',
'image_edit': 'Render a saved revision of an image session to a picture'},
diff --git a/freevideo_engine/agent/schema.py b/freevideo_engine/agent/schema.py
index cc2ce11..21521dd 100644
--- a/freevideo_engine/agent/schema.py
+++ b/freevideo_engine/agent/schema.py
@@ -355,6 +355,22 @@ def stems(*, local=False):
**{'x-job-kind': 'stems'})
+SOUND_FIELDS = dict(
+ music={'oneOf': [obj(dict(caption=MUSIC_FIELDS['caption'], seed=MUSIC_FIELDS['seed']), ('caption',)),
+ obj(dict(source=EDITOR_SOURCE), ('source',))],
+ 'description': 'For an empty a2: a caption to compose, or a source to lay. Omit for no music.'},
+ keep_dialogue_volume={'type': 'boolean', 'default': False})
+
+
+def finish_sound(*, local=False):
+ return obj(dict(kind={'const': 'finish_sound', 'type': 'string'},
+ editor_project=string(34, pattern=r'^e-[0-9a-f]{32}$'),
+ revision=number(1, 2**53 - 1, integer=True),
+ **SOUND_FIELDS, **common()), ('kind', 'editor_project', 'revision'),
+ description='Finish the sound of a joined cut: split, one score on a2, even loudness at -18 LUFS, room tone.',
+ **{'x-job-kind': 'finish_sound'})
+
+
VOICE_SELECTION = obj(dict(clip_id=string(64), start=number(0, 36000, description='Seconds from the clip start; default 0.'),
end=number(0, 36000, description='Seconds from the clip start; default the clip end.')), ('clip_id',),
description='The sound clip, or part of it, whose voice to change: the original sound, or its dialogue sub-track.')
@@ -420,12 +436,13 @@ def image_edit(*, local=False):
KINDS = dict(generate=generate, assemble=assemble, vlm=vlm, enhance=enhance, image=image,
- export=export, listen=listen, music=music, stems=stems, voice_lines=voice_lines, voice=voice, image_edit=image_edit)
+ export=export, listen=listen, music=music, stems=stems, finish_sound=finish_sound, voice_lines=voice_lines, voice=voice,
+ image_edit=image_edit)
def job_schema(*, local=False, refs=False, batch=False):
# Music scores a joined cut, so it is not a storyboard row.
- value = {'oneOf': [f(local=local) for kind, f in KINDS.items() if not (batch and kind in ('music', 'stems', 'voice_lines', 'voice'))], 'x-discriminator': 'kind',
+ value = {'oneOf': [f(local=local) for kind, f in KINDS.items() if not (batch and kind in ('music', 'stems', 'finish_sound', 'voice_lines', 'voice'))], 'x-discriminator': 'kind',
'description': 'x-discriminator selects field diagnostics by kind; all accepted branches use standard oneOf.'}
return batch_references(value) if refs else value
@@ -542,7 +559,7 @@ def contracts(*, local=False):
if method == 'GET':
extra['query'] = shape
else:
- extra.update(body=shape, idempotent=True, **({'status': 201} if name in ('create', 'generate', 'export', 'listen', 'music', 'stems', 'voice_lines', 'voice_replace') else {}))
+ extra.update(body=shape, idempotent=True, **({'status': 201} if name in ('create', 'generate', 'export', 'listen', 'music', 'stems', 'finish_sound', 'voice_lines', 'voice_replace') else {}))
routes[method, suffix] = route(summary, **extra)
for name, (method, suffix, summary) in WHITEBOX_OPERATIONS.items():
extra = dict(path=obj({'id': WHITEBOX}, ('id',))) if '{id}' in suffix else {}
@@ -938,11 +955,12 @@ PROJECT_OPERATIONS = {
'voice_shorten': ('POST', '/projects/{id}/voice/shorten', 'Shorten one line to fit a number of seconds. The answer is a text; nothing is saved or spoken.'),
'voice_estimate': ('POST', '/projects/{id}/voice/estimate', 'Estimate the seconds the changed lines take to make. The answer comes at once.'),
'music': ('POST', '/projects/{id}/music', 'Compose music as long as the joined cut and lay it on sound track a2 at frame 0, under the video sound. First get the revision; a2 must be empty. Wait for the returned job; the first use downloads the model (20 GB).'),
+ 'finish_sound': ('POST', '/projects/{id}/finish_sound', 'Finish the sound of a joined cut in one job: split each clip\'s sound where offered, one score on a2, even dialogue, -18 LUFS, room tone. First get the revision; wait for the job.'),
}
PROJECT_TOOLS = dict(list='freevideo_projects', get='freevideo_project_read', create='freevideo_project_create',
write='freevideo_project_write', operations='freevideo_editor_operations',
generate='freevideo_project_generate', export='freevideo_export',
- listen='freevideo_project_listen', music='freevideo_project_music',
+ listen='freevideo_project_listen', music='freevideo_project_music', finish_sound='freevideo_project_finish_sound',
stems='freevideo_project_stems', stems_export='freevideo_project_stems_export',
voice_lines='freevideo_project_voice_lines', voice_replace='freevideo_project_voice_replace',
voice_shorten='freevideo_project_voice_shorten', voice_estimate='freevideo_project_voice_estimate')
@@ -1066,6 +1084,8 @@ def project_schema(name):
return obj(dict(revision=revision, **LISTEN_FIELDS, ref=REF), ('revision', 'mode'))
if name == 'music':
return obj(dict(revision=revision, **MUSIC_FIELDS, ref=REF), ('revision', 'caption'))
+ if name == 'finish_sound':
+ return obj(dict(revision=revision, **SOUND_FIELDS, ref=REF), ('revision',))
if name in ('stems', 'stems_export'):
return obj(dict(revision=revision, **STEMS_FIELDS, **(STEMS_LAY if name == 'stems' else {}), ref=REF), ('revision', 'clip_id'))
if name == 'voice_lines':
diff --git a/freevideo_engine/agent/skill/freevideo/references/edit.txt b/freevideo_engine/agent/skill/freevideo/references/edit.txt
index f7618aa..2c0f585 100644
--- a/freevideo_engine/agent/skill/freevideo/references/edit.txt
+++ b/freevideo_engine/agent/skill/freevideo/references/edit.txt
@@ -9,4 +9,5 @@ Projects are the timelines shared with the FreeVideo editor page.
4. `freevideo_editor_operations` applies 1 to 64 edits atomically. Prefer it to `freevideo_project_write`, which replaces the whole timeline.
5. `freevideo_project_generate` makes new picture and sound for selected clips or a frame range, as the Generate button does.
6. Subtitles: `freevideo_project_listen` with mode `align` times lines you give, `transcribe` writes down what is said.
-7. `freevideo_export` renders one saved revision to MP4 in the saved videos. Wait for the returned job.
+7. `freevideo_project_finish_sound` finishes the sound of a joined cut in one job: one score on a2, even dialogue, -18 LUFS, room tone. Wait for the returned job.
+8. `freevideo_export` renders one saved revision to MP4 in the saved videos. Wait for the returned job.
diff --git a/freevideo_engine/agent/sound.py b/freevideo_engine/agent/sound.py
new file mode 100644
index 0000000..4c5d197
--- /dev/null
+++ b/freevideo_engine/agent/sound.py
@@ -0,0 +1,745 @@
+"""Finishing the sound of a whole cut in one job, after the cut is made.
+
+Five steps, each an ordinary edit that stays visible and changeable on the timeline:
+1. the sound of each picture's clip is split into dialogue, music and ambience (a4-a6), once per source file;
+2. the separated music (a5) of each block goes where it is a sustained score (score_like); separation also puts shots,
+ explosions and crowds there, so a block whose music part is not a score keeps it;
+3. one piece of music lies on a2 from the first frame to the last, at one volume and lowered during speech
+ (a piece already on a2 stays, only its volume changes);
+4. one loudness: the dialogue of each block moves towards the common dialogue level, the ambience follows it down
+ but never up, the music sits MUSIC_UNDER_LU under the dialogue, every volume then moves by the same amount to
+ TARGET_LUFS, and the export holds the true peak under PEAK_DB (the project's master_peak_db);
+5. digital silence in the dialogue and ambience is filled with the block's own room tone (a new ambience file).
+
+Levels are measured on the export's own audio-only render, so speed, fades, crossfades and ducking count as the
+user will hear them. Without sound separation (Mac, AMD, small cards) steps 1, 2 and 5 are skipped."""
+import copy
+import json
+import math
+import secrets
+import shutil
+from pathlib import Path
+
+from .. import comfy_editor
+from .jobs import JobError
+
+FOLDER = 'freevideo-sound'
+RATE = 48000
+SAMPLES_PER_FRAME = RATE // comfy_editor.FPS
+TARGET_LUFS = -18.
+PEAK_DB = -1.
+SWING_DB = 4. # the most one block's dialogue moves towards the common level
+LIMIT_TAKES_DB = 5. # the most the export's limiter is left to take off a block's loudest peak
+MUSIC_UNDER_LU = 16.
+SPEECH_LUFS = -45. # a block whose dialogue is quieter has no speech; it keeps its level
+MAX_VOLUME = 2.
+HOLE_DB, HOLE_SECONDS = -70., .04
+CUT_GAP_SECONDS, CUT_SPAN_SECONDS = .01, .4
+LEVEL_ROUNDS = 3
+# score_like: a separated music part is a sustained score when it is this loud (RMS), sounds (0.5 s windows over that
+# RMS) more than this share of the time, and more than this share of its sounding frames are tonal: at least
+# TONAL_LINE of the 60 Hz-5 kHz energy on spectral lines 10 dB over their neighbourhood.
+SCORE_RMS, SCORE_SOUNDING, SCORE_TONAL, TONAL_LINE = .005, .5, .5, .3
+FADE_FRAMES = 72 # a given piece longer than the cut fades out over its last 3 s
+FADE_IN_FRAMES = 48 # and, when it starts inside the piece, fades in over 2 s
+
+
+def _cancelled(row):
+ if row.get('_cancel'):
+ raise JobError('cancelled', 'Cancelled')
+
+
+def _db(value):
+ return 20 * math.log10(value) if value > 0 else -math.inf
+
+
+def _round(value, digits=2):
+ return round(value, digits) if value is not None and math.isfinite(value) else None
+
+
+def _audio_tracks(project):
+ return {track['id']: track for track in project['tracks'] if track['kind'] == 'audio'}
+
+
+def blocks(project):
+ """The picture's clips in order, each with its linked sound clip (None when it has none) and that clip's parts."""
+ tracks = _audio_tracks(project)
+ clips = {clip['id']: clip for track in tracks.values() for clip in track['clips']}
+ parts = {}
+ for track in tracks.values():
+ if track.get('stem'):
+ for clip in track['clips']:
+ if clip.get('stem_of'):
+ parts.setdefault(clip['stem_of'], {})[track['stem']] = clip
+ video = next(track for track in project['tracks'] if track['id'] == 'v1')
+ result = []
+ for clip in sorted(video['clips'], key=lambda clip: clip['start']):
+ if not clip.get('source'):
+ continue
+ sound = clips.get(clip.get('link'))
+ sound = sound if sound is not None and sound.get('source') else None
+ result.append(dict(video=clip, sound=sound, parts=parts.get(sound['id'], {}) if sound else {}))
+ return result
+
+
+def cuts(found):
+ """Timeline frames where one picture clip meets the next."""
+ return [b['video']['start'] for a, b in zip(found, found[1:]) if a['video']['start'] + a['video']['frames'] == b['video']['start']]
+
+
+def level_gains(dialogue, peaks, *, keep_dialogue=False):
+ """Per-block dialogue and ambience gains (dB) towards the median dialogue level of the blocks with speech.
+
+ dialogue: integrated dialogue loudness per block (-inf for none); peaks: true peak of dialogue+ambience per block."""
+ speech = [value for value in dialogue if value > SPEECH_LUFS]
+ if not speech:
+ return None, [dict(dialogue_db=0., ambience_db=0.) for _ in dialogue]
+ speech.sort()
+ middle = len(speech) // 2
+ target = speech[middle] if len(speech) % 2 else (speech[middle - 1] + speech[middle]) / 2
+ gains = []
+ for loudness, peak in zip(dialogue, peaks):
+ gain = 0.
+ if loudness > SPEECH_LUFS and not keep_dialogue:
+ gain = max(-SWING_DB, min(SWING_DB, target - loudness))
+ if math.isfinite(peak):
+ gain = min(gain, PEAK_DB - peak + LIMIT_TAKES_DB)
+ # Lifting a quiet voice must not lift the crash or the crowd next to it.
+ gains.append(dict(dialogue_db=gain, ambience_db=min(gain, 0.)))
+ return target, gains
+
+
+def cut_steps(mix, music, cut_frames):
+ """At each cut: the music's level change (RMS 0.4 s each side, 10 ms clear of the cut) and the mix's short-term
+ loudness change (the 3 s window ending at the cut against the one ending 0.6 s later)."""
+ import numpy as np
+ from ..audio_loudness import short_term
+ curve = short_term(mix, RATE) if mix.shape[1] >= 3 * RATE else np.zeros(0)
+ gap, span = round(CUT_GAP_SECONDS * RATE), round(CUT_SPAN_SECONDS * RATE)
+
+ def rms_db(x):
+ return max(-70., _db(float(np.sqrt(np.mean(np.square(x))))) if x.size else -70.)
+
+ rows = []
+ for frame in cut_frames:
+ at = frame * SAMPLES_PER_FRAME
+ row = dict(frame=frame)
+ if music is not None:
+ row['music_step_db'] = round(rms_db(music[:, at + gap:at + gap + span]) - rms_db(music[:, max(0, at - gap - span):at - gap]), 2)
+ ending = round(frame / comfy_editor.FPS * 10) - 30
+ if curve.size and 0 <= ending and ending + 6 < curve.size:
+ row['mix_step_lu'] = round(float(max(curve[ending + 6], -70.) - max(curve[ending], -70.)), 2)
+ rows.append(row)
+ return rows
+
+
+def summary(rows, key):
+ import numpy as np
+ values = np.abs([row[key] for row in rows if key in row])
+ return dict(median=round(float(np.median(values)), 2), max=round(float(values.max()), 2)) if values.size else None
+
+
+def holes(audio, start, end):
+ """[first, last) sample runs in [start, end) where the audio stays under HOLE_DB for HOLE_SECONDS or longer."""
+ import numpy as np
+ hop = RATE // 100
+ count = (end - start) // hop
+ if count <= 0:
+ return []
+ frames = audio[:, start:start + count * hop].reshape(audio.shape[0], count, hop)
+ quiet = np.sqrt(np.mean(np.square(frames), axis=(0, 2))) < 10 ** (HOLE_DB / 20)
+ runs, index = [], 0
+ while index < count:
+ if quiet[index]:
+ stop = index
+ while stop < count and quiet[stop]:
+ stop += 1
+ if (stop - index) * hop >= HOLE_SECONDS * RATE:
+ runs.append((start + index * hop, start + stop * hop))
+ index = stop
+ else:
+ index += 1
+ return runs
+
+
+def _window_db(x, size, hop):
+ import numpy as np
+ count = max(0, (x.shape[1] - size) // hop + 1)
+ if not count:
+ return np.zeros(0)
+ total = np.concatenate([[0.], np.cumsum(np.mean(np.square(x.astype(np.float64)), axis=0))])
+ starts = np.arange(count) * hop
+ return 10 * np.log10(np.maximum((total[starts + size] - total[starts]) / size, 1e-20))
+
+
+def room_tone(ambience, dialogue, rate, start, end, quiet):
+ """A steady stretch of ambience with no speech in [start, end), at the block's usual background level
+ (25th percentile of the candidates), longest first: 1 s, 0.5 s, 0.25 s. None when the block has none.
+
+ quiet: a boolean per 10 ms of the range that is digital silence, which a candidate must not touch."""
+ import numpy as np
+ hop, hop_10 = round(.05 * rate), rate // 100
+ for seconds in (1., .5, .25):
+ size = round(seconds * rate)
+ if end - start < size:
+ continue
+ level = _window_db(ambience[:, start:end], size, hop)
+ speech = _window_db(dialogue[:, start:end], size, hop)
+ found = []
+ for index in np.nonzero((speech < -55) & (level > -68) & (level < -35))[0]:
+ at = start + index * hop
+ if np.any(quiet[(at - start) // hop_10:(at - start + size) // hop_10]):
+ continue
+ if np.std(_window_db(ambience[:, at:at + size], hop, hop)) <= 3.: # steady: room tone, not an effect hit
+ found.append((at, level[index]))
+ if found:
+ usual = np.percentile([level for _, level in found], 25)
+ at, _ = min(found, key=lambda item: abs(item[1] - usual))
+ return ambience[:, at:at + size].copy()
+ return None
+
+
+def tile(tone, count, rate):
+ """`count` samples of `tone` laid end to end, every other copy reversed, joined and faded with 15 ms
+ equal-power ramps."""
+ import numpy as np
+ fade = min(round(.015 * rate), tone.shape[1] // 2)
+ rise = np.sin(np.linspace(0, math.pi / 2, fade))[None, :]
+ fall = np.cos(np.linspace(0, math.pi / 2, fade))[None, :]
+ out = np.zeros((tone.shape[0], count + tone.shape[1] + fade))
+ position, flip = 0, False
+ while position < count + fade:
+ piece = (tone[:, ::-1] if flip else tone).astype(np.float64)
+ if position:
+ piece = piece.copy()
+ piece[:, :fade] *= rise
+ out[:, position:position + fade] *= fall
+ out[:, position + fade:position + piece.shape[1]] = 0
+ out[:, position:position + piece.shape[1]] += piece
+ position += piece.shape[1] - fade
+ flip = not flip
+ out = out[:, :count]
+ edge = min(fade, count // 2)
+ if edge:
+ out[:, :edge] *= rise[:, :edge]
+ out[:, count - edge:] *= fall[:, fade - edge:]
+ return out
+
+
+class _Renders:
+ """Audio-only renders of the project through the export, as float [2, samples] at 48 kHz."""
+
+ def __init__(self, jobs, row, folder):
+ self.jobs, self.row, self.folder, self.count = jobs, row, folder, 0
+
+ def __call__(self, project, *, solo=None, muted=(), master=False):
+ import numpy as np
+ from .. import editor_export
+ value = copy.deepcopy(project)
+ if not master:
+ value.pop('master_peak_db', None)
+ for track in value['tracks']:
+ if track['kind'] != 'audio':
+ continue
+ if solo is not None:
+ track['solo'] = track['id'] in solo
+ if track['id'] in muted:
+ track['muted'] = True
+ self.count += 1
+ target = self.folder / ('render-%d.wav' % self.count)
+ try:
+ editor_export.render(value, editor_export.resolve_source, target,
+ options=dict(only='sound', sound_format='wav'),
+ cancelled=lambda: bool(self.row.get('_cancel')))
+ except editor_export.ExportCancelled:
+ raise JobError('cancelled', 'Cancelled') from None
+ except editor_export.ExportError as error:
+ raise JobError('sound_failed', 'The sound of the cut cannot be rendered (%s)' % error, kind='sound') from None
+ try:
+ import av
+ with av.open(str(target)) as container:
+ chunks = [frame.to_ndarray() for frame in container.decode(audio=0)]
+ finally:
+ target.unlink(missing_ok=True)
+ samples = np.concatenate(chunks, axis=1) if chunks else np.zeros((1, 0), np.int16)
+ samples = samples.reshape(-1, 2).T if samples.shape[0] == 1 else samples
+ return samples.astype(np.float64) / 32768
+
+
+def run_finish_sound(jobs, row, *, poll=.5):
+ spec, port = row['spec'], jobs.port
+ identity = spec['editor_project']
+ try:
+ project = jobs.projects.get(identity)['project']
+ except FileNotFoundError as error:
+ raise JobError('not_found', 'The project does not exist') from error
+ if not any(found['sound'] for found in blocks(project)):
+ raise JobError('invalid_request', 'The cut has no picture clips with sound')
+ folder = '%s/%s-%s' % (FOLDER, row['id'], secrets.token_hex(4))
+ work = port.output_path(folder + '/work/.keep', create=True).parent
+ made = []
+
+ def phase(name, fraction=None):
+ row['_phase'] = name
+ row['_detail'] = json.dumps(dict(fraction=round(fraction, 3))) if fraction is not None else None
+
+ result = dict(editor_project=identity, skipped={})
+ try:
+ render = _Renders(jobs, row, work)
+ # Run again on a finished cut (it carries the export ceiling), only the blocks split this time are judged and
+ # levelled; the rest keep what the first run gave them, so gains do not add up.
+ finished = jobs.projects.get(identity)['project'].get('master_peak_db') is not None
+ separated = _split(jobs, row, identity, phase, poll, result, made)
+ fresh = set(result.get('split', {}).get('clips', ())) if finished else None
+ _cancelled(row)
+ if separated:
+ scores = _separated_music_levels(jobs.projects.get(identity)['project'], render, result, fresh)
+ _cancelled(row)
+ _set_volumes(jobs, identity, {identity_: 0. for identity_ in scores})
+ _cancelled(row)
+ _music(jobs, row, spec, identity, phase, poll, result, made)
+ _cancelled(row)
+ phase('balancing loudness')
+ _levels(jobs, row, identity, render, result, keep_dialogue=bool(spec.get('keep_dialogue_volume')), fresh=fresh)
+ _cancelled(row)
+ if separated:
+ phase('filling silence')
+ _fill(jobs, row, identity, render, port, folder, result, made)
+ else:
+ result['skipped'].setdefault('room_tone', 'stems_unavailable')
+ _cancelled(row)
+ phase('measuring loudness')
+ project = jobs.projects.get(identity)['project']
+ _measure(render, project, result)
+ except BaseException:
+ # Parts already placed stay with the project (they are ordinary edits); only unplaced files go.
+ try:
+ used = {clip['source']['id'] for track in jobs.projects.get(identity)['project']['tracks']
+ for clip in track['clips'] if clip.get('source') and clip['source']['kind'] == 'generated'}
+ except Exception: # noqa: BLE001 - unreadable: keep every file rather than break the project
+ used = None
+ for relative in made:
+ if used is None or any(name == relative or name.startswith(relative + '/') for name in used):
+ continue
+ path = port.output_path(relative)
+ if path.is_dir():
+ shutil.rmtree(path, ignore_errors=True)
+ else:
+ path.unlink(missing_ok=True)
+ raise
+ finally:
+ shutil.rmtree(work, ignore_errors=True)
+ result['revision'] = jobs.projects.get(identity)['project']['revision']
+ return result
+
+
+def _split(jobs, row, identity, phase, poll, result, made):
+ """Step 1; False when separation is not offered here, so nothing separated exists to work with."""
+ from ..comfy_bridge import installation
+ from ..editor_export import resolve_source
+ from ..stems import service
+ from . import stems
+ from .routes import stems_supported
+ try:
+ machine = installation()[1]
+ except (OSError, ValueError):
+ machine = {}
+ project = jobs.projects.get(identity)['project']
+ found = [block for block in blocks(project) if block['sound']]
+ if not stems_supported(machine):
+ result['skipped'].update(split='stems_unavailable', music_removed='stems_unavailable')
+ return any(block['parts'] for block in found)
+ waiting = [block['sound'] for block in found if not block['parts'] and block['sound']['volume'] > 0]
+ by_source = {}
+ for clip in waiting:
+ by_source.setdefault(_key(clip), []).append(clip)
+ laid = []
+ for index, (key, clips) in enumerate(by_source.items()):
+ _cancelled(row)
+ try:
+ source = resolve_source(key)['audio']
+ except (OSError, ValueError, KeyError) as error:
+ raise JobError('stems_failed', 'The clip media is not readable', kind='stems') from error
+ if source is None:
+ continue
+ name = '%s/%s-%d-%s' % (stems.FOLDER, row['id'], index, secrets.token_hex(4))
+ out = jobs.port.output_path(name + '/dialogue.mp4', create=True).parent
+ made.append(name)
+ stems._split(jobs, row, service, source, out, lambda step, fraction=None: phase(
+ 'separating %d/%d' % (index + 1, len(by_source)) if step not in ('waiting', 'downloading') else step, fraction),
+ poll)
+ sources = {part: 'generated:%s/%s.mp4' % (name, part) for part in stems.NAMES}
+ for clip in clips:
+ phase('placing')
+ jobs.projects.lay_stems(identity, clip['id'], sources)
+ laid.append(clip['id'])
+ result['split'] = dict(clips=laid, already=[block['sound']['id'] for block in found if block['parts']])
+ return True
+
+
+def score_like(samples, rate):
+ """Whether a separated music part ([2, n] floats) is a sustained score, with the numbers behind it. Separation puts
+ some effects with the music; shots, explosions and crowds are broadband, so they fail the tonal test, and short
+ stings fail the sounding one."""
+ import numpy as np
+ from scipy.ndimage import median_filter
+ mono = np.asarray(samples, np.float64).mean(axis=0)
+ rms = float(np.sqrt(np.mean(mono ** 2))) if mono.size else 0.
+ half = rate // 2
+ windows = mono[:mono.size // half * half].reshape(-1, half)
+ sounding = float(np.mean(np.sqrt(np.mean(windows ** 2, axis=1)) > SCORE_RMS)) if windows.size else 0.
+ size, hop = 4096, 2048
+ tonal = 0.
+ if mono.size >= size:
+ window = np.hanning(size)
+ frames = np.lib.stride_tricks.sliding_window_view(mono, size)[::hop] * window
+ power = np.abs(np.fft.rfft(frames, axis=1)) ** 2 + 1e-14
+ frequency = np.fft.rfftfreq(size, 1 / rate)
+ band = power[:, (frequency > 60) & (frequency < 5000)]
+ lines = (band * (band > 10 * median_filter(band, size=(1, 15)))).sum(axis=1) / band.sum(axis=1)
+ loud = np.sqrt(np.mean(frames ** 2, axis=1) / np.mean(window ** 2)) > SCORE_RMS
+ tonal = float(np.mean(lines[loud] >= TONAL_LINE)) if loud.any() else 0.
+ found = rms >= SCORE_RMS and sounding > SCORE_SOUNDING and tonal > SCORE_TONAL
+ return found, dict(music_rms=round(rms, 4), sounding=round(sounding, 2), tonal=round(tonal, 2))
+
+
+def _separated_music_levels(project, render, result, fresh=None):
+ """Before step 2: for each split block, whether its music part is a score (it goes) or effects (it stays), and the
+ block's level with and without that part and of the part alone. Levels are the K-weighted mean over the block,
+ ungated, so taking a part away can only lower them; the loudest values are the highest 400 ms momentary loudness.
+ Returns the ids of the music parts to silence. fresh: when given, only blocks whose sound clip is in it."""
+ import numpy as np
+ from ..audio_loudness import _block_powers, _lufs
+ tracks = _audio_tracks(project)
+ music = tuple(identity for identity, track in tracks.items() if track.get('stem') == 'music')
+ rest = tuple(identity for identity, track in tracks.items() if track.get('stem') != 'music' and identity != 'a2')
+ found = [block for block in blocks(project) if block['sound'] and block['parts'].get('music')
+ and block['parts']['music']['volume'] > 0 and (fresh is None or block['sound']['id'] in fresh)]
+ if not music or not found:
+ return []
+ before, after, alone = render(project, solo=rest + music), render(project, solo=rest), render(project, solo=music)
+
+ def level(samples):
+ powers = _block_powers(samples, RATE, samples.shape[1] / RATE) if samples.shape[1] else np.zeros(0)
+ return float(_lufs(powers[0])) if powers.size and powers[0] > 0 else -math.inf
+
+ def loudest(samples):
+ powers = _block_powers(samples, RATE, .4)
+ return float(_lufs(powers.max())) if powers.size and powers.max() > 0 else -math.inf
+
+ rows, scores = [], []
+ for block in found:
+ a, b = _span(block)
+ score, numbers = score_like(alone[:, a:b], RATE)
+ if score:
+ scores.append(block['parts']['music']['id'])
+ with_music, without = level(before[:, a:b]), level(after[:, a:b])
+ rows.append(dict(clip_id=block['video']['id'], removed=score, **numbers, with_music_db=_round(with_music),
+ without_music_db=_round(without),
+ drop_db=_round(with_music - without) if math.isfinite(with_music + without) else None,
+ music_db=_round(level(alone[:, a:b])), music_loudest_lufs=_round(loudest(alone[:, a:b])),
+ without_music_loudest_lufs=_round(loudest(after[:, a:b]))))
+ result['music_removed'] = dict(blocks=rows, removed=len(scores), kept=len(rows) - len(scores))
+ return scores
+
+
+def _music(jobs, row, spec, identity, phase, poll, result, made):
+ """Step 3: keep the music on a2, or lay the given piece, or compose one as long as the cut."""
+ from ..music import service
+ from . import music
+ project = jobs.projects.get(identity)['project']
+ track = _audio_tracks(project).get('a2')
+ if track is not None and any(clip.get('source') for clip in track['clips']):
+ result['music'] = dict(kept=[clip['id'] for clip in track['clips'] if clip.get('source')])
+ return
+ asked = spec.get('music') or {}
+ frames = comfy_editor.duration(project)
+ if not asked:
+ result['skipped']['music'] = 'not_asked'
+ return
+ if 'source' in asked:
+ source = asked['source']
+ else:
+ if frames / comfy_editor.FPS > service.MAX_SECONDS:
+ result['skipped']['music'] = 'too_long'
+ return
+ relative = '%s/%s-%s.mp4' % (music.FOLDER, row['id'], secrets.token_hex(4))
+ out = jobs.port.output_path(relative, create=True)
+ made.append(relative)
+ seed = asked.get('seed', secrets.randbelow(2**32))
+ music._compose(jobs, row, service, out, frames, seed, [], phase, poll, caption=asked['caption'])
+ source = 'generated:' + relative
+ result['music_seed'] = seed
+ phase('placing')
+ try:
+ saved, clip_ids = jobs.projects.lay_music(identity, source)
+ except (ValueError, KeyError) as error:
+ raise JobError('invalid_request', 'The music source is not a sound or video file of this installation') from error
+
+ laid = [clip for track in saved['tracks'] for clip in track['clips'] if clip['id'] in clip_ids]
+ skip = 0
+ if len(laid) == 1 and laid[0]['frames'] > frames:
+ # A given piece longer than the cut: start it where its own swells and breaks fall clear of the cuts.
+ try:
+ power = _music_power(source)
+ except Exception: # noqa: BLE001 - unreadable here: start at its beginning, as placed
+ power = None
+ if power is not None:
+ seconds = music_offset(power, [frame / comfy_editor.FPS for frame in cuts(blocks(project))],
+ frames / comfy_editor.FPS)
+ skip = min(round(seconds * comfy_editor.FPS), laid[0]['frames'] - frames)
+
+ def lowered(project):
+ # As on the page: placed music is lowered during speech. A longer piece ends with the cut, faded in where it
+ # starts inside the piece and faded out where it stops inside it, so the cut does not grow.
+ value = copy.deepcopy(project)
+ for track in value['tracks']:
+ for clip in track['clips']:
+ if clip['id'] in clip_ids:
+ clip['duck'] = True
+ if clip['start'] + clip['frames'] > frames:
+ clip['in'] += skip
+ clip['frames'] = max(1, frames - clip['start'])
+ if skip:
+ clip['fade_in'] = min(FADE_IN_FRAMES, clip['frames'] // 2)
+ if clip['in'] + clip['frames'] < clip['length']:
+ clip['fade_out'] = min(FADE_FRAMES, clip['frames'] // 2)
+ return dict(ok=True, project=value)
+ jobs.projects._update(identity, lowered)
+ result['music'] = dict(laid=clip_ids, source=source)
+ if skip:
+ result['music']['starts_at_seconds'] = round(skip / comfy_editor.FPS, 2)
+
+
+def _music_power(source):
+ """Mean square of the piece every 10 ms, at 48 kHz, both channels together."""
+ import av
+ import numpy as np
+ from ..editor_export import resolve_source
+ path = resolve_source(source)['audio']
+ if path is None:
+ return None
+ chunks = []
+ with av.open(str(path)) as container:
+ resampler = av.AudioResampler(format='flt', layout='mono', rate=RATE)
+ for frame in container.decode(audio=0):
+ chunks.extend(out.to_ndarray().reshape(-1) for out in resampler.resample(frame))
+ chunks.extend(out.to_ndarray().reshape(-1) for out in resampler.resample(None) or ())
+ samples = np.concatenate(chunks).astype(np.float64) if chunks else np.zeros(0)
+ hop = RATE // 100
+ count = samples.size // hop
+ return np.mean(np.square(samples[:count * hop].reshape(count, hop)), axis=1)
+
+
+def music_offset(power, cut_seconds, film_seconds, *, hop=.01):
+ """Where in a piece longer than the cut to start it (seconds): the start whose own level changes least across
+ the cuts (RMS 0.4 s each side, 10 ms clear, as cut_steps measures), and among the starts within 1 dB of that, the
+ latest, so the piece's own ending comes as close to the film's end as it can."""
+ import numpy as np
+ span = int(round(film_seconds / hop))
+ room = power.size - span
+ if room <= 0 or not cut_seconds:
+ return max(0, room) * hop
+ total = np.concatenate([[0.], np.cumsum(power)])
+ offsets = np.arange(room + 1)
+ gap, size = round(CUT_GAP_SECONDS / hop), round(CUT_SPAN_SECONDS / hop)
+ floor = 10 ** (-70 / 10)
+ worst = np.zeros(offsets.size)
+ for cut in cut_seconds:
+ at = offsets + int(round(cut / hop))
+ low, high = np.clip(at - gap - size, 0, power.size), np.clip(at - gap, 0, power.size)
+ before = (total[high] - total[low]) / size
+ low, high = np.clip(at + gap, 0, power.size), np.clip(at + gap + size, 0, power.size)
+ after = (total[high] - total[low]) / size
+ worst = np.maximum(worst, np.abs(10 * np.log10(np.maximum(after, floor) / np.maximum(before, floor))))
+ return float(offsets[np.nonzero(worst <= worst.min() + 1.)[0][-1]] * hop)
+
+
+def _set_volumes(jobs, identity, factors):
+ """Multiply clip volumes (clip id -> linear factor), capped at MAX_VOLUME; returns the ids that hit the cap."""
+ capped = set()
+
+ def change(project):
+ value = copy.deepcopy(project)
+ for track in value['tracks']:
+ for clip in track['clips']:
+ if clip['id'] in factors and track['kind'] == 'audio':
+ wanted = clip['volume'] * factors[clip['id']]
+ if wanted > MAX_VOLUME:
+ capped.add(clip['id'])
+ clip['volume'] = round(min(MAX_VOLUME, wanted), 4)
+ return dict(ok=True, project=value)
+ jobs.projects._update(identity, change)
+ return capped
+
+
+def _key(clip):
+ return '%s:%s' % (clip['source']['kind'], clip['source']['id'])
+
+
+def _span(block):
+ clip = block['video']
+ return clip['start'] * SAMPLES_PER_FRAME, (clip['start'] + clip['frames']) * SAMPLES_PER_FRAME
+
+
+def _levels(jobs, row, identity, render, result, *, keep_dialogue, fresh=None):
+ """Step 4. fresh: when given, only blocks whose sound clip is in it get their own gain; all count for the target."""
+ from ..audio_loudness import integrated, true_peak
+ project = jobs.projects.get(identity)['project']
+ found = [block for block in blocks(project) if block['sound']]
+ # A split clip is silent on a1 and an unsplit one has nothing on a4, so together they are the dialogue of each
+ # block (with the whole original sound where it was not split).
+ dialogue = render(project, solo=('a1', 'a4'))
+ # The peak of everything but the score: music parts that stayed are effects and move with the ambience.
+ both = render(project, solo=('a1', 'a4', 'a5', 'a6'))
+ _cancelled(row)
+ loudness, peaks = [], []
+ for block in found:
+ a, b = _span(block)
+ loudness.append(integrated(dialogue[:, a:b], RATE) if b - a >= RATE // 2 else -math.inf)
+ peaks.append(true_peak(both[:, a:b], RATE) if b > a else -math.inf)
+ target, gains = level_gains(loudness, peaks, keep_dialogue=keep_dialogue)
+ factors = {}
+ if fresh is not None:
+ gains = [gain if block['sound']['id'] in fresh else dict(dialogue_db=0., ambience_db=0.)
+ for block, gain in zip(found, gains)]
+ for block, gain in zip(found, gains):
+ speech = block['parts'].get('dialogue') or block['sound']
+ ambience = block['parts'].get('ambience')
+ if speech is not None:
+ factors[speech['id']] = 10 ** (gain['dialogue_db'] / 20)
+ if ambience is not None:
+ factors[ambience['id']] = 10 ** (gain['ambience_db'] / 20)
+ kept = block['parts'].get('music')
+ if kept is not None and kept['volume'] > 0:
+ factors[kept['id']] = 10 ** (gain['ambience_db'] / 20)
+ capped = _set_volumes(jobs, identity, factors)
+ result['blocks'] = [dict(clip_id=block['video']['id'], dialogue_lufs=_round(value), gain_db=round(gain['dialogue_db'], 2),
+ ambience_gain_db=round(gain['ambience_db'], 2))
+ for block, value, gain in zip(found, loudness, gains)]
+ result['dialogue_lufs'] = _round(target)
+ project = jobs.projects.get(identity)['project']
+ music_clips = [clip for clip in _audio_tracks(project).get('a2', {}).get('clips', ()) if clip.get('source')]
+ if music_clips and target is not None:
+ bed = render(project, solo=('a2',))
+ level = integrated(bed, RATE)
+ if math.isfinite(level):
+ gain = target - MUSIC_UNDER_LU - level
+ capped |= _set_volumes(jobs, identity, {clip['id']: 10 ** (gain / 20) for clip in music_clips})
+ result['music_gain_db'] = round(gain, 2)
+ # One factor for every clip, until the whole cut sits at TARGET_LUFS.
+ for _ in range(LEVEL_ROUNDS):
+ _cancelled(row)
+ project = jobs.projects.get(identity)['project']
+ level = integrated(render(project), RATE)
+ if not math.isfinite(level) or abs(level - TARGET_LUFS) <= .2:
+ break
+ factor = 10 ** ((TARGET_LUFS - level) / 20)
+ everything = {clip['id']: factor for track in project['tracks'] if track['kind'] == 'audio'
+ for clip in track['clips'] if clip.get('source') and clip['volume'] > 0}
+ capped |= _set_volumes(jobs, identity, everything)
+
+ def ceiling(project):
+ return dict(ok=True, project=dict(copy.deepcopy(project), master_peak_db=PEAK_DB))
+ jobs.projects._update(identity, ceiling)
+ if capped:
+ result['volume_capped'] = sorted(capped)
+
+
+def _fill(jobs, row, identity, render, port, folder, result, made):
+ """Step 5: room tone into each block's ambience file wherever dialogue and ambience fall to digital silence.
+
+ The tone is the block's own; a block with no steady stretch of it borrows the nearest block's."""
+ import numpy as np
+ from ..editor_export import resolve_source
+ from ..editor_speed import changed as speed_changed
+ from ..stems.separate import SAMPLE_RATE, read_audio, write_mp4
+ project = jobs.projects.get(identity)['project']
+ found = [block for block in blocks(project) if block['parts'].get('ambience') and block['parts'].get('dialogue')]
+ both = render(project, solo=('a4', 'a6'))
+ runs_of = [holes(both, *_span(block)) for block in found]
+ read, tones = {}, {}
+
+ def material(index):
+ """The block's ambience and dialogue sources, its range in them and the timeline-to-source mapping."""
+ if index not in read:
+ block = found[index]
+ ambience, dialogue = block['parts']['ambience'], block['parts']['dialogue']
+ read[index] = None
+ if speed_changed(ambience) or ambience.get('reverse'):
+ return None
+ try:
+ tone_source, _ = read_audio(resolve_source(_key(ambience))['audio'])
+ speech_source, _ = read_audio(resolve_source(_key(dialogue))['audio'])
+ except Exception: # noqa: BLE001 - an unreadable part keeps its silence; the report counts it
+ return None
+
+ def source_at(sample, ambience=ambience):
+ # Timeline sample -> source sample at the separation rate.
+ return round((ambience['in'] * SAMPLES_PER_FRAME + sample - ambience['start'] * SAMPLES_PER_FRAME)
+ * SAMPLE_RATE / RATE)
+ a, b = _span(block)
+ first = source_at(max(a, ambience['start'] * SAMPLES_PER_FRAME))
+ last = min(source_at(min(b, (ambience['start'] + ambience['frames']) * SAMPLES_PER_FRAME)),
+ tone_source.shape[1], speech_source.shape[1])
+ read[index] = dict(tone=tone_source, speech=speech_source, first=first, last=last, at=source_at)
+ return read[index]
+
+ def tone(index):
+ if index not in tones:
+ tones[index] = None
+ found_ = material(index)
+ if found_ is not None and found_['last'] > found_['first']:
+ hop = SAMPLE_RATE // 100
+ quiet = np.zeros((found_['last'] - found_['first']) // hop + 1, bool)
+ for x, y in runs_of[index]:
+ quiet[max(0, (found_['at'](x) - found_['first']) // hop):max(0, (found_['at'](y) - found_['first']) // hop) + 1] = True
+ tones[index] = room_tone(found_['tone'], found_['speech'], SAMPLE_RATE, found_['first'], found_['last'], quiet)
+ return tones[index]
+
+ filled, seconds, unfilled = [], 0., []
+ for index, (block, runs) in enumerate(zip(found, runs_of)):
+ _cancelled(row)
+ if not runs:
+ continue
+ own = material(index)
+ donor = index if own is not None and tone(index) is not None else None
+ if own is not None and donor is None:
+ for distance in range(1, len(found)):
+ donor = next((j for j in (index - distance, index + distance) if 0 <= j < len(found) and tone(j) is not None), None)
+ if donor is not None:
+ break
+ if donor is None:
+ unfilled.append(dict(clip_id=block['video']['id'], holes=len(runs)))
+ continue
+ patched = own['tone'].astype(np.float64)
+ for x, y in runs:
+ p, q = max(own['first'], own['at'](x)), min(own['last'], own['at'](y))
+ if q > p:
+ patched[:, p:q] += tile(tones[donor], q - p, SAMPLE_RATE)
+ seconds += (y - x) / RATE
+ ambience = block['parts']['ambience']
+ relative = '%s/%s.mp4' % (folder, ambience['id'])
+ made.append(relative)
+ write_mp4(port.output_path(relative, create=True), patched.astype(np.float32))
+ jobs.projects.replace_voice_source(identity, ambience['id'], 'generated:' + relative)
+ filled.append(dict(clip_id=block['video']['id'], holes=len(runs), **(
+ {} if donor == index else dict(tone_from=found[donor]['video']['id']))))
+ after = render(jobs.projects.get(identity)['project'], solo=('a4', 'a6'))
+ left = sum(len(holes(after, *_span(block))) for block in found)
+ result['room_tone'] = dict(blocks=filled, seconds=round(seconds, 2), holes_left=left, not_filled=unfilled)
+
+
+def _measure(render, project, result):
+ """The finished cut as exported: loudness, true peak and the steps at each cut."""
+ from ..audio_loudness import integrated, true_peak
+ finished = render(project, master=True)
+ result['loudness_lufs'] = _round(integrated(finished, RATE))
+ result['true_peak_db'] = _round(true_peak(finished, RATE))
+ music = None
+ if any(clip.get('source') for clip in _audio_tracks(project).get('a2', {}).get('clips', ())):
+ # The music as it sounds in the cut, lowered during speech: the mix less the mix without it.
+ music = render(project) - render(project, muted=('a2',))
+ rows = cut_steps(finished, music, cuts(blocks(project)))
+ result['cuts'] = rows
+ result['cut_steps'] = dict(mix_lu=summary(rows, 'mix_step_lu'), music_db=summary(rows, 'music_step_db'))
diff --git a/freevideo_engine/agent/spec.py b/freevideo_engine/agent/spec.py
index 468e495..241a161 100644
--- a/freevideo_engine/agent/spec.py
+++ b/freevideo_engine/agent/spec.py
@@ -300,6 +300,10 @@ def normalize(value, *, port=None):
elif kind == 'stems':
result.update(editor_project=value['editor_project'], revision=value['revision'], clip_id=value['clip_id'],
lay=value.get('lay', True))
+ elif kind == 'finish_sound':
+ result.update(editor_project=value['editor_project'], revision=value['revision'],
+ keep_dialogue_volume=bool(value.get('keep_dialogue_volume', False)),
+ **({'music': dict(value['music'])} if 'music' in value else {}))
elif kind == 'voice_lines':
result.update(editor_project=value['editor_project'], revision=value['revision'], selection=dict(value['selection']),
language=value.get('language', 'auto'), separate_first=bool(value.get('separate_first', False)),
diff --git a/freevideo_engine/audio_loudness.py b/freevideo_engine/audio_loudness.py
new file mode 100644
index 0000000..983577c
--- /dev/null
+++ b/freevideo_engine/audio_loudness.py
@@ -0,0 +1,163 @@
+"""Loudness of a whole cut (ITU-R BS.1770-4 / EBU R128) and a true-peak limiter for export.
+
+Planar float arrays [channels, samples]; channels all weigh 1 (mono and stereo, which is all the editor writes).
+"""
+import math
+
+import numpy as np
+
+ABSOLUTE_GATE = -70.
+RELATIVE_GATE = -10.
+BLOCK_SECONDS, STEP_SECONDS = .4, .1 # 400 ms blocks with 75 % overlap
+SHORT_SECONDS = 3.
+OVERSAMPLE = 4
+
+
+def _k_filters(rate):
+ """The two K-weighting stages (high shelf, then high pass) for any sample rate, as (b, a) pairs."""
+ # Bilinear form of the BS.1770 prototypes (as in libebur128), which reproduces the 48 kHz table exactly.
+ gain, q, fc = 3.999843853973347, .7071752369554196, 1681.974450955533
+ k = math.tan(math.pi * fc / rate)
+ vh = 10 ** (gain / 20)
+ vb = vh ** .4996667741545416
+ shelf_b = [vh + vb * k / q + k * k, 2 * (k * k - vh), vh - vb * k / q + k * k]
+ shelf_a = [1 + k / q + k * k, 2 * (k * k - 1), 1 - k / q + k * k]
+ q, fc = .5003270373238773, 38.13547087602444
+ k = math.tan(math.pi * fc / rate)
+ pass_b = [1., -2., 1.]
+ pass_a = [1., 2 * (k * k - 1) / (1 + k / q + k * k), (1 - k / q + k * k) / (1 + k / q + k * k)]
+ return [(np.array(shelf_b) / shelf_a[0], np.array(shelf_a) / shelf_a[0]), (np.array(pass_b), np.array(pass_a))]
+
+
+def _planar(samples):
+ samples = np.asarray(samples, dtype=np.float64)
+ if samples.ndim == 1:
+ samples = samples[None, :]
+ if samples.ndim != 2 or not np.isfinite(samples).all():
+ raise ValueError('Expected finite planar audio')
+ return samples
+
+
+def _block_powers(samples, rate, seconds, step=STEP_SECONDS):
+ """Mean square of the K-weighted signal, summed over channels, for each window of `seconds` every `step`."""
+ from scipy.signal import lfilter
+ weighted = _planar(samples)
+ for b, a in _k_filters(rate):
+ weighted = lfilter(b, a, weighted, axis=1)
+ size, hop = round(seconds * rate), round(step * rate)
+ if weighted.shape[1] < size:
+ return np.zeros(0)
+ energy = np.concatenate([np.zeros(1), np.cumsum(np.square(weighted).sum(axis=0))])
+ starts = np.arange(0, weighted.shape[1] - size + 1, hop)
+ return (energy[starts + size] - energy[starts]) / size
+
+
+def _lufs(power):
+ return -.691 + 10 * np.log10(np.maximum(power, 1e-20))
+
+
+def integrated(samples, rate):
+ """Gated integrated loudness in LUFS; -inf when nothing is over the absolute gate."""
+ powers = _block_powers(samples, rate, BLOCK_SECONDS)
+ loud = powers[_lufs(powers) > ABSOLUTE_GATE]
+ if not loud.size:
+ return -math.inf
+ gate = _lufs(loud.mean()) + RELATIVE_GATE
+ kept = loud[_lufs(loud) > gate]
+ return float(_lufs(kept.mean()))
+
+
+def short_term(samples, rate, step=STEP_SECONDS):
+ """Short-term loudness (3 s windows) every `step` seconds; value k covers [k*step, k*step + 3 s)."""
+ return _lufs(_block_powers(samples, rate, SHORT_SECONDS, step))
+
+
+def true_peak(samples, rate):
+ """Highest absolute sample after 4x oversampling, in dBTP (-inf for silence)."""
+ from scipy.signal import resample_poly
+ samples = _planar(samples)
+ if not samples.size:
+ return -math.inf
+ peak = float(np.max(np.abs(resample_poly(samples, OVERSAMPLE, 1, axis=1))))
+ return 20 * math.log10(peak) if peak > 0 else -math.inf
+
+
+class Limiter:
+ """Streaming true-peak limiter: the output stays under `ceiling_db` (4x oversampled) and quiet passages pass unchanged.
+
+ `push` returns the samples that are ready and `flush` the rest; together they return exactly the input length, with
+ the output lagging the input inside the limiter. The gain ramps down over the look-ahead before a peak (no step, so
+ no click) and comes back over `release_seconds`."""
+
+ CONTEXT = 16 # input samples each side that the 4x interpolation filter reaches
+
+ def __init__(self, ceiling_db, rate, channels=2, *, lookahead_seconds=.005, release_seconds=.1):
+ # Aim 0.1 dB under the ceiling: the gain moves inside the interpolation filter's reach, which can add a few mdB.
+ self.ceiling = 10 ** ((ceiling_db - .1) / 20)
+ self.window = max(1, round(lookahead_seconds * rate)) | 1
+ self.log_release = -1 / max(1., release_seconds * rate)
+ self.past = np.zeros((channels, self.CONTEXT))
+ self.pending = np.zeros((channels, 0))
+ self.required = np.zeros(0)
+ self.ramp = None # the last window-1 look-ahead minimums; at the start, the first one repeated
+ self.gain = 1.
+
+ def _measure(self, final):
+ """Extend `required` (the gain each pending sample needs on its own) as far as the filter context allows."""
+ from scipy.signal import resample_poly
+ start, size, edge = self.required.size, self.pending.shape[1], self.CONTEXT
+ end = size if final else size - edge
+ if end <= start:
+ return
+ context = np.concatenate([self.past, self.pending], axis=1)[:, start:end + 2 * edge]
+ up = np.abs(resample_poly(context, OVERSAMPLE, 1, axis=1)).max(axis=0)
+ up = np.pad(up, (0, max(0, (end - start + edge) * OVERSAMPLE - up.size)))
+ peaks = up[edge * OVERSAMPLE:(end - start + edge) * OVERSAMPLE].reshape(end - start, OVERSAMPLE).max(axis=1)
+ peaks = np.maximum(peaks, np.abs(self.pending[:, start:end]).max(axis=0))
+ self.required = np.concatenate([self.required, np.minimum(1., self.ceiling / np.maximum(peaks, 1e-12))])
+
+ def push(self, block):
+ block = _planar(block)
+ self.pending = np.concatenate([self.pending, block], axis=1)
+ self._measure(False)
+ return self._emit(max(0, self.required.size - (self.window - 1)))
+
+ def flush(self):
+ self._measure(True)
+ return self._emit(self.pending.shape[1])
+
+ def _release(self, target):
+ """g[i] = min(target[i], 1 - (1 - g[i-1]) * r), in closed form chunk by chunk."""
+ out = np.empty_like(target)
+ held = 1 - self.gain
+ chunk = max(1, int(30 / -self.log_release))
+ for start in range(0, target.size, chunk):
+ dip = np.maximum(1 - target[start:start + chunk], 0.)
+ steps = np.arange(dip.size) * self.log_release
+ with np.errstate(divide='ignore'):
+ depth = np.exp(np.maximum.accumulate(np.log(dip) - steps) + steps)
+ depth = np.maximum(depth, held * np.exp(steps + self.log_release))
+ out[start:start + dip.size] = 1 - depth
+ held = depth[-1]
+ self.gain = 1 - held
+ return out
+
+ def _emit(self, count):
+ from scipy.ndimage import minimum_filter1d
+ channels = self.pending.shape[0]
+ if count <= 0:
+ return np.zeros((channels, 0))
+ half = self.window // 2
+ need = np.pad(self.required, (0, max(0, count + self.window - 1 - self.required.size)), constant_values=1.)
+ lowest = minimum_filter1d(need[:count + self.window - 1], self.window, mode='nearest')[half:half + count]
+ if self.ramp is None:
+ self.ramp = np.full(self.window - 1, lowest[0])
+ joined = np.concatenate([self.ramp, lowest])
+ total = np.concatenate([[0.], np.cumsum(joined)])
+ target = (total[self.window:] - total[:-self.window])[:count] / self.window
+ self.ramp = joined[-(self.window - 1):] if self.window > 1 else self.ramp
+ out = self.pending[:, :count] * self._release(target)[None, :]
+ self.past = np.concatenate([self.past, self.pending[:, :count]], axis=1)[:, -self.CONTEXT:]
+ self.pending = self.pending[:, count:]
+ self.required = self.required[count:]
+ return out
diff --git a/freevideo_engine/comfy_editor.py b/freevideo_engine/comfy_editor.py
index 767d598..f862d6b 100644
--- a/freevideo_engine/comfy_editor.py
+++ b/freevideo_engine/comfy_editor.py
@@ -334,6 +334,12 @@ def validate(record):
raise ValueError('Invalid empty edit retention')
if record['keep_empty']:
result['keep_empty'] = True
+ if 'master_peak_db' in record:
+ # The export's true-peak ceiling, set by the finishing step; without it the export only clips at full scale.
+ peak = record['master_peak_db']
+ if type(peak) not in (int, float) or not math.isfinite(peak) or not -12 <= peak <= 0:
+ raise ValueError('Invalid master peak')
+ result['master_peak_db'] = peak
result.update(marker_fields(record))
if 'replacements' in record:
replacements = record['replacements']
diff --git a/freevideo_engine/editor_export.py b/freevideo_engine/editor_export.py
index b85ba97..a5fa123 100644
--- a/freevideo_engine/editor_export.py
+++ b/freevideo_engine/editor_export.py
@@ -27,6 +27,7 @@ from .comfy_continuation import audio_lead_in, apply_audio_lead_in
from .editor_limits import PROJECT_BYTES
from .editor_text import is_text_track, has_text_data, validate_text_project, paint_project, check_export_text, MissingGlyphs
from . import editor_export_options
+from .audio_loudness import Limiter
from .editor_speed import validate_clip, changed as speed_changed, audio_changed, independent_join, VideoReader, AudioReader
from .editor_audio import (clip_fields, track_fields, clip_audible, track_audible, crossfade_reads, apply_track_audio,
duck_ranges, duck_gain, duck_sources)
@@ -663,6 +664,9 @@ def _render_once(project, resolve, target, *, progress, cancelled, options, repa
try:
width, height, total, tracks = _layout(project)
settings = editor_export_options.effective(options, width, height, total)
+ peak = project.get('master_peak_db')
+ if peak is not None and (type(peak) not in (int, float) or not math.isfinite(peak) or not -12 <= peak <= 0):
+ raise ExportError()
width, height, total = settings.width, settings.height, settings.frames
if settings.only == 'sound':
tracks = {key: clips for key, clips in tracks.items() if key != 'v1'}
@@ -737,7 +741,32 @@ def _render_once(project, resolve, target, *, progress, cancelled, options, repa
readers.callback(lambda: [decoder.close() for decoder in decoders.values()])
black = np.zeros((height, width, 3), dtype=np.uint8) if video is not None else None
written = 0
+
+ def encode_sound(block, number):
+ samples, sample_format = editor_export_options.sound_samples(block, settings)
+ sound = _audio_frame(samples, format=sample_format, layout='stereo')
+ sound.sample_rate = SAMPLE_RATE
+ sound.pts, sound.time_base = (number - settings.start) * SAMPLES_PER_FRAME, Fraction(1, SAMPLE_RATE)
+ for packet in audio.encode(sound):
+ output.mux(packet)
+ if video is None and progress:
+ progress((number - settings.start + 1) / total * .99)
+
+ # With a master ceiling the mix goes through a true-peak limiter, whose output lags its input;
+ # its frames are encoded by their own timeline position.
+ limiter = Limiter(peak, SAMPLE_RATE) if peak is not None else None
+ limited, limited_frame = np.zeros((2, 0)), None
+
+ def drain(final):
+ nonlocal limited, limited_frame
+ while limited.shape[1] >= SAMPLES_PER_FRAME or (final and limited.shape[1]):
+ block = np.clip(limited[:, :SAMPLES_PER_FRAME], -1., 1.).astype(np.float32)
+ limited = limited[:, SAMPLES_PER_FRAME:]
+ if limited_frame >= settings.start:
+ encode_sound(block, limited_frame)
+ limited_frame += 1
lead_start = editor_export_options.audio_lead_start(settings.start, tracks)
+ limited_frame = lead_start
for number in range(lead_start, settings.start + total):
_check(cancelled)
for track, clips in editor_export_options.frame_tracks(tracks, settings.start, number):
@@ -813,17 +842,17 @@ def _render_once(project, resolve, target, *, progress, cancelled, options, repa
mixed += apply_track_audio(track_block, number * SAMPLES_PER_FRAME, audio_tracks[track],
project['tracks'], extras=extras)
del extras
+ if limiter is not None:
+ limited = np.concatenate([limited, limiter.push(mixed)], axis=1)
+ drain(False)
+ continue
np.clip(mixed, -1., 1., out=mixed)
if number < settings.start:
continue
- samples, sample_format = editor_export_options.sound_samples(mixed, settings)
- sound = _audio_frame(samples, format=sample_format, layout='stereo')
- sound.sample_rate = SAMPLE_RATE
- sound.pts, sound.time_base = (number - settings.start) * SAMPLES_PER_FRAME, Fraction(1, SAMPLE_RATE)
- for packet in audio.encode(sound):
- output.mux(packet)
- if video is None and progress:
- progress((number - settings.start + 1) / total * .99)
+ encode_sound(mixed, number)
+ if limiter is not None:
+ limited = np.concatenate([limited, limiter.flush()], axis=1)
+ drain(True)
_check(cancelled)
for stream in (video, audio):
if stream is None:
diff --git a/web/editor_model.js b/web/editor_model.js
index 76b2a2c..3a78f1d 100644
--- a/web/editor_model.js
+++ b/web/editor_model.js
@@ -219,6 +219,11 @@ export function parse(value) {
result.size_mode = value.size_mode ?? 'first';
check(value.keep_empty === undefined || typeof value.keep_empty === 'boolean', 'invalid');
if (value.keep_empty) result.keep_empty = true;
+ // The export's true-peak ceiling (dBTP), written by the finishing step.
+ if (value.master_peak_db !== undefined) {
+ check(typeof value.master_peak_db === 'number' && Number.isFinite(value.master_peak_db) && value.master_peak_db >= -12 && value.master_peak_db <= 0, 'invalid');
+ result.master_peak_db = value.master_peak_db;
+ }
Object.assign(result, markerFields(value));
if (value.replacements !== undefined) {
check(Array.isArray(value.replacements) && value.replacements.length > 0 && value.replacements.length <= RULES.maxClips, 'invalid');