@nodetool-ai/timeline 0.8.19 → 0.8.21
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/dist/.nodetool-build-stamp +1 -1
- package/dist/autoCrossfade.d.ts +30 -0
- package/dist/autoCrossfade.d.ts.map +1 -0
- package/dist/autoCrossfade.js +130 -0
- package/dist/autoCrossfade.js.map +1 -0
- package/dist/automation.d.ts +32 -1
- package/dist/automation.d.ts.map +1 -1
- package/dist/automation.js +78 -1
- package/dist/automation.js.map +1 -1
- package/dist/automationRanges.d.ts +375 -0
- package/dist/automationRanges.d.ts.map +1 -0
- package/dist/automationRanges.js +375 -0
- package/dist/automationRanges.js.map +1 -0
- package/dist/clipAudio.d.ts +3 -1
- package/dist/clipAudio.d.ts.map +1 -1
- package/dist/clipAudio.js.map +1 -1
- package/dist/clipLoop.d.ts +67 -0
- package/dist/clipLoop.d.ts.map +1 -0
- package/dist/clipLoop.js +339 -0
- package/dist/clipLoop.js.map +1 -0
- package/dist/clipSpeed.d.ts.map +1 -1
- package/dist/clipSpeed.js +7 -0
- package/dist/clipSpeed.js.map +1 -1
- package/dist/defaults.d.ts.map +1 -1
- package/dist/defaults.js +9 -0
- package/dist/defaults.js.map +1 -1
- package/dist/dropResolve.d.ts.map +1 -1
- package/dist/dropResolve.js +56 -17
- package/dist/dropResolve.js.map +1 -1
- package/dist/harmony/chordChart.d.ts +25 -0
- package/dist/harmony/chordChart.d.ts.map +1 -0
- package/dist/harmony/chordChart.js +99 -0
- package/dist/harmony/chordChart.js.map +1 -0
- package/dist/harmony/chordRange.d.ts +9 -0
- package/dist/harmony/chordRange.d.ts.map +1 -0
- package/dist/harmony/chordRange.js +32 -0
- package/dist/harmony/chordRange.js.map +1 -0
- package/dist/harmony/chordSymbols.d.ts +28 -0
- package/dist/harmony/chordSymbols.d.ts.map +1 -0
- package/dist/harmony/chordSymbols.js +147 -0
- package/dist/harmony/chordSymbols.js.map +1 -0
- package/dist/harmony/chordTrack.d.ts +13 -0
- package/dist/harmony/chordTrack.d.ts.map +1 -0
- package/dist/harmony/chordTrack.js +11 -0
- package/dist/harmony/chordTrack.js.map +1 -0
- package/dist/harmony/detect.d.ts +35 -0
- package/dist/harmony/detect.d.ts.map +1 -0
- package/dist/harmony/detect.js +138 -0
- package/dist/harmony/detect.js.map +1 -0
- package/dist/harmony/detectAudio.d.ts +62 -0
- package/dist/harmony/detectAudio.d.ts.map +1 -0
- package/dist/harmony/detectAudio.js +226 -0
- package/dist/harmony/detectAudio.js.map +1 -0
- package/dist/harmony/expand.d.ts +7 -12
- package/dist/harmony/expand.d.ts.map +1 -1
- package/dist/harmony/expand.js +63 -11
- package/dist/harmony/expand.js.map +1 -1
- package/dist/harmony/index.d.ts +7 -0
- package/dist/harmony/index.d.ts.map +1 -1
- package/dist/harmony/index.js +7 -0
- package/dist/harmony/index.js.map +1 -1
- package/dist/harmony/progressionLibrary.d.ts +33 -0
- package/dist/harmony/progressionLibrary.d.ts.map +1 -0
- package/dist/harmony/progressionLibrary.js +71 -0
- package/dist/harmony/progressionLibrary.js.map +1 -0
- package/dist/harmony/summary.d.ts +4 -0
- package/dist/harmony/summary.d.ts.map +1 -0
- package/dist/harmony/summary.js +22 -0
- package/dist/harmony/summary.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/interchange.d.ts.map +1 -1
- package/dist/interchange.js +72 -12
- package/dist/interchange.js.map +1 -1
- package/dist/loopWindow.d.ts +17 -0
- package/dist/loopWindow.d.ts.map +1 -0
- package/dist/loopWindow.js +34 -0
- package/dist/loopWindow.js.map +1 -0
- package/dist/midi/cacheKey.d.ts +11 -2
- package/dist/midi/cacheKey.d.ts.map +1 -1
- package/dist/midi/cacheKey.js +69 -9
- package/dist/midi/cacheKey.js.map +1 -1
- package/dist/midi/controllerEdit.d.ts +35 -0
- package/dist/midi/controllerEdit.d.ts.map +1 -0
- package/dist/midi/controllerEdit.js +94 -0
- package/dist/midi/controllerEdit.js.map +1 -0
- package/dist/midi/controllerRange.d.ts +13 -0
- package/dist/midi/controllerRange.d.ts.map +1 -0
- package/dist/midi/controllerRange.js +13 -0
- package/dist/midi/controllerRange.js.map +1 -0
- package/dist/midi/controllers.d.ts +95 -0
- package/dist/midi/controllers.d.ts.map +1 -0
- package/dist/midi/controllers.js +421 -0
- package/dist/midi/controllers.js.map +1 -0
- package/dist/midi/curveCut.d.ts +17 -0
- package/dist/midi/curveCut.d.ts.map +1 -0
- package/dist/midi/curveCut.js +45 -0
- package/dist/midi/curveCut.js.map +1 -0
- package/dist/midi/edit.d.ts +13 -1
- package/dist/midi/edit.d.ts.map +1 -1
- package/dist/midi/edit.js +19 -3
- package/dist/midi/edit.js.map +1 -1
- package/dist/midi/engines/bl1.d.ts +10 -1
- package/dist/midi/engines/bl1.d.ts.map +1 -1
- package/dist/midi/engines/bl1.js +26 -6
- package/dist/midi/engines/bl1.js.map +1 -1
- package/dist/midi/engines/dr1.d.ts +11 -1
- package/dist/midi/engines/dr1.d.ts.map +1 -1
- package/dist/midi/engines/dr1.js +21 -3
- package/dist/midi/engines/dr1.js.map +1 -1
- package/dist/midi/engines/dsp.d.ts +1 -1
- package/dist/midi/engines/dsp.d.ts.map +1 -1
- package/dist/midi/engines/dsp.js +10 -2
- package/dist/midi/engines/dsp.js.map +1 -1
- package/dist/midi/engines/wt1.d.ts +10 -1
- package/dist/midi/engines/wt1.d.ts.map +1 -1
- package/dist/midi/engines/wt1.js +28 -5
- package/dist/midi/engines/wt1.js.map +1 -1
- package/dist/midi/events.d.ts +13 -4
- package/dist/midi/events.d.ts.map +1 -1
- package/dist/midi/events.js +33 -6
- package/dist/midi/events.js.map +1 -1
- package/dist/midi/expression.d.ts +25 -0
- package/dist/midi/expression.d.ts.map +1 -0
- package/dist/midi/expression.js +69 -0
- package/dist/midi/expression.js.map +1 -0
- package/dist/midi/index.d.ts +8 -0
- package/dist/midi/index.d.ts.map +1 -1
- package/dist/midi/index.js +8 -0
- package/dist/midi/index.js.map +1 -1
- package/dist/midi/instrument.d.ts.map +1 -1
- package/dist/midi/instrument.js +4 -0
- package/dist/midi/instrument.js.map +1 -1
- package/dist/midi/noteFunctions.d.ts +29 -0
- package/dist/midi/noteFunctions.d.ts.map +1 -0
- package/dist/midi/noteFunctions.js +213 -0
- package/dist/midi/noteFunctions.js.map +1 -0
- package/dist/midi/notes.d.ts +7 -1
- package/dist/midi/notes.d.ts.map +1 -1
- package/dist/midi/notes.js +43 -1
- package/dist/midi/notes.js.map +1 -1
- package/dist/midi/notesEdit.d.ts +13 -0
- package/dist/midi/notesEdit.d.ts.map +1 -1
- package/dist/midi/notesEdit.js +73 -2
- package/dist/midi/notesEdit.js.map +1 -1
- package/dist/midi/presets.d.ts +1 -1
- package/dist/midi/regionParams.d.ts +41 -0
- package/dist/midi/regionParams.d.ts.map +1 -0
- package/dist/midi/regionParams.js +164 -0
- package/dist/midi/regionParams.js.map +1 -0
- package/dist/midi/sampler.d.ts +2 -2
- package/dist/midi/sampler.d.ts.map +1 -1
- package/dist/midi/sampler.js +99 -21
- package/dist/midi/sampler.js.map +1 -1
- package/dist/midi/stepEditor.d.ts +50 -0
- package/dist/midi/stepEditor.d.ts.map +1 -0
- package/dist/midi/stepEditor.js +118 -0
- package/dist/midi/stepEditor.js.map +1 -0
- package/dist/midi/stepPattern.d.ts +173 -0
- package/dist/midi/stepPattern.d.ts.map +1 -0
- package/dist/midi/stepPattern.js +1390 -0
- package/dist/midi/stepPattern.js.map +1 -0
- package/dist/midi/stepPatternExpand.d.ts +149 -0
- package/dist/midi/stepPatternExpand.d.ts.map +1 -0
- package/dist/midi/stepPatternExpand.js +1068 -0
- package/dist/midi/stepPatternExpand.js.map +1 -0
- package/dist/midi/stepPatternLibrary.d.ts +65 -0
- package/dist/midi/stepPatternLibrary.d.ts.map +1 -0
- package/dist/midi/stepPatternLibrary.js +640 -0
- package/dist/midi/stepPatternLibrary.js.map +1 -0
- package/dist/midi/tempo.d.ts.map +1 -1
- package/dist/midi/tempo.js +3 -0
- package/dist/midi/tempo.js.map +1 -1
- package/dist/midi/voice.d.ts +51 -5
- package/dist/midi/voice.d.ts.map +1 -1
- package/dist/midi/voice.js +191 -21
- package/dist/midi/voice.js.map +1 -1
- package/dist/model3dBake.d.ts +13 -0
- package/dist/model3dBake.d.ts.map +1 -1
- package/dist/model3dBake.js +25 -0
- package/dist/model3dBake.js.map +1 -1
- package/dist/model3dCameraDescription.d.ts +11 -6
- package/dist/model3dCameraDescription.d.ts.map +1 -1
- package/dist/model3dCameraDescription.js +112 -30
- package/dist/model3dCameraDescription.js.map +1 -1
- package/dist/model3dReference.d.ts +25 -4
- package/dist/model3dReference.d.ts.map +1 -1
- package/dist/model3dReference.js +19 -6
- package/dist/model3dReference.js.map +1 -1
- package/dist/music/align.d.ts +111 -0
- package/dist/music/align.d.ts.map +1 -0
- package/dist/music/align.js +282 -0
- package/dist/music/align.js.map +1 -0
- package/dist/music/cut.d.ts +33 -0
- package/dist/music/cut.d.ts.map +1 -0
- package/dist/music/cut.js +50 -0
- package/dist/music/cut.js.map +1 -0
- package/dist/music/frames.d.ts +51 -0
- package/dist/music/frames.d.ts.map +1 -0
- package/dist/music/frames.js +110 -0
- package/dist/music/frames.js.map +1 -0
- package/dist/music/gain.d.ts +24 -0
- package/dist/music/gain.d.ts.map +1 -0
- package/dist/music/gain.js +38 -0
- package/dist/music/gain.js.map +1 -0
- package/dist/music/index.d.ts +9 -0
- package/dist/music/index.d.ts.map +1 -0
- package/dist/music/index.js +9 -0
- package/dist/music/index.js.map +1 -0
- package/dist/music/plan.d.ts +104 -0
- package/dist/music/plan.d.ts.map +1 -0
- package/dist/music/plan.js +300 -0
- package/dist/music/plan.js.map +1 -0
- package/dist/music/process.d.ts +9 -0
- package/dist/music/process.d.ts.map +1 -0
- package/dist/music/process.js +9 -0
- package/dist/music/process.js.map +1 -0
- package/dist/music/roles.d.ts +14 -0
- package/dist/music/roles.d.ts.map +1 -0
- package/dist/music/roles.js +30 -0
- package/dist/music/roles.js.map +1 -0
- package/dist/music/splice.d.ts +37 -0
- package/dist/music/splice.d.ts.map +1 -0
- package/dist/music/splice.js +67 -0
- package/dist/music/splice.js.map +1 -0
- package/dist/music/tempoRules.d.ts +41 -0
- package/dist/music/tempoRules.d.ts.map +1 -0
- package/dist/music/tempoRules.js +59 -0
- package/dist/music/tempoRules.js.map +1 -0
- package/dist/ops/apply.d.ts.map +1 -1
- package/dist/ops/apply.js +856 -234
- package/dist/ops/apply.js.map +1 -1
- package/dist/ops/automation.d.ts +6 -0
- package/dist/ops/automation.d.ts.map +1 -0
- package/dist/ops/automation.js +217 -0
- package/dist/ops/automation.js.map +1 -0
- package/dist/ops/barTime.d.ts +14 -0
- package/dist/ops/barTime.d.ts.map +1 -0
- package/dist/ops/barTime.js +33 -0
- package/dist/ops/barTime.js.map +1 -0
- package/dist/ops/chords.d.ts +43 -0
- package/dist/ops/chords.d.ts.map +1 -0
- package/dist/ops/chords.js +691 -0
- package/dist/ops/chords.js.map +1 -0
- package/dist/ops/groupBake.d.ts +24 -0
- package/dist/ops/groupBake.d.ts.map +1 -0
- package/dist/ops/groupBake.js +81 -0
- package/dist/ops/groupBake.js.map +1 -0
- package/dist/ops/index.d.ts +7 -0
- package/dist/ops/index.d.ts.map +1 -1
- package/dist/ops/index.js +5 -0
- package/dist/ops/index.js.map +1 -1
- package/dist/ops/join.d.ts +20 -0
- package/dist/ops/join.d.ts.map +1 -0
- package/dist/ops/join.js +265 -0
- package/dist/ops/join.js.map +1 -0
- package/dist/ops/loop.d.ts +29 -0
- package/dist/ops/loop.d.ts.map +1 -0
- package/dist/ops/loop.js +107 -0
- package/dist/ops/loop.js.map +1 -0
- package/dist/ops/midiMix.d.ts +41 -0
- package/dist/ops/midiMix.d.ts.map +1 -0
- package/dist/ops/midiMix.js +522 -0
- package/dist/ops/midiMix.js.map +1 -0
- package/dist/ops/op.d.ts +61 -11
- package/dist/ops/op.d.ts.map +1 -1
- package/dist/ops/op.js +60 -1
- package/dist/ops/op.js.map +1 -1
- package/dist/ops/parse.d.ts.map +1 -1
- package/dist/ops/parse.js +308 -4
- package/dist/ops/parse.js.map +1 -1
- package/dist/ops/pasteRange.d.ts +46 -0
- package/dist/ops/pasteRange.d.ts.map +1 -0
- package/dist/ops/pasteRange.js +276 -0
- package/dist/ops/pasteRange.js.map +1 -0
- package/dist/ops/plugin.d.ts +57 -0
- package/dist/ops/plugin.d.ts.map +1 -0
- package/dist/ops/plugin.js +875 -0
- package/dist/ops/plugin.js.map +1 -0
- package/dist/ops/sections.d.ts +98 -0
- package/dist/ops/sections.d.ts.map +1 -0
- package/dist/ops/sections.js +930 -0
- package/dist/ops/sections.js.map +1 -0
- package/dist/ops/serialize.d.ts +79 -1
- package/dist/ops/serialize.d.ts.map +1 -1
- package/dist/ops/serialize.js +85 -3
- package/dist/ops/serialize.js.map +1 -1
- package/dist/ops/stepPattern.d.ts +44 -0
- package/dist/ops/stepPattern.d.ts.map +1 -0
- package/dist/ops/stepPattern.js +2106 -0
- package/dist/ops/stepPattern.js.map +1 -0
- package/dist/ops/stepPatternFolders.d.ts +54 -0
- package/dist/ops/stepPatternFolders.d.ts.map +1 -0
- package/dist/ops/stepPatternFolders.js +299 -0
- package/dist/ops/stepPatternFolders.js.map +1 -0
- package/dist/ops/types.d.ts +238 -2
- package/dist/ops/types.d.ts.map +1 -1
- package/dist/rippleEdit.d.ts +5 -1
- package/dist/rippleEdit.d.ts.map +1 -1
- package/dist/rippleEdit.js +24 -9
- package/dist/rippleEdit.js.map +1 -1
- package/dist/script.d.ts +2 -1
- package/dist/script.d.ts.map +1 -1
- package/dist/script.js +3 -2
- package/dist/script.js.map +1 -1
- package/dist/splitClip.d.ts +29 -1
- package/dist/splitClip.d.ts.map +1 -1
- package/dist/splitClip.js +231 -1
- package/dist/splitClip.js.map +1 -1
- package/dist/storyboard.d.ts +14 -0
- package/dist/storyboard.d.ts.map +1 -1
- package/dist/storyboard.js +31 -0
- package/dist/storyboard.js.map +1 -1
- package/dist/testing/host-ops.js +3 -3
- package/dist/testing/host-ops.js.map +1 -1
- package/dist/testing/ops.d.ts +6 -1
- package/dist/testing/ops.d.ts.map +1 -1
- package/dist/testing/ops.js +823 -11
- package/dist/testing/ops.js.map +1 -1
- package/dist/timeRemap.d.ts +2 -2
- package/dist/timeRemap.d.ts.map +1 -1
- package/dist/timeRemap.js +18 -0
- package/dist/timeRemap.js.map +1 -1
- package/dist/trimClip.d.ts +1 -1
- package/dist/trimClip.d.ts.map +1 -1
- package/dist/trimClip.js +41 -7
- package/dist/trimClip.js.map +1 -1
- package/dist/types.d.ts +307 -16
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +11 -5
- package/dist/render/curveFitting.d.ts +0 -28
- package/dist/render/curveFitting.d.ts.map +0 -1
- package/dist/render/curveFitting.js +0 -94
- package/dist/render/curveFitting.js.map +0 -1
|
@@ -0,0 +1,1068 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Step Sequencer pure timing, hashing, scheduling, pitch resolution and
|
|
3
|
+
* expansion (SQ6, SQ9–SQ19, SQ41–SQ42, SQ60).
|
|
4
|
+
*
|
|
5
|
+
* Pure and deterministic: no `Math.random`, no external runtime dependencies.
|
|
6
|
+
* Every function and vector here is mirrored in C++ `PatternExpand` for native
|
|
7
|
+
* parity (D104).
|
|
8
|
+
*/
|
|
9
|
+
import { automationBreakpoints, automationModeOf, automationModePlays, automationValueAt, isAutomationLaneActive } from "../automation.js";
|
|
10
|
+
import { chordAtTick, CHORD_QUALITIES } from "../harmony/chords.js";
|
|
11
|
+
import { chordTrackFor } from "../harmony/chordTrack.js";
|
|
12
|
+
import { chordScale, remapNote, scaleMask, scaleRemap, SCALES } from "../harmony/scales.js";
|
|
13
|
+
import { activeLoopMs } from "../loopWindow.js";
|
|
14
|
+
import { controllerValueRange } from "./controllerRange.js";
|
|
15
|
+
import { sortNotes } from "./notes.js";
|
|
16
|
+
import { resolveTempo } from "./tempo.js";
|
|
17
|
+
import { MIDI_PPQ, msToTicks, ticksToMs } from "./ticks.js";
|
|
18
|
+
/** All 22 Step Sequencer step rates (SQ6). */
|
|
19
|
+
export const STEP_RATES = [
|
|
20
|
+
"4", "2", "1", "1/2", "1/4", "1/8", "1/16", "1/32", "1/64",
|
|
21
|
+
"1T", "2T", "4T", "8T", "16T", "32T",
|
|
22
|
+
"1D", "2D", "4D", "8D", "16D", "32D", "64D"
|
|
23
|
+
];
|
|
24
|
+
const STEP_RATE_TICKS = {
|
|
25
|
+
"4": 15360,
|
|
26
|
+
"2": 7680,
|
|
27
|
+
"1": 3840,
|
|
28
|
+
"1/2": 1920,
|
|
29
|
+
"1/4": 960,
|
|
30
|
+
"1/8": 480,
|
|
31
|
+
"1/16": 240,
|
|
32
|
+
"1/32": 120,
|
|
33
|
+
"1/64": 60,
|
|
34
|
+
"1T": 2560,
|
|
35
|
+
"2T": 1280,
|
|
36
|
+
"4T": 640,
|
|
37
|
+
"8T": 320,
|
|
38
|
+
"16T": 160,
|
|
39
|
+
"32T": 80,
|
|
40
|
+
"1D": 5760,
|
|
41
|
+
"2D": 2880,
|
|
42
|
+
"4D": 1440,
|
|
43
|
+
"8D": 720,
|
|
44
|
+
"16D": 360,
|
|
45
|
+
"32D": 180,
|
|
46
|
+
"64D": 90
|
|
47
|
+
};
|
|
48
|
+
/** Step rates ordered by ascending tick length (60..15360) for increment/decrement (SQ6). */
|
|
49
|
+
export const STEP_RATES_BY_TICKS = [...STEP_RATES].sort((a, b) => STEP_RATE_TICKS[a] - STEP_RATE_TICKS[b]);
|
|
50
|
+
/** Length of one step at `rate` in integer ticks at MIDI_PPQ = 960 (SQ6, SQ60). */
|
|
51
|
+
export function stepRateTicks(rate) {
|
|
52
|
+
const ticks = STEP_RATE_TICKS[rate];
|
|
53
|
+
if (ticks === undefined) {
|
|
54
|
+
throw new Error(`Unknown stepRate "${String(rate)}". Valid values: ${STEP_RATES.join(", ")}.`);
|
|
55
|
+
}
|
|
56
|
+
return ticks;
|
|
57
|
+
}
|
|
58
|
+
/** Move `rate` by `delta` steps along tick-ordered `STEP_RATES_BY_TICKS` (clamped). */
|
|
59
|
+
export function nudgeStepRate(rate, delta) {
|
|
60
|
+
const idx = STEP_RATES_BY_TICKS.indexOf(rate);
|
|
61
|
+
if (idx < 0)
|
|
62
|
+
return rate;
|
|
63
|
+
const next = Math.max(0, Math.min(STEP_RATES_BY_TICKS.length - 1, idx + Math.trunc(delta)));
|
|
64
|
+
return STEP_RATES_BY_TICKS[next];
|
|
65
|
+
}
|
|
66
|
+
// ── Deterministic hashing (SQ13, SQ60) ──────────────────────────────────────
|
|
67
|
+
/** 32-bit FNV-1a over a byte sequence. */
|
|
68
|
+
export function fnv1a32Bytes(bytes) {
|
|
69
|
+
let h = 0x811c9dc5;
|
|
70
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
71
|
+
h ^= bytes[i] & 0xff;
|
|
72
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
73
|
+
}
|
|
74
|
+
return h >>> 0;
|
|
75
|
+
}
|
|
76
|
+
/** 32-bit FNV-1a of a UTF-8 row id string (SQ13). */
|
|
77
|
+
export function rowKeyHash(rowId) {
|
|
78
|
+
const bytes = [];
|
|
79
|
+
for (let i = 0; i < rowId.length; i++) {
|
|
80
|
+
const code = rowId.charCodeAt(i);
|
|
81
|
+
if (code < 0x80) {
|
|
82
|
+
bytes.push(code);
|
|
83
|
+
}
|
|
84
|
+
else if (code < 0x800) {
|
|
85
|
+
bytes.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
|
|
86
|
+
}
|
|
87
|
+
else if (code >= 0xd800 && code <= 0xdbff && i + 1 < rowId.length) {
|
|
88
|
+
const low = rowId.charCodeAt(++i);
|
|
89
|
+
const cp = 0x10000 + (((code & 0x3ff) << 10) | (low & 0x3ff));
|
|
90
|
+
bytes.push(0xf0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
bytes.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return fnv1a32Bytes(bytes);
|
|
97
|
+
}
|
|
98
|
+
/** 32-bit FNV-1a over four little-endian uint32 values (SQ60). */
|
|
99
|
+
export function fnv1a32Uints(a, b, c, d) {
|
|
100
|
+
const u = [a >>> 0, b >>> 0, c >>> 0, d >>> 0];
|
|
101
|
+
let h = 0x811c9dc5;
|
|
102
|
+
for (let k = 0; k < 4; k++) {
|
|
103
|
+
const v = u[k];
|
|
104
|
+
h ^= v & 0xff;
|
|
105
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
106
|
+
h ^= (v >>> 8) & 0xff;
|
|
107
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
108
|
+
h ^= (v >>> 16) & 0xff;
|
|
109
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
110
|
+
h ^= (v >>> 24) & 0xff;
|
|
111
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
112
|
+
}
|
|
113
|
+
return h >>> 0;
|
|
114
|
+
}
|
|
115
|
+
/** Single mulberry32 step returning a float in [0, 1). */
|
|
116
|
+
export function mulberry32(state) {
|
|
117
|
+
let t = (state + 0x6d2b79f5) >>> 0;
|
|
118
|
+
t = Math.imul(t ^ (t >>> 15), t | 1) >>> 0;
|
|
119
|
+
t ^= (t + Math.imul(t ^ (t >>> 7), t | 61)) >>> 0;
|
|
120
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Deterministic hash in [0, 1) for chance, wander, shuffle and random modes (SQ13, SQ60).
|
|
124
|
+
* Accepts either a pre-hashed uint32 `rowKey` or a row id string.
|
|
125
|
+
*/
|
|
126
|
+
export function stepHash(seed, rowKey, step, cycle) {
|
|
127
|
+
const rk = typeof rowKey === "string" ? rowKeyHash(rowKey) : rowKey >>> 0;
|
|
128
|
+
return mulberry32(fnv1a32Uints(seed >>> 0, rk, step >>> 0, cycle >>> 0));
|
|
129
|
+
}
|
|
130
|
+
// ── Playback order, scheduling, swing and note repeat (SQ10–SQ15, SQ60) ─────
|
|
131
|
+
const PLAY_ORDER_SALT_SHUFFLE = 0x51a7f1e1;
|
|
132
|
+
const PLAY_ORDER_SALT_RANDOM = 0x72616e64;
|
|
133
|
+
const PLAY_ORDER_SALT_WANDER = 0x77616e64;
|
|
134
|
+
function wanderCycle(n, wSeed, rowKey, cycle, startPos, startAtZero) {
|
|
135
|
+
const out = [];
|
|
136
|
+
let pos = startPos;
|
|
137
|
+
if (startAtZero) {
|
|
138
|
+
out.push(pos);
|
|
139
|
+
for (let i = 1; i < n; i++) {
|
|
140
|
+
const dir = stepHash(wSeed, rowKey, i, cycle) < 0.5 ? -1 : 1;
|
|
141
|
+
pos = (pos + dir + n) % n;
|
|
142
|
+
out.push(pos);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
for (let i = 0; i < n; i++) {
|
|
147
|
+
const dir = stepHash(wSeed, rowKey, i, cycle) < 0.5 ? -1 : 1;
|
|
148
|
+
pos = (pos + dir + n) % n;
|
|
149
|
+
out.push(pos);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
/** Step indices visited during one cycle of `n` active steps (SQ11, SQ60). */
|
|
155
|
+
export function playOrder(n, mode, seed, cycle, rowKey = 0, prevPos) {
|
|
156
|
+
if (n <= 0)
|
|
157
|
+
return [];
|
|
158
|
+
if (n === 1)
|
|
159
|
+
return [0];
|
|
160
|
+
switch (mode) {
|
|
161
|
+
case "forward":
|
|
162
|
+
return Array.from({ length: n }, (_, i) => i);
|
|
163
|
+
case "backward":
|
|
164
|
+
return Array.from({ length: n }, (_, i) => n - 1 - i);
|
|
165
|
+
case "pingPong": {
|
|
166
|
+
const out = [];
|
|
167
|
+
for (let i = 0; i < n; i++)
|
|
168
|
+
out.push(i);
|
|
169
|
+
for (let i = n - 1; i >= 0; i--)
|
|
170
|
+
out.push(i);
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
case "pendulum": {
|
|
174
|
+
const out = [];
|
|
175
|
+
for (let i = 0; i < n; i++)
|
|
176
|
+
out.push(i);
|
|
177
|
+
for (let i = n - 2; i >= 1; i--)
|
|
178
|
+
out.push(i);
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
case "shuffle": {
|
|
182
|
+
const sSeed = (seed ^ PLAY_ORDER_SALT_SHUFFLE) >>> 0;
|
|
183
|
+
const arr = Array.from({ length: n }, (_, i) => i);
|
|
184
|
+
for (let i = n - 1; i > 0; i--) {
|
|
185
|
+
const j = Math.floor(stepHash(sSeed, rowKey, i, cycle) * (i + 1));
|
|
186
|
+
const tmp = arr[i];
|
|
187
|
+
arr[i] = arr[j];
|
|
188
|
+
arr[j] = tmp;
|
|
189
|
+
}
|
|
190
|
+
return arr;
|
|
191
|
+
}
|
|
192
|
+
case "random": {
|
|
193
|
+
const rSeed = (seed ^ PLAY_ORDER_SALT_RANDOM) >>> 0;
|
|
194
|
+
const out = [];
|
|
195
|
+
for (let i = 0; i < n; i++) {
|
|
196
|
+
out.push(Math.floor(stepHash(rSeed, rowKey, i, cycle) * n));
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
case "wander": {
|
|
201
|
+
const wSeed = (seed ^ PLAY_ORDER_SALT_WANDER) >>> 0;
|
|
202
|
+
if (cycle <= 0)
|
|
203
|
+
return wanderCycle(n, wSeed, rowKey, 0, 0, true);
|
|
204
|
+
if (prevPos !== undefined) {
|
|
205
|
+
return wanderCycle(n, wSeed, rowKey, cycle, ((prevPos % n) + n) % n, false);
|
|
206
|
+
}
|
|
207
|
+
let pos = 0;
|
|
208
|
+
for (let c = 0; c < cycle; c++) {
|
|
209
|
+
const ord = wanderCycle(n, wSeed, rowKey, c, pos, c === 0);
|
|
210
|
+
pos = ord[ord.length - 1];
|
|
211
|
+
}
|
|
212
|
+
return wanderCycle(n, wSeed, rowKey, cycle, pos, false);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* One cycle's played steps for `row`, in cycle-local ticks starting at 0 (SQ10, SQ60).
|
|
218
|
+
* Skipped steps take no time and are omitted.
|
|
219
|
+
*/
|
|
220
|
+
export function rowSchedule(row, pattern, cycle = 0) {
|
|
221
|
+
const start = Math.max(0, Math.min(row.steps.length, row.loopStart));
|
|
222
|
+
const end = Math.max(start, Math.min(row.steps.length, row.loopEnd));
|
|
223
|
+
const active = [];
|
|
224
|
+
for (let i = start; i < end; i++) {
|
|
225
|
+
if (!row.steps[i]?.skip)
|
|
226
|
+
active.push(i);
|
|
227
|
+
}
|
|
228
|
+
if (active.length === 0)
|
|
229
|
+
return [];
|
|
230
|
+
const order = playOrder(active.length, row.playback, pattern.seed, cycle, rowKeyHash(row.id));
|
|
231
|
+
const out = [];
|
|
232
|
+
let cursor = 0;
|
|
233
|
+
for (const pos of order) {
|
|
234
|
+
const stepIndex = active[pos];
|
|
235
|
+
const step = row.steps[stepIndex];
|
|
236
|
+
const lengthTicks = stepRateTicks(step?.stepRate ?? row.stepRate);
|
|
237
|
+
out.push({ stepIndex, startTick: cursor, lengthTicks });
|
|
238
|
+
cursor += lengthTicks;
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
function gcdBig(a, b) {
|
|
243
|
+
let x = a < 0n ? -a : a;
|
|
244
|
+
let y = b < 0n ? -b : b;
|
|
245
|
+
while (y !== 0n) {
|
|
246
|
+
const t = x % y;
|
|
247
|
+
x = y;
|
|
248
|
+
y = t;
|
|
249
|
+
}
|
|
250
|
+
return x;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Musical length of `pattern` in ticks: LCM of non-random row cycle lengths,
|
|
254
|
+
* capped at 64 bars (SQ10).
|
|
255
|
+
*/
|
|
256
|
+
export function patternMusicalLengthTicks(pattern, barTicks = 4 * MIDI_PPQ) {
|
|
257
|
+
const cap = BigInt(Math.max(1, Math.round(64 * barTicks)));
|
|
258
|
+
let acc = 0n;
|
|
259
|
+
for (const row of pattern.rows) {
|
|
260
|
+
if (row.playback === "random")
|
|
261
|
+
continue;
|
|
262
|
+
const sched = rowSchedule(row, pattern, 0);
|
|
263
|
+
const cycleLen = sched.reduce((sum, e) => sum + e.lengthTicks, 0);
|
|
264
|
+
if (cycleLen <= 0)
|
|
265
|
+
continue;
|
|
266
|
+
const len = BigInt(cycleLen);
|
|
267
|
+
if (acc === 0n) {
|
|
268
|
+
acc = len;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
const g = gcdBig(acc, len);
|
|
272
|
+
acc = (acc / g) * len;
|
|
273
|
+
}
|
|
274
|
+
if (acc >= cap)
|
|
275
|
+
return Number(cap);
|
|
276
|
+
}
|
|
277
|
+
if (acc === 0n) {
|
|
278
|
+
return Math.min(Number(cap), Math.max(1, pattern.lengthSteps) * stepRateTicks(pattern.stepRate));
|
|
279
|
+
}
|
|
280
|
+
return Number(acc);
|
|
281
|
+
}
|
|
282
|
+
/** Swing delay in ticks for a step starting at `tick` from the pattern start (SQ14, SQ60). */
|
|
283
|
+
export function swingDelay(tick, division, swing) {
|
|
284
|
+
if (swing <= 0)
|
|
285
|
+
return 0;
|
|
286
|
+
const divTicks = stepRateTicks(division);
|
|
287
|
+
if (tick < 0 || tick % divTicks !== 0)
|
|
288
|
+
return 0;
|
|
289
|
+
const slot = Math.floor(tick / divTicks);
|
|
290
|
+
if (slot % 2 !== 1)
|
|
291
|
+
return 0;
|
|
292
|
+
return Math.round((divTicks * swing) / 200);
|
|
293
|
+
}
|
|
294
|
+
/** Start offsets (in ticks from step start) of N note repeats inside length T (SQ15, SQ60). */
|
|
295
|
+
export function repeatPositions(totalTicks, repeat, distribution) {
|
|
296
|
+
const n = Math.max(1, Math.round(repeat));
|
|
297
|
+
if (n === 1)
|
|
298
|
+
return [0];
|
|
299
|
+
const exponent = Math.pow(2, -distribution / 100);
|
|
300
|
+
const out = [];
|
|
301
|
+
for (let k = 0; k < n; k++) {
|
|
302
|
+
out.push(Math.round(totalTicks * Math.pow(k / n, exponent)));
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
306
|
+
/** Velocities (1..127) of N note repeats with velocityRamp and velocityCurve (SQ15, SQ60). */
|
|
307
|
+
export function repeatVelocities(velocity, repeat, velocityRamp, velocityCurve) {
|
|
308
|
+
const n = Math.max(1, Math.round(repeat));
|
|
309
|
+
const clampVel = (v) => Math.max(1, Math.min(127, Math.round(v)));
|
|
310
|
+
if (n === 1)
|
|
311
|
+
return [clampVel(velocity)];
|
|
312
|
+
const curveExp = Math.pow(2, velocityCurve / 50);
|
|
313
|
+
const out = [];
|
|
314
|
+
for (let k = 0; k < n; k++) {
|
|
315
|
+
const x = k / (n - 1);
|
|
316
|
+
const scaled = velocity * (1 + (velocityRamp / 100) * Math.pow(x, curveExp));
|
|
317
|
+
out.push(clampVel(scaled));
|
|
318
|
+
}
|
|
319
|
+
return out;
|
|
320
|
+
}
|
|
321
|
+
// ── Pitch resolution (SQ4, SQ18, SQ19, SQ42, SQ60) ──────────────────────────
|
|
322
|
+
const TONIC_TO_PC = {
|
|
323
|
+
C: 0, "C#": 1, Db: 1, D: 2, "D#": 3, Eb: 3, E: 4, F: 5,
|
|
324
|
+
"F#": 6, Gb: 6, G: 7, "G#": 8, Ab: 8, A: 9, "A#": 10, Bb: 10, B: 11
|
|
325
|
+
};
|
|
326
|
+
/** Convert a document `MusicKey` (string tonic) or `StepKey` to `StepKey`. */
|
|
327
|
+
export function resolveStepKey(key) {
|
|
328
|
+
if (!key)
|
|
329
|
+
return { tonic: 0, mode: "major" };
|
|
330
|
+
if (typeof key.tonic === "number") {
|
|
331
|
+
return {
|
|
332
|
+
tonic: ((Math.round(key.tonic) % 12) + 12) % 12,
|
|
333
|
+
mode: key.mode
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
return { tonic: TONIC_TO_PC[key.tonic] ?? 0, mode: key.mode };
|
|
337
|
+
}
|
|
338
|
+
/** Signed shortest semitone interval from `fromTonic` to `toTonic` in [-5, +6]. */
|
|
339
|
+
export function shortestTonicDelta(fromTonic, toTonic) {
|
|
340
|
+
let d = (((toTonic - fromTonic) % 12) + 12) % 12;
|
|
341
|
+
if (d > 6)
|
|
342
|
+
d -= 12;
|
|
343
|
+
return d;
|
|
344
|
+
}
|
|
345
|
+
function degreeSemitones(degree, chord, keyMask) {
|
|
346
|
+
const iv = CHORD_QUALITIES[chord.quality].iv;
|
|
347
|
+
const mods = iv.map((x) => ((x % 12) + 12) % 12);
|
|
348
|
+
const scaleId = chordScale(chord, keyMask);
|
|
349
|
+
const scaleIv = SCALES[scaleId].iv;
|
|
350
|
+
const findIn = (candidates) => candidates.find((c) => mods.includes(c));
|
|
351
|
+
switch (degree) {
|
|
352
|
+
case "root":
|
|
353
|
+
return 0;
|
|
354
|
+
case "2nd":
|
|
355
|
+
return findIn([2, 1]) ?? scaleIv[1] ?? 2;
|
|
356
|
+
case "3rd":
|
|
357
|
+
return findIn([4, 3]) ?? scaleIv[2] ?? 4;
|
|
358
|
+
case "4th":
|
|
359
|
+
return findIn([5, 6]) ?? scaleIv[3] ?? 5;
|
|
360
|
+
case "5th":
|
|
361
|
+
return findIn([7, 6, 8]) ?? scaleIv[4] ?? 7;
|
|
362
|
+
case "6th":
|
|
363
|
+
return findIn([9, 8]) ?? scaleIv[5] ?? 9;
|
|
364
|
+
case "7th":
|
|
365
|
+
if (chord.quality === "dim7")
|
|
366
|
+
return 9;
|
|
367
|
+
return findIn([10, 11]) ?? scaleIv[6] ?? 10;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/** MIDI note number of pad `pad` on `instrument` (SQ4, SQ42). */
|
|
371
|
+
export function kitPadPitch(pad, instrument) {
|
|
372
|
+
if (instrument?.type === "drum") {
|
|
373
|
+
return Math.max(0, Math.min(127, instrument.baseNote + pad));
|
|
374
|
+
}
|
|
375
|
+
if (instrument?.type === "sampler") {
|
|
376
|
+
const zone = instrument.zones[pad];
|
|
377
|
+
if (zone)
|
|
378
|
+
return Math.max(0, Math.min(127, zone.rootNote));
|
|
379
|
+
}
|
|
380
|
+
return Math.max(0, Math.min(127, 36 + pad));
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Resolve the MIDI pitch for `step` on `row`, or `null` when the row produces
|
|
384
|
+
* no note (automation row, or a degree row with no sounding chord).
|
|
385
|
+
* Octave convention (D80, SQ60): C of octave `n` is `(n + 2) * 12` (C3 = 60).
|
|
386
|
+
*/
|
|
387
|
+
export function stepPitch(row, step, ctx = {}) {
|
|
388
|
+
const assign = row.assignment;
|
|
389
|
+
if (assign.type === "automation")
|
|
390
|
+
return null;
|
|
391
|
+
if (assign.type === "kit") {
|
|
392
|
+
return kitPadPitch(assign.pad, ctx.instrument);
|
|
393
|
+
}
|
|
394
|
+
const pattern = ctx.pattern;
|
|
395
|
+
const patternKey = resolveStepKey(pattern?.key);
|
|
396
|
+
const projectKey = resolveStepKey(ctx.key ?? pattern?.key);
|
|
397
|
+
const activeKey = pattern?.pitchSource === "key" ? projectKey : patternKey;
|
|
398
|
+
let pitch;
|
|
399
|
+
if (assign.type === "degree") {
|
|
400
|
+
if (pattern && pattern.pitchSource !== "chords")
|
|
401
|
+
return null;
|
|
402
|
+
const chord = ctx.chords ? chordAtTick(ctx.chords, ctx.docTick ?? 0) : undefined;
|
|
403
|
+
if (!chord)
|
|
404
|
+
return null;
|
|
405
|
+
const keyMask = scaleMask(activeKey.tonic, activeKey.mode);
|
|
406
|
+
const semi = degreeSemitones(assign.degree, chord, keyMask);
|
|
407
|
+
const octave = step.octave ?? assign.octave;
|
|
408
|
+
pitch = (octave + 2) * 12 + chord.root + semi + (step.note ?? 0);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
const basePitch = assign.pitch;
|
|
412
|
+
const pc = ((basePitch % 12) + 12) % 12;
|
|
413
|
+
const base = step.octave !== undefined ? (step.octave + 2) * 12 + pc : basePitch;
|
|
414
|
+
pitch = base + (step.note ?? 0);
|
|
415
|
+
if (pattern?.pitchSource === "key" && !pattern.noTranspose) {
|
|
416
|
+
pitch += shortestTonicDelta(patternKey.tonic, projectKey.tonic);
|
|
417
|
+
}
|
|
418
|
+
if (pattern?.scaleQuantize) {
|
|
419
|
+
const scaleId = pattern.scale !== "chromatic"
|
|
420
|
+
? pattern.scale
|
|
421
|
+
: pattern.pitchSource === "key"
|
|
422
|
+
? activeKey.mode
|
|
423
|
+
: "chromatic";
|
|
424
|
+
if (scaleId !== "chromatic") {
|
|
425
|
+
const table = scaleRemap(scaleMask(activeKey.tonic, scaleId), 0);
|
|
426
|
+
pitch = remapNote(table, pitch);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return Math.max(0, Math.min(127, Math.round(pitch)));
|
|
431
|
+
}
|
|
432
|
+
/** Pitch bend in semitones for a kit row step (SQ42, D90). */
|
|
433
|
+
export function kitStepPitchBend(padPitch, step) {
|
|
434
|
+
const baseOctave = Math.floor(padPitch / 12) - 2;
|
|
435
|
+
const octShift = step.octave !== undefined ? (step.octave - baseOctave) * 12 : 0;
|
|
436
|
+
return Math.max(-48, Math.min(48, Math.round((step.note ?? 0) + octShift)));
|
|
437
|
+
}
|
|
438
|
+
function conditionPasses(condition, cycle, lastCycle) {
|
|
439
|
+
if (!condition || condition.type === "always")
|
|
440
|
+
return true;
|
|
441
|
+
switch (condition.type) {
|
|
442
|
+
case "first":
|
|
443
|
+
return cycle === 0;
|
|
444
|
+
case "last":
|
|
445
|
+
return cycle === lastCycle;
|
|
446
|
+
case "recurrence":
|
|
447
|
+
return cycle % condition.cycles === condition.iteration - 1;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
function buildSegments(clipLengthTicks, loopLengthTicks, patternPhaseTicks) {
|
|
451
|
+
const total = Math.max(0, Math.round(clipLengthTicks));
|
|
452
|
+
if (total <= 0)
|
|
453
|
+
return [];
|
|
454
|
+
const phase = Math.max(0, Math.round(patternPhaseTicks));
|
|
455
|
+
const hasLoop = loopLengthTicks !== undefined &&
|
|
456
|
+
Number.isFinite(loopLengthTicks) &&
|
|
457
|
+
loopLengthTicks > 0 &&
|
|
458
|
+
loopLengthTicks < total;
|
|
459
|
+
if (!hasLoop) {
|
|
460
|
+
return [{ segIdx: 0, segStart: 0, segEnd: total, segLen: total, phase }];
|
|
461
|
+
}
|
|
462
|
+
const loopLen = Math.max(1, Math.round(loopLengthTicks));
|
|
463
|
+
const out = [];
|
|
464
|
+
for (let r = 0; r * loopLen < total; r++) {
|
|
465
|
+
const segStart = r * loopLen;
|
|
466
|
+
const segEnd = Math.min((r + 1) * loopLen, total);
|
|
467
|
+
out.push({
|
|
468
|
+
segIdx: r,
|
|
469
|
+
segStart,
|
|
470
|
+
segEnd,
|
|
471
|
+
segLen: segEnd - segStart,
|
|
472
|
+
phase: r === 0 ? phase : 0
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
return out;
|
|
476
|
+
}
|
|
477
|
+
function rowSegmentOccurrences(row, pattern, segments, rowState) {
|
|
478
|
+
const start = Math.max(0, Math.min(row.steps.length, row.loopStart));
|
|
479
|
+
const end = Math.max(start, Math.min(row.steps.length, row.loopEnd));
|
|
480
|
+
const active = [];
|
|
481
|
+
let maxNegPull = 0;
|
|
482
|
+
for (let i = start; i < end; i++) {
|
|
483
|
+
const st = row.steps[i];
|
|
484
|
+
if (st?.skip)
|
|
485
|
+
continue;
|
|
486
|
+
active.push(i);
|
|
487
|
+
const off = st?.offset ?? 0;
|
|
488
|
+
if (off < 0) {
|
|
489
|
+
const len = stepRateTicks(st?.stepRate ?? row.stepRate);
|
|
490
|
+
const pull = -Math.round((off / 100) * len);
|
|
491
|
+
if (pull > maxNegPull)
|
|
492
|
+
maxNegPull = pull;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (active.length === 0) {
|
|
496
|
+
return {
|
|
497
|
+
perSegment: segments.map(() => []),
|
|
498
|
+
lastCycle: rowState?.lastCycle ?? -1,
|
|
499
|
+
segmentStarts: segments.map(() => ({ cycleOffset: rowState?.cycleOffset ?? 0 })),
|
|
500
|
+
segmentLocalLastCycles: segments.map(() => -1)
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
const rk = rowKeyHash(row.id);
|
|
504
|
+
const perSegment = [];
|
|
505
|
+
const segmentStarts = [];
|
|
506
|
+
const segmentLocalLastCycles = [];
|
|
507
|
+
let cycle = rowState?.cycleOffset ?? 0;
|
|
508
|
+
let lastCycle = -1;
|
|
509
|
+
let prevWanderPos = rowState?.wanderPos;
|
|
510
|
+
for (const seg of segments) {
|
|
511
|
+
const segmentStart = { cycleOffset: cycle };
|
|
512
|
+
if (row.playback === "wander" && prevWanderPos !== undefined) {
|
|
513
|
+
segmentStart.wanderPos = prevWanderPos;
|
|
514
|
+
}
|
|
515
|
+
segmentStarts.push(segmentStart);
|
|
516
|
+
const limit = seg.phase + seg.segLen;
|
|
517
|
+
// For an unlooped clip, look ahead enough for a negative-offset step on the
|
|
518
|
+
// next pass to pull into the end of this window (SQ23 split continuity).
|
|
519
|
+
const passLimit = segments.length === 1 ? limit + maxNegPull : limit;
|
|
520
|
+
const occs = [];
|
|
521
|
+
let passStart = 0;
|
|
522
|
+
let safety = 0;
|
|
523
|
+
let segLastCycle = -1;
|
|
524
|
+
while (passStart < passLimit && safety < 16384) {
|
|
525
|
+
safety++;
|
|
526
|
+
const order = playOrder(active.length, row.playback, pattern.seed, cycle, rk, prevWanderPos);
|
|
527
|
+
if (order.length === 0)
|
|
528
|
+
break;
|
|
529
|
+
prevWanderPos = order[order.length - 1];
|
|
530
|
+
let cursor = 0;
|
|
531
|
+
if (passStart < limit) {
|
|
532
|
+
lastCycle = cycle;
|
|
533
|
+
segLastCycle = cycle;
|
|
534
|
+
}
|
|
535
|
+
for (const pos of order) {
|
|
536
|
+
const stepIndex = active[pos];
|
|
537
|
+
const step = row.steps[stepIndex] ?? {};
|
|
538
|
+
const lengthTicks = stepRateTicks(step.stepRate ?? row.stepRate);
|
|
539
|
+
occs.push({
|
|
540
|
+
stepIndex,
|
|
541
|
+
step,
|
|
542
|
+
rawStartTick: passStart + cursor,
|
|
543
|
+
lengthTicks,
|
|
544
|
+
cycle
|
|
545
|
+
});
|
|
546
|
+
cursor += lengthTicks;
|
|
547
|
+
}
|
|
548
|
+
if (cursor <= 0)
|
|
549
|
+
break;
|
|
550
|
+
passStart += cursor;
|
|
551
|
+
cycle += 1;
|
|
552
|
+
}
|
|
553
|
+
perSegment.push(occs);
|
|
554
|
+
segmentLocalLastCycles.push(segLastCycle);
|
|
555
|
+
}
|
|
556
|
+
return {
|
|
557
|
+
perSegment,
|
|
558
|
+
lastCycle: rowState?.lastCycle ?? lastCycle,
|
|
559
|
+
segmentStarts,
|
|
560
|
+
segmentLocalLastCycles
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Compute per-segment row cycle states for unrolling a looped pattern clip so
|
|
565
|
+
* every unrolled repeat preserves cycle indices, `last` conditions and `wander`
|
|
566
|
+
* continuity even for nonzero phases or loop periods that do not divide every
|
|
567
|
+
* row cycle (SQ13, SQ25).
|
|
568
|
+
*/
|
|
569
|
+
export function computeRowSegmentStates(pattern, clipLengthTicks, loopLengthTicks, patternPhaseTicks, initialRowCycles) {
|
|
570
|
+
const segments = buildSegments(clipLengthTicks, loopLengthTicks, patternPhaseTicks);
|
|
571
|
+
const perSeg = segments.map(() => ({}));
|
|
572
|
+
for (const row of pattern.rows) {
|
|
573
|
+
const initial = initialRowCycles?.[row.id];
|
|
574
|
+
const hasLast = row.steps.some((s) => s.condition?.type === "last");
|
|
575
|
+
const cycleDependent = initial !== undefined ||
|
|
576
|
+
row.playback === "shuffle" ||
|
|
577
|
+
row.playback === "random" ||
|
|
578
|
+
row.playback === "wander" ||
|
|
579
|
+
row.steps.some((s) => (s.chance !== undefined && s.chance < 100) ||
|
|
580
|
+
(s.condition !== undefined && s.condition.type !== "always"));
|
|
581
|
+
if (!cycleDependent)
|
|
582
|
+
continue;
|
|
583
|
+
const { lastCycle, segmentStarts, segmentLocalLastCycles } = rowSegmentOccurrences(row, pattern, segments, initial);
|
|
584
|
+
for (let s = 0; s < segments.length; s++) {
|
|
585
|
+
const start = segmentStarts[s];
|
|
586
|
+
const localLast = segmentLocalLastCycles[s];
|
|
587
|
+
const needsLast = hasLast && lastCycle >= 0 && lastCycle !== localLast;
|
|
588
|
+
const needsWander = row.playback === "wander" && start.wanderPos !== undefined;
|
|
589
|
+
if (start.cycleOffset > 0 || needsLast || needsWander) {
|
|
590
|
+
const state = { cycleOffset: start.cycleOffset };
|
|
591
|
+
if (needsLast)
|
|
592
|
+
state.lastCycle = lastCycle;
|
|
593
|
+
if (needsWander)
|
|
594
|
+
state.wanderPos = start.wanderPos;
|
|
595
|
+
perSeg[s][row.id] = state;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return perSeg.map((rec) => (Object.keys(rec).length > 0 ? rec : undefined));
|
|
600
|
+
}
|
|
601
|
+
function defaultAutomationValue(target) {
|
|
602
|
+
if (target.kind === "cc" && target.cc === 10)
|
|
603
|
+
return 64;
|
|
604
|
+
return 0;
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Expand `pattern` into MIDI notes, controller lanes and derived device
|
|
608
|
+
* automation lanes in clip content ticks `[0, clipLengthTicks)` (SQ9–SQ19, SQ41–SQ42).
|
|
609
|
+
*/
|
|
610
|
+
export function expandPattern(pattern, ctx) {
|
|
611
|
+
const segments = buildSegments(ctx.clipLengthTicks, ctx.loopLengthTicks, ctx.patternPhaseTicks ?? 0);
|
|
612
|
+
if (segments.length === 0 || pattern.rows.length === 0) {
|
|
613
|
+
return { notes: [], controllers: [], deviceAutomation: [] };
|
|
614
|
+
}
|
|
615
|
+
const hasSolo = pattern.rows.some((r) => r.solo);
|
|
616
|
+
const clipStartDocTick = ctx.clipStartDocTick ?? 0;
|
|
617
|
+
const notes = [];
|
|
618
|
+
const controllerMap = new Map();
|
|
619
|
+
const deviceAutomation = [];
|
|
620
|
+
for (const row of pattern.rows) {
|
|
621
|
+
const muted = row.mute || (hasSolo && !row.solo);
|
|
622
|
+
if (muted)
|
|
623
|
+
continue;
|
|
624
|
+
const rowKey = rowKeyHash(row.id);
|
|
625
|
+
const { perSegment, lastCycle } = rowSegmentOccurrences(row, pattern, segments, ctx.rowCycles?.[row.id]);
|
|
626
|
+
if (row.assignment.type === "automation") {
|
|
627
|
+
const target = row.assignment.target;
|
|
628
|
+
const rowChannel = Math.max(0, Math.min(15, (row.midiChannel || pattern.midiChannel || 1) - 1));
|
|
629
|
+
const interpolation = row.automationMode === "slide" ? "linear" : "step";
|
|
630
|
+
const pts = [];
|
|
631
|
+
for (let sIdx = 0; sIdx < segments.length; sIdx++) {
|
|
632
|
+
const seg = segments[sIdx];
|
|
633
|
+
const occs = perSegment[sIdx];
|
|
634
|
+
for (const occ of occs) {
|
|
635
|
+
const step = occ.step;
|
|
636
|
+
if (!step.on)
|
|
637
|
+
continue;
|
|
638
|
+
if (!conditionPasses(step.condition, occ.cycle, lastCycle))
|
|
639
|
+
continue;
|
|
640
|
+
const chance = step.chance ?? 100;
|
|
641
|
+
if (chance <= 0)
|
|
642
|
+
continue;
|
|
643
|
+
if (chance < 100 && stepHash(pattern.seed, rowKey, occ.stepIndex, occ.cycle) * 100 >= chance) {
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
const swung = occ.rawStartTick + swingDelay(occ.rawStartTick, pattern.swingDivision, pattern.swing);
|
|
647
|
+
const offsetTicks = Math.round(((step.offset ?? 0) / 100) * occ.lengthTicks);
|
|
648
|
+
let rawTick = swung + offsetTicks;
|
|
649
|
+
if (seg.phase === 0 && rawTick < 0)
|
|
650
|
+
rawTick = 0;
|
|
651
|
+
if (rawTick < seg.phase || rawTick >= seg.phase + seg.segLen)
|
|
652
|
+
continue;
|
|
653
|
+
const tick = seg.segStart + (rawTick - seg.phase);
|
|
654
|
+
const val = step.value ?? defaultAutomationValue(target);
|
|
655
|
+
pts.push({
|
|
656
|
+
id: `pa:${row.id}:${seg.segIdx}:${occ.cycle}:${occ.stepIndex}`,
|
|
657
|
+
tick,
|
|
658
|
+
value: val
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (pts.length === 0)
|
|
663
|
+
continue;
|
|
664
|
+
pts.sort((a, b) => a.tick - b.tick);
|
|
665
|
+
// Keep last value when two points land on the same tick.
|
|
666
|
+
const deduped = [];
|
|
667
|
+
for (const pt of pts) {
|
|
668
|
+
const prev = deduped[deduped.length - 1];
|
|
669
|
+
if (prev && prev.tick === pt.tick)
|
|
670
|
+
deduped[deduped.length - 1] = pt;
|
|
671
|
+
else
|
|
672
|
+
deduped.push(pt);
|
|
673
|
+
}
|
|
674
|
+
if (target.kind === "device") {
|
|
675
|
+
deviceAutomation.push({
|
|
676
|
+
rowId: row.id,
|
|
677
|
+
target,
|
|
678
|
+
automationMode: row.automationMode,
|
|
679
|
+
points: deduped.map(({ tick, value }) => ({ tick, value }))
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
683
|
+
const kind = target.kind;
|
|
684
|
+
const { min, max } = controllerValueRange(kind);
|
|
685
|
+
const key = `${kind}|${target.kind === "cc" ? target.cc : ""}|${rowChannel}`;
|
|
686
|
+
const existing = controllerMap.get(key);
|
|
687
|
+
const clampedPoints = deduped.map((p) => ({
|
|
688
|
+
id: p.id,
|
|
689
|
+
tick: p.tick,
|
|
690
|
+
value: Math.max(min, Math.min(max, Math.round(p.value)))
|
|
691
|
+
}));
|
|
692
|
+
if (existing) {
|
|
693
|
+
existing.points = [...existing.points, ...clampedPoints].sort((a, b) => a.tick - b.tick);
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
const lane = {
|
|
697
|
+
id: `p:${row.id}`,
|
|
698
|
+
kind,
|
|
699
|
+
channel: rowChannel,
|
|
700
|
+
interpolation,
|
|
701
|
+
points: clampedPoints
|
|
702
|
+
};
|
|
703
|
+
if (target.kind === "cc")
|
|
704
|
+
lane.cc = target.cc;
|
|
705
|
+
controllerMap.set(key, lane);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
// Note, melodic, kit or degree row.
|
|
711
|
+
for (let sIdx = 0; sIdx < segments.length; sIdx++) {
|
|
712
|
+
const seg = segments[sIdx];
|
|
713
|
+
const occs = perSegment[sIdx];
|
|
714
|
+
for (let i = 0; i < occs.length; i++) {
|
|
715
|
+
const occ = occs[i];
|
|
716
|
+
const step = occ.step;
|
|
717
|
+
if (step.tie)
|
|
718
|
+
continue; // Continues previous step; never starts a note (SQ12).
|
|
719
|
+
if (!step.on)
|
|
720
|
+
continue;
|
|
721
|
+
if (!conditionPasses(step.condition, occ.cycle, lastCycle))
|
|
722
|
+
continue;
|
|
723
|
+
const chance = step.chance ?? 100;
|
|
724
|
+
if (chance <= 0)
|
|
725
|
+
continue;
|
|
726
|
+
if (chance < 100 && stepHash(pattern.seed, rowKey, occ.stepIndex, occ.cycle) * 100 >= chance) {
|
|
727
|
+
continue;
|
|
728
|
+
}
|
|
729
|
+
// Collect tie chain starting at occ[i] through consecutive played tie steps (SQ12).
|
|
730
|
+
const chain = [occ];
|
|
731
|
+
let j = i + 1;
|
|
732
|
+
while (j < occs.length) {
|
|
733
|
+
const next = occs[j];
|
|
734
|
+
if (next.step.tie !== true)
|
|
735
|
+
break;
|
|
736
|
+
if (!conditionPasses(next.step.condition, next.cycle, lastCycle))
|
|
737
|
+
break;
|
|
738
|
+
const nextChance = next.step.chance ?? 100;
|
|
739
|
+
if (nextChance <= 0)
|
|
740
|
+
break;
|
|
741
|
+
if (nextChance < 100 &&
|
|
742
|
+
stepHash(pattern.seed, rowKey, next.stepIndex, next.cycle) * 100 >= nextChance) {
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
chain.push(next);
|
|
746
|
+
j++;
|
|
747
|
+
}
|
|
748
|
+
const swung = occ.rawStartTick + swingDelay(occ.rawStartTick, pattern.swingDivision, pattern.swing);
|
|
749
|
+
const offsetTicks = Math.round(((step.offset ?? 0) / 100) * occ.lengthTicks);
|
|
750
|
+
let rawTick = swung + offsetTicks;
|
|
751
|
+
if (seg.phase === 0 && rawTick < 0)
|
|
752
|
+
rawTick = 0;
|
|
753
|
+
if (rawTick < seg.phase || rawTick >= seg.phase + seg.segLen)
|
|
754
|
+
continue;
|
|
755
|
+
const contentStart = seg.segStart + (rawTick - seg.phase);
|
|
756
|
+
const pitch = stepPitch(row, step, {
|
|
757
|
+
pattern,
|
|
758
|
+
chords: ctx.chords,
|
|
759
|
+
docTick: clipStartDocTick + contentStart,
|
|
760
|
+
key: ctx.key,
|
|
761
|
+
instrument: ctx.instrument
|
|
762
|
+
});
|
|
763
|
+
if (pitch === null)
|
|
764
|
+
continue;
|
|
765
|
+
const lastOcc = chain[chain.length - 1];
|
|
766
|
+
const lastGate = lastOcc.step.gate ?? 100;
|
|
767
|
+
const repeat = Math.max(1, Math.min(16, Math.round(step.repeat ?? 1)));
|
|
768
|
+
const velocity = Math.max(1, Math.min(127, Math.round(step.velocity ?? 100)));
|
|
769
|
+
const ch1 = step.midiChannel ?? row.midiChannel ?? pattern.midiChannel ?? 1;
|
|
770
|
+
const channel = Math.max(0, Math.min(15, Math.round(ch1) - 1));
|
|
771
|
+
const kitBend = row.assignment.type === "kit" ? kitStepPitchBend(pitch, step) : 0;
|
|
772
|
+
if (repeat === 1) {
|
|
773
|
+
let prefixTicks = 0;
|
|
774
|
+
for (let c = 0; c < chain.length - 1; c++) {
|
|
775
|
+
prefixTicks += chain[c].lengthTicks;
|
|
776
|
+
}
|
|
777
|
+
let durationTick = Math.max(1, prefixTicks + Math.round((lastOcc.lengthTicks * lastGate) / 100));
|
|
778
|
+
if (pattern.legato && lastGate === 100)
|
|
779
|
+
durationTick += 1;
|
|
780
|
+
// Stop at segment / clip end (SQ17), except allow legato's +1 tick when
|
|
781
|
+
// inside the clip window.
|
|
782
|
+
const maxDur = seg.segEnd - contentStart;
|
|
783
|
+
if (maxDur <= 0)
|
|
784
|
+
continue;
|
|
785
|
+
if (durationTick > maxDur)
|
|
786
|
+
durationTick = maxDur;
|
|
787
|
+
const note = {
|
|
788
|
+
id: `p:${row.id}:${seg.segIdx}:${occ.cycle}:${occ.stepIndex}:0`,
|
|
789
|
+
pitch,
|
|
790
|
+
velocity,
|
|
791
|
+
startTick: contentStart,
|
|
792
|
+
durationTick,
|
|
793
|
+
channel
|
|
794
|
+
};
|
|
795
|
+
if (kitBend !== 0) {
|
|
796
|
+
note.expression = { pitch: [{ tick: 0, value: kitBend }] };
|
|
797
|
+
}
|
|
798
|
+
notes.push(note);
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
801
|
+
const chainLen = chain.reduce((sum, c) => sum + c.lengthTicks, 0);
|
|
802
|
+
const positions = repeatPositions(chainLen, repeat, step.distribution ?? 0);
|
|
803
|
+
const velocities = repeatVelocities(velocity, repeat, step.velocityRamp ?? 0, step.velocityCurve ?? 0);
|
|
804
|
+
for (let k = 0; k < repeat; k++) {
|
|
805
|
+
const subStart = contentStart + positions[k];
|
|
806
|
+
if (subStart >= seg.segEnd)
|
|
807
|
+
break;
|
|
808
|
+
const nextPos = k + 1 < repeat ? positions[k + 1] : chainLen;
|
|
809
|
+
const subSpan = Math.max(1, nextPos - positions[k]);
|
|
810
|
+
let subDur = Math.max(1, Math.round((subSpan * lastGate) / 100));
|
|
811
|
+
if (pattern.legato && lastGate === 100)
|
|
812
|
+
subDur += 1;
|
|
813
|
+
const maxDur = seg.segEnd - subStart;
|
|
814
|
+
if (maxDur <= 0)
|
|
815
|
+
continue;
|
|
816
|
+
if (subDur > maxDur)
|
|
817
|
+
subDur = maxDur;
|
|
818
|
+
const note = {
|
|
819
|
+
id: `p:${row.id}:${seg.segIdx}:${occ.cycle}:${occ.stepIndex}:${k}`,
|
|
820
|
+
pitch,
|
|
821
|
+
velocity: velocities[k],
|
|
822
|
+
startTick: subStart,
|
|
823
|
+
durationTick: subDur,
|
|
824
|
+
channel
|
|
825
|
+
};
|
|
826
|
+
if (kitBend !== 0) {
|
|
827
|
+
note.expression = { pitch: [{ tick: 0, value: kitBend }] };
|
|
828
|
+
}
|
|
829
|
+
notes.push(note);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
notes: sortNotes(notes),
|
|
837
|
+
controllers: [...controllerMap.values()],
|
|
838
|
+
deviceAutomation
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Expand a clip's `pattern` against document tempo, chords, key and track
|
|
843
|
+
* instrument. Returns `null` when `clip.pattern` is absent.
|
|
844
|
+
*/
|
|
845
|
+
export function expandPatternClip(clip, doc = {}) {
|
|
846
|
+
if (!clip.pattern)
|
|
847
|
+
return null;
|
|
848
|
+
const tempo = resolveTempo(doc);
|
|
849
|
+
const clipLengthTicks = Math.max(0, Math.round(msToTicks(clip.durationMs, tempo.bpm)));
|
|
850
|
+
const loopMs = activeLoopMs(clip);
|
|
851
|
+
const loopLengthTicks = loopMs !== undefined ? Math.max(1, Math.round(msToTicks(loopMs, tempo.bpm))) : undefined;
|
|
852
|
+
const inPointTicks = Math.max(0, Math.round(msToTicks(clip.inPointMs ?? 0, tempo.bpm)));
|
|
853
|
+
const patternPhaseTicks = (clip.patternPhaseTicks ?? 0) + inPointTicks;
|
|
854
|
+
const clipStartDocTick = Math.round(msToTicks(clip.startMs - tempo.offsetMs, tempo.bpm));
|
|
855
|
+
const track = doc.tracks?.find((t) => t.id === clip.trackId);
|
|
856
|
+
const chordTrack = doc.tracks ? chordTrackFor(undefined, doc.tracks) : undefined;
|
|
857
|
+
return expandPattern(clip.pattern, {
|
|
858
|
+
clipLengthTicks,
|
|
859
|
+
loopLengthTicks,
|
|
860
|
+
patternPhaseTicks,
|
|
861
|
+
rowCycles: clip.patternRowCycles,
|
|
862
|
+
clipStartDocTick,
|
|
863
|
+
chords: chordTrack?.chords,
|
|
864
|
+
key: tempo.key,
|
|
865
|
+
instrument: track?.instrument
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
const BOUNDARY_EPS_MS = 1e-3;
|
|
869
|
+
/**
|
|
870
|
+
* Overlay derived `kind: "device"` automation from pattern clips on `track`
|
|
871
|
+
* onto `track.automation` (SQ41). Stored automation wins outside each pattern
|
|
872
|
+
* clip's `[startMs, startMs + durationMs)` window.
|
|
873
|
+
*/
|
|
874
|
+
export function deriveTrackAutomationFromPatterns(track, clips, doc = {}) {
|
|
875
|
+
const tempo = resolveTempo(doc);
|
|
876
|
+
const patternClips = clips
|
|
877
|
+
.filter((c) => c.trackId === track.id && c.pattern !== undefined && !c.muted && !c.hidden)
|
|
878
|
+
.sort((a, b) => a.startMs - b.startMs);
|
|
879
|
+
const base = (track.automation ?? []).map((lane) => ({
|
|
880
|
+
...lane,
|
|
881
|
+
points: lane.points.map((p) => ({ ...p }))
|
|
882
|
+
}));
|
|
883
|
+
if (patternClips.length === 0)
|
|
884
|
+
return base;
|
|
885
|
+
const byKey = new Map();
|
|
886
|
+
const originalStored = new Map();
|
|
887
|
+
for (const lane of base) {
|
|
888
|
+
if (lane.target.kind === "device") {
|
|
889
|
+
const key = `${lane.target.deviceId}:${lane.target.param}`;
|
|
890
|
+
byKey.set(key, lane);
|
|
891
|
+
if (lane.points.length > 0) {
|
|
892
|
+
originalStored.set(key, lane.points.map((p) => ({ ...p })));
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
for (const clip of patternClips) {
|
|
897
|
+
const expanded = expandPatternClip(clip, {
|
|
898
|
+
tracks: doc.tracks ?? [track],
|
|
899
|
+
tempo
|
|
900
|
+
});
|
|
901
|
+
if (!expanded || expanded.deviceAutomation.length === 0)
|
|
902
|
+
continue;
|
|
903
|
+
const clipStart = clip.startMs;
|
|
904
|
+
const clipEnd = clip.startMs + clip.durationMs;
|
|
905
|
+
for (const derived of expanded.deviceAutomation) {
|
|
906
|
+
if (derived.points.length === 0)
|
|
907
|
+
continue;
|
|
908
|
+
const key = `${derived.target.deviceId}:${derived.target.param}`;
|
|
909
|
+
const easing = derived.automationMode === "latch" ? "hold" : undefined;
|
|
910
|
+
const clipPoints = derived.points.map((p, idx) => {
|
|
911
|
+
const pt = {
|
|
912
|
+
timeMs: clipStart + ticksToMs(p.tick, tempo.bpm),
|
|
913
|
+
value: p.value
|
|
914
|
+
};
|
|
915
|
+
if (idx > 0 && easing)
|
|
916
|
+
pt.easing = easing;
|
|
917
|
+
return pt;
|
|
918
|
+
});
|
|
919
|
+
const existing = byKey.get(key);
|
|
920
|
+
if (!existing) {
|
|
921
|
+
const created = {
|
|
922
|
+
id: `pattern-auto:${clip.id}:${derived.rowId}`,
|
|
923
|
+
target: { ...derived.target },
|
|
924
|
+
points: clipPoints,
|
|
925
|
+
enabled: true
|
|
926
|
+
};
|
|
927
|
+
base.push(created);
|
|
928
|
+
byKey.set(key, created);
|
|
929
|
+
}
|
|
930
|
+
else {
|
|
931
|
+
// Stored lane wins outside [clipStart, clipEnd); pattern points replace inside.
|
|
932
|
+
const origPoints = originalStored.get(key);
|
|
933
|
+
const entryTime = clipPoints[0].timeMs;
|
|
934
|
+
const before = existing.points.filter((p) => p.timeMs < entryTime);
|
|
935
|
+
const after = existing.points.filter((p) => p.timeMs > clipEnd);
|
|
936
|
+
const prePoints = [];
|
|
937
|
+
if (existing.points.length > 0 && entryTime > 0) {
|
|
938
|
+
const preTime = Math.max(0, entryTime - BOUNDARY_EPS_MS);
|
|
939
|
+
const lastBefore = before[before.length - 1];
|
|
940
|
+
if (!lastBefore || lastBefore.timeMs < preTime - 1e-6) {
|
|
941
|
+
prePoints.push({
|
|
942
|
+
timeMs: preTime,
|
|
943
|
+
value: automationValueAt(existing.points, preTime)
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
// Hold across the tiny pre-guard -> entry boundary so the stored lane
|
|
947
|
+
// keeps its exact slope up to the clip and jumps cleanly at entryTime.
|
|
948
|
+
clipPoints[0] = { ...clipPoints[0], easing: "hold" };
|
|
949
|
+
}
|
|
950
|
+
const postPoints = [];
|
|
951
|
+
if (origPoints && origPoints.length > 0) {
|
|
952
|
+
const lastClipPt = clipPoints[clipPoints.length - 1];
|
|
953
|
+
if (clipEnd > lastClipPt.timeMs) {
|
|
954
|
+
postPoints.push({
|
|
955
|
+
timeMs: clipEnd,
|
|
956
|
+
value: automationValueAt(origPoints, clipEnd),
|
|
957
|
+
easing: "hold"
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
existing.points = [
|
|
962
|
+
...before,
|
|
963
|
+
...prePoints,
|
|
964
|
+
...clipPoints,
|
|
965
|
+
...postPoints,
|
|
966
|
+
...after
|
|
967
|
+
].sort((a, b) => a.timeMs - b.timeMs);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return base;
|
|
972
|
+
}
|
|
973
|
+
const patternAutoTrackCache = new WeakMap();
|
|
974
|
+
/**
|
|
975
|
+
* Return `tracks` with every pattern clip's `kind: "device"` automation rows
|
|
976
|
+
* overlaid onto its track's `automation` lanes (SQ41). Tracks without pattern
|
|
977
|
+
* device automation keep their original object identity, and results are
|
|
978
|
+
* memoized on `(tracks, clips, tempo, chords)` for per-frame playback.
|
|
979
|
+
*/
|
|
980
|
+
export function tracksWithPatternAutomation(tracks, clips, doc = {}) {
|
|
981
|
+
const tempo = resolveTempo(doc);
|
|
982
|
+
const chordTrack = chordTrackFor(undefined, doc.tracks ?? tracks);
|
|
983
|
+
let byClips = patternAutoTrackCache.get(tracks);
|
|
984
|
+
const hit = byClips?.get(clips);
|
|
985
|
+
if (hit &&
|
|
986
|
+
hit.bpm === tempo.bpm &&
|
|
987
|
+
hit.offsetMs === tempo.offsetMs &&
|
|
988
|
+
hit.key === tempo.key &&
|
|
989
|
+
hit.chords === chordTrack?.chords) {
|
|
990
|
+
return hit.tracks;
|
|
991
|
+
}
|
|
992
|
+
const patternTracks = new Set();
|
|
993
|
+
for (const c of clips) {
|
|
994
|
+
if (c.pattern &&
|
|
995
|
+
!c.muted &&
|
|
996
|
+
!c.hidden &&
|
|
997
|
+
c.pattern.rows.some((r) => r.assignment.type === "automation" && r.assignment.target.kind === "device")) {
|
|
998
|
+
patternTracks.add(c.trackId);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
let changed = false;
|
|
1002
|
+
const result = patternTracks.size === 0
|
|
1003
|
+
? tracks
|
|
1004
|
+
: tracks.map((track) => {
|
|
1005
|
+
if (!patternTracks.has(track.id))
|
|
1006
|
+
return track;
|
|
1007
|
+
const merged = deriveTrackAutomationFromPatterns(track, clips, {
|
|
1008
|
+
tracks: doc.tracks ?? tracks,
|
|
1009
|
+
tempo
|
|
1010
|
+
});
|
|
1011
|
+
changed = true;
|
|
1012
|
+
return { ...track, automation: merged };
|
|
1013
|
+
});
|
|
1014
|
+
const out = changed ? result : tracks;
|
|
1015
|
+
if (!byClips) {
|
|
1016
|
+
byClips = new WeakMap();
|
|
1017
|
+
patternAutoTrackCache.set(tracks, byClips);
|
|
1018
|
+
}
|
|
1019
|
+
byClips.set(clips, {
|
|
1020
|
+
bpm: tempo.bpm,
|
|
1021
|
+
offsetMs: tempo.offsetMs,
|
|
1022
|
+
key: tempo.key,
|
|
1023
|
+
chords: chordTrack?.chords,
|
|
1024
|
+
tracks: out
|
|
1025
|
+
});
|
|
1026
|
+
return out;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Derive clip-local subtractive instrument `cutoffHz` control breakpoints from
|
|
1030
|
+
* an effective track's automation lanes (stored lanes + SQ41 pattern overlays).
|
|
1031
|
+
* Shared by preview rendering, browser export, and server export.
|
|
1032
|
+
*/
|
|
1033
|
+
export function instrumentCutoffControls(track, clip, sampleRate) {
|
|
1034
|
+
if (!track || !automationModePlays(automationModeOf(track)))
|
|
1035
|
+
return undefined;
|
|
1036
|
+
const lane = (track.automation ?? []).find((l) => isAutomationLaneActive(l) &&
|
|
1037
|
+
l.target.kind === "device" &&
|
|
1038
|
+
l.target.deviceId === "instrument" &&
|
|
1039
|
+
(l.target.param === "cutoffHz" || l.target.param === "filter.cutoff"));
|
|
1040
|
+
if (!lane)
|
|
1041
|
+
return undefined;
|
|
1042
|
+
const clipEndMs = clip.startMs + clip.durationMs;
|
|
1043
|
+
const maxHz = Math.max(20, sampleRate / 2 - 1);
|
|
1044
|
+
const points = [];
|
|
1045
|
+
for (const bp of automationBreakpoints(lane.points, clip.startMs, {
|
|
1046
|
+
maxStepMs: 10
|
|
1047
|
+
})) {
|
|
1048
|
+
if (bp.timeMs > clipEndMs)
|
|
1049
|
+
break;
|
|
1050
|
+
const frame = Math.max(0, Math.round(((bp.timeMs - clip.startMs) / 1000) * sampleRate));
|
|
1051
|
+
const value = Math.min(maxHz, Math.max(20, bp.value));
|
|
1052
|
+
const last = points[points.length - 1];
|
|
1053
|
+
if (last && last.frame === frame) {
|
|
1054
|
+
if (last.value !== value && frame > 0) {
|
|
1055
|
+
last.frame = frame - 1;
|
|
1056
|
+
points.push({ frame, value });
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
last.value = value;
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
else {
|
|
1063
|
+
points.push({ frame, value });
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
return points.length > 0 ? { cutoffHz: points } : undefined;
|
|
1067
|
+
}
|
|
1068
|
+
//# sourceMappingURL=stepPatternExpand.js.map
|