-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantizer.js
More file actions
73 lines (60 loc) · 1.98 KB
/
quantizer.js
File metadata and controls
73 lines (60 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(function createQuantizer(root, factory) {
const exported = factory();
if (typeof module !== "undefined" && module.exports) {
module.exports = exported;
}
if (root) {
root.Quantizer = exported;
}
})(
typeof globalThis !== "undefined" ? globalThis : this,
function buildQuantizer() {
const DEFAULT_STEPS_PER_BEAT = 4;
function getTicksPerStep(ppq, stepsPerBeat) {
if (!Number.isFinite(ppq) || ppq <= 0) {
throw new Error("ppq must be a positive number");
}
if (!Number.isFinite(stepsPerBeat) || stepsPerBeat <= 0) {
throw new Error("stepsPerBeat must be a positive number");
}
return ppq / stepsPerBeat;
}
function tickToStep(tick, ppq, stepsPerBeat = DEFAULT_STEPS_PER_BEAT) {
if (!Number.isFinite(tick)) {
throw new Error("tick must be a finite number");
}
const ticksPerStep = getTicksPerStep(ppq, stepsPerBeat);
return Math.round(tick / ticksPerStep);
}
function durationToSteps(durationTicks, ppq, stepsPerBeat = DEFAULT_STEPS_PER_BEAT) {
if (!Number.isFinite(durationTicks) || durationTicks < 0) {
throw new Error("durationTicks must be a number >= 0");
}
const ticksPerStep = getTicksPerStep(ppq, stepsPerBeat);
const raw = Math.round(durationTicks / ticksPerStep);
return Math.max(1, raw);
}
function quantizeNote(note, ppq, stepsPerBeat = DEFAULT_STEPS_PER_BEAT) {
const step = tickToStep(note.startTick, ppq, stepsPerBeat);
const duration = durationToSteps(note.durationTicks, ppq, stepsPerBeat);
return {
...note,
step,
duration,
};
}
function quantizeTrack(track, ppq, stepsPerBeat = DEFAULT_STEPS_PER_BEAT) {
return {
...track,
notes: track.notes.map((note) => quantizeNote(note, ppq, stepsPerBeat)),
};
}
return {
DEFAULT_STEPS_PER_BEAT,
tickToStep,
durationToSteps,
quantizeNote,
quantizeTrack,
};
},
);