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.
Record audio in the Edit view: record against the picture and place the take on a sound track...
Bowen Xuea31f23d50293
diff --git a/freevideo_engine/comfy_editor.py b/freevideo_engine/comfy_editor.pyindex 35d8ba0..abde354 100644--- a/freevideo_engine/comfy_editor.py+++ b/freevideo_engine/comfy_editor.py@@ -187,9 +187,13 @@ def validate(record):if not _text(json.dumps(settings, ensure_ascii=False, allow_nan=False, separators=(',', ':')), 1024 * 1024):raise ValueError('Settings are too large')raw_tracks = record.get('tracks')- expected = [('v1', 'video'), ('a1', 'audio'), ('a2', 'audio'), ('a3', 'audio'), ('a4', 'audio'), ('a5', 'audio'), ('a6', 'audio')]- if not isinstance(raw_tracks, list) or len(raw_tracks) not in (2, 3, 7):+ # v1 and a1, then an optional second sound track (a2), the recording sub-track under a1 (a3) and, only together with both, the three separated-sound tracks (a4-a6), in that order.+ if not isinstance(raw_tracks, list) or len(raw_tracks) not in (2, 3, 4, 7):raise ValueError('Invalid tracks')+ later = [raw.get('id') if isinstance(raw, dict) else None for raw in raw_tracks[2:]]+ if later not in ([], ['a2'], ['a3'], ['a2', 'a3']):+ raise ValueError('Invalid tracks')+ expected = [('v1', 'video'), ('a1', 'audio')] + [(track_id, 'audio') for track_id in later]tracks, seen = [], {}for raw_track, (track_id, kind) in zip(raw_tracks, expected):if (not isinstance(raw_track, dict) or raw_track.get('id') != track_id or raw_track.get('kind') != kinddiff --git a/freevideo_engine/editor_export.py b/freevideo_engine/editor_export.pyindex 9517c45..b85ba97 100644--- a/freevideo_engine/editor_export.py+++ b/freevideo_engine/editor_export.py@@ -113,7 +113,7 @@ def _layout(project):project = validate(project)raw_tracks = [t for t in project['tracks'] if not is_text_track(t)]if not isinstance(raw_tracks, list) or [t.get('id') for t in raw_tracks if isinstance(t, dict)] not in (- ['v1', 'a1'], ['v1', 'a1', 'a2'], ['v1', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6']):+ ['v1', 'a1'], ['v1', 'a1', 'a2'], ['v1', 'a1', 'a3'], ['v1', 'a1', 'a2', 'a3'], ['v1', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6']):raise ExportError()tracks, seen = {}, set()for raw in raw_tracks:diff --git a/web/editor.css b/web/editor.cssindex 576689e..9bb0997 100644--- a/web/editor.css+++ b/web/editor.css@@ -209,7 +209,8 @@.fv-editor .fv-ed-name-menu .fv-ed-delete-confirm{color:var(--fv-danger)}.fv-editor .fv-ed-name-menu .fv-ed-delete-confirm:not(:disabled):is(:hover,:focus-visible){background:var(--fv-danger-subtle)}.fv-ed-delete-question{margin:6px 10px 10px;line-height:1.5;overflow-wrap:anywhere;white-space:normal}-.fv-ed-notice{position:absolute;bottom:340px;right:16px;max-width:min(560px,calc(100vw - 32px));border:1px solid var(--fv-border);background:var(--fv-card);padding:10px 14px;border-radius:8px;z-index:9;color:var(--fv-ink)}+.fv-ed-notice{position:absolute;bottom:362px;right:300px;max-width:min(560px,calc(100vw - 32px));border:1px solid var(--fv-border);background:var(--fv-card);padding:10px 14px;border-radius:8px;z-index:9;color:var(--fv-ink)}+@media(min-width:681px) and (max-width:1279px){.fv-ed-notice{right:32px;max-width:min(560px,calc(100vw - 330px))}}.fv-ed-notice .fv-issue-button{margin-left:12px;vertical-align:middle;pointer-events:auto}@media(max-width:680px){.fv-studio.fv-editor{grid-template-columns:minmax(0,1fr);grid-template-rows:auto minmax(max(0px,calc(180px - var(--fv-ed-description-growth,0px))),1fr) 40px var(--fv-ed-generation-row,auto) 40px 236px;overflow:hidden}diff --git a/web/editor.js b/web/editor.jsindex f4dcfb1..b64a124 100644--- a/web/editor.js+++ b/web/editor.js@@ -26,6 +26,7 @@ import {createStemsIntegration} from './editor_stems.js';import {createSpeedComponent, readSpeedAudioSamples, handleSpeedKey, speedShortcuts} from './editor_speed.js';import {createEditorGenbar, loadEditorComponents, editorBlockStates, blockMessages} from './editor_genbar.js';import {createEditorMusic} from './editor_music.js';+import {createEditorRecord, RECORD_ICON} from './editor_record.js';import {MAX_MUSIC_TEXT as MUSIC_TEXT_LIMIT} from './editor_music_model.js';import {createProgressConnection} from './progress_connection.js';import {createSelectionActions, deletionStatus, isMultiple, isTyping, playheadIds, removableGaps, removeGaps} from './editor_select.js';@@ -457,7 +458,7 @@ export function openEditor({api, app, from = null, panel = {}, components: given// The nearest spot at or after the chosen one where a part of the same length fits.function emptySpot(selection) {if (!['range', 'gap'].includes(selection?.kind)) return null;- const range = selectionRange(selection), clips = state.record.tracks.filter(track => ['v1', 'a1', 'a2'].includes(track.id)).flatMap(track => track.clips);+ const range = selectionRange(selection), clips = state.record.tracks.filter(track => ['v1', 'a1', 'a2', 'a3'].includes(track.id)).flatMap(track => track.clips);const starts = [range.start, ...clips.map(clip => clip.start + clip.frames).filter(at => at > range.start)].sort((a, b) => a - b);for (const start of starts) {if (clips.some(clip => clip.start < start + range.frames && clip.start + clip.frames > start)) continue;@@ -470,7 +471,7 @@ export function openEditor({api, app, from = null, panel = {}, components: givenfunction flushSpot(selection) {if (selection?.kind !== 'range') return null;const range = selectionRange(selection), most = model.RULES.minSeconds * model.FPS;- const clips = state.record.tracks.filter(track => ['v1', 'a1', 'a2'].includes(track.id)).flatMap(track => track.clips);+ const clips = state.record.tracks.filter(track => ['v1', 'a1', 'a2', 'a3'].includes(track.id)).flatMap(track => track.clips);const end = Math.max(-1, ...state.record.tracks[0].clips.map(clip => clip.start + clip.frames).filter(at => at < range.start));if (end < 0 || range.start - end >= most || clips.some(clip => clip.start < range.start && clip.start + clip.frames > end)) return null;const moved = {kind: 'range', start: end, frames: range.frames, tracks: ['v1', 'a1']};@@ -1401,7 +1402,8 @@ export function openEditor({api, app, from = null, panel = {}, components: givenfinally { if (!disposed) updateSecondPass(); }}actions.runSecondPass = runSecondPass;- historyTools.append(toolUndo, toolRedo); editTools.append(split, remove, closeGap, previous);+ const recordTool = toolButton(t('Record', '录音'), RECORD_ICON, () => {}, 'fv-ed-record-toggle'); recordTool.setAttribute('aria-pressed', 'false'); recordTool.dataset.tourId = 'record-entry';+ historyTools.append(toolUndo, toolRedo); editTools.append(split, remove, closeGap, previous, recordTool);const pictureTools = createPictureTools({state, sources: store.sources, t, capabilities: () => capability, error});toolbar.append(historyTools, editTools, zoomHost); element.insertBefore(toolbar, tracksHost);const transportControls = createEditorTransport({element, preview, transport, toolbar, editTools, zoomHost, state, player, t, notify});@@ -1413,6 +1415,8 @@ export function openEditor({api, app, from = null, panel = {}, components: givenconst audio = createAudioIntegration({state, player, t, error, adjust, library: libraryHost, store});stems = createStemsIntegration({state, api, queue, store, t, error, notify, toolbar, show: job => genbar?.update(job),restore: () => genbar?.update(mostRecentJob ? barJob(jobStatuses.get(mostRecentJob)) : null)});+ const recording = createEditorRecord({state, player, t, error, notify, button: recordTool, bar: element, library: {importFiles: (files, options) => actions.importFiles(files, options)}});+ element.insertBefore(recording.element, tracksHost);registerEditorShortcuts(AUDIO_SHORTCUTS, speedShortcuts);adjust.register('speed', createSpeedComponent({state, t, onError: error}));function selectedContent() {@@ -1552,6 +1556,7 @@ export function openEditor({api, app, from = null, panel = {}, components: givenif (tracks?.cancelGesture()) { event.preventDefault(); return; }if (document.fullscreenElement) { event.preventDefault(); document.exitFullscreen().catch(() => {}); return; }}+ if (recording.keyboard(event)) return;if (inside && event.target.closest?.('.fv-ed-refs')) {// The reference pictures' own buttons and menu keep their keys: no timeline, audio or transport shortcut.for (let target = event.target; target && target !== element; target = target.parentNode) target.onkeydown?.(event);@@ -1619,7 +1624,7 @@ export function openEditor({api, app, from = null, panel = {}, components: givenawait store.activateProject(null).catch(() => {});rememberView(); disposed = true; music.dispose(); guide?.dispose(); transportControls.dispose(); toolbarMore.dispose(); player.dispose();textTools.dispose(); adjust.dispose(); notes.dispose(); pictureEdit.dispose();- audio.dispose(); stems?.dispose(); headerFit.dispose();+ audio.dispose(); stems?.dispose(); recording.dispose(); headerFit.dispose();listeners.splice(0).forEach(remove => remove());for (const node of [element, ...element.querySelectorAll('*')]) for (const name of ['onclick', 'ondblclick', 'oninput', 'onchange', 'onkeydown', 'ondragstart']) node[name] = null;unsubscribe(); stopSources(); library?.dispose(); genbar.dispose(); tracks?.dispose(); stopExport?.();diff --git a/web/editor_audio.css b/web/editor_audio.cssindex 498e0f5..0a64fc8 100644--- a/web/editor_audio.css+++ b/web/editor_audio.css@@ -3,8 +3,8 @@.fv-ed-audio-panel [hidden]{display:none!important}.fv-ed-track-view .fv-ed-track-head{justify-content:space-between;gap:4px;padding:0 6px}.fv-ed-track-head>svg{display:none}-.fv-ed-track-head>span:not(.fv-ed-audio-track-controls){white-space:nowrap}-.fv-ed-audio-track-controls{display:flex;gap:1px}+.fv-ed-track-head>span:not(.fv-ed-audio-track-controls){white-space:nowrap;min-width:0;overflow:hidden;text-overflow:ellipsis}+.fv-ed-audio-track-controls{display:flex;gap:1px;flex:none}.fv-ed-track-view .fv-ed-audio-track-switch{display:grid;place-items:center;padding:2px;width:24px;height:28px;border:0;border-radius:4px;background:transparent;opacity:.5;cursor:pointer}.fv-ed-audio-track-switch:hover,.fv-ed-audio-track-switch[aria-pressed=true]{opacity:1!important}.fv-ed-audio-track-switch[aria-pressed=true]{color:var(--fv-accent)!important}diff --git a/web/editor_audio.js b/web/editor_audio.jsindex 40c79ac..305b2f6 100644--- a/web/editor_audio.js+++ b/web/editor_audio.js@@ -238,7 +238,10 @@ export function attachAudioTracks(element, {state, t = en => en, actions = {}})for (const name of track.kind === 'audio' ? ['muted', 'solo', 'locked'] : ['muted', 'locked']) {const active = !!track[name], button = el('button', 'fv-ed-audio-track-switch'); button.type = 'button'; button.dataset.flag = name; button.dataset.track = track.id;const labels = {muted: active ? t('Unmute', '取消静音') : t('Mute this track', '静音这条轨道'), solo: active ? t('Hear all tracks', '恢复全部轨道') : t('Solo this track', '只听这条轨道'), locked: active ? t('Unlock', '解除锁定') : t('Lock this track', '锁定这条轨道')};- button.title = labels[name]; button.setAttribute('aria-label', button.title); button.setAttribute('aria-pressed', String(active));+ button.title = labels[name];+ // The recording track is mixed like the others but is only ever heard or muted: solo and lock stay in place, unavailable.+ if (track.id === 'a3' && name !== 'muted') { button.disabled = true; button.title = name === 'solo' ? t('Not available on the recording track', '录音轨不能单独试听') : t('Not available on the recording track', '录音轨不能锁定'); }+ button.setAttribute('aria-label', button.title); button.setAttribute('aria-pressed', String(active));const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'), path = document.createElementNS(svg.namespaceURI, 'path'); svg.setAttribute('viewBox', '0 0 16 16'); svg.setAttribute('aria-hidden', 'true'); path.setAttribute('d', paths[name]); svg.append(path); button.append(svg);// Switches are view settings: they change every snapshot alike and never enter the undo history.const activate = () => { try { state.settle(record => sound.setTrackFlag(record, track.id, name, !active)); } catch (error) { actions.editError?.(error); } };@@ -337,6 +340,8 @@ export function createSpeechSpans(fetchSpans, {retry = 1000} = {}) {};return {map, want(keys) { keys.forEach(ask); }, get version() { return version; }, dispose() { disposed = true; }};}+// A picture's own sound: still joined to it, or taken from it and kept.+export const isOriginalSound = clip => !!clip.link || !!clip.detached_from;export function createAudioPreview({state, sources, api, volumePreview = new Map(), readSamples = null, error = () => {}, speech = null}) {let context = null, generation = 0, timer = null, identity = null, pending = false, disposed = false;const cache = new Map(), nodes = new Set(); let origin = 0, cursor = 0, previousPosition = 0;@@ -422,10 +427,12 @@ export function createAudioPreview({state, sources, api, volumePreview = new Mapfinally { pending = false; }}return {- render({record, position, playing, playbackRate = 1}) {+ render({record, position, playing, playbackRate = 1, silenceOriginal = false}) {speech?.want(sound.duckSources(record.tracks));if (!edited(record)) { if (identity) stop(); return false; }if (volumePreview.size) record = {...record, tracks: record.tracks.map(track => ({...track, clips: track.clips.map(clip => volumePreview.has(clip.id) ? {...clip, volume: volumePreview.get(clip.id)} : clip)}))};+ // While a take is recorded the picture's own sound stays out of the mix.+ if (silenceOriginal) record = {...record, tracks: record.tracks.map(track => ({...track, clips: track.clips.filter(clip => !isOriginalSound(clip))}))};if (!playing) { stop(); previousPosition = position; return true; }if (playbackRate !== 1) {const unavailable = `shuttle:${playbackRate}`;diff --git a/web/editor_library.js b/web/editor_library.jsindex 2500814..ce6f383 100644--- a/web/editor_library.js+++ b/web/editor_library.js@@ -218,13 +218,13 @@ export function createEditorLibrary(host, {store, t, actions, state}) {if (row?.state !== 'ready') continue;const value = metadata(row, 'import'), at = head.place.at;if (value.hasVideo) head.place.at += value.frames;- const insert = {source: {kind: 'import', id: row.id}, at, frames: value.frames, hasVideo: value.hasVideo, hasAudio: value.hasAudio};+ const insert = {source: {kind: 'import', id: row.id}, at, frames: value.frames, hasVideo: value.hasVideo, hasAudio: value.hasAudio, ...(!value.hasVideo && head.place.track ? {targetTrack: head.place.track, replace: head.place.replace === true} : {})};placed = placed.then(() => disposed ? null : actions.insertSource(insert, {loadMetadata: true})).catch(actions.error);}}- async function importFiles(files, {at} = {}) {+ async function importFiles(files, {at, track, replace = false} = {}) {choose('import');- const place = Number.isSafeInteger(at) && at >= 0 ? {at} : null;+ const place = Number.isSafeInteger(at) && at >= 0 ? {at, ...(['a1', 'a2', 'a3'].includes(track) ? {track, replace} : {})} : null;for (const value of files) {if (disposed) break;const id = 'upload-' + Math.random(), row = {id, name: value.name, kind: value.type.startsWith('audio') ? 'audio' : 'video', state: 'preparing', fraction: 0, sending: true};diff --git a/web/editor_lifecycle.js b/web/editor_lifecycle.jsindex 4054e09..561d2f6 100644--- a/web/editor_lifecycle.js+++ b/web/editor_lifecycle.js@@ -7,7 +7,7 @@ export function discardEmptyEdit(record, initial, {busy = false, hasMedia = fals|| record.tracks.some(track => track.clips.length) || record.replacements?.length) return false;// Compare settings, track controls and sparse project fields with initial().// An untouched empty second sound track does not make an edit differ from a new one.- const bare = track => track.id === 'a2' && !track.clips.length && Object.keys(track).every(key => ['id', 'kind', 'clips'].includes(key));+ const bare = track => ['a2', 'a3'].includes(track.id) && !track.clips.length && Object.keys(track).every(key => ['id', 'kind', 'clips'].includes(key));const content = value => {const {id, revision, serial, name, ...rest} = value;return {...rest, tracks: rest.tracks.filter(track => !bare(track))};diff --git a/web/editor_menu.js b/web/editor_menu.jsindex 7bc6b84..e0420b3 100644--- a/web/editor_menu.js+++ b/web/editor_menu.js@@ -5,6 +5,7 @@ import {clickSelection, createSelectionActions, isTyping, selectedIds, playheadIconst registered = new Set();// Groups appear in first-registration order; matching groups stay together.// Built-ins use 'edit' and 'split'. Return the disposer when unloading an extension.+// visible(context), when given, leaves the item out of menus it does not belong in.// label/shortcut accept a string or (context) => string, so extensions can use context.t.export function registerMenuItem(item) {if (!['part', 'empty'].includes(item.where) || item.group == null || !item.label || typeof item.run !== 'function')@@ -52,7 +53,7 @@ export function menuItems(context) {if (preview && context.actions.runSecondPass) items.push({group: 'generate', label: t('Run second pass', '继续二采'), shortcut: '',enabled: !context.actions.generationSending?.(), run: () => context.actions.runSecondPass(context.state.selection)});const value = field => typeof field === 'function' ? field(context) : field;- for (const item of registered) if (item.where === context.where) items.push({...item,+ for (const item of registered) if (item.where === context.where && (!item.visible || item.visible(context))) items.push({...item,label: value(item.label), shortcut: value(item.shortcut) || '', enabled: item.enabled ? !!item.enabled(context) : true,run: () => item.run(context)});const groups = new Map();diff --git a/web/editor_model.js b/web/editor_model.jsindex 0678e2f..3838930 100644--- a/web/editor_model.js+++ b/web/editor_model.js@@ -124,6 +124,7 @@ export function addAudioTrack(record) {const next = serialize(record);check(!next.tracks.some(track => track.id === 'a2'), 'track');next.tracks.push({id: 'a2', kind: 'audio', clips: []});+ next.tracks.sort((a, b) => TRACK_ORDER.indexOf(a.id) - TRACK_ORDER.indexOf(b.id));return parse(next);}export function removeAudioTrack(record) {@@ -148,11 +149,13 @@ export function parse(value) {const settings = jsonData(value.settings);check(JSON.stringify(settings).length <= 1024 * 1024, 'settings');check(Array.isArray(value.tracks) && value.tracks.length >= 2 && value.tracks.length <= RULES.maxTracks- && value.tracks.length !== 4 && value.tracks.length !== 5 && value.tracks.length !== 6, 'track');+ && value.tracks.length !== 5 && value.tracks.length !== 6, 'track');const ids = new Set();const tracks = value.tracks.map((raw, index) => {- const id = ['v1', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6'][index], kind = index === 0 ? 'video' : 'audio';+ // v1 and a1 by position; then an optional second sound track, the recording sub-track under a1 and the three separated-sound tracks, in that order.+ const id = index < 2 ? ['v1', 'a1'][index] : object(raw) ? raw.id : null, kind = index === 0 ? 'video' : 'audio';check(object(raw) && raw.id === id && raw.kind === kind, 'track');+ check(index < 2 || LATER_TRACKS.some(later => later.length === value.tracks.length - 2 && later[index - 2] === id), 'track');// The three tracks after a3 are the separated sound of one clip each: all of them or none.check(index < 4 ? raw.stem === undefined : raw.stem === STEM_TRACKS[index - 4], 'track');check(Array.isArray(raw.clips) && raw.clips.length <= RULES.maxClips, 'track');@@ -344,8 +347,12 @@ function rollback(record, id, pending, reason = null) {tx.rolledBack = true;return finish(next);}+const TRACK_ORDER = ['v1', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6'];function audioTrack(record, id = 'a1') {- if (!record.tracks.some(track => track.id === id)) record.tracks.push({id, kind: 'audio', clips: []});+ if (!record.tracks.some(track => track.id === id)) {+ record.tracks.push({id, kind: 'audio', clips: []});+ record.tracks.sort((a, b) => TRACK_ORDER.indexOf(a.id) - TRACK_ORDER.indexOf(b.id));+ }return trackBy(record, id);}const freshId = record => `c${++record.serial}`;@@ -483,15 +490,27 @@ function storedSize(record) {return size;}// A saved video brings the description it was generated from; one that cannot be kept is left out.-export function insertSource(record, {source, frames, hasVideo, hasAudio, width, height, at: position = 0, text = '', by = 'user', targetTrack}) {+// Empties [start, start + frames) on one track by cutting the clips that cross its edges and removing what lies inside; the rest stays.+function clearStretch(record, trackId, start, frames) {+ let next = record;+ for (const frame of [start, start + frames]) {+ const hit = trackBy(next, trackId).clips.find(clip => clip.start < frame && end(clip) > frame);+ if (hit) next = split(next, hit.id, frame);+ }+ const inside = trackBy(next, trackId).clips.filter(clip => clip.start >= start && end(clip) <= start + frames).map(clip => clip.id);+ return inside.length ? remove(next, inside) : next;+}+export function insertSource(record, {source, frames, hasVideo, hasAudio, width, height, at: position = 0, text = '', by = 'user', targetTrack, replace = false}) {const given = source; source = sourceData(source); check(whole(frames, 1) && whole(position), 'range');check(typeof hasVideo === 'boolean' && typeof hasAudio === 'boolean' && (hasVideo || hasAudio), 'source');check(TEXT_BY.includes(by), 'text');const words = hasVideo && typeof text === 'string' && validText(text.trim()) ? text.trim() : '';+ // A take that is recorded again over an older one replaces what it covers, at the same place as the picture.+ if (replace && !hasVideo && targetTrack === 'a3' && record.tracks.some(row => row.id === 'a3')) record = clearStretch(record, 'a3', position, frames);const next = copy(record);if (hasVideo) followFirstSize(next, width, height);const explicitAudioTrack = !hasVideo && targetTrack !== undefined;- if (explicitAudioTrack) check(['a1', 'a2'].includes(targetTrack), 'track');+ if (explicitAudioTrack) check(['a1', 'a2', 'a3'].includes(targetTrack), 'track');let track = explicitAudioTrack ? audioTrack(next, targetTrack) : trackBy(next, hasVideo ? 'v1' : 'a1');if (!hasVideo && !explicitAudioTrack && track.clips.some(clip => overlaps(clip, position, frames))) track = audioTrack(next, 'a2');check(!explicitAudioTrack || !track.locked, 'locked');@@ -508,12 +527,14 @@ export function insertSource(record, {source, frames, hasVideo, hasAudio, width,}const result = finish(next);// The description is a convenience: a project it would make too large to save takes the video without it.- if (words && storedSize(result) > STORED_ROOM) return insertSource(record, {source: given, frames, hasVideo, hasAudio, width, height, at: position, targetTrack});+ if (words && storedSize(result) > STORED_ROOM) return insertSource(record, {source: given, frames, hasVideo, hasAudio, width, height, at: position, targetTrack, replace});return result;}// The separated sound of an audio clip: three tracks after a2 hold one clip each (dialogue, music, ambience)// over the parent's range, and the parent goes silent. Deleting every stem clip gives the parent its volume back// (normalizeAudioEdits), so the silence needs no separate undo.+// The tracks after a1, in order: a second sound track, the recording sub-track, and the separated sound that needs both.+const LATER_TRACKS = [[], ['a2'], ['a3'], ['a2', 'a3'], ['a2', 'a3', 'a4', 'a5', 'a6']];export const STEM_TRACKS = Object.freeze(['dialogue', 'music', 'ambience']);export function placeStems(record, id, sources) {const next = copy(record), parent = find(next, id);@@ -545,7 +566,7 @@ export function move(record, id, start, {targetTrack} = {}) {let track = trackOf(next, primary.id);// Only independent audio changes rows; linked pairs keep automatic routing.if (clips.length === 1 && track.kind === 'audio' && targetTrack !== undefined) {- check(['a1', 'a2'].includes(targetTrack), 'track');+ check(['a1', 'a2', 'a3'].includes(targetTrack), 'track');assertUnlocked(record, [primary.id]);track = audioTrack(next, targetTrack);check(!track.locked, 'locked');@@ -975,7 +996,7 @@ export function planRange(record, {start, frames, parts, target = 'both', text =if (!record.tracks.some(track => track.clips.some(clip => overlaps(clip, start, frames)))) frames = Math.max(RULES.minSeconds, Math.round(frames / FPS)) * FPS;const videoHits = trackBy(record, 'v1').clips.filter(clip => overlaps(clip, start, frames));const audioHits = trackBy(record, 'a1').clips.filter(clip => overlaps(clip, start, frames));- const otherHits = record.tracks.find(track => track.id === 'a2')?.clips.filter(clip => overlaps(clip, start, frames)) || [];+ const otherHits = record.tracks.filter(track => track.id === 'a2' || track.id === 'a3').flatMap(track => track.clips.filter(clip => overlaps(clip, start, frames)));const hits = [...videoHits, ...audioHits];if (hits.length || otherHits.length) {check(hits.length > 0, 'unsupported');diff --git a/web/editor_player.js b/web/editor_player.jsindex 83d3ed1..b10d2bf 100644--- a/web/editor_player.js+++ b/web/editor_player.js@@ -5,7 +5,7 @@ import {createPreviewPicture} from './editor_preview_picture.js';import {sourceKey, mediaURL} from './editor_store.js';import {clipPictureSize, createPictureGuide} from './editor_frame.js';import {createPlaybackClock} from './editor_transport_model.js';-import {createAudioPreview} from './editor_audio.js';+import {createAudioPreview, isOriginalSound} from './editor_audio.js';import {previewSpeed, speedAudioFrame, separateSpeedAudio} from './editor_speed.js';import {sourceFrame, percent} from './editor_speed_model.js';const el = (tag, cls) => { const e = document.createElement(tag); if (cls) e.className = cls; return e; };@@ -48,7 +48,7 @@ export function createEditorPlayer(host, transport, {state, sources, api, t, errconst picturePreview = createPreviewPicture(host, frame, {state, t});const playButton = el('button', 'fv-ed-play'); playButton.type = 'button'; playButton.title = t('Space', '空格');const time = el('span', 'fv-ed-time'); time.setAttribute('aria-live', 'off'); transport.append(playButton, time);- let active = 0, clipId = null, playing = false, disposed = false, raf = null, clockStart = 0, frameStart = 0, ticking = false, preview = null, previewSize = null, previewProject = null;+ let active = 0, clipId = null, silenced = false, playing = false, disposed = false, raf = null, clockStart = 0, frameStart = 0, ticking = false, preview = null, previewSize = null, previewProject = null;let audioContext = null, textRenderer = null, chains = [], chainSignature = null, chainRequest = null, chainEpoch = 0;const failedChains = new Set(), entries = new Map();let seekGeneration = 0, forcePosition = false, pictureView = null, processedSound = false;@@ -299,19 +299,19 @@ export function createEditorPlayer(host, transport, {state, sources, api, t, errclipId = clip.id;}const linked = hasPicture ? [...map.values()].find(row => row.clip.id === clip.link) : null;- const processedAudio = audioPreview.render({record, position, playing: playing && playback.rate > 0, playbackRate: playback.rate});+ const processedAudio = audioPreview.render({record, position, playing: playing && playback.rate > 0, playbackRate: playback.rate, silenceOriginal: silenced});processedSound = !!processedAudio;// The joined sound is the plain forward path only: sound edits, changed speed and// backward playback keep their own routes and the original sound.const plainSpeed = id => !separateSpeedAudio(model.find(record, id));- const availableChains = processedAudio || playback.rate !== 1 ? [] : chains.filter(c => !failedChains.has(c.key) && c.clips.every(plainSpeed));+ const availableChains = processedAudio || silenced || playback.rate !== 1 ? [] : chains.filter(c => !failedChains.has(c.key) && c.clips.every(plainSpeed));const selected = new Map([...map].filter(([track]) => track !== 'v1').map(([track, row]) =>[track, useChain(selectAudioChain(availableChains, track, row.clip, position, volumePreview), track)]));const linkedChain = linked && [...selected.values()].some(value => value?.chain.clips.includes(linked.clip.id));const originalSeparate = processedAudio || !!linked?.clip.splice || !hasPicture || separateSpeedAudio(linked?.clip);const separate = originalSeparate || linkedChain;if (hasPicture) {- const video = videos[active], speedPlayback = previewSpeed(video, clip, playing, linked?.clip); video.muted = !linked || separate || playback.rate < 0 || speedPlayback.muted;+ const video = videos[active], speedPlayback = previewSpeed(video, clip, playing, linked?.clip); video.muted = !linked || separate || silenced || playback.rate < 0 || speedPlayback.muted;video._timelineClip = clip;delete video.dataset.preparedClip;seekMedia(video, mediaURL(api, source.video), (videoRow.frame + (playing && playback.rate > 0 ? 0 : .5)) / 24, speedPlayback.running && map.get('v1')?.clip.id === clip.id, clipVolume(linked?.clip));@@ -336,7 +336,7 @@ export function createEditorPlayer(host, transport, {state, sources, api, t, err}if (carriedByVideo || slot >= audio.length) continue;const url = mediaURL(api, metadata.audio); if (!url) continue;- const media = audio[slot++], speed = previewSpeed(media, row.clip, playing); delete media.dataset.audioChain; media.muted = speed.muted;+ const media = audio[slot++], speed = previewSpeed(media, row.clip, playing); delete media.dataset.audioChain; media.muted = speed.muted || silenced && isOriginalSound(row.clip);media._timelineClip = row.clip;seekMedia(media, url, audioSeconds(row.clip, speedAudioFrame(row.clip, shown - row.clip.start)), speed.running, clipVolume(row.clip));}@@ -449,7 +449,9 @@ export function createEditorPlayer(host, transport, {state, sources, api, t, errelse if ((row?.clip.id || null) !== clipId) render();});render();- return {element: frame, playback, picturePreview, play, pause, toggle: () => playing ? pause() : play(), refresh: render,+ return {element: frame, playback, picturePreview, play, pause,+ // While a take is recorded the picture's own sound stays off, so the microphone does not pick it up.+ silenceOriginal(on) { if (silenced === !!on) return; silenced = !!on; resetEntries(); forcePosition = true; render(); }, toggle: () => playing ? pause() : play(), refresh: render,clearPreview() { clearPreview(); render(); },get playing() { return playing; },setTextRenderer(callback) { textRenderer = callback; },diff --git a/web/editor_record.css b/web/editor_record.cssnew file mode 100644index 0000000..a0c1b05--- /dev/null+++ b/web/editor_record.css@@ -0,0 +1,43 @@+.fv-ed-record{grid-column:1/-1;grid-row:4;z-index:8;box-sizing:border-box;display:grid;grid-template-columns:minmax(0,1fr) auto minmax(0,1fr);align-items:center;column-gap:16px;min-height:60px;padding:10px 16px;border-top:1px solid var(--fv-border);border-bottom:1px solid var(--fv-border);background:var(--fv-card);color:var(--fv-ink);font:13px/1.4 var(--fv-font,system-ui);min-width:0}+.fv-ed-record[hidden]{display:none}+.fv-ed-record-side{display:flex;min-width:0}+.fv-ed-record-center{display:flex;align-items:center;gap:14px;min-width:0}+.fv-ed-record-close{justify-self:end}+.fv-ed-record-device{appearance:none;flex:0 1 auto;width:auto;max-width:min(100%,260px);height:32px;padding:0 12px;border:1px solid var(--fv-border);border-radius:16px;background:var(--fv-card);color:var(--fv-muted);font:inherit;text-overflow:ellipsis;cursor:pointer}+.fv-ed-record-device:hover:not(:disabled){color:var(--fv-ink)}+.fv-ed-record-device:disabled{opacity:.5;cursor:default}+/* The level is a row of dots that is always drawn, so it reads as a meter before anything is said; the lit part follows the voice. */+.fv-ed-record-meter{flex:0 0 132px;height:8px;overflow:hidden;background:color-mix(in srgb,var(--fv-ink) 16%,transparent);-webkit-mask:repeating-linear-gradient(90deg,#000 0 4px,transparent 4px 8px);mask:repeating-linear-gradient(90deg,#000 0 4px,transparent 4px 8px)}+.fv-ed-record-level{width:100%;height:100%;background:var(--fv-accent);transform:scaleX(0);transform-origin:left;transition:transform .08s linear}+.fv-editor .fv-ed-record .fv-ed-record-main{flex:none;display:inline-flex;align-items:center;gap:8px;height:36px;padding:0 18px 0 14px;border:1px solid color-mix(in srgb,var(--fv-danger,#d33) 55%,transparent);border-radius:18px;background:color-mix(in srgb,var(--fv-danger,#d33) 12%,var(--fv-card));color:var(--fv-ink);font:inherit;font-weight:var(--fv-semibold,600);cursor:pointer}+.fv-ed-record-main::before{content:"";width:10px;height:10px;border-radius:50%;background:var(--fv-danger,#d33)}+.fv-editor .fv-ed-record .fv-ed-record-main:hover:not(:disabled){background:color-mix(in srgb,var(--fv-danger,#d33) 20%,var(--fv-card))}+.fv-editor .fv-ed-record[data-phase=recording] .fv-ed-record-main,.fv-editor .fv-ed-record[data-phase=counting] .fv-ed-record-main,.fv-editor .fv-ed-record[data-phase=saving] .fv-ed-record-main{border-color:transparent;background:#e5484d;color:#fff}+.fv-ed-record:is([data-phase=recording],[data-phase=counting],[data-phase=saving]) .fv-ed-record-main::before{width:9px;height:9px;border-radius:2px;background:#fff}+.fv-ed-record[data-phase=recording] .fv-ed-record-main::before{animation:fv-ed-record-pulse 1.2s ease-in-out infinite}+.fv-editor .fv-ed-record .fv-ed-record-main:disabled{border-color:transparent;background:color-mix(in srgb,var(--fv-ink) 12%,transparent);color:var(--fv-muted);cursor:not-allowed}+.fv-ed-record-main:disabled::before{background:var(--fv-muted)}+.fv-ed-record-note{min-width:2.4em;color:var(--fv-muted);font-variant-numeric:tabular-nums;overflow-wrap:anywhere}+.fv-ed-record[data-phase=counting] .fv-ed-record-note{color:var(--fv-ink);font-size:22px;font-weight:var(--fv-semibold,600);line-height:1}+.fv-ed-record[data-phase=recording] .fv-ed-record-note{color:var(--fv-ink);font-size:15px}+.fv-editor .fv-ed-record .fv-ed-record-close{flex:none;width:28px;height:28px;border:0;border-radius:6px;background:transparent;color:var(--fv-muted);font-size:18px;line-height:1;cursor:pointer}+.fv-ed-record-close:hover{background:color-mix(in srgb,var(--fv-ink) 10%,transparent)}+@keyframes fv-ed-record-pulse{50%{opacity:.45}}+@keyframes fv-ed-record-land{from{box-shadow:0 0 0 3px var(--fv-accent)}to{box-shadow:0 0 0 3px transparent}}+.fv-ed-clip[data-landed=true]{animation:fv-ed-record-land 1.4s ease-out}+@media (prefers-reduced-motion:reduce){.fv-ed-record-main::before,.fv-ed-clip[data-landed=true]{animation:none!important}.fv-ed-record-level{transition:none}}+@media (max-width:760px){+ .fv-ed-record{grid-column:1;grid-template-columns:minmax(0,1fr) auto;row-gap:8px;padding:10px 12px}+ .fv-ed-record-center{grid-column:1;grid-row:1;gap:10px}+ .fv-ed-record-close{grid-column:2;grid-row:1}+ .fv-ed-record-side{grid-column:1/-1;grid-row:2}+ .fv-ed-record-side:has(.fv-ed-record-device[hidden]){display:none}+ .fv-ed-record-meter{flex:1 1 48px;min-width:48px}+}++/* The recording sub-track sits one level under a1, with the same head as the other sound tracks. */+.fv-ed-track-row[data-sub=true] .fv-ed-track-empty{opacity:.8}+/* The bar takes the generation bar's place while it is open, so the row is only as tall as the bar. */+.fv-studio.fv-editor:has(.fv-ed-record:not([hidden])) .fv-ed-genbar{display:none}+.fv-ed-audio-track-switch:disabled{opacity:.35;cursor:default}+.fv-ed-record-meter[hidden]{display:none}diff --git a/web/editor_record.js b/web/editor_record.jsnew file mode 100644index 0000000..3c75b72--- /dev/null+++ b/web/editor_record.js@@ -0,0 +1,189 @@+// Recording from the microphone onto a sound track. A small bar above the tracks holds the microphone choice, the level,+// and the start/stop button; the take is saved as a WAV and placed like any imported sound, never over what is already there.+import {registerMenuItem} from './editor_menu.js';+import {COUNTDOWN_SECONDS, elapsedText, deviceName, recordable, recordRange, landingTrack, replacesTake, secondsOf, takeName, level, mixdown, encodeWav} from './editor_record_model.js';+const el = (tag, cls, text) => { const node = document.createElement(tag); if (cls) node.className = cls; if (text != null) node.textContent = text; return node; };+let styled = false;+function styles() {+ if (styled) return; styled = true;+ const link = el('link'); link.rel = 'stylesheet'; link.href = new URL('./editor_record.css', import.meta.url).href; document.head.append(link);+}+const MIC_PATH = 'M8 1.5a2.5 2.5 0 0 0-2.5 2.5v4a2.5 2.5 0 0 0 5 0V4A2.5 2.5 0 0 0 8 1.5M3.5 7.5a4.5 4.5 0 0 0 9 0M8 12v2.5M5.5 14.5h5';++export function createEditorRecord({state, player, library, t, error = () => {}, notify = () => {}, bar: host, button: toolbarButton}) {+ styles();+ const bar = el('div', 'fv-ed-record'); bar.hidden = true; bar.setAttribute('role', 'group'); bar.setAttribute('aria-label', t('Record', '录音'));+ const devices = el('select', 'fv-ed-record-device'); devices.setAttribute('aria-label', t('Microphone', '麦克风')); devices.hidden = true;+ const meter = el('div', 'fv-ed-record-meter'), fill = el('div', 'fv-ed-record-level'); meter.append(fill); meter.setAttribute('aria-hidden', 'true');+ const main = el('button', 'fv-ed-record-main', t('Start recording', '开始录音')); main.type = 'button';+ const note = el('span', 'fv-ed-record-note'); note.setAttribute('role', 'status');+ const close = el('button', 'fv-ed-record-close', '×'); close.type = 'button'; close.setAttribute('aria-label', t('Close', '关闭'));+ const side = el('div', 'fv-ed-record-side'), center = el('div', 'fv-ed-record-center');+ side.append(devices); center.append(main, meter, note); bar.append(side, center, close);++ let phase = 'idle', denied = false, stream = null, recorder = null, chunks = [], context = null, frame = null, timers = [], disposed = false, session = 0, ticker = null, listening = null, startedAt = 0;+ const clear = () => { timers.forEach(clearTimeout); timers = []; clearInterval(ticker); ticker = null; };+ // The level moves only while the bar is open: the microphone is listened to from opening until closing, and the take reuses that stream.+ const release = () => {+ cancelAnimationFrame(frame); frame = null; listening = null;+ stream?.getTracks().forEach(track => track.stop()); stream = null; context?.close().catch(() => {}); context = null; fill.style.transform = 'scaleX(0)';+ };+ // One request at a time: a quick second choice of microphone waits for the first, then replaces its stream.+ let queue = Promise.resolve();+ const listen = () => queue = queue.then(listening_);+ async function listening_() {+ if (disposed || bar.hidden) return false;+ if (stream && listening === devices.value) return true;+ release();+ const mine = session;+ try {+ const next = await media().getUserMedia({audio: devices.value ? {deviceId: {exact: devices.value}} : true});+ if (mine !== session || disposed || bar.hidden) { next.getTracks().forEach(track => track.stop()); return false; }+ stream = next; listening = devices.value; context = new AudioContext();+ const analyser = context.createAnalyser(); analyser.fftSize = 1024;+ context.createMediaStreamSource(stream).connect(analyser);+ const block = new Float32Array(analyser.fftSize);+ const loop = () => { analyser.getFloatTimeDomainData(block); fill.style.transform = `scaleX(${level(block)})`; frame = requestAnimationFrame(loop); };+ loop(); return true;+ } catch { return false; }+ }+ function paint() {+ main.textContent = phase === 'idle' ? t('Start recording', '开始录音') : t('Stop', '停止');+ main.disabled = denied && phase === 'idle'; meter.hidden = main.disabled; devices.disabled = phase !== 'idle';+ bar.dataset.phase = phase;+ toolbarButton?.setAttribute('aria-pressed', String(!bar.hidden));+ }+ const say = text => { note.textContent = text; };+ const denial = () => { denied = true; say(t('Microphone access was not granted.', '未获得麦克风权限。')); paint(); };+ const media = () => globalThis.navigator?.mediaDevices;++ // Opening the bar asks for the microphone once, so the choice of devices can carry their names.+ async function probe() {+ const mine = ++session;+ try {+ if (!media()?.getUserMedia) throw new Error('unavailable');+ if (!await listen()) throw new Error('denied');+ if (mine !== session || disposed) return;+ denied = false; say('');+ const all = (await media().enumerateDevices()).filter(device => device.kind === 'audioinput');+ // Chrome lists the default and communications choices again under the real device's name: they are left out when a real one exists.+ const real = all.filter(device => !['default', 'communications'].includes(device.deviceId)), inputs = real.length ? real : all.slice(0, 1);+ devices.replaceChildren(...inputs.map((device, index) => { const option = el('option', null, deviceName(device.label, index + 1, t)); option.value = device.deviceId; return option; }));+ devices.hidden = inputs.length < 2;+ // The stream was opened on the system default: show the real device behind it, or open the one shown when none is known.+ const behind = all.find(device => device.deviceId === 'default' && device.groupId);+ const match = behind && inputs.find(device => device.groupId === behind.groupId);+ if (match) devices.value = match.deviceId;+ if (stream) listening = match || inputs.length < 2 ? devices.value : null;+ if (stream && listening === null) await listen();+ paint();+ } catch { if (mine === session && !disposed) denial(); }+ }+ function open() {+ if (disposed || !bar.hidden) return;+ bar.hidden = false; denied = false; say(''); paint(); probe();+ }+ function hide() {+ if (phase !== 'idle') abort();+ session++; bar.hidden = true; release(); paint();+ }+ function abort() {+ clear(); if (recorder && recorder.state !== 'inactive') { recorder.ondataavailable = recorder.onstop = null; try { recorder.stop(); } catch { /* already stopped */ } }+ recorder = null; chunks = []; player.silenceOriginal?.(false); player.pause?.(); phase = 'idle'; say(''); paint();+ }++ async function begin() {+ if (phase !== 'idle' || denied) return;+ const selection = state.selection, range = recordRange(selection, state.playhead);+ if (!landingTrack(state.record, selection, range.start, range.frames ?? 1)) {+ say(t('The recording track is locked.', '录音轨已锁定。')); return;+ }+ const mine = ++session;+ phase = 'counting'; paint();+ if (!await listen()) { if (mine === session) { phase = 'idle'; denial(); } return; }+ if (mine !== session || disposed) return;+ // The count runs before the take, so the speaker has a moment to get ready.+ let left = COUNTDOWN_SECONDS;+ const tick = () => {+ if (mine !== session) return;+ if (left > 0) { say(String(left--)); timers.push(setTimeout(tick, 1000)); } else go(range, mine, selection);+ };+ tick();+ }+ function go(range, mine, selection) {+ phase = 'recording'; paint(); startedAt = performance.now(); say(elapsedText(0));+ ticker = setInterval(() => say(elapsedText((performance.now() - startedAt) / 1000)), 250);+ recorder = new MediaRecorder(stream); chunks = [];+ recorder.ondataavailable = event => { if (event.data.size) chunks.push(event.data); };+ recorder.onstop = () => finish(range, mine, selection);+ state.seek(range.start); player.silenceOriginal?.(true);+ recorder.start(); player.play?.();+ if (range.frames) timers.push(setTimeout(stop, Math.ceil(secondsOf(range.frames) * 1000)));+ }+ function stop() {+ if (phase === 'counting') { abort(); return; }+ if (phase !== 'recording') return;+ clear(); phase = 'saving'; paint(); say(t('Saving…', '正在保存…'));+ player.pause?.(); player.silenceOriginal?.(false);+ if (recorder.state !== 'inactive') recorder.stop();+ }+ async function finish(range, mine, selection) {+ const blob = new Blob(chunks, {type: recorder?.mimeType || 'audio/webm'}); chunks = []; recorder = null;+ let file = null, frames = 0;+ try {+ const decoder = new AudioContext(), decoded = await decoder.decodeAudioData(await blob.arrayBuffer()); decoder.close().catch(() => {});+ const limit = range.frames ? Math.round(secondsOf(range.frames) * decoded.sampleRate) : Infinity;+ const samples = mixdown(Array.from({length: decoded.numberOfChannels}, (_, index) => decoded.getChannelData(index)), limit);+ frames = Math.min(range.frames ?? Infinity, Math.ceil(samples.length / decoded.sampleRate * 24));+ if (samples.length) file = new File([encodeWav(samples, decoded.sampleRate)], takeName(new Date(), t), {type: 'audio/wav'});+ } catch (value) { error(value); }+ if (disposed || mine !== session) return;+ phase = 'idle'; say(''); paint();+ if (!file) { notify(t('Nothing was recorded.', '没有录到声音。')); return; }+ bar.hidden = true; release(); paint();+ const before = new Set(state.record.tracks.flatMap(item => item.clips.map(clip => clip.id)));+ const track = landingTrack(state.record, selection, range.start, frames) ?? undefined, replace = track === 'a3' && replacesTake(state.record, range.start, frames);+ await library.importFiles([file], {at: range.start, track, replace: track === 'a3'});+ land(before);+ if (replace) notify(t('Replaced the earlier recording in this range.', '已替换这一段原有的录音。'));+ }+ // The clip just placed settles in with a short highlight, so the eye follows the take from the bar to the track.+ function land(before) {+ const until = performance.now() + 15000, fresh = () => state.record.tracks.flatMap(item => item.clips).filter(clip => !before.has(clip.id)).map(clip => clip.id);+ let shown = 0;+ // The timeline redraws on its own schedule, so the mark is put on each frame until the highlight has run.+ const mark = () => {+ const nodes = fresh().flatMap(id => [...document.querySelectorAll(`.fv-ed-clip[data-clip="${CSS.escape(id)}"]`)]);+ if (nodes.length && !shown) shown = performance.now();+ for (const node of nodes) node.dataset.landed = 'true';+ if (performance.now() < (shown ? shown + 1400 : until)) requestAnimationFrame(mark);+ else for (const node of document.querySelectorAll('.fv-ed-clip[data-landed]')) delete node.dataset.landed;+ };+ requestAnimationFrame(mark);+ }+ // R starts and stops while the bar is open; Esc cancels a take in progress, then closes the bar.+ const typing = target => target?.closest?.('input, textarea, [contenteditable=true]');+ // Called by the editor's key router; true when the key was taken.+ function keys(event) {+ if (event.type !== 'keydown' || bar.hidden || event.isComposing || event.ctrlKey || event.metaKey || event.altKey || typing(event.target)) return false;+ const key = event.key.toLowerCase();+ if (key === 'escape') {+ event.preventDefault(); if (phase !== 'idle') abort(); else hide(); return true;+ }+ if (key === 'r' && !event.target?.closest?.('select') && !event.repeat) { event.preventDefault(); main.onclick(); return true; }+ return false;+ }+ devices.onchange = () => { if (!bar.hidden && phase === 'idle') listen(); };+ main.onclick = () => phase === 'idle' ? begin() : stop();+ close.onclick = hide;+ if (toolbarButton) toolbarButton.onclick = () => bar.hidden ? open() : hide();++ // The menu entry sits on the sound tracks, where an empty spot or a stretch chosen can take a recording.+ const unregister = registerMenuItem({where: 'empty', group: 'sound', label: context => context.t('Record', '录音'),+ visible: context => !context.track || ['a1', 'a2'].includes(context.track), run: () => open()});+ const unsubscribe = state.subscribe(event => { if (event.type === 'selection' && toolbarButton) toolbarButton.hidden = !recordable(state.selection); });+ if (toolbarButton) toolbarButton.hidden = !recordable(state.selection);+ paint();+ return {element: bar, open, hide, keyboard: keys, get phase() { return phase; }, dispose() { disposed = true; unregister(); unsubscribe(); abort(); release(); bar.remove(); }};+}+export const RECORD_ICON = MIC_PATH;diff --git a/web/editor_record_model.js b/web/editor_record_model.jsnew file mode 100644index 0000000..a74d368--- /dev/null+++ b/web/editor_record_model.js@@ -0,0 +1,84 @@+// Recording from a microphone onto the sound tracks. Pure helpers: where a take starts and stops,+// the 16-bit WAV a take is saved as (the server tells sound from picture by file suffix), the level shown while recording.+import {FPS} from './editor_model.js';++export const COUNTDOWN_SECONDS = 3;+const AUDIO_TRACKS = ['a1', 'a2', 'a3'];++// Whether the chosen stretch is on a sound track (a stretch of the picture track does not offer recording).+export function recordable(selection) {+ if (!selection) return true;+ if (selection.kind === 'gap') return AUDIO_TRACKS.includes(selection.track);+ if (selection.kind === 'range') return !selection.tracks?.length || selection.tracks.some(id => AUDIO_TRACKS.includes(id));+ return false;+}++// A stretch chosen starts the take at its start and ends it at its end; with none, the take starts at the play position and runs until stopped.+export function recordRange(selection, playhead) {+ if (selection && ['gap', 'range'].includes(selection.kind) && Number.isSafeInteger(selection.start) && selection.frames > 0)+ return {start: selection.start, frames: selection.frames};+ return {start: Math.max(0, Math.round(playhead) || 0), frames: null};+}++// The sound track a take lands on: a1 when its stretch is free, otherwise the recording sub-track a3 under it, where+// it replaces whatever an older take covers there. Null only when a3 is locked: a take never lands on the music track a2.+export function landingTrack(record, selection, start, frames) {+ const free = (track, ignoreLock) => !track || ((ignoreLock || !track.locked) && !track.clips.some(clip => clip.start < start + frames && start < clip.start + clip.frames));+ const a1 = record.tracks.find(row => row.id === 'a1'), a3 = record.tracks.find(row => row.id === 'a3');+ return free(a1) ? 'a1' : a3?.locked ? null : 'a3';+}++// Whether a take on the sub-track would cover part of an older take.+export function replacesTake(record, start, frames) {+ return !!record.tracks.find(row => row.id === 'a3')?.clips.some(clip => clip.start < start + frames && start < clip.start + clip.frames);+}++export const secondsOf = frames => frames / FPS;++// A take's name in the library: 录音 / Recording and the time it began.+export function takeName(date, t = en => en) {+ const pad = value => String(value).padStart(2, '0');+ return `${t('Recording', '录音')} ${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}.wav`;+}++// Loudness of a block of samples, 0 to 1, for the level meter.+export function level(samples) {+ if (!samples?.length) return 0;+ let sum = 0;+ for (let i = 0; i < samples.length; i++) sum += samples[i] * samples[i];+ return Math.min(1, Math.sqrt(sum / samples.length) * 3);+}++// Mono mix of channel arrays, trimmed to `limit` samples when given.+export function mixdown(channels, limit = Infinity) {+ const length = Math.min(limit, Math.max(0, ...channels.map(channel => channel.length)));+ const out = new Float32Array(length);+ for (const channel of channels) for (let i = 0; i < length && i < channel.length; i++) out[i] += channel[i] / channels.length;+ return out;+}++// 16-bit PCM mono WAV bytes.+export function encodeWav(samples, sampleRate) {+ if (!Number.isInteger(sampleRate) || sampleRate <= 0) throw new RangeError('sampleRate');+ const bytes = new Uint8Array(44 + samples.length * 2), view = new DataView(bytes.buffer);+ const text = (at, value) => { for (let i = 0; i < value.length; i++) view.setUint8(at + i, value.charCodeAt(i)); };+ text(0, 'RIFF'); view.setUint32(4, 36 + samples.length * 2, true); text(8, 'WAVE'); text(12, 'fmt ');+ view.setUint32(16, 16, true); view.setUint16(20, 1, true); view.setUint16(22, 1, true);+ view.setUint32(24, sampleRate, true); view.setUint32(28, sampleRate * 2, true); view.setUint16(32, 2, true); view.setUint16(34, 16, true);+ text(36, 'data'); view.setUint32(40, samples.length * 2, true);+ for (let i = 0; i < samples.length; i++) {+ const value = Math.max(-1, Math.min(1, samples[i]));+ view.setInt16(44 + i * 2, value < 0 ? value * 0x8000 : value * 0x7fff, true);+ }+ return bytes;+}++// The elapsed time of a take, as m:ss.+export const elapsedText = seconds => `${Math.floor(Math.max(0, seconds) / 60)}:${String(Math.floor(Math.max(0, seconds) % 60)).padStart(2, '0')}`;++// A microphone by a short name: the system's default marker and the hardware id in brackets are dropped; a name that is empty or+// only an id falls back to the number.+export function deviceName(label, number, t) {+ const name = String(label || '').replace(/^(default|communications)\s*-\s*/i, '').replace(/\s*\([0-9a-f]{4}:[0-9a-f]{4}\)\s*$/i, '').trim();+ return name || t(`Microphone ${number}`, `麦克风 ${number}`);+}diff --git a/web/editor_text_model.js b/web/editor_text_model.jsindex 7ed1059..790e109 100644--- a/web/editor_text_model.js+++ b/web/editor_text_model.js@@ -7,7 +7,9 @@ export const SUBTITLE_DEFAULTS = Object.freeze({font: 'sans', size: 44 / 1080, cexport const isText = clip => ['text', 'subtitle'].includes(clip?.text_kind);export const isTextTrack = track => ['text', 'subtitle'].includes(track?.kind);export const hasTextData = record => record?.subtitle_style !== undefined || record?.tracks?.some(isTextTrack);-export const displayTracks = record => [...record.tracks.filter(t => t.kind === 'subtitle'), ...record.tracks.filter(t => t.kind === 'text'), ...record.tracks.filter(t => !isTextTrack(t))];+// The recording sub-track (a3) is drawn directly under a1.+const media = rows => { const sub = rows.find(t => t.id === 'a3'); return sub ? rows.filter(t => t !== sub).flatMap(t => t.id === 'a1' ? [t, sub] : [t]) : rows; };+export const displayTracks = record => [...record.tracks.filter(t => t.kind === 'subtitle'), ...record.tracks.filter(t => t.kind === 'text'), ...media(record.tracks.filter(t => !isTextTrack(t)))];export const defaults = kind => kind === 'subtitle' ? SUBTITLE_DEFAULTS : TEXT_DEFAULTS;export const styleOf = (record, clip) => ({...defaults(clip.text_kind), ...(clip.text_kind === 'subtitle' ? record.subtitle_style : clip.text_style)});const check = (ok, code = 'text') => { if (!ok) throw new model.EditError(code); };diff --git a/web/editor_tracks.js b/web/editor_tracks.jsindex 654a52f..3bd9334 100644--- a/web/editor_tracks.js+++ b/web/editor_tracks.js@@ -222,7 +222,7 @@ export function createTracks(host, {state, sources = {}, t = en => en, actions =};const audioTargetAt = (source, event) => {const track = pointerTrackAt(event);- return !source.hasVideo && source.hasAudio && ['a1', 'a2'].includes(track) ? track : undefined;+ return !source.hasVideo && source.hasAudio && ['a1', 'a2', 'a3'].includes(track) ? track : undefined;};const sourceInfo = key => typeof sources === 'function' ? sources(key) : sources.get?.(key) || sources[key];// The snapping switch lives in the transport model: it empties the point list (and Alt flips it per gesture).@@ -500,8 +500,8 @@ export function createTracks(host, {state, sources = {}, t = en => en, actions =const STEM_NAMES = {dialogue: t('Dialogue', '人声'), music: t('Music', '音乐'), ambience: t('Ambience', '环境声')};let top = 24, parted = false;for (const track of tracks) {- const name = track.kind === 'text' ? t('Text', '文字') : track.kind === 'subtitle' ? t('Subtitles', '字幕') : track.kind === 'video' ? t('Video', '画面') : track.stem ? STEM_NAMES[track.stem] : twoAudio- ? t(`Audio ${track.id.slice(1)}`, `声音 ${track.id.slice(1)}`) : t('Audio', '声音');+ const name = track.kind === 'text' ? t('Text', '文字') : track.kind === 'subtitle' ? t('Subtitles', '字幕') : track.kind === 'video' ? t('Video', '画面') : track.stem ? STEM_NAMES[track.stem] : track.id === 'a3'+ ? t('Voice', '录音') : twoAudio ? t(`Audio ${track.id.slice(1)}`, `声音 ${track.id.slice(1)}`) : t('Audio', '声音');let row = rows.get(track.id);if (!row) {const node = create('div', 'fv-ed-track-row'); node.dataset.track = track.id;@@ -518,6 +518,7 @@ export function createTracks(host, {state, sources = {}, t = en => en, actions =// separated music lane where new music replaces the old.const tourId = track.stem === 'music' ? 'audio-music-stem' : track.stem ? '' : {a1: 'audio-a1-track', a2: 'audio-a2-track'}[track.id] || '';if (tourId) row.element.dataset.tourId = tourId; else delete row.element.dataset.tourId;+ row.head.dataset.sub = row.element.dataset.sub = String(track.id === 'a3');row.empty.hidden = track.clips.length > 0;// A picture track with any block description grows a strip for it under the pictures.const noted = !!actions.describedTrack?.(track); row.element.dataset.noted = String(noted);@@ -838,8 +839,8 @@ export function createTracks(host, {state, sources = {}, t = en => en, actions =const result = moveParameters(gesture.clip, gesture.anchor, frame, dragPoints(desired, gesture.clip.frames), state.zoom);result.track = isText(gesture.clip) ? pointerHit(event).track : null;const pointedTrack = pointerTrackAt(event);- result.targetTrack = gesture.ids.length === 1 && ['a1', 'a2'].includes(gesture.track)- && ['a1', 'a2'].includes(pointedTrack) ? pointedTrack : undefined;+ result.targetTrack = gesture.ids.length === 1 && ['a1', 'a2', 'a3'].includes(gesture.track)+ && ['a1', 'a2', 'a3'].includes(pointedTrack) ? pointedTrack : undefined;gesture.result = result;const track = state.record.tracks.find(row => row.id === (result.targetTrack || gesture.track));const drop = isText(gesture.clip) ? {hit: false} : insertionPoint(track.clips, result.frame, gesture.ids);@@ -1127,7 +1128,7 @@ export function createTracks(host, {state, sources = {}, t = en => en, actions =if (draggedSource) try {const preview = model.insertSource(state.record, {...draggedSource, at: frame.frame, targetTrack: audioTargetAt(draggedSource, event)});const added = previewSourcePlacement(preview), sound = added.find(item => item.track !== 'v1');- const name = sound ? preview.tracks.length === 3+ const name = sound ? !preview.tracks.some(track => track.id === 'a3') && preview.tracks.length === 3? t(`Audio ${sound.track === 'a1' ? 1 : 2}`, `声音 ${sound.track === 'a1' ? 1 : 2}`) : t('Audio', '声音') : '';dropPoint = {frame: added[0].clip.start, label: `${clock(added[0].clip.start)} – ${clock(added[0].clip.start + draggedSource.frames)}${name ? ` · ${name}` : ''}`};} catch (error) {