fablecut 1.17.0 → 1.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +107 -3
- package/README.md +22 -1
- package/analyze.js +3 -96
- package/app.js +486 -6
- package/beats.js +458 -0
- package/highlights.js +296 -0
- package/index.html +3 -0
- package/mcp-server.js +274 -7
- package/package.json +1 -1
- package/reframe.js +297 -0
- package/style.css +3 -0
package/CLAUDE.md
CHANGED
|
@@ -46,6 +46,12 @@ Every Claude Code session then has these tools:
|
|
|
46
46
|
file and switches the clip's stems to it). See "Audio mix" below.
|
|
47
47
|
- `fablecut_silence` — find the pauses in speech and, with `cut:true`, cut them out
|
|
48
48
|
(extract on the targeted tracks, stems in step). See "Audio mix" below.
|
|
49
|
+
- `fablecut_beats` — find a music clip's tempo, beat and drop; mark the beats, snap the
|
|
50
|
+
cuts to them, or re-cut a track's clips on the beat. See "Audio mix" below.
|
|
51
|
+
- `fablecut_reframe` — reframe for another shape (9:16, 1:1, 4:5): a copy of the sequence
|
|
52
|
+
on that canvas whose shots follow the subject. See "Sequences" below.
|
|
53
|
+
- `fablecut_highlights` — the best moments of long footage cut into a short edit, in a new
|
|
54
|
+
sequence. See "Sequences" below.
|
|
49
55
|
- `fablecut_transcribe` — turn speech into words with times (Whisper, in the browser),
|
|
50
56
|
read them on the timeline, find a phrase and cut it out. See "Transcripts and
|
|
51
57
|
text-based editing" below.
|
|
@@ -379,6 +385,7 @@ Examples in `library/svg/`: `sparkles.svg` (loop), `lower-third.svg`,
|
|
|
379
385
|
| `fit` | "contain" | contain · cover (fill canvas, crop overflow) · stretch · none (native px) |
|
|
380
386
|
| `cropL` `cropR` `cropT` `cropB` | 0 | % of the source trimmed off each edge |
|
|
381
387
|
| `cornerRadius` | 0 | px, rounded corners — the PiP look |
|
|
388
|
+
| `reframe` | — | set by Reframe (`"follow"` or `"centre"`): the clip's `fit` and position are Reframe's own, so running it again replaces them (see "Sequences") |
|
|
382
389
|
| `flipH`, `flipV` | false | mirror |
|
|
383
390
|
|
|
384
391
|
**Filter / color** (video/image/svg):
|
|
@@ -634,6 +641,33 @@ edits the key under the playhead. Agents:
|
|
|
634
641
|
as amber bands, **Cut N** extracts them (one undo step), **Clear** removes the
|
|
635
642
|
marks. The marks drop when the timeline, a clip's file, speed or level, or the
|
|
636
643
|
frame rate changes.
|
|
644
|
+
- **Beats** — `fablecut_beats {clipId, markers?, snap?, retime?, every?, force?}`
|
|
645
|
+
listens to a music clip (an audio clip, or a video's sound) and finds its tempo
|
|
646
|
+
(BPM), a **steady beat grid** — the tempo's period, phased and fitted to the
|
|
647
|
+
hits, so it doesn't drift over a song — and the drop, all in timeline seconds
|
|
648
|
+
through the clip's in point and speed. On its own it only reports. Then, in one
|
|
649
|
+
revision: `markers:true` puts a cyan marker on every beat (other markers stay, and a
|
|
650
|
+
beat that already has one keeps it);
|
|
651
|
+
`snap:true` (or `{tracks, maxShift}`) rolls each cut on the targeted video tracks
|
|
652
|
+
to the nearest beat within half a beat — a roll, so nothing else moves and
|
|
653
|
+
linked stems follow; a cut whose clips have no spare footage to reach the beat
|
|
654
|
+
stays where it is, and the reply says how many; `retime:{track?, every?, from?}`
|
|
655
|
+
lays a track's clips (default the lowest video track with clips) back to back
|
|
656
|
+
on the beat, `every` beats each (1, 2, 4, 8 or 16; default 2), from the first
|
|
657
|
+
beat at or after `from` (default the music's start), in their order — a clip
|
|
658
|
+
whose footage runs out gets fewer beats, clips past the music keep their
|
|
659
|
+
length, and one with less than a beat of footage refuses the whole re-cut.
|
|
660
|
+
Locked clips are left alone by snap and refuse retime, unless `force:true`
|
|
661
|
+
(only when the user asked). A version is kept before snap or retime; if saving it
|
|
662
|
+
fails, the edit still goes ahead and the reply says so in a warning. A re-cut clip
|
|
663
|
+
that gets shorter loses the keyframes past its new end, and its transitions are
|
|
664
|
+
kept no longer than the clip, as with a trim. The music clip itself (and its linked
|
|
665
|
+
stems) is never re-cut, even when it sits on the track being re-cut. Cuts land
|
|
666
|
+
on whole frames. A clip with a speed ramp is refused: its music doesn't keep
|
|
667
|
+
time on the timeline. In the editor: the Inspector's **Beats** section on an
|
|
668
|
+
audio or video clip — **Find beats** shows the beat as ticks across the
|
|
669
|
+
timeline and the BPM and drop below; then **Mark**, **Snap cuts**, and
|
|
670
|
+
**Re-cut** a track every N beats (each one undo step).
|
|
637
671
|
- In the editor, each audio clip shows its **volume line** (yellow; dashed =
|
|
638
672
|
after ducking): drag it to change the level, Ctrl/Cmd-click to add a keyframe,
|
|
639
673
|
drag a point to move it, Alt-click or double-click to remove one. The corner
|
|
@@ -1744,7 +1778,9 @@ list short without deleting anything.
|
|
|
1744
1778
|
|
|
1745
1779
|
### Versions
|
|
1746
1780
|
|
|
1747
|
-
Every edit is saved as it happens, and undo covers the session.
|
|
1781
|
+
Every edit is saved as it happens, and undo covers the session. An edit that
|
|
1782
|
+
reaches the open editor from outside (an agent, another tab) is one undo step of
|
|
1783
|
+
its own there: Ctrl+Z takes it back first, then the user's own edits. **Versions** are
|
|
1748
1784
|
what you come back to later: full copies of a project, kept beside it, that are
|
|
1749
1785
|
never overwritten. Restoring one is not destructive either — what is there is
|
|
1750
1786
|
saved as a version first, so a restore can be undone by restoring that.
|
|
@@ -1872,10 +1908,68 @@ sequence.
|
|
|
1872
1908
|
sequence's changes on its own, sequences added and removed, and which one
|
|
1873
1909
|
was open.
|
|
1874
1910
|
|
|
1875
|
-
|
|
1911
|
+
**Reframe** — `fablecut_reframe {aspect?:"9:16"|"1:1"|"4:5"|"16:9"|"4:3"|"2:3", name?, clipIds?, follow?, smooth?, force?}`
|
|
1912
|
+
makes a copy of the open sequence on that canvas shape and opens it (the
|
|
1913
|
+
project's shorter side kept: 1920×1080 → 1080×1920 for 9:16, 1080×1080 for
|
|
1914
|
+
1:1), so the original cut stays as it was; without `aspect` it reframes the
|
|
1915
|
+
open sequence on its own canvas. Each full-frame shot — a video or still that
|
|
1916
|
+
isn't moved, scaled, cropped or keyed by hand — gets `fit:"cover"` and an `x`
|
|
1917
|
+
(or `y`) that keeps its subject in view. Frames are sampled 5 times a second at
|
|
1918
|
+
96 px; the subject is where motion (against the shot's own movement, so a pan
|
|
1919
|
+
or a flicker doesn't count), detail and colour gather, pulled gently toward
|
|
1920
|
+
the centre. A virtual camera operator then holds still while the subject
|
|
1921
|
+
stays inside a dead zone and eases after it no faster than a pan speed, and
|
|
1922
|
+
the path becomes ordinary keyframes (thinned to within ~2 px), or one steady
|
|
1923
|
+
position. Shots with no clear subject are centred. Titles, picture-in-picture,
|
|
1924
|
+
clips placed or animated by hand and locked clips (unless `force:true`) are
|
|
1925
|
+
left alone and listed in the reply — check them on the new canvas with
|
|
1926
|
+
`fablecut_frame {time}`. `follow:false` only fits and centres; `smooth` 0…1
|
|
1927
|
+
(default 0.5) holds longer and pans slower as it rises. `props.reframe` marks
|
|
1928
|
+
what Reframe did, so running it again replaces its own keys. A version is kept
|
|
1929
|
+
first. In the editor: **New sequence…** with a copy of the open sequence on
|
|
1930
|
+
another shape has **Follow the subject in each shot**; the Inspector's
|
|
1931
|
+
**Reframe** section on a video or picture of another shape than the canvas
|
|
1932
|
+
has **Follow subject**, **Centre** and Lively / Balanced / Steady (the selected
|
|
1933
|
+
clips, one undo step, with Cancel).
|
|
1934
|
+
|
|
1935
|
+
**Highlights** — `fablecut_highlights {duration?, shot?, clipIds?, mediaIds?, name?, aspect?}`
|
|
1936
|
+
finds the best moments of long footage and cuts them into a short edit: a new
|
|
1937
|
+
sequence (`Highlights 30 s`, or `name`), opened, so the timeline it came from
|
|
1938
|
+
stays as it was. It reads the video clips on the open timeline (`clipIds`
|
|
1939
|
+
limits them; `mediaIds` reads whole files instead), up to an hour of footage
|
|
1940
|
+
at a time: frames twice a second at 64 px, and the sound. Every half second is
|
|
1941
|
+
scored for movement, sound louder than the 20 s around it (a cheer, a laugh, a
|
|
1942
|
+
hit), its overall level, and a clear picture; black frames and flat colour
|
|
1943
|
+
count for little. Cuts already in the footage are found, and a moment never
|
|
1944
|
+
crosses one. It then takes the best moments of `shot` seconds (default
|
|
1945
|
+
`duration` / 8, kept within 1.5–4 s), apart in the same footage (by half a
|
|
1946
|
+
moment, at least a second) and spread over its shots, until the edit is `duration` seconds
|
|
1947
|
+
(default 30; 5…600), trims the last ones to fit exactly, and nudges each end up
|
|
1948
|
+
to 0.3 s to the quietest point near it, so no word or hit is cut through. The
|
|
1949
|
+
moments go back to back on V1 in story order, each a clip of its source with
|
|
1950
|
+
its props (a reframed clip's framing isn't carried over) and its linked sound
|
|
1951
|
+
clips beside it (their own tracks, linked again); their keyframes aren't
|
|
1952
|
+
carried over. A moment from a whole file (`mediaIds`) is one video clip that
|
|
1953
|
+
plays its own sound, with no separate audio clips. A clip played faster or
|
|
1954
|
+
slower counts by its length on the timeline: `duration` and `shot` are always
|
|
1955
|
+
seconds of the edit. In the editor (and Studio) the sound of a file longer than
|
|
1956
|
+
30 minutes isn't read, so as not to decode it whole in the browser; its picture
|
|
1957
|
+
decides. Disabled clips and clips with a speed ramp are not read. `aspect`
|
|
1958
|
+
makes the new sequence 9:16, 1:1 or 4:5: the shots are then letterboxed, and
|
|
1959
|
+
`fablecut_reframe {}` fits them and follows the subject. The reply lists each
|
|
1960
|
+
moment (its source, times, length, why it was picked and its score). A version
|
|
1961
|
+
is kept first. In the editor: the sequence menu's **Highlights…** (15, 30, 60 or
|
|
1962
|
+
90 s; a canvas, and **Follow the subject in each shot** on another shape)
|
|
1963
|
+
reads the selected video clips, else every video clip on the timeline, with
|
|
1964
|
+
progress and **Cancel**.
|
|
1965
|
+
|
|
1966
|
+
Recipes: **vertical cut of a finished edit** — `fablecut_reframe {aspect:"9:16"}`,
|
|
1967
|
+
then check the titles with `fablecut_frame`; by hand, `addSequence {name:"Reel 9:16",
|
|
1876
1968
|
from:"open", width:1080, height:1920}`, then `inSequence` ops to re-position
|
|
1877
1969
|
or re-scale the shots and `setProject {delivery:"instagram-reels"}` in it;
|
|
1878
|
-
**
|
|
1970
|
+
**reel from a long video** — `fablecut_highlights {duration:30, aspect:"9:16"}`,
|
|
1971
|
+
then `fablecut_reframe {}`, music on an audio track and `fablecut_beats {clipId:<the music>, retime:{every:2}}`,
|
|
1972
|
+
then captions; **cut-downs** — duplicate the edit as "30 s" and "15 s" and tighten each with
|
|
1879
1973
|
the trim ops inside `inSequence`; **selects reel** — an empty sequence the
|
|
1880
1974
|
user drops favourite takes on, cut into the edit from the Source monitor.
|
|
1881
1975
|
|
|
@@ -2230,6 +2324,16 @@ so all takes sit at one loudness, then keep the music on its own lane and pull
|
|
|
2230
2324
|
that lane down: `{op:"setTrack", id:"A3", set:{gain:-14}}`. Then dip it under the
|
|
2231
2325
|
voice: `fablecut_auto_duck {clipIds:[<music clip>], under:["A1"], amount:-10}`.
|
|
2232
2326
|
|
|
2327
|
+
**Cut a montage to the music**: put the shots on V1 in the order you want (rough
|
|
2328
|
+
lengths are fine) and the music on an audio lane, then
|
|
2329
|
+
`fablecut_beats {clipId:<the music>, retime:{every:2}}` — each shot gets two beats
|
|
2330
|
+
where its footage allows (fewer when it runs out; clips past the music keep their
|
|
2331
|
+
length; one with less than a beat of footage refuses the re-cut), the cuts land on the beat. `every:1` for a fast reel, `4` for a calm one;
|
|
2332
|
+
re-cut the part after the drop separately with `retime:{from:<the drop>, every:1}`.
|
|
2333
|
+
For an edit whose cuts are already chosen, `snap:true` just nudges each one onto
|
|
2334
|
+
the nearest beat (cuts on untargeted tracks, beside locked clips, or without spare
|
|
2335
|
+
footage to reach it stay where they are; the reply counts them). Check a cut with `fablecut_frame` before exporting.
|
|
2336
|
+
|
|
2233
2337
|
**Tighten a talking-head take**: lock the music lane (`setProject {lockedTracks:["A3"]}`),
|
|
2234
2338
|
then `fablecut_silence {clipIds:[<the take>]}` to see the pauses (it does the
|
|
2235
2339
|
listening; `fablecut_frame` only shows the picture, handy to check a cut won't land
|
package/README.md
CHANGED
|
@@ -49,6 +49,15 @@ can edit video, and the open browser UI hot-reloads within ~150 ms via
|
|
|
49
49
|
server-sent events. A human and an agent can work on the same timeline at the
|
|
50
50
|
same time.
|
|
51
51
|
|
|
52
|
+
**Taking back what the agent did.** Every agent edit is one undo step of its
|
|
53
|
+
own: Ctrl+Z takes back the agent's last change first (and says so), then
|
|
54
|
+
goes on to yours, so the two never get mixed. For bigger rollbacks there are
|
|
55
|
+
**Versions**: one is kept when an agent starts editing (again at most every
|
|
56
|
+
10 minutes while it works), before it rewrites the whole project, before every
|
|
57
|
+
big operation (cutting silences, snapping to the beat, reframing), and every
|
|
58
|
+
10 minutes while you edit. Restore one from the Versions panel, or ask the agent
|
|
59
|
+
(`fablecut_versions`), and compare any version with now first.
|
|
60
|
+
|
|
52
61
|
## Features
|
|
53
62
|
|
|
54
63
|
**Editing**
|
|
@@ -127,10 +136,22 @@ same time.
|
|
|
127
136
|
Strong) measures the file's noise floor and renders a cleaned copy with
|
|
128
137
|
ffmpeg; the clip's audio switches to it and **Off** switches back. Agents
|
|
129
138
|
use `fablecut_denoise`.
|
|
139
|
+
- **Reframe** — make a 9:16, 1:1 or 4:5 copy of your edit whose shots follow
|
|
140
|
+
the subject: a virtual camera pans after what matters in each shot and
|
|
141
|
+
writes ordinary position keys you can still edit. From **New sequence…**
|
|
142
|
+
or the Inspector's **Reframe** section; agents use `fablecut_reframe`.
|
|
143
|
+
- **Highlights** — the best moments of long footage (movement, sound, a
|
|
144
|
+
clear picture) cut into a short edit in a new sequence, in story order and
|
|
145
|
+
never across a cut. From the sequence menu's **Highlights…**; agents use
|
|
146
|
+
`fablecut_highlights`.
|
|
130
147
|
- **Silence removal** — the inspector's **Silence** section (or right-click →
|
|
131
148
|
**Find silences**) listens to a take and marks every pause on the timeline;
|
|
132
149
|
**Cut** extracts them all in one undo step, keeping a little quiet beside
|
|
133
150
|
each phrase so words aren't clipped. Agents use `fablecut_silence`.
|
|
151
|
+
- **Beats** — the inspector's **Beats** section finds a music clip's tempo, a
|
|
152
|
+
steady beat grid and the drop, and shows the beat across the timeline. Mark
|
|
153
|
+
the beats, snap every cut onto the nearest one, or re-cut a track so each
|
|
154
|
+
shot lasts 1, 2, 4, 8 or 16 beats. Agents use `fablecut_beats`.
|
|
134
155
|
- **Transcripts and text-based editing** — the **Transcript** tab turns speech
|
|
135
156
|
into words with times, in the browser (Whisper; the model downloads once and
|
|
136
157
|
the audio never leaves your machine). Click a word to go there, select words
|
|
@@ -471,7 +492,7 @@ Three equivalent control surfaces:
|
|
|
471
492
|
`fablecut_get_project`, `fablecut_set_project`, `fablecut_patch_project`,
|
|
472
493
|
`fablecut_import_media`, `fablecut_analyze_reference`, `fablecut_frame`,
|
|
473
494
|
`fablecut_encode_profiles`, `fablecut_normalize_audio`, `fablecut_auto_duck`,
|
|
474
|
-
`fablecut_denoise`, `fablecut_silence`, `fablecut_transcribe`, `fablecut_captions`, `fablecut_export`, `fablecut_scopes`, `fablecut_track`.
|
|
495
|
+
`fablecut_denoise`, `fablecut_silence`, `fablecut_beats`, `fablecut_reframe`, `fablecut_highlights`, `fablecut_transcribe`, `fablecut_captions`, `fablecut_export`, `fablecut_scopes`, `fablecut_track`.
|
|
475
496
|
|
|
476
497
|
FableCut is also published on the **official MCP registry** as
|
|
477
498
|
[`io.github.ronak-create/fablecut`](https://registry.modelcontextprotocol.io/v0/servers?search=fablecut)
|
package/analyze.js
CHANGED
|
@@ -15,6 +15,7 @@ const fs = require("fs");
|
|
|
15
15
|
const path = require("path");
|
|
16
16
|
const { spawn } = require("child_process");
|
|
17
17
|
const Stats = require("./frame-stats");
|
|
18
|
+
const Beats = require("./beats");
|
|
18
19
|
|
|
19
20
|
const SR = 22050; // analysis sample rate
|
|
20
21
|
const HOP = 512; // onset-envelope hop size (~23 ms)
|
|
@@ -86,103 +87,9 @@ async function decodePCM(file) {
|
|
|
86
87
|
return new Int16Array(buf.buffer, buf.byteOffset, Math.floor(buf.length / 2));
|
|
87
88
|
}
|
|
88
89
|
|
|
90
|
+
/* beats, BPM, energy curve and drop: beats.js, shared with the editor's Beats section and fablecut_beats */
|
|
89
91
|
function analyzeAudio(pcm) {
|
|
90
|
-
|
|
91
|
-
if (nFrames < 20) return { beats: [], bpm: null, energy: { step: 0.5, values: [] }, drop: null };
|
|
92
|
-
|
|
93
|
-
// frame energies
|
|
94
|
-
const E = new Float64Array(nFrames);
|
|
95
|
-
for (let i = 0; i < nFrames; i++) {
|
|
96
|
-
let s = 0;
|
|
97
|
-
const o = i * HOP;
|
|
98
|
-
for (let j = 0; j < WIN; j++) { const x = pcm[o + j] / 32768; s += x * x; }
|
|
99
|
-
E[i] = s / WIN;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// onset envelope: energy rise vs. trailing average
|
|
103
|
-
const O = new Float64Array(nFrames);
|
|
104
|
-
for (let i = 1; i < nFrames; i++) {
|
|
105
|
-
let m = 0, k = 0;
|
|
106
|
-
for (let j = Math.max(0, i - 8); j < i; j++) { m += E[j]; k++; }
|
|
107
|
-
O[i] = Math.max(0, E[i] - m / k);
|
|
108
|
-
}
|
|
109
|
-
let oMax = 0;
|
|
110
|
-
for (let i = 0; i < nFrames; i++) if (O[i] > oMax) oMax = O[i];
|
|
111
|
-
if (oMax > 0) for (let i = 0; i < nFrames; i++) O[i] /= oMax;
|
|
112
|
-
|
|
113
|
-
// peak-pick beats: local max, above an adaptive floor, min 0.25 s apart
|
|
114
|
-
const frameT = (i) => Math.round(((i * HOP + WIN / 2) / SR) * 1000) / 1000;
|
|
115
|
-
const minGap = Math.round(0.25 * SR / HOP);
|
|
116
|
-
const halfWin = Math.round(1.0 * SR / HOP);
|
|
117
|
-
const beats = [];
|
|
118
|
-
let last = -minGap;
|
|
119
|
-
for (let i = 3; i < nFrames - 3; i++) {
|
|
120
|
-
if (O[i] < 0.05) continue;
|
|
121
|
-
let isMax = true;
|
|
122
|
-
for (let j = -3; j <= 3; j++) if (O[i + j] > O[i]) { isMax = false; break; }
|
|
123
|
-
if (!isMax) continue;
|
|
124
|
-
let m = 0, k = 0;
|
|
125
|
-
for (let j = Math.max(0, i - halfWin); j < Math.min(nFrames, i + halfWin); j++) { m += O[j]; k++; }
|
|
126
|
-
if (O[i] < (m / k) * 1.5) continue;
|
|
127
|
-
if (i - last < minGap) continue;
|
|
128
|
-
beats.push(frameT(i));
|
|
129
|
-
last = i;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// BPM: autocorrelation of the onset envelope over 60–200 BPM lags,
|
|
133
|
-
// with a mild prior toward ~120; fold octaves into 70–180.
|
|
134
|
-
let bpm = null;
|
|
135
|
-
const lagMin = Math.round(60 / 200 * SR / HOP), lagMax = Math.round(60 / 60 * SR / HOP);
|
|
136
|
-
let best = 0, bestLag = 0;
|
|
137
|
-
for (let lag = lagMin; lag <= Math.min(lagMax, nFrames - 1); lag++) {
|
|
138
|
-
let s = 0;
|
|
139
|
-
for (let i = 0; i + lag < nFrames; i++) s += O[i] * O[i + lag];
|
|
140
|
-
const cand = 60 / (lag * HOP / SR);
|
|
141
|
-
const w = Math.exp(-0.5 * Math.pow(Math.log2(cand / 120) / 1.0, 2));
|
|
142
|
-
if (s * w > best) { best = s * w; bestLag = lag; }
|
|
143
|
-
}
|
|
144
|
-
if (bestLag) {
|
|
145
|
-
bpm = 60 / (bestLag * HOP / SR);
|
|
146
|
-
while (bpm < 70) bpm *= 2;
|
|
147
|
-
while (bpm > 180) bpm /= 2;
|
|
148
|
-
// refine: if the picked beats are regular, the median inter-beat interval
|
|
149
|
-
// is a much finer tempo estimate than the autocorrelation lag grid
|
|
150
|
-
if (beats.length >= 6) {
|
|
151
|
-
const gaps = beats.slice(1).map((t, i) => t - beats[i]).sort((a, b) => a - b);
|
|
152
|
-
const med = gaps[Math.floor(gaps.length / 2)];
|
|
153
|
-
const spread = gaps[Math.floor(gaps.length * 0.85)] - gaps[Math.floor(gaps.length * 0.15)];
|
|
154
|
-
if (med > 0 && spread < med * 0.15) {
|
|
155
|
-
// full span / count averages out the hop-grid quantization of each beat
|
|
156
|
-
let refined = 60 / ((beats[beats.length - 1] - beats[0]) / (beats.length - 1));
|
|
157
|
-
while (refined < 70) refined *= 2;
|
|
158
|
-
while (refined > 180) refined /= 2;
|
|
159
|
-
if (Math.abs(refined - bpm) / bpm < 0.15) bpm = refined;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
bpm = Math.round(bpm * 10) / 10;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// loudness curve, one value per 0.5 s, normalized 0–100
|
|
166
|
-
const step = 0.5;
|
|
167
|
-
const perStep = Math.round(step * SR / HOP);
|
|
168
|
-
const values = [];
|
|
169
|
-
for (let i = 0; i < nFrames; i += perStep) {
|
|
170
|
-
let s = 0, k = 0;
|
|
171
|
-
for (let j = i; j < Math.min(nFrames, i + perStep); j++) { s += Math.sqrt(E[j]); k++; }
|
|
172
|
-
values.push(k ? s / k : 0);
|
|
173
|
-
}
|
|
174
|
-
const vMax = Math.max(...values, 1e-9);
|
|
175
|
-
const energy = values.map((v) => Math.round(v / vMax * 100));
|
|
176
|
-
|
|
177
|
-
// the drop: biggest sustained energy rise (skip the very start)
|
|
178
|
-
let drop = null, bestRise = 0;
|
|
179
|
-
for (let i = 2; i < energy.length - 1; i++) {
|
|
180
|
-
const before = (energy[i - 2] + energy[i - 1]) / 2;
|
|
181
|
-
const after = (energy[i] + energy[Math.min(i + 1, energy.length - 1)]) / 2;
|
|
182
|
-
if (after - before > bestRise && after > 55) { bestRise = after - before; drop = Math.round(i * step * 10) / 10; }
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return { beats, bpm, energy: { step, values: energy }, drop };
|
|
92
|
+
return Beats.analyze(pcm, { scale: 1 / 32768 });
|
|
186
93
|
}
|
|
187
94
|
|
|
188
95
|
/* ── Extract the reference's music track into its own audio file ── */
|