@nodetool-ai/timeline 0.8.18 → 0.8.20
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/animation/custom.d.ts.map +1 -1
- package/dist/animation/custom.js +35 -0
- package/dist/animation/custom.js.map +1 -1
- package/dist/animation/sample.d.ts +14 -0
- package/dist/animation/sample.d.ts.map +1 -1
- package/dist/animation/sample.js +22 -1
- package/dist/animation/sample.js.map +1 -1
- package/dist/animation/types.d.ts +8 -1
- package/dist/animation/types.d.ts.map +1 -1
- package/dist/animation/types.js +31 -3
- package/dist/animation/types.js.map +1 -1
- package/dist/authoredStyles.d.ts +7 -1
- package/dist/authoredStyles.d.ts.map +1 -1
- package/dist/authoredStyles.js +6 -1
- package/dist/authoredStyles.js.map +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/finish-storyboard.d.ts.map +1 -1
- package/dist/finish-storyboard.js +6 -1
- package/dist/finish-storyboard.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 +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/interchange.d.ts.map +1 -1
- package/dist/interchange.js +3 -0
- package/dist/interchange.js.map +1 -1
- package/dist/keyframes.d.ts +12 -2
- package/dist/keyframes.d.ts.map +1 -1
- package/dist/keyframes.js +24 -1
- package/dist/keyframes.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/model3d.d.ts +132 -8
- package/dist/model3d.d.ts.map +1 -1
- package/dist/model3d.js +230 -7
- package/dist/model3d.js.map +1 -1
- package/dist/model3dBake.d.ts +41 -11
- package/dist/model3dBake.d.ts.map +1 -1
- package/dist/model3dBake.js +58 -21
- package/dist/model3dBake.js.map +1 -1
- package/dist/model3dBlockout.d.ts +62 -0
- package/dist/model3dBlockout.d.ts.map +1 -0
- package/dist/model3dBlockout.js +59 -0
- package/dist/model3dBlockout.js.map +1 -0
- package/dist/model3dCameraDescription.d.ts +32 -0
- package/dist/model3dCameraDescription.d.ts.map +1 -0
- package/dist/model3dCameraDescription.js +253 -0
- package/dist/model3dCameraDescription.js.map +1 -0
- package/dist/model3dCameraKeys.d.ts +47 -0
- package/dist/model3dCameraKeys.d.ts.map +1 -0
- package/dist/model3dCameraKeys.js +156 -0
- package/dist/model3dCameraKeys.js.map +1 -0
- package/dist/model3dCameraMoves.d.ts +97 -0
- package/dist/model3dCameraMoves.d.ts.map +1 -0
- package/dist/model3dCameraMoves.js +329 -0
- package/dist/model3dCameraMoves.js.map +1 -0
- package/dist/model3dReference.d.ts +96 -0
- package/dist/model3dReference.d.ts.map +1 -0
- package/dist/model3dReference.js +113 -0
- package/dist/model3dReference.js.map +1 -0
- 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 +106 -0
- package/dist/music/plan.d.ts.map +1 -0
- package/dist/music/plan.js +304 -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 +1009 -230
- 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 +94 -11
- package/dist/ops/op.d.ts.map +1 -1
- package/dist/ops/op.js +62 -1
- package/dist/ops/op.js.map +1 -1
- package/dist/ops/parse.d.ts.map +1 -1
- package/dist/ops/parse.js +316 -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 +260 -2
- package/dist/ops/types.d.ts.map +1 -1
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.d.ts.map +1 -1
- package/dist/render/index.js +1 -0
- package/dist/render/index.js.map +1 -1
- package/dist/render/model3dReferenceSamples.d.ts +25 -0
- package/dist/render/model3dReferenceSamples.d.ts.map +1 -0
- package/dist/render/model3dReferenceSamples.js +25 -0
- package/dist/render/model3dReferenceSamples.js.map +1 -0
- package/dist/render/sceneModel.d.ts +1 -1
- package/dist/render/sceneModel.d.ts.map +1 -1
- package/dist/render/sceneModel.js +10 -5
- package/dist/render/sceneModel.js.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/scene.d.ts +1 -0
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +1 -0
- package/dist/scene.js.map +1 -1
- package/dist/shotLink.d.ts +35 -0
- package/dist/shotLink.d.ts.map +1 -1
- package/dist/shotLink.js +100 -0
- package/dist/shotLink.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/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 +872 -13
- 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 +331 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +11 -5
|
@@ -0,0 +1,1390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Step Sequencer data model helpers: defaults, normalization, compaction,
|
|
3
|
+
* labels, length resizing, subrows, sorting, transforms, text format (SQ47),
|
|
4
|
+
* and MIDI -> Pattern conversion (SQ21).
|
|
5
|
+
*/
|
|
6
|
+
import { controllerValueAt } from "./controllers.js";
|
|
7
|
+
import { GM_DRUM_LANES } from "./stepEditor.js";
|
|
8
|
+
import { STEP_RATES, kitPadPitch, nudgeStepRate, resolveStepKey, rowKeyHash, shortestTonicDelta, stepHash, stepRateTicks } from "./stepPatternExpand.js";
|
|
9
|
+
export const STEP_PATTERN_MAX_ROWS = 128;
|
|
10
|
+
export const STEP_PATTERN_MAX_STEPS = 64;
|
|
11
|
+
export const STEP_PATTERN_PAGE_SIZE = 16;
|
|
12
|
+
export const STEP_PLAYBACK_MODES = [
|
|
13
|
+
"forward",
|
|
14
|
+
"backward",
|
|
15
|
+
"pingPong",
|
|
16
|
+
"pendulum",
|
|
17
|
+
"wander",
|
|
18
|
+
"shuffle",
|
|
19
|
+
"random"
|
|
20
|
+
];
|
|
21
|
+
export const STEP_EDIT_MODES = [
|
|
22
|
+
"onOff",
|
|
23
|
+
"velocity",
|
|
24
|
+
"value",
|
|
25
|
+
"gate",
|
|
26
|
+
"tie",
|
|
27
|
+
"note",
|
|
28
|
+
"octave",
|
|
29
|
+
"repeat",
|
|
30
|
+
"chance",
|
|
31
|
+
"offset",
|
|
32
|
+
"stepRate",
|
|
33
|
+
"skip",
|
|
34
|
+
"loop"
|
|
35
|
+
];
|
|
36
|
+
export const STEP_DEGREES = [
|
|
37
|
+
"root", "2nd", "3rd", "4th", "5th", "6th", "7th"
|
|
38
|
+
];
|
|
39
|
+
export const STEP_ROW_SORTS = [
|
|
40
|
+
"none",
|
|
41
|
+
"typeAsc",
|
|
42
|
+
"typeDesc",
|
|
43
|
+
"typeAndNoteAsc",
|
|
44
|
+
"typeAndNoteDesc",
|
|
45
|
+
"typeAndNameAsc",
|
|
46
|
+
"typeAndNameDesc",
|
|
47
|
+
"typeAndKitPadAsc",
|
|
48
|
+
"typeAndKitPadDesc"
|
|
49
|
+
];
|
|
50
|
+
const NOTE_SUBROW_MODES = [
|
|
51
|
+
"velocity", "gate", "tie", "note", "octave", "repeat", "chance", "offset", "stepRate", "skip", "loop"
|
|
52
|
+
];
|
|
53
|
+
const AUTOMATION_SUBROW_MODES = [
|
|
54
|
+
"value", "chance", "offset", "stepRate", "skip", "loop"
|
|
55
|
+
];
|
|
56
|
+
const clampInt = (v, lo, hi) => Math.max(lo, Math.min(hi, Math.round(v)));
|
|
57
|
+
/** Normalize one step cell with the exact SQ5 / D77 defaults. */
|
|
58
|
+
export function normalizeStep(step = {}) {
|
|
59
|
+
const cond = step.condition?.type === "recurrence"
|
|
60
|
+
? {
|
|
61
|
+
type: "recurrence",
|
|
62
|
+
cycles: clampInt(step.condition.cycles, 2, 16),
|
|
63
|
+
iteration: clampInt(step.condition.iteration, 1, clampInt(step.condition.cycles, 2, 16))
|
|
64
|
+
}
|
|
65
|
+
: step.condition?.type === "first" || step.condition?.type === "last"
|
|
66
|
+
? { type: step.condition.type }
|
|
67
|
+
: { type: "always" };
|
|
68
|
+
return {
|
|
69
|
+
on: step.on === true,
|
|
70
|
+
velocity: step.velocity !== undefined ? clampInt(step.velocity, 1, 127) : 100,
|
|
71
|
+
gate: step.gate !== undefined ? clampInt(step.gate, 1, 100) : 100,
|
|
72
|
+
tie: step.tie === true,
|
|
73
|
+
note: step.note !== undefined ? clampInt(step.note, -12, 12) : 0,
|
|
74
|
+
octave: step.octave !== undefined ? clampInt(step.octave, -2, 8) : undefined,
|
|
75
|
+
repeat: step.repeat !== undefined ? clampInt(step.repeat, 1, 16) : 1,
|
|
76
|
+
chance: step.chance !== undefined ? clampInt(step.chance, 0, 100) : 100,
|
|
77
|
+
offset: step.offset !== undefined ? clampInt(step.offset, -50, 50) : 0,
|
|
78
|
+
stepRate: step.stepRate,
|
|
79
|
+
skip: step.skip === true,
|
|
80
|
+
value: step.value !== undefined && Number.isFinite(step.value) ? step.value : undefined,
|
|
81
|
+
condition: cond,
|
|
82
|
+
midiChannel: step.midiChannel !== undefined ? clampInt(step.midiChannel, 1, 16) : undefined,
|
|
83
|
+
articulation: step.articulation !== undefined ? clampInt(step.articulation, 0, 255) : 0,
|
|
84
|
+
velocityRamp: step.velocityRamp !== undefined ? clampInt(step.velocityRamp, -100, 100) : 0,
|
|
85
|
+
velocityCurve: step.velocityCurve !== undefined ? clampInt(step.velocityCurve, -100, 100) : 0,
|
|
86
|
+
distribution: step.distribution !== undefined ? clampInt(step.distribution, -100, 100) : 0
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Omit every field equal to its SQ5 default so stored patterns stay small. */
|
|
90
|
+
export function compactStep(step) {
|
|
91
|
+
const n = normalizeStep(step);
|
|
92
|
+
const out = {};
|
|
93
|
+
if (n.on)
|
|
94
|
+
out.on = true;
|
|
95
|
+
if (n.velocity !== 100)
|
|
96
|
+
out.velocity = n.velocity;
|
|
97
|
+
if (n.gate !== 100)
|
|
98
|
+
out.gate = n.gate;
|
|
99
|
+
if (n.tie)
|
|
100
|
+
out.tie = true;
|
|
101
|
+
if (n.note !== 0)
|
|
102
|
+
out.note = n.note;
|
|
103
|
+
if (n.octave !== undefined)
|
|
104
|
+
out.octave = n.octave;
|
|
105
|
+
if (n.repeat !== 1)
|
|
106
|
+
out.repeat = n.repeat;
|
|
107
|
+
if (n.chance !== 100)
|
|
108
|
+
out.chance = n.chance;
|
|
109
|
+
if (n.offset !== 0)
|
|
110
|
+
out.offset = n.offset;
|
|
111
|
+
if (n.stepRate !== undefined)
|
|
112
|
+
out.stepRate = n.stepRate;
|
|
113
|
+
if (n.skip)
|
|
114
|
+
out.skip = true;
|
|
115
|
+
if (n.value !== undefined)
|
|
116
|
+
out.value = n.value;
|
|
117
|
+
if (n.condition.type !== "always")
|
|
118
|
+
out.condition = n.condition;
|
|
119
|
+
if (n.midiChannel !== undefined)
|
|
120
|
+
out.midiChannel = n.midiChannel;
|
|
121
|
+
if (n.articulation !== 0)
|
|
122
|
+
out.articulation = n.articulation;
|
|
123
|
+
if (n.velocityRamp !== 0)
|
|
124
|
+
out.velocityRamp = n.velocityRamp;
|
|
125
|
+
if (n.velocityCurve !== 0)
|
|
126
|
+
out.velocityCurve = n.velocityCurve;
|
|
127
|
+
if (n.distribution !== 0)
|
|
128
|
+
out.distribution = n.distribution;
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
/** Default subrows per row type (SQ8). */
|
|
132
|
+
export function defaultSubrowsForAssignment(assignment) {
|
|
133
|
+
switch (assignment.type) {
|
|
134
|
+
case "note":
|
|
135
|
+
case "kit":
|
|
136
|
+
return ["velocity", "gate"];
|
|
137
|
+
case "melodic":
|
|
138
|
+
return ["note", "octave"];
|
|
139
|
+
case "degree":
|
|
140
|
+
return ["velocity", "octave"];
|
|
141
|
+
case "automation":
|
|
142
|
+
return ["value"];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export function isNoteRow(row) {
|
|
146
|
+
return row.assignment.type !== "automation";
|
|
147
|
+
}
|
|
148
|
+
function fitSteps(steps, lengthSteps) {
|
|
149
|
+
const src = steps ?? [];
|
|
150
|
+
const out = [];
|
|
151
|
+
for (let i = 0; i < lengthSteps; i++) {
|
|
152
|
+
out.push(src[i] ? compactStep(src[i]) : {});
|
|
153
|
+
}
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
/** Normalize one row against pattern-level defaults (SQ3). */
|
|
157
|
+
export function normalizeRow(partial, defaults) {
|
|
158
|
+
const maxLen = clampInt(defaults.lengthSteps, 1, STEP_PATTERN_MAX_STEPS);
|
|
159
|
+
const lengthSteps = clampInt(partial.lengthSteps ?? maxLen, 1, maxLen);
|
|
160
|
+
const loopEnd = clampInt(partial.loopEnd ?? lengthSteps, 1, lengthSteps);
|
|
161
|
+
const loopStart = clampInt(partial.loopStart ?? 0, 0, loopEnd - 1);
|
|
162
|
+
const subrows = partial.subrows
|
|
163
|
+
? [...new Set(partial.subrows)]
|
|
164
|
+
: defaultSubrowsForAssignment(partial.assignment);
|
|
165
|
+
const row = {
|
|
166
|
+
id: partial.id,
|
|
167
|
+
assignment: structuredClone(partial.assignment),
|
|
168
|
+
mute: partial.mute === true,
|
|
169
|
+
solo: partial.solo === true,
|
|
170
|
+
lengthSteps,
|
|
171
|
+
loopStart,
|
|
172
|
+
loopEnd,
|
|
173
|
+
stepRate: partial.stepRate ?? defaults.stepRate,
|
|
174
|
+
playback: partial.playback ?? defaults.playback,
|
|
175
|
+
midiChannel: clampInt(partial.midiChannel ?? defaults.midiChannel, 1, 16),
|
|
176
|
+
automationMode: partial.automationMode ?? defaults.automationMode,
|
|
177
|
+
subrows,
|
|
178
|
+
steps: fitSteps(partial.steps, lengthSteps)
|
|
179
|
+
};
|
|
180
|
+
if (partial.name !== undefined)
|
|
181
|
+
row.name = partial.name;
|
|
182
|
+
if (partial.color !== undefined)
|
|
183
|
+
row.color = partial.color;
|
|
184
|
+
if (partial.subrowsExpanded !== undefined)
|
|
185
|
+
row.subrowsExpanded = partial.subrowsExpanded;
|
|
186
|
+
return row;
|
|
187
|
+
}
|
|
188
|
+
/** Normalize a pattern with exact SQ2 defaults. Pure and deterministic. */
|
|
189
|
+
export function normalizePattern(partial = {}) {
|
|
190
|
+
const lengthSteps = clampInt(partial.lengthSteps ?? 16, 1, STEP_PATTERN_MAX_STEPS);
|
|
191
|
+
const stepRate = partial.stepRate ?? "1/16";
|
|
192
|
+
const playback = partial.playback ?? "forward";
|
|
193
|
+
const midiChannel = clampInt(partial.midiChannel ?? 1, 1, 16);
|
|
194
|
+
const automationMode = partial.automationMode ?? "latch";
|
|
195
|
+
const seed = partial.seed !== undefined && Number.isFinite(partial.seed)
|
|
196
|
+
? partial.seed >>> 0
|
|
197
|
+
: 1;
|
|
198
|
+
const defaults = { lengthSteps, stepRate, playback, midiChannel, automationMode };
|
|
199
|
+
const rows = (partial.rows ?? [])
|
|
200
|
+
.slice(0, STEP_PATTERN_MAX_ROWS)
|
|
201
|
+
.map((r) => normalizeRow(r, defaults));
|
|
202
|
+
const out = {
|
|
203
|
+
version: 1,
|
|
204
|
+
seed,
|
|
205
|
+
lengthSteps,
|
|
206
|
+
stepRate,
|
|
207
|
+
playback,
|
|
208
|
+
legato: partial.legato === true,
|
|
209
|
+
swing: clampInt(partial.swing ?? 0, 0, 100),
|
|
210
|
+
swingDivision: partial.swingDivision ?? "1/16",
|
|
211
|
+
pitchSource: partial.pitchSource ?? "off",
|
|
212
|
+
scale: (partial.scale ?? "chromatic"),
|
|
213
|
+
noTranspose: partial.noTranspose === true,
|
|
214
|
+
scaleQuantize: partial.scaleQuantize === true,
|
|
215
|
+
midiChannel,
|
|
216
|
+
automationMode,
|
|
217
|
+
mono: partial.mono === true,
|
|
218
|
+
name: partial.name ?? "Pattern",
|
|
219
|
+
rows
|
|
220
|
+
};
|
|
221
|
+
if (partial.key !== undefined)
|
|
222
|
+
out.key = resolveStepKey(partial.key);
|
|
223
|
+
if (partial.rowSort !== undefined &&
|
|
224
|
+
partial.rowSort !== "none" &&
|
|
225
|
+
STEP_ROW_SORTS.includes(partial.rowSort)) {
|
|
226
|
+
out.rowSort = partial.rowSort;
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
/** Validate a stored pattern and return human-readable problems. */
|
|
231
|
+
export function validateStepPattern(pattern) {
|
|
232
|
+
const problems = [];
|
|
233
|
+
if (pattern.version !== 1) {
|
|
234
|
+
problems.push(`Unsupported pattern version ${String(pattern.version)}; expected 1.`);
|
|
235
|
+
}
|
|
236
|
+
if (!Number.isInteger(pattern.lengthSteps) || pattern.lengthSteps < 1 || pattern.lengthSteps > STEP_PATTERN_MAX_STEPS) {
|
|
237
|
+
problems.push(`lengthSteps must be an integer 1..${STEP_PATTERN_MAX_STEPS}, got ${pattern.lengthSteps}.`);
|
|
238
|
+
}
|
|
239
|
+
if (!STEP_RATES.includes(pattern.stepRate)) {
|
|
240
|
+
problems.push(`Invalid pattern stepRate "${String(pattern.stepRate)}".`);
|
|
241
|
+
}
|
|
242
|
+
if (pattern.rowSort !== undefined && !STEP_ROW_SORTS.includes(pattern.rowSort)) {
|
|
243
|
+
problems.push(`Invalid pattern rowSort "${String(pattern.rowSort)}". Valid values: ${STEP_ROW_SORTS.join(", ")}.`);
|
|
244
|
+
}
|
|
245
|
+
if (pattern.rows.length > STEP_PATTERN_MAX_ROWS) {
|
|
246
|
+
problems.push(`A pattern supports at most ${STEP_PATTERN_MAX_ROWS} rows; found ${pattern.rows.length}.`);
|
|
247
|
+
}
|
|
248
|
+
const ids = new Set();
|
|
249
|
+
for (const row of pattern.rows) {
|
|
250
|
+
if (ids.has(row.id)) {
|
|
251
|
+
problems.push(`Duplicate row id "${row.id}".`);
|
|
252
|
+
}
|
|
253
|
+
ids.add(row.id);
|
|
254
|
+
if (row.lengthSteps < 1 || row.lengthSteps > pattern.lengthSteps) {
|
|
255
|
+
problems.push(`Row "${row.id}" lengthSteps (${row.lengthSteps}) must be within 1..${pattern.lengthSteps}.`);
|
|
256
|
+
}
|
|
257
|
+
if (row.steps.length !== row.lengthSteps) {
|
|
258
|
+
problems.push(`Row "${row.id}" has ${row.steps.length} steps, expected ${row.lengthSteps}.`);
|
|
259
|
+
}
|
|
260
|
+
if (row.loopStart < 0 || row.loopEnd > row.lengthSteps || row.loopStart >= row.loopEnd) {
|
|
261
|
+
problems.push(`Row "${row.id}" has invalid loop range [${row.loopStart}, ${row.loopEnd}).`);
|
|
262
|
+
}
|
|
263
|
+
if (new Set(row.subrows).size !== row.subrows.length) {
|
|
264
|
+
problems.push(`Row "${row.id}" has duplicate subrow edit modes.`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return problems;
|
|
268
|
+
}
|
|
269
|
+
// ── Labels (SQ4) ────────────────────────────────────────────────────────────
|
|
270
|
+
const NOTE_NAMES_SHARP = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
271
|
+
/** Step Sequencer note name with C3 = 60 (SQ4, D80). */
|
|
272
|
+
export function stepNoteName(pitch) {
|
|
273
|
+
const p = clampInt(pitch, 0, 127);
|
|
274
|
+
const name = NOTE_NAMES_SHARP[p % 12];
|
|
275
|
+
const octave = Math.floor(p / 12) - 2;
|
|
276
|
+
return `${name}${octave}`;
|
|
277
|
+
}
|
|
278
|
+
const DEGREE_LABELS = {
|
|
279
|
+
root: "Root",
|
|
280
|
+
"2nd": "2nd",
|
|
281
|
+
"3rd": "3rd",
|
|
282
|
+
"4th": "4th",
|
|
283
|
+
"5th": "5th",
|
|
284
|
+
"6th": "6th",
|
|
285
|
+
"7th": "7th"
|
|
286
|
+
};
|
|
287
|
+
/** Display label of `row` (SQ4). */
|
|
288
|
+
export function rowLabel(row, instrument) {
|
|
289
|
+
if (row.name && row.name.trim().length > 0)
|
|
290
|
+
return row.name;
|
|
291
|
+
const a = row.assignment;
|
|
292
|
+
switch (a.type) {
|
|
293
|
+
case "kit": {
|
|
294
|
+
if (instrument?.type === "drum") {
|
|
295
|
+
return instrument.pads[a.pad]?.name ?? `Pad ${a.pad + 1}`;
|
|
296
|
+
}
|
|
297
|
+
if (instrument?.type === "sampler") {
|
|
298
|
+
return instrument.zones[a.pad]?.name ?? `Pad ${a.pad + 1}`;
|
|
299
|
+
}
|
|
300
|
+
const gm = GM_DRUM_LANES.find((l) => l.pitch === 36 + a.pad);
|
|
301
|
+
return gm?.name ?? `Pad ${a.pad + 1}`;
|
|
302
|
+
}
|
|
303
|
+
case "note": {
|
|
304
|
+
const isNonMelodic = instrument?.type === "drum" ||
|
|
305
|
+
(instrument?.type === "sampler" && instrument.oneShot);
|
|
306
|
+
if (isNonMelodic) {
|
|
307
|
+
const gm = GM_DRUM_LANES.find((l) => l.pitch === a.pitch);
|
|
308
|
+
if (gm)
|
|
309
|
+
return gm.name;
|
|
310
|
+
}
|
|
311
|
+
return stepNoteName(a.pitch);
|
|
312
|
+
}
|
|
313
|
+
case "melodic":
|
|
314
|
+
return stepNoteName(a.pitch);
|
|
315
|
+
case "degree":
|
|
316
|
+
return `${DEGREE_LABELS[a.degree]} : ${a.octave}`;
|
|
317
|
+
case "automation": {
|
|
318
|
+
const t = a.target;
|
|
319
|
+
if (t.kind === "cc")
|
|
320
|
+
return `CC ${t.cc}`;
|
|
321
|
+
if (t.kind === "pitchBend")
|
|
322
|
+
return "Pitch Bend";
|
|
323
|
+
if (t.kind === "channelPressure")
|
|
324
|
+
return "Channel Pressure";
|
|
325
|
+
return t.param;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
// ── Pattern & row mutations (SQ2–SQ8, SQ18, SQ36–SQ37) ──────────────────────
|
|
330
|
+
/** Append a row to `pattern`, enforcing the 128-row limit (SQ3) and active rowSort (SQ36). */
|
|
331
|
+
export function addPatternRow(pattern, input, instrument) {
|
|
332
|
+
if (pattern.rows.length >= STEP_PATTERN_MAX_ROWS) {
|
|
333
|
+
throw new Error(`Cannot add row: a pattern holds at most ${STEP_PATTERN_MAX_ROWS} rows.`);
|
|
334
|
+
}
|
|
335
|
+
const row = normalizeRow(input, pattern);
|
|
336
|
+
return applyPatternRowSort({ ...pattern, rows: [...pattern.rows, row] }, instrument);
|
|
337
|
+
}
|
|
338
|
+
/** Set pattern stepRate and write through to every row (SQ2). */
|
|
339
|
+
export function setPatternStepRate(pattern, stepRate) {
|
|
340
|
+
stepRateTicks(stepRate);
|
|
341
|
+
return {
|
|
342
|
+
...pattern,
|
|
343
|
+
stepRate,
|
|
344
|
+
rows: pattern.rows.map((r) => ({ ...r, stepRate }))
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/** Set pattern playback mode and write through to every row (SQ2). */
|
|
348
|
+
export function setPatternPlayback(pattern, playback) {
|
|
349
|
+
return {
|
|
350
|
+
...pattern,
|
|
351
|
+
playback,
|
|
352
|
+
rows: pattern.rows.map((r) => ({ ...r, playback }))
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/** Set pattern MIDI channel and write through to every row (SQ2). */
|
|
356
|
+
export function setPatternMidiChannel(pattern, midiChannel) {
|
|
357
|
+
const ch = clampInt(midiChannel, 1, 16);
|
|
358
|
+
return {
|
|
359
|
+
...pattern,
|
|
360
|
+
midiChannel: ch,
|
|
361
|
+
rows: pattern.rows.map((r) => ({ ...r, midiChannel: ch }))
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/** Set pattern automation mode and write through to every row (SQ2). */
|
|
365
|
+
export function setPatternAutomationMode(pattern, automationMode) {
|
|
366
|
+
return {
|
|
367
|
+
...pattern,
|
|
368
|
+
automationMode,
|
|
369
|
+
rows: pattern.rows.map((r) => ({ ...r, automationMode }))
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
/** Write loop start/end through to every row (SQ2). */
|
|
373
|
+
export function setPatternLoop(pattern, loopStart, loopEnd) {
|
|
374
|
+
return {
|
|
375
|
+
...pattern,
|
|
376
|
+
rows: pattern.rows.map((r) => {
|
|
377
|
+
const nextEnd = loopEnd !== undefined ? clampInt(loopEnd, 1, r.lengthSteps) : r.loopEnd;
|
|
378
|
+
const nextStart = loopStart !== undefined
|
|
379
|
+
? clampInt(loopStart, 0, nextEnd - 1)
|
|
380
|
+
: Math.min(r.loopStart, nextEnd - 1);
|
|
381
|
+
return { ...r, loopStart: nextStart, loopEnd: nextEnd };
|
|
382
|
+
})
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
/** Resize a row's step array, repeating from step 0 when growing (SQ7). */
|
|
386
|
+
export function resizeRowLength(row, newLength) {
|
|
387
|
+
const nextLen = clampInt(newLength, 1, STEP_PATTERN_MAX_STEPS);
|
|
388
|
+
const oldLen = row.steps.length;
|
|
389
|
+
const steps = [];
|
|
390
|
+
for (let i = 0; i < nextLen; i++) {
|
|
391
|
+
const src = oldLen > 0 ? row.steps[i % oldLen] : undefined;
|
|
392
|
+
steps.push(src ? structuredClone(src) : {});
|
|
393
|
+
}
|
|
394
|
+
const loopEnd = row.loopEnd === row.lengthSteps ? nextLen : Math.min(row.loopEnd, nextLen);
|
|
395
|
+
const loopStart = Math.min(row.loopStart, loopEnd - 1);
|
|
396
|
+
return {
|
|
397
|
+
...row,
|
|
398
|
+
lengthSteps: nextLen,
|
|
399
|
+
loopStart,
|
|
400
|
+
loopEnd,
|
|
401
|
+
steps
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Resize pattern length (SQ7): growing repeats steps from step 0; rows whose
|
|
406
|
+
* length matched the old pattern length follow, shorter rows stay shorter, and
|
|
407
|
+
* rows longer than `newLength` are cut.
|
|
408
|
+
*/
|
|
409
|
+
export function resizePatternLength(pattern, newLength) {
|
|
410
|
+
const nextLen = clampInt(newLength, 1, STEP_PATTERN_MAX_STEPS);
|
|
411
|
+
const oldLen = pattern.lengthSteps;
|
|
412
|
+
const rows = pattern.rows.map((row) => {
|
|
413
|
+
if (row.lengthSteps === oldLen || row.lengthSteps > nextLen) {
|
|
414
|
+
return resizeRowLength(row, nextLen);
|
|
415
|
+
}
|
|
416
|
+
return row;
|
|
417
|
+
});
|
|
418
|
+
return { ...pattern, lengthSteps: nextLen, rows };
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Change pattern key, transposing pitched (`note` / `melodic`) rows by the
|
|
422
|
+
* shortest tonic interval unless `noTranspose` is set (SQ18).
|
|
423
|
+
*/
|
|
424
|
+
export function setPatternKey(pattern, nextKey) {
|
|
425
|
+
const oldK = resolveStepKey(pattern.key);
|
|
426
|
+
const newK = resolveStepKey(nextKey);
|
|
427
|
+
if (pattern.noTranspose || oldK.tonic === newK.tonic) {
|
|
428
|
+
return { ...pattern, key: newK };
|
|
429
|
+
}
|
|
430
|
+
const delta = shortestTonicDelta(oldK.tonic, newK.tonic);
|
|
431
|
+
const rows = pattern.rows.map((row) => {
|
|
432
|
+
if (row.assignment.type !== "note" && row.assignment.type !== "melodic") {
|
|
433
|
+
return row;
|
|
434
|
+
}
|
|
435
|
+
const oldPc = ((row.assignment.pitch % 12) + 12) % 12;
|
|
436
|
+
const octWrap = Math.floor((oldPc + delta) / 12);
|
|
437
|
+
const nextPitch = clampInt(row.assignment.pitch + delta, 0, 127);
|
|
438
|
+
const steps = octWrap === 0
|
|
439
|
+
? row.steps
|
|
440
|
+
: row.steps.map((s) => s.octave !== undefined
|
|
441
|
+
? compactStep({ ...s, octave: clampInt(s.octave + octWrap, -2, 8) })
|
|
442
|
+
: s);
|
|
443
|
+
return {
|
|
444
|
+
...row,
|
|
445
|
+
assignment: { ...row.assignment, pitch: nextPitch },
|
|
446
|
+
steps
|
|
447
|
+
};
|
|
448
|
+
});
|
|
449
|
+
return { ...pattern, key: newK, rows };
|
|
450
|
+
}
|
|
451
|
+
/** Add a subrow set to the next unused edit mode for `row` (SQ8). */
|
|
452
|
+
export function addRowSubrow(row) {
|
|
453
|
+
const candidates = row.assignment.type === "automation" ? AUTOMATION_SUBROW_MODES : NOTE_SUBROW_MODES;
|
|
454
|
+
const used = new Set(row.subrows);
|
|
455
|
+
const next = candidates.find((m) => !used.has(m));
|
|
456
|
+
if (!next) {
|
|
457
|
+
throw new Error(`Row "${row.id}" already shows every available edit mode.`);
|
|
458
|
+
}
|
|
459
|
+
return { ...row, subrows: [...row.subrows, next] };
|
|
460
|
+
}
|
|
461
|
+
/** Change subrow `index` to `mode`, refusing modes already used by another subrow (SQ8). */
|
|
462
|
+
export function setRowSubrowMode(row, index, mode) {
|
|
463
|
+
if (index < 0 || index >= row.subrows.length) {
|
|
464
|
+
throw new Error(`Subrow index ${index} out of range 0..${row.subrows.length - 1}.`);
|
|
465
|
+
}
|
|
466
|
+
if (row.subrows.some((m, i) => i !== index && m === mode)) {
|
|
467
|
+
throw new Error(`Subrow mode "${mode}" is already used on row "${row.id}". Two subrows cannot share a mode.`);
|
|
468
|
+
}
|
|
469
|
+
const subrows = [...row.subrows];
|
|
470
|
+
subrows[index] = mode;
|
|
471
|
+
return { ...row, subrows };
|
|
472
|
+
}
|
|
473
|
+
const VALUE_ACTIVATION_KEYS = [
|
|
474
|
+
"velocity", "gate", "note", "octave", "repeat", "chance", "offset",
|
|
475
|
+
"value", "condition", "midiChannel", "articulation", "velocityRamp",
|
|
476
|
+
"velocityCurve", "distribution"
|
|
477
|
+
];
|
|
478
|
+
/**
|
|
479
|
+
* Edit one step on `row` (SQ4, SQ32, SQ52).
|
|
480
|
+
* - Editing `note` or `octave` on a fixed `note` row converts it to `melodic`.
|
|
481
|
+
* - Editing a value field on an off step turns it on (unless `on: false` is explicit
|
|
482
|
+
* or `keepOff` is set, as in SQ33 Shift-ramp).
|
|
483
|
+
*/
|
|
484
|
+
export function setStepValue(row, stepIndex, values, options = {}) {
|
|
485
|
+
if (stepIndex < 0 || stepIndex >= row.steps.length) {
|
|
486
|
+
throw new Error(`Step ${stepIndex + 1} is outside row length 1..${row.steps.length}.`);
|
|
487
|
+
}
|
|
488
|
+
let assignment = row.assignment;
|
|
489
|
+
if (assignment.type === "note" &&
|
|
490
|
+
(values.note !== undefined || values.octave !== undefined)) {
|
|
491
|
+
assignment = { type: "melodic", pitch: assignment.pitch };
|
|
492
|
+
}
|
|
493
|
+
const current = row.steps[stepIndex] ?? {};
|
|
494
|
+
const merged = { ...current, ...values };
|
|
495
|
+
const setsValue = VALUE_ACTIVATION_KEYS.some((k) => values[k] !== undefined);
|
|
496
|
+
if (setsValue && values.on === undefined && !options.keepOff && !current.on) {
|
|
497
|
+
merged.on = true;
|
|
498
|
+
}
|
|
499
|
+
const steps = [...row.steps];
|
|
500
|
+
steps[stepIndex] = compactStep(merged);
|
|
501
|
+
return { ...row, assignment, steps };
|
|
502
|
+
}
|
|
503
|
+
/** In Mono mode, turn off all other note/kit/melodic/degree rows at `stepIndices` (SQ37). */
|
|
504
|
+
export function applyMonoAtSteps(pattern, activeRowId, stepIndices) {
|
|
505
|
+
if (!pattern.mono || stepIndices.length === 0)
|
|
506
|
+
return pattern;
|
|
507
|
+
const activeRow = pattern.rows.find((r) => r.id === activeRowId);
|
|
508
|
+
if (!activeRow || !isNoteRow(activeRow))
|
|
509
|
+
return pattern;
|
|
510
|
+
const idxSet = new Set(stepIndices);
|
|
511
|
+
return {
|
|
512
|
+
...pattern,
|
|
513
|
+
rows: pattern.rows.map((r) => {
|
|
514
|
+
if (r.id === activeRowId || !isNoteRow(r))
|
|
515
|
+
return r;
|
|
516
|
+
let changed = false;
|
|
517
|
+
const steps = r.steps.map((s, i) => {
|
|
518
|
+
if (!idxSet.has(i) || !s.on)
|
|
519
|
+
return s;
|
|
520
|
+
changed = true;
|
|
521
|
+
return compactStep({ ...s, on: false });
|
|
522
|
+
});
|
|
523
|
+
return changed ? { ...r, steps } : r;
|
|
524
|
+
})
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
/** Deep equality of two normalized patterns (for SQ24 join). */
|
|
528
|
+
export function patternsEqual(a, b) {
|
|
529
|
+
return JSON.stringify(normalizePattern(a)) === JSON.stringify(normalizePattern(b));
|
|
530
|
+
}
|
|
531
|
+
/** Return a template copy of `pattern`: rows, subrows and settings kept, step values cleared (SQ44). */
|
|
532
|
+
export function stripPatternSteps(pattern) {
|
|
533
|
+
return {
|
|
534
|
+
...structuredClone(pattern),
|
|
535
|
+
rows: pattern.rows.map((r) => ({
|
|
536
|
+
...structuredClone(r),
|
|
537
|
+
steps: Array.from({ length: r.lengthSteps }, () => ({}))
|
|
538
|
+
}))
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
/** Decode a persisted `StepRowSort` value into criterion + direction, or `null` for none. */
|
|
542
|
+
export function parseStepRowSort(rowSort) {
|
|
543
|
+
switch (rowSort) {
|
|
544
|
+
case "typeAsc":
|
|
545
|
+
return { by: "type", descending: false };
|
|
546
|
+
case "typeDesc":
|
|
547
|
+
return { by: "type", descending: true };
|
|
548
|
+
case "typeAndNoteAsc":
|
|
549
|
+
return { by: "typeAndNote", descending: false };
|
|
550
|
+
case "typeAndNoteDesc":
|
|
551
|
+
return { by: "typeAndNote", descending: true };
|
|
552
|
+
case "typeAndNameAsc":
|
|
553
|
+
return { by: "typeAndName", descending: false };
|
|
554
|
+
case "typeAndNameDesc":
|
|
555
|
+
return { by: "typeAndName", descending: true };
|
|
556
|
+
case "typeAndKitPadAsc":
|
|
557
|
+
return { by: "typeAndKitPad", descending: false };
|
|
558
|
+
case "typeAndKitPadDesc":
|
|
559
|
+
return { by: "typeAndKitPad", descending: true };
|
|
560
|
+
default:
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
/** Encode a sort criterion + direction into its persisted `StepRowSort` value. */
|
|
565
|
+
export function formatStepRowSort(by, descending = false) {
|
|
566
|
+
return `${by}${descending ? "Desc" : "Asc"}`;
|
|
567
|
+
}
|
|
568
|
+
/** Re-apply `pattern.rowSort` if active, otherwise return `pattern` unchanged. */
|
|
569
|
+
export function applyPatternRowSort(pattern, instrument) {
|
|
570
|
+
const spec = parseStepRowSort(pattern.rowSort);
|
|
571
|
+
return spec ? sortPatternRows(pattern, spec.by, spec.descending, instrument) : pattern;
|
|
572
|
+
}
|
|
573
|
+
function assignmentTypeRank(a) {
|
|
574
|
+
switch (a.type) {
|
|
575
|
+
case "note":
|
|
576
|
+
case "kit":
|
|
577
|
+
return 0;
|
|
578
|
+
case "melodic":
|
|
579
|
+
case "degree":
|
|
580
|
+
return 1;
|
|
581
|
+
case "automation":
|
|
582
|
+
switch (a.target.kind) {
|
|
583
|
+
case "cc":
|
|
584
|
+
return 2;
|
|
585
|
+
case "pitchBend":
|
|
586
|
+
return 3;
|
|
587
|
+
case "channelPressure":
|
|
588
|
+
return 4;
|
|
589
|
+
case "device":
|
|
590
|
+
return 5;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function assignmentNoteKey(a, instrument) {
|
|
595
|
+
switch (a.type) {
|
|
596
|
+
case "note":
|
|
597
|
+
case "melodic":
|
|
598
|
+
return a.pitch;
|
|
599
|
+
case "kit":
|
|
600
|
+
return kitPadPitch(a.pad, instrument);
|
|
601
|
+
case "degree":
|
|
602
|
+
return (a.octave + 2) * 12 + STEP_DEGREES.indexOf(a.degree);
|
|
603
|
+
case "automation":
|
|
604
|
+
return a.target.kind === "cc" ? a.target.cc : 0;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/** Sort rows according to SQ36 View > Sort Rows rules. */
|
|
608
|
+
export function sortPatternRows(pattern, by, descending = false, instrument) {
|
|
609
|
+
const dir = descending ? -1 : 1;
|
|
610
|
+
const indexed = pattern.rows.map((row, idx) => ({ row, idx }));
|
|
611
|
+
indexed.sort((a, b) => {
|
|
612
|
+
const typeCmp = assignmentTypeRank(a.row.assignment) - assignmentTypeRank(b.row.assignment);
|
|
613
|
+
if (typeCmp !== 0)
|
|
614
|
+
return typeCmp * dir;
|
|
615
|
+
if (by === "typeAndNote") {
|
|
616
|
+
const noteCmp = assignmentNoteKey(a.row.assignment, instrument) -
|
|
617
|
+
assignmentNoteKey(b.row.assignment, instrument);
|
|
618
|
+
if (noteCmp !== 0)
|
|
619
|
+
return noteCmp * dir;
|
|
620
|
+
}
|
|
621
|
+
else if (by === "typeAndName") {
|
|
622
|
+
const nameCmp = rowLabel(a.row, instrument).localeCompare(rowLabel(b.row, instrument));
|
|
623
|
+
if (nameCmp !== 0)
|
|
624
|
+
return nameCmp * dir;
|
|
625
|
+
}
|
|
626
|
+
else if (by === "typeAndKitPad") {
|
|
627
|
+
const padA = a.row.assignment.type === "kit" ? a.row.assignment.pad : assignmentNoteKey(a.row.assignment, instrument);
|
|
628
|
+
const padB = b.row.assignment.type === "kit" ? b.row.assignment.pad : assignmentNoteKey(b.row.assignment, instrument);
|
|
629
|
+
if (padA !== padB)
|
|
630
|
+
return (padA - padB) * dir;
|
|
631
|
+
}
|
|
632
|
+
return a.idx - b.idx;
|
|
633
|
+
});
|
|
634
|
+
return { ...pattern, rows: indexed.map((x) => x.row) };
|
|
635
|
+
}
|
|
636
|
+
// ── Pattern text format (SQ47, SQ60) ────────────────────────────────────────
|
|
637
|
+
const DRUM_ALIASES = {
|
|
638
|
+
kick: 36, bd: 36, "bass drum": 36,
|
|
639
|
+
snare: 38, sd: 38, "snare 2": 40,
|
|
640
|
+
"side stick": 37, rim: 37, rimshot: 37,
|
|
641
|
+
clap: 39, cp: 39,
|
|
642
|
+
"closed hi-hat": 42, "closed hat": 42, "hi-hat": 42, hihat: 42, hat: 42, ch: 42, hh: 42,
|
|
643
|
+
"pedal hi-hat": 44, "pedal hat": 44,
|
|
644
|
+
"open hi-hat": 46, "open hat": 46, oh: 46,
|
|
645
|
+
"low tom": 41, "mid tom": 45, "high tom": 48,
|
|
646
|
+
crash: 49, ride: 51, "ride bell": 53, tambourine: 54, tamb: 54, cowbell: 56, cb: 56
|
|
647
|
+
};
|
|
648
|
+
const NOTE_NAME_RE = /^([A-Ga-g])([#b]?)(-?\d)$/;
|
|
649
|
+
const LETTER_SEMITONE = {
|
|
650
|
+
C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11
|
|
651
|
+
};
|
|
652
|
+
/** Parse a note name with C3 = 60 (SQ4, D80), or undefined. */
|
|
653
|
+
export function parseStepNoteName(name) {
|
|
654
|
+
const m = NOTE_NAME_RE.exec(name.trim());
|
|
655
|
+
if (!m)
|
|
656
|
+
return undefined;
|
|
657
|
+
const base = LETTER_SEMITONE[m[1].toUpperCase()];
|
|
658
|
+
const acc = m[2] === "#" ? 1 : m[2] === "b" ? -1 : 0;
|
|
659
|
+
const oct = Number(m[3]);
|
|
660
|
+
const pitch = (oct + 2) * 12 + base + acc;
|
|
661
|
+
return pitch >= 0 && pitch <= 127 ? pitch : undefined;
|
|
662
|
+
}
|
|
663
|
+
/** Infer a row assignment from a pattern-text row name (SQ47). */
|
|
664
|
+
export function assignmentFromRowName(name, instrument) {
|
|
665
|
+
const trimmed = name.trim();
|
|
666
|
+
const lower = trimmed.toLowerCase();
|
|
667
|
+
// Kit pads on DR-1 or Sampler
|
|
668
|
+
if (instrument?.type === "drum") {
|
|
669
|
+
const padIdx = instrument.pads.findIndex((p) => p.name.toLowerCase() === lower);
|
|
670
|
+
if (padIdx >= 0)
|
|
671
|
+
return { assignment: { type: "kit", pad: padIdx } };
|
|
672
|
+
}
|
|
673
|
+
else if (instrument?.type === "sampler") {
|
|
674
|
+
const zoneIdx = instrument.zones.findIndex((z) => z.name.toLowerCase() === lower);
|
|
675
|
+
if (zoneIdx >= 0 && zoneIdx < 16)
|
|
676
|
+
return { assignment: { type: "kit", pad: zoneIdx } };
|
|
677
|
+
}
|
|
678
|
+
// CC / pitchBend / channelPressure
|
|
679
|
+
const ccMatch = /^cc\s*(\d+)$/i.exec(trimmed);
|
|
680
|
+
if (ccMatch) {
|
|
681
|
+
const cc = Number(ccMatch[1]);
|
|
682
|
+
if (cc >= 0 && cc <= 127) {
|
|
683
|
+
return { assignment: { type: "automation", target: { kind: "cc", cc } } };
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (lower === "pitch bend" || lower === "pitchbend") {
|
|
687
|
+
return { assignment: { type: "automation", target: { kind: "pitchBend" } } };
|
|
688
|
+
}
|
|
689
|
+
if (lower === "channel pressure" || lower === "channelpressure") {
|
|
690
|
+
return { assignment: { type: "automation", target: { kind: "channelPressure" } } };
|
|
691
|
+
}
|
|
692
|
+
// Degree (`Root : 3`)
|
|
693
|
+
const degMatch = /^(root|2nd|3rd|4th|5th|6th|7th)\s*:\s*(-?\d+)$/i.exec(trimmed);
|
|
694
|
+
if (degMatch) {
|
|
695
|
+
const degree = degMatch[1].toLowerCase();
|
|
696
|
+
const octave = clampInt(Number(degMatch[2]), -2, 8);
|
|
697
|
+
return { assignment: { type: "degree", degree, octave } };
|
|
698
|
+
}
|
|
699
|
+
// GM drum names & aliases
|
|
700
|
+
const gmLane = GM_DRUM_LANES.find((l) => l.name.toLowerCase() === lower);
|
|
701
|
+
if (gmLane) {
|
|
702
|
+
if (instrument?.type === "drum") {
|
|
703
|
+
const idx = gmLane.pitch - instrument.baseNote;
|
|
704
|
+
if (idx >= 0 && idx < instrument.pads.length) {
|
|
705
|
+
return { assignment: { type: "kit", pad: idx } };
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return { assignment: { type: "note", pitch: gmLane.pitch }, customName: gmLane.name };
|
|
709
|
+
}
|
|
710
|
+
const aliasPitch = DRUM_ALIASES[lower];
|
|
711
|
+
if (aliasPitch !== undefined) {
|
|
712
|
+
if (instrument?.type === "drum") {
|
|
713
|
+
const idx = aliasPitch - instrument.baseNote;
|
|
714
|
+
if (idx >= 0 && idx < instrument.pads.length) {
|
|
715
|
+
return { assignment: { type: "kit", pad: idx } };
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return { assignment: { type: "note", pitch: aliasPitch }, customName: trimmed };
|
|
719
|
+
}
|
|
720
|
+
// Note name (C3 = 60)
|
|
721
|
+
const notePitch = parseStepNoteName(trimmed);
|
|
722
|
+
if (notePitch !== undefined) {
|
|
723
|
+
return { assignment: { type: "note", pitch: notePitch } };
|
|
724
|
+
}
|
|
725
|
+
return { assignment: { type: "note", pitch: 60 }, customName: trimmed };
|
|
726
|
+
}
|
|
727
|
+
/** Encode one step to its SQ47 character. */
|
|
728
|
+
export function stepToChar(step) {
|
|
729
|
+
if (step.skip)
|
|
730
|
+
return "_";
|
|
731
|
+
if (step.tie)
|
|
732
|
+
return "-";
|
|
733
|
+
if (!step.on)
|
|
734
|
+
return ".";
|
|
735
|
+
const vel = step.velocity ?? 100;
|
|
736
|
+
if (vel >= 120)
|
|
737
|
+
return "X";
|
|
738
|
+
if (vel <= 60)
|
|
739
|
+
return "o";
|
|
740
|
+
return "x";
|
|
741
|
+
}
|
|
742
|
+
/** Decode one SQ47 character to a compacted `Step`. */
|
|
743
|
+
export function charToStep(ch) {
|
|
744
|
+
switch (ch) {
|
|
745
|
+
case "x":
|
|
746
|
+
return { on: true };
|
|
747
|
+
case "X":
|
|
748
|
+
return { on: true, velocity: 127 };
|
|
749
|
+
case "o":
|
|
750
|
+
return { on: true, velocity: 50 };
|
|
751
|
+
case ".":
|
|
752
|
+
return {};
|
|
753
|
+
case "-":
|
|
754
|
+
return { tie: true };
|
|
755
|
+
case "_":
|
|
756
|
+
return { skip: true };
|
|
757
|
+
default:
|
|
758
|
+
throw new Error(`Invalid pattern step character "${ch}". Valid characters: x (on), X (vel 127), o (vel 50), . (off), - (tie), _ (skip).`);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
/** Render `pattern` to SQ47 text (one line per row, steps grouped by 4). */
|
|
762
|
+
export function patternToText(pattern, instrument) {
|
|
763
|
+
return pattern.rows
|
|
764
|
+
.map((row) => {
|
|
765
|
+
const label = rowLabel(row, instrument);
|
|
766
|
+
const ratePart = row.stepRate !== pattern.stepRate ? ` | ${row.stepRate}` : "";
|
|
767
|
+
const chars = row.steps.map(stepToChar);
|
|
768
|
+
const groups = [];
|
|
769
|
+
for (let i = 0; i < chars.length; i += 4) {
|
|
770
|
+
groups.push(chars.slice(i, i + 4).join(""));
|
|
771
|
+
}
|
|
772
|
+
return `${label}${ratePart}: ${groups.join(" ")}`;
|
|
773
|
+
})
|
|
774
|
+
.join("\n");
|
|
775
|
+
}
|
|
776
|
+
/** Parse SQ47 pattern text lines into structured row entries. */
|
|
777
|
+
export function parsePatternTextLines(text) {
|
|
778
|
+
const lines = text.split(/\r?\n/);
|
|
779
|
+
const parsed = [];
|
|
780
|
+
for (const raw of lines) {
|
|
781
|
+
const trimmed = raw.trim();
|
|
782
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
783
|
+
continue;
|
|
784
|
+
const colon = trimmed.lastIndexOf(":");
|
|
785
|
+
if (colon <= 0) {
|
|
786
|
+
throw new Error(`Invalid pattern text line "${trimmed}". Expected "<row name> [| rate]: <steps>".`);
|
|
787
|
+
}
|
|
788
|
+
const header = trimmed.slice(0, colon).trim();
|
|
789
|
+
const stepsPart = trimmed.slice(colon + 1);
|
|
790
|
+
let name = header;
|
|
791
|
+
let stepRate;
|
|
792
|
+
const pipe = header.lastIndexOf("|");
|
|
793
|
+
if (pipe > 0) {
|
|
794
|
+
name = header.slice(0, pipe).trim();
|
|
795
|
+
const rateText = header.slice(pipe + 1).trim();
|
|
796
|
+
if (!STEP_RATES.includes(rateText)) {
|
|
797
|
+
throw new Error(`Invalid step rate "${rateText}" on row "${name}". Valid rates: ${STEP_RATES.join(", ")}.`);
|
|
798
|
+
}
|
|
799
|
+
stepRate = rateText;
|
|
800
|
+
}
|
|
801
|
+
const compactChars = stepsPart.replace(/[\s|]+/g, "");
|
|
802
|
+
if (compactChars.length === 0) {
|
|
803
|
+
throw new Error(`Row "${name}" has no step characters after ":".`);
|
|
804
|
+
}
|
|
805
|
+
if (compactChars.length > STEP_PATTERN_MAX_STEPS) {
|
|
806
|
+
throw new Error(`Row "${name}" has ${compactChars.length} steps; maximum is ${STEP_PATTERN_MAX_STEPS}.`);
|
|
807
|
+
}
|
|
808
|
+
const steps = [];
|
|
809
|
+
for (let i = 0; i < compactChars.length; i++) {
|
|
810
|
+
steps.push(charToStep(compactChars[i]));
|
|
811
|
+
}
|
|
812
|
+
parsed.push({ name, stepRate, steps });
|
|
813
|
+
}
|
|
814
|
+
return parsed;
|
|
815
|
+
}
|
|
816
|
+
/** Parse SQ47 pattern text into a normalized `StepPattern` (SQ47, SQ60). */
|
|
817
|
+
export function patternFromText(text, options = {}) {
|
|
818
|
+
const lines = parsePatternTextLines(text);
|
|
819
|
+
const lengthSteps = lines.length > 0
|
|
820
|
+
? clampInt(Math.max(...lines.map((l) => l.steps.length)), 1, STEP_PATTERN_MAX_STEPS)
|
|
821
|
+
: 16;
|
|
822
|
+
const baseRate = options.stepRate ?? "1/16";
|
|
823
|
+
const rows = lines.map((line, i) => {
|
|
824
|
+
const inferred = assignmentFromRowName(line.name, options.instrument);
|
|
825
|
+
const id = options.newRowId ? options.newRowId(i) : `row_${i + 1}`;
|
|
826
|
+
const partialRow = {
|
|
827
|
+
id,
|
|
828
|
+
assignment: inferred.assignment,
|
|
829
|
+
lengthSteps: line.steps.length,
|
|
830
|
+
loopStart: 0,
|
|
831
|
+
loopEnd: line.steps.length,
|
|
832
|
+
stepRate: line.stepRate ?? baseRate,
|
|
833
|
+
steps: line.steps
|
|
834
|
+
};
|
|
835
|
+
if (inferred.customName !== undefined)
|
|
836
|
+
partialRow.name = inferred.customName;
|
|
837
|
+
return normalizeRow(partialRow, {
|
|
838
|
+
lengthSteps,
|
|
839
|
+
stepRate: baseRate,
|
|
840
|
+
playback: "forward",
|
|
841
|
+
midiChannel: 1,
|
|
842
|
+
automationMode: "latch"
|
|
843
|
+
});
|
|
844
|
+
});
|
|
845
|
+
return normalizePattern({
|
|
846
|
+
name: options.name ?? "Pattern",
|
|
847
|
+
seed: options.seed ?? 1,
|
|
848
|
+
lengthSteps,
|
|
849
|
+
stepRate: baseRate,
|
|
850
|
+
rows
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
/** Convert MIDI notes and controller lanes into a StepPattern (SQ21, SQ60). */
|
|
854
|
+
export function midiToPattern(notes, controllers = [], options) {
|
|
855
|
+
const stepRate = options.stepRate ?? "1/16";
|
|
856
|
+
const stepTicks = stepRateTicks(stepRate);
|
|
857
|
+
const windowStart = options.windowStartTick ?? 0;
|
|
858
|
+
const rawSteps = Math.ceil(Math.max(1, options.clipLengthTicks) / stepTicks);
|
|
859
|
+
const lengthSteps = Math.max(1, Math.min(STEP_PATTERN_MAX_STEPS, rawSteps));
|
|
860
|
+
const windowEnd = windowStart + options.clipLengthTicks;
|
|
861
|
+
const inWindow = notes.filter((n) => !n.muted && n.startTick >= windowStart && n.startTick < windowEnd);
|
|
862
|
+
// Group by pitch, highest first (SQ21).
|
|
863
|
+
const byPitch = new Map();
|
|
864
|
+
for (const note of inWindow) {
|
|
865
|
+
const list = byPitch.get(note.pitch);
|
|
866
|
+
if (list)
|
|
867
|
+
list.push(note);
|
|
868
|
+
else
|
|
869
|
+
byPitch.set(note.pitch, [note]);
|
|
870
|
+
}
|
|
871
|
+
const pitches = [...byPitch.keys()].sort((a, b) => b - a);
|
|
872
|
+
let droppedNotes = 0;
|
|
873
|
+
let mergedCollisions = 0;
|
|
874
|
+
const rows = [];
|
|
875
|
+
let rowCounter = 0;
|
|
876
|
+
const nextRowId = () => options.newRowId ? options.newRowId(rowCounter++) : `row_${++rowCounter}`;
|
|
877
|
+
const patternDefaults = {
|
|
878
|
+
lengthSteps,
|
|
879
|
+
stepRate,
|
|
880
|
+
playback: "forward",
|
|
881
|
+
midiChannel: 1,
|
|
882
|
+
automationMode: "latch"
|
|
883
|
+
};
|
|
884
|
+
for (const pitch of pitches) {
|
|
885
|
+
if (rows.length >= STEP_PATTERN_MAX_ROWS) {
|
|
886
|
+
droppedNotes += byPitch.get(pitch).length;
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
const pitchNotes = [...byPitch.get(pitch)].sort((a, b) => a.startTick - b.startTick);
|
|
890
|
+
const steps = Array.from({ length: lengthSteps }, () => ({}));
|
|
891
|
+
const placed = [];
|
|
892
|
+
const byStep = new Map();
|
|
893
|
+
for (const note of pitchNotes) {
|
|
894
|
+
const rel = note.startTick - windowStart;
|
|
895
|
+
if (rel < 0 || rel >= lengthSteps * stepTicks) {
|
|
896
|
+
droppedNotes++;
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
const idx = Math.min(lengthSteps - 1, Math.round(rel / stepTicks));
|
|
900
|
+
const rem = rel - idx * stepTicks;
|
|
901
|
+
const offset = clampInt((rem / stepTicks) * 100, -50, 50);
|
|
902
|
+
const prev = byStep.get(idx);
|
|
903
|
+
if (prev) {
|
|
904
|
+
mergedCollisions++;
|
|
905
|
+
if (note.velocity > prev.note.velocity) {
|
|
906
|
+
prev.note = note;
|
|
907
|
+
prev.offset = offset;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
else {
|
|
911
|
+
const entry = { idx, note, offset };
|
|
912
|
+
byStep.set(idx, entry);
|
|
913
|
+
placed.push(entry);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
if (placed.length === 0)
|
|
917
|
+
continue;
|
|
918
|
+
placed.sort((a, b) => a.idx - b.idx);
|
|
919
|
+
for (let p = 0; p < placed.length; p++) {
|
|
920
|
+
const { idx, note, offset } = placed[p];
|
|
921
|
+
const nextNoteIdx = p + 1 < placed.length ? placed[p + 1].idx : lengthSteps;
|
|
922
|
+
const maxSpan = Math.max(1, nextNoteIdx - idx);
|
|
923
|
+
const desiredSpan = Math.max(1, Math.ceil(note.durationTick / stepTicks));
|
|
924
|
+
const span = Math.min(maxSpan, desiredSpan);
|
|
925
|
+
const ch1 = (note.channel ?? 0) + 1;
|
|
926
|
+
if (span === 1) {
|
|
927
|
+
const gate = clampInt((note.durationTick / stepTicks) * 100, 1, 100);
|
|
928
|
+
steps[idx] = compactStep({
|
|
929
|
+
on: true,
|
|
930
|
+
velocity: note.velocity,
|
|
931
|
+
gate,
|
|
932
|
+
offset,
|
|
933
|
+
midiChannel: ch1 !== 1 ? ch1 : undefined
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
else {
|
|
937
|
+
steps[idx] = compactStep({
|
|
938
|
+
on: true,
|
|
939
|
+
velocity: note.velocity,
|
|
940
|
+
gate: 100,
|
|
941
|
+
offset,
|
|
942
|
+
midiChannel: ch1 !== 1 ? ch1 : undefined
|
|
943
|
+
});
|
|
944
|
+
for (let k = 1; k < span; k++) {
|
|
945
|
+
const isLast = k === span - 1;
|
|
946
|
+
const tailTicks = note.durationTick - (span - 1) * stepTicks;
|
|
947
|
+
const gate = isLast ? clampInt((tailTicks / stepTicks) * 100, 1, 100) : 100;
|
|
948
|
+
steps[idx + k] = compactStep({ tie: true, gate });
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
let assignment = { type: "note", pitch };
|
|
953
|
+
if (options.instrument?.type === "drum") {
|
|
954
|
+
const pad = pitch - options.instrument.baseNote;
|
|
955
|
+
if (pad >= 0 && pad < options.instrument.pads.length) {
|
|
956
|
+
assignment = { type: "kit", pad };
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
else if (options.instrument?.type === "sampler") {
|
|
960
|
+
const pad = options.instrument.zones.findIndex((z) => z.rootNote === pitch);
|
|
961
|
+
if (pad >= 0 && pad < 16) {
|
|
962
|
+
assignment = { type: "kit", pad };
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
rows.push(normalizeRow({ id: nextRowId(), assignment, lengthSteps, loopStart: 0, loopEnd: lengthSteps, steps }, patternDefaults));
|
|
966
|
+
}
|
|
967
|
+
// Controller lanes -> automation rows (SQ21).
|
|
968
|
+
for (const lane of controllers) {
|
|
969
|
+
if (rows.length >= STEP_PATTERN_MAX_ROWS)
|
|
970
|
+
break;
|
|
971
|
+
let target = null;
|
|
972
|
+
if (lane.kind === "cc" && lane.cc !== undefined) {
|
|
973
|
+
target = { kind: "cc", cc: lane.cc };
|
|
974
|
+
}
|
|
975
|
+
else if (lane.kind === "pitchBend") {
|
|
976
|
+
target = { kind: "pitchBend" };
|
|
977
|
+
}
|
|
978
|
+
else if (lane.kind === "channelPressure") {
|
|
979
|
+
target = { kind: "channelPressure" };
|
|
980
|
+
}
|
|
981
|
+
if (!target)
|
|
982
|
+
continue;
|
|
983
|
+
const steps = Array.from({ length: lengthSteps }, () => ({}));
|
|
984
|
+
let anyOn = false;
|
|
985
|
+
for (let i = 0; i < lengthSteps; i++) {
|
|
986
|
+
const s0 = windowStart + i * stepTicks;
|
|
987
|
+
const s1 = s0 + stepTicks;
|
|
988
|
+
const inStep = lane.points.filter((p) => p.tick >= s0 && p.tick < s1);
|
|
989
|
+
if (inStep.length === 0)
|
|
990
|
+
continue;
|
|
991
|
+
const val = controllerValueAt(lane, s0) ?? inStep[0].value;
|
|
992
|
+
steps[i] = compactStep({ on: true, value: Math.round(val) });
|
|
993
|
+
anyOn = true;
|
|
994
|
+
}
|
|
995
|
+
if (!anyOn)
|
|
996
|
+
continue;
|
|
997
|
+
rows.push(normalizeRow({
|
|
998
|
+
id: nextRowId(),
|
|
999
|
+
assignment: { type: "automation", target },
|
|
1000
|
+
lengthSteps,
|
|
1001
|
+
loopStart: 0,
|
|
1002
|
+
loopEnd: lengthSteps,
|
|
1003
|
+
midiChannel: (lane.channel ?? 0) + 1,
|
|
1004
|
+
automationMode: lane.interpolation === "step" ? "latch" : "slide",
|
|
1005
|
+
steps
|
|
1006
|
+
}, patternDefaults));
|
|
1007
|
+
}
|
|
1008
|
+
const pattern = normalizePattern({
|
|
1009
|
+
name: options.name ?? "Pattern",
|
|
1010
|
+
seed: options.seed ?? 1,
|
|
1011
|
+
lengthSteps,
|
|
1012
|
+
stepRate,
|
|
1013
|
+
rows
|
|
1014
|
+
});
|
|
1015
|
+
return { pattern, droppedNotes, mergedCollisions };
|
|
1016
|
+
}
|
|
1017
|
+
// ── Selection & transform helpers for SQ51–SQ53 ─────────────────────────────
|
|
1018
|
+
/**
|
|
1019
|
+
* Parse 1-based step selector ("1,5,9" | "1-16" | [1, 5]) into sorted unique
|
|
1020
|
+
* 0-based step indices within `[0, maxSteps)` (D97, SQ52).
|
|
1021
|
+
*/
|
|
1022
|
+
export function parseStepSelection(spec, maxSteps) {
|
|
1023
|
+
const out = new Set();
|
|
1024
|
+
const add1Based = (n) => {
|
|
1025
|
+
if (!Number.isInteger(n) || n < 1 || n > maxSteps) {
|
|
1026
|
+
throw new Error(`Step ${n} is out of range. Valid steps are 1..${maxSteps}.`);
|
|
1027
|
+
}
|
|
1028
|
+
out.add(n - 1);
|
|
1029
|
+
};
|
|
1030
|
+
if (Array.isArray(spec)) {
|
|
1031
|
+
for (const n of spec)
|
|
1032
|
+
add1Based(n);
|
|
1033
|
+
}
|
|
1034
|
+
else {
|
|
1035
|
+
const trimmed = String(spec).trim();
|
|
1036
|
+
if (!trimmed)
|
|
1037
|
+
throw new Error(`Empty step list. Valid steps are 1..${maxSteps}.`);
|
|
1038
|
+
for (const part of trimmed.split(",")) {
|
|
1039
|
+
const token = part.trim();
|
|
1040
|
+
if (!token)
|
|
1041
|
+
continue;
|
|
1042
|
+
const range = /^(\d+)\s*-\s*(\d+)$/.exec(token);
|
|
1043
|
+
if (range) {
|
|
1044
|
+
const a = Number(range[1]);
|
|
1045
|
+
const b = Number(range[2]);
|
|
1046
|
+
if (a > b)
|
|
1047
|
+
throw new Error(`Invalid step range "${token}": start must be <= end.`);
|
|
1048
|
+
for (let k = a; k <= b; k++)
|
|
1049
|
+
add1Based(k);
|
|
1050
|
+
}
|
|
1051
|
+
else if (/^\d+$/.test(token)) {
|
|
1052
|
+
add1Based(Number(token));
|
|
1053
|
+
}
|
|
1054
|
+
else {
|
|
1055
|
+
throw new Error(`Invalid step selector "${token}". Use 1-based numbers or ranges like "1-4,9".`);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
return [...out].sort((a, b) => a - b);
|
|
1060
|
+
}
|
|
1061
|
+
const MODE_TO_FIELD = {
|
|
1062
|
+
onOff: "on",
|
|
1063
|
+
velocity: "velocity",
|
|
1064
|
+
value: "value",
|
|
1065
|
+
gate: "gate",
|
|
1066
|
+
tie: "tie",
|
|
1067
|
+
note: "note",
|
|
1068
|
+
octave: "octave",
|
|
1069
|
+
repeat: "repeat",
|
|
1070
|
+
chance: "chance",
|
|
1071
|
+
offset: "offset",
|
|
1072
|
+
stepRate: "stepRate",
|
|
1073
|
+
skip: "skip"
|
|
1074
|
+
};
|
|
1075
|
+
/** Rotate a row's steps (or only one edit mode's values) by `by` (+ right, - left) (SQ35, SQ53). */
|
|
1076
|
+
export function rotateRowSteps(row, by, mode) {
|
|
1077
|
+
const n = row.steps.length;
|
|
1078
|
+
if (n <= 1)
|
|
1079
|
+
return row;
|
|
1080
|
+
const shift = ((Math.trunc(by) % n) + n) % n;
|
|
1081
|
+
if (shift === 0)
|
|
1082
|
+
return row;
|
|
1083
|
+
if (!mode) {
|
|
1084
|
+
const steps = row.steps.map((_, i) => row.steps[(i - shift + n) % n]);
|
|
1085
|
+
return { ...row, steps };
|
|
1086
|
+
}
|
|
1087
|
+
const field = MODE_TO_FIELD[mode];
|
|
1088
|
+
if (!field)
|
|
1089
|
+
return row;
|
|
1090
|
+
const steps = row.steps.map((cur, i) => {
|
|
1091
|
+
const donor = row.steps[(i - shift + n) % n];
|
|
1092
|
+
const next = { ...cur };
|
|
1093
|
+
if (donor[field] === undefined)
|
|
1094
|
+
delete next[field];
|
|
1095
|
+
else
|
|
1096
|
+
next[field] = donor[field];
|
|
1097
|
+
return compactStep(next);
|
|
1098
|
+
});
|
|
1099
|
+
return { ...row, steps };
|
|
1100
|
+
}
|
|
1101
|
+
/** Clear one mode (or all fields) on the selected step indices of `row` (SQ35, SQ52). */
|
|
1102
|
+
export function clearRowSteps(row, indices, mode) {
|
|
1103
|
+
const target = indices ? new Set(indices) : null;
|
|
1104
|
+
const field = mode ? MODE_TO_FIELD[mode] : undefined;
|
|
1105
|
+
const steps = row.steps.map((s, i) => {
|
|
1106
|
+
if (target && !target.has(i))
|
|
1107
|
+
return s;
|
|
1108
|
+
if (!mode)
|
|
1109
|
+
return {};
|
|
1110
|
+
if (!field)
|
|
1111
|
+
return s;
|
|
1112
|
+
const next = { ...s };
|
|
1113
|
+
delete next[field];
|
|
1114
|
+
return compactStep(next);
|
|
1115
|
+
});
|
|
1116
|
+
return { ...row, steps };
|
|
1117
|
+
}
|
|
1118
|
+
/** Transpose pitched row or selected steps by `semitones` (SQ35, SQ53). */
|
|
1119
|
+
export function transposeRowSteps(row, semitones, indices, instrument) {
|
|
1120
|
+
if (row.assignment.type === "automation" || semitones === 0)
|
|
1121
|
+
return row;
|
|
1122
|
+
const semi = Math.trunc(semitones);
|
|
1123
|
+
if (semi === 0)
|
|
1124
|
+
return row;
|
|
1125
|
+
if (!indices) {
|
|
1126
|
+
if (row.assignment.type === "note" || row.assignment.type === "melodic") {
|
|
1127
|
+
const oldPitch = row.assignment.pitch;
|
|
1128
|
+
const nextPitch = clampInt(oldPitch + semi, 0, 127);
|
|
1129
|
+
const actualSemi = nextPitch - oldPitch;
|
|
1130
|
+
const oldPc = ((oldPitch % 12) + 12) % 12;
|
|
1131
|
+
const octWrap = Math.floor((oldPc + actualSemi) / 12);
|
|
1132
|
+
const steps = octWrap === 0
|
|
1133
|
+
? row.steps
|
|
1134
|
+
: row.steps.map((s) => s.octave !== undefined
|
|
1135
|
+
? compactStep({ ...s, octave: clampInt(s.octave + octWrap, -2, 8) })
|
|
1136
|
+
: s);
|
|
1137
|
+
return { ...row, assignment: { ...row.assignment, pitch: nextPitch }, steps };
|
|
1138
|
+
}
|
|
1139
|
+
if (row.assignment.type === "degree") {
|
|
1140
|
+
const octDelta = Math.trunc(semi / 12);
|
|
1141
|
+
const octave = clampInt(row.assignment.octave + octDelta, -2, 8);
|
|
1142
|
+
return { ...row, assignment: { ...row.assignment, octave } };
|
|
1143
|
+
}
|
|
1144
|
+
if (row.assignment.type === "kit") {
|
|
1145
|
+
const targetIdx = [];
|
|
1146
|
+
row.steps.forEach((s, i) => {
|
|
1147
|
+
if (s.on || s.note !== undefined || s.octave !== undefined)
|
|
1148
|
+
targetIdx.push(i);
|
|
1149
|
+
});
|
|
1150
|
+
return targetIdx.length > 0
|
|
1151
|
+
? transposeRowSteps(row, semi, targetIdx, instrument)
|
|
1152
|
+
: row;
|
|
1153
|
+
}
|
|
1154
|
+
return row;
|
|
1155
|
+
}
|
|
1156
|
+
let nextRow = row;
|
|
1157
|
+
for (const idx of indices) {
|
|
1158
|
+
const cur = nextRow.steps[idx] ?? {};
|
|
1159
|
+
const total = (cur.note ?? 0) + semi;
|
|
1160
|
+
const baseOct = cur.octave ??
|
|
1161
|
+
(nextRow.assignment.type === "degree"
|
|
1162
|
+
? nextRow.assignment.octave
|
|
1163
|
+
: nextRow.assignment.type === "kit"
|
|
1164
|
+
? Math.floor(kitPadPitch(nextRow.assignment.pad, instrument) / 12) - 2
|
|
1165
|
+
: nextRow.assignment.type === "note" || nextRow.assignment.type === "melodic"
|
|
1166
|
+
? Math.floor(nextRow.assignment.pitch / 12) - 2
|
|
1167
|
+
: 3);
|
|
1168
|
+
let noteVal = total;
|
|
1169
|
+
let octaveVal = cur.octave;
|
|
1170
|
+
if (noteVal > 12 || noteVal < -12) {
|
|
1171
|
+
const octShift = Math.trunc(noteVal / 12);
|
|
1172
|
+
noteVal -= octShift * 12;
|
|
1173
|
+
octaveVal = clampInt(baseOct + octShift, -2, 8);
|
|
1174
|
+
}
|
|
1175
|
+
const values = { note: clampInt(noteVal, -12, 12) };
|
|
1176
|
+
if (octaveVal !== undefined)
|
|
1177
|
+
values.octave = octaveVal;
|
|
1178
|
+
nextRow = setStepValue(nextRow, idx, values, { keepOff: !cur.on });
|
|
1179
|
+
}
|
|
1180
|
+
return nextRow;
|
|
1181
|
+
}
|
|
1182
|
+
/** Increment/decrement values for `mode` across `row` (SQ35, SQ53). */
|
|
1183
|
+
export function nudgeRowValues(row, mode, delta, instrument) {
|
|
1184
|
+
const isDeviceValue = mode === "value" &&
|
|
1185
|
+
row.assignment.type === "automation" &&
|
|
1186
|
+
row.assignment.target.kind === "device";
|
|
1187
|
+
if (!Number.isFinite(delta) || delta === 0)
|
|
1188
|
+
return row;
|
|
1189
|
+
const d = Math.trunc(delta);
|
|
1190
|
+
if (!isDeviceValue && d === 0)
|
|
1191
|
+
return row;
|
|
1192
|
+
if (mode === "onOff") {
|
|
1193
|
+
const on = d > 0;
|
|
1194
|
+
return {
|
|
1195
|
+
...row,
|
|
1196
|
+
steps: row.steps.map((s) => compactStep({ ...s, on }))
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
if (mode === "tie") {
|
|
1200
|
+
if (d > 0) {
|
|
1201
|
+
return {
|
|
1202
|
+
...row,
|
|
1203
|
+
steps: row.steps.map((s, i) => i > 0 ? compactStep({ ...s, tie: true }) : compactStep({ ...s, tie: false }))
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
return {
|
|
1207
|
+
...row,
|
|
1208
|
+
steps: row.steps.map((s) => compactStep({ ...s, tie: false }))
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
if (mode === "skip") {
|
|
1212
|
+
return {
|
|
1213
|
+
...row,
|
|
1214
|
+
steps: row.steps.map((s) => compactStep({ ...s, skip: d > 0 }))
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
if (mode === "loop") {
|
|
1218
|
+
const loopEnd = clampInt(row.loopEnd + d, row.loopStart + 1, row.lengthSteps);
|
|
1219
|
+
return { ...row, loopEnd };
|
|
1220
|
+
}
|
|
1221
|
+
const steps = row.steps.map((s) => {
|
|
1222
|
+
const n = normalizeStep(s);
|
|
1223
|
+
switch (mode) {
|
|
1224
|
+
case "velocity":
|
|
1225
|
+
return compactStep({ ...s, velocity: clampInt(n.velocity + d, 1, 127) });
|
|
1226
|
+
case "gate":
|
|
1227
|
+
return compactStep({ ...s, gate: clampInt(n.gate + d, 1, 100) });
|
|
1228
|
+
case "note":
|
|
1229
|
+
return compactStep({ ...s, note: clampInt(n.note + d, -12, 12) });
|
|
1230
|
+
case "octave": {
|
|
1231
|
+
const curOct = n.octave ??
|
|
1232
|
+
(row.assignment.type === "degree"
|
|
1233
|
+
? row.assignment.octave
|
|
1234
|
+
: row.assignment.type === "kit"
|
|
1235
|
+
? Math.floor(kitPadPitch(row.assignment.pad, instrument) / 12) - 2
|
|
1236
|
+
: row.assignment.type === "note" || row.assignment.type === "melodic"
|
|
1237
|
+
? Math.floor(row.assignment.pitch / 12) - 2
|
|
1238
|
+
: 3);
|
|
1239
|
+
return compactStep({ ...s, octave: clampInt(curOct + d, -2, 8) });
|
|
1240
|
+
}
|
|
1241
|
+
case "repeat":
|
|
1242
|
+
return compactStep({ ...s, repeat: clampInt(n.repeat + d, 1, 16) });
|
|
1243
|
+
case "chance":
|
|
1244
|
+
return compactStep({ ...s, chance: clampInt(n.chance + d, 0, 100) });
|
|
1245
|
+
case "offset":
|
|
1246
|
+
return compactStep({ ...s, offset: clampInt(n.offset + d, -50, 50) });
|
|
1247
|
+
case "stepRate":
|
|
1248
|
+
return compactStep({ ...s, stepRate: nudgeStepRate(n.stepRate ?? row.stepRate, d) });
|
|
1249
|
+
case "value": {
|
|
1250
|
+
const cur = n.value ?? 0;
|
|
1251
|
+
if (isDeviceValue) {
|
|
1252
|
+
return compactStep({ ...s, value: cur + delta });
|
|
1253
|
+
}
|
|
1254
|
+
const max = row.assignment.type === "automation" && row.assignment.target.kind === "pitchBend"
|
|
1255
|
+
? 8191
|
|
1256
|
+
: 127;
|
|
1257
|
+
const min = row.assignment.type === "automation" && row.assignment.target.kind === "pitchBend"
|
|
1258
|
+
? -8192
|
|
1259
|
+
: 0;
|
|
1260
|
+
return compactStep({ ...s, value: clampInt(cur + d, min, max) });
|
|
1261
|
+
}
|
|
1262
|
+
default:
|
|
1263
|
+
return s;
|
|
1264
|
+
}
|
|
1265
|
+
});
|
|
1266
|
+
const assignment = (mode === "note" || mode === "octave") && row.assignment.type === "note"
|
|
1267
|
+
? { type: "melodic", pitch: row.assignment.pitch }
|
|
1268
|
+
: row.assignment;
|
|
1269
|
+
return { ...row, assignment, steps };
|
|
1270
|
+
}
|
|
1271
|
+
/** Tie or untie steps from 0-based `fromIdx` to `toIdx` inclusive (SQ33, SQ53). */
|
|
1272
|
+
export function tieRowSteps(row, fromIdx, toIdx, tie) {
|
|
1273
|
+
const lo = Math.max(0, Math.min(fromIdx, toIdx));
|
|
1274
|
+
const hi = Math.min(row.steps.length - 1, Math.max(fromIdx, toIdx));
|
|
1275
|
+
const steps = [...row.steps];
|
|
1276
|
+
if (tie) {
|
|
1277
|
+
if (!steps[lo]?.on) {
|
|
1278
|
+
steps[lo] = compactStep({ ...steps[lo], on: true, tie: false });
|
|
1279
|
+
}
|
|
1280
|
+
for (let i = lo + 1; i <= hi; i++) {
|
|
1281
|
+
steps[i] = compactStep({ ...steps[i], tie: true });
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
else {
|
|
1285
|
+
for (let i = lo; i <= hi; i++) {
|
|
1286
|
+
steps[i] = compactStep({ ...steps[i], tie: false });
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
return { ...row, steps };
|
|
1290
|
+
}
|
|
1291
|
+
/** Seeded deterministic randomize of `mode` across selected rows/steps (SQ35, SQ53, D78, D98). */
|
|
1292
|
+
export function randomizePatternSteps(pattern, mode, options) {
|
|
1293
|
+
const density = clampInt(options.density ?? 50, 0, 100);
|
|
1294
|
+
const targetSteps = options.stepIndices ? new Set(options.stepIndices) : null;
|
|
1295
|
+
const seed = options.seed >>> 0;
|
|
1296
|
+
// Track mono occupancy if mode === "onOff" and pattern.mono is enabled (SQ35).
|
|
1297
|
+
const monoOccupied = new Set();
|
|
1298
|
+
if (mode === "onOff" && pattern.mono) {
|
|
1299
|
+
for (const r of pattern.rows) {
|
|
1300
|
+
if (options.rowIds && !options.rowIds.has(r.id) && isNoteRow(r)) {
|
|
1301
|
+
r.steps.forEach((s, i) => {
|
|
1302
|
+
if (s.on)
|
|
1303
|
+
monoOccupied.add(i);
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
const rows = pattern.rows.map((row) => {
|
|
1309
|
+
if (options.rowIds && !options.rowIds.has(row.id))
|
|
1310
|
+
return row;
|
|
1311
|
+
const rk = rowKeyHash(row.id);
|
|
1312
|
+
let nextRow = row;
|
|
1313
|
+
let devRange;
|
|
1314
|
+
if (mode === "value" &&
|
|
1315
|
+
row.assignment.type === "automation" &&
|
|
1316
|
+
row.assignment.target.kind === "device") {
|
|
1317
|
+
const vals = row.steps
|
|
1318
|
+
.map((st) => st.value)
|
|
1319
|
+
.filter((v) => v !== undefined && Number.isFinite(v));
|
|
1320
|
+
if (vals.length >= 2) {
|
|
1321
|
+
const lo = Math.min(...vals);
|
|
1322
|
+
const hi = Math.max(...vals);
|
|
1323
|
+
devRange = hi > lo ? [lo, hi] : lo > 1 ? [0, lo * 2] : [0, 1];
|
|
1324
|
+
}
|
|
1325
|
+
else if (vals.length === 1) {
|
|
1326
|
+
devRange = vals[0] > 1 ? [0, vals[0] * 2] : [0, 1];
|
|
1327
|
+
}
|
|
1328
|
+
else {
|
|
1329
|
+
devRange = [0, 1];
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
const steps = row.steps.map((s, i) => {
|
|
1333
|
+
if (targetSteps && !targetSteps.has(i))
|
|
1334
|
+
return s;
|
|
1335
|
+
const u = stepHash(seed, rk, i, 0);
|
|
1336
|
+
switch (mode) {
|
|
1337
|
+
case "onOff": {
|
|
1338
|
+
let on = u * 100 < density;
|
|
1339
|
+
if (on && pattern.mono && isNoteRow(row)) {
|
|
1340
|
+
if (monoOccupied.has(i))
|
|
1341
|
+
on = false;
|
|
1342
|
+
else
|
|
1343
|
+
monoOccupied.add(i);
|
|
1344
|
+
}
|
|
1345
|
+
return compactStep({ ...s, on });
|
|
1346
|
+
}
|
|
1347
|
+
case "velocity":
|
|
1348
|
+
return compactStep({ ...s, velocity: clampInt(1 + u * 126, 1, 127) });
|
|
1349
|
+
case "gate":
|
|
1350
|
+
return compactStep({ ...s, gate: clampInt(10 + u * 90, 1, 100) });
|
|
1351
|
+
case "tie":
|
|
1352
|
+
return compactStep({ ...s, tie: i > 0 && u < 0.35 });
|
|
1353
|
+
case "note":
|
|
1354
|
+
return compactStep({ ...s, note: clampInt(-12 + u * 24, -12, 12) });
|
|
1355
|
+
case "octave":
|
|
1356
|
+
return compactStep({ ...s, octave: clampInt(1 + u * 4, -2, 8) });
|
|
1357
|
+
case "repeat":
|
|
1358
|
+
return compactStep({ ...s, repeat: clampInt(1 + u * 3, 1, 16) });
|
|
1359
|
+
case "chance":
|
|
1360
|
+
return compactStep({ ...s, chance: clampInt(u * 100, 0, 100) });
|
|
1361
|
+
case "offset":
|
|
1362
|
+
return compactStep({ ...s, offset: clampInt(-50 + u * 100, -50, 50) });
|
|
1363
|
+
case "stepRate": {
|
|
1364
|
+
const choices = ["1/8", "1/16", "1/32", "16T"];
|
|
1365
|
+
return compactStep({ ...s, stepRate: choices[Math.floor(u * choices.length)] });
|
|
1366
|
+
}
|
|
1367
|
+
case "skip":
|
|
1368
|
+
return compactStep({ ...s, skip: u < 0.25 });
|
|
1369
|
+
case "value": {
|
|
1370
|
+
if (devRange) {
|
|
1371
|
+
const [lo, hi] = devRange;
|
|
1372
|
+
return compactStep({ ...s, value: Number((lo + u * (hi - lo)).toFixed(4)) });
|
|
1373
|
+
}
|
|
1374
|
+
if (row.assignment.type === "automation" && row.assignment.target.kind === "pitchBend") {
|
|
1375
|
+
return compactStep({ ...s, value: clampInt(-8192 + u * 16383, -8192, 8191) });
|
|
1376
|
+
}
|
|
1377
|
+
return compactStep({ ...s, value: clampInt(u * 127, 0, 127) });
|
|
1378
|
+
}
|
|
1379
|
+
case "loop":
|
|
1380
|
+
return s;
|
|
1381
|
+
}
|
|
1382
|
+
});
|
|
1383
|
+
if ((mode === "note" || mode === "octave") && nextRow.assignment.type === "note") {
|
|
1384
|
+
nextRow = { ...nextRow, assignment: { type: "melodic", pitch: nextRow.assignment.pitch } };
|
|
1385
|
+
}
|
|
1386
|
+
return { ...nextRow, steps };
|
|
1387
|
+
});
|
|
1388
|
+
return { ...pattern, rows };
|
|
1389
|
+
}
|
|
1390
|
+
//# sourceMappingURL=stepPattern.js.map
|