@ac-kit/media-piano-fingering 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # @ac-kit/media-piano-fingering
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c18e8f3: New package: `estimatePianoFingering`, the hand and finger of each piano note,
8
+ from an ergonomic cost (Balliauw et al.'s finger-pair spans for a small, medium
9
+ or large hand, thumb passing, black keys, hand shifts against the time they
10
+ have) minimised chord by chord. Keeps the hands and fingers a score gives.
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [c18e8f3]
15
+ - Updated dependencies [c18e8f3]
16
+ - @ac-kit/algo@0.3.0
17
+ - @ac-kit/model-music@0.6.0
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,3 +1,61 @@
1
- # Temporary Holding Version
1
+ # @ac-kit/media-piano-fingering
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Which hand and which finger play each note of a piano performance or score.
4
+
5
+ ```ts
6
+ import { estimatePianoFingering } from "@ac-kit/media-piano-fingering";
7
+
8
+ estimatePianoFingering([
9
+ { pitch: 60, startSeconds: 0, endSeconds: 1 },
10
+ { pitch: 64, startSeconds: 0, endSeconds: 1 },
11
+ { pitch: 67, startSeconds: 0, endSeconds: 1 },
12
+ ]);
13
+ // [{ hand: "right", finger: 1 }, { hand: "right", finger: 3 }, { hand: "right", finger: 5 }]
14
+ ```
15
+
16
+ ## What it exposes
17
+
18
+ - `estimatePianoFingering` — a hand and a finger for every note, given as key
19
+ presses: pitch, key-down and key-up times. A `@ac-kit/model-music`
20
+ `SongNote` is one as it stands. Notes may carry a `hand` (a score's staff) or
21
+ a `fingering` (a printed finger) the result keeps. The `handSize` option,
22
+ `small`, `medium` or `large`, sets how far the hand reaches.
23
+
24
+ Separating the hands and fingering them are one search, because whether a
25
+ chord fits a hand is a fingering question: no hand is ever given a chord beyond
26
+ its reach. The estimate minimises a hand-written ergonomic cost — finger-pair
27
+ stretch limits, thumb passing, black keys, hand shifts against the time
28
+ available for them — by dynamic programming over the chords. Nothing is learned
29
+ from data.
30
+
31
+ The result is a judgement, not a rule: two pianists agree on about seven
32
+ fingers in ten.
33
+
34
+ ## Not in scope
35
+
36
+ - **Instruments other than the piano.** Organ pedals, guitar frets and string
37
+ positions are different models.
38
+ - **Choosing which parts a pianist plays.** The caller passes the notes of the
39
+ piano part.
40
+ - **Printing the fingering.** The result is data; a score carries it through
41
+ `@ac-kit/model-music`'s `SongNote.fingering`.
42
+
43
+ Portable: no Node.js built-ins.
44
+
45
+ See the
46
+ [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/media-piano-fingering/)
47
+ for the full reference.
48
+
49
+ ## Further reading
50
+
51
+ - R. Parncutt, J. A. Sloboda, E. F. Clarke, M. Raekallio, P. Desain, _An
52
+ ergonomic model of keyboard fingering for melodic fragments_, Music
53
+ Perception 14(4), 1997.
54
+ - M. Balliauw, D. Herremans, D. Palhazi Cuervo, K. Sörensen, _A variable
55
+ neighborhood search algorithm to generate piano fingerings for polyphonic
56
+ sheet music_, International Transactions in Operational Research 24(3),
57
+ 2017.
58
+ - A. Al Kasimi, E. Nichols, C. Raphael, _A simple algorithm for automatic
59
+ generation of polyphonic piano fingerings_, ISMIR 2007.
60
+ - E. Nakamura, Y. Saito, K. Yoshii, _Statistical learning and estimation of
61
+ piano fingering_, Information Sciences 517, 2020.
@@ -0,0 +1,49 @@
1
+ import type { PianoFinger, PianoHand, PianoHandSize } from "../types.js";
2
+ /**
3
+ * A key a finger plays: where it sits, its colour, the finger, and when the key
4
+ * comes up.
5
+ *
6
+ * @internal
7
+ */
8
+ export type PlacedKey_ = {
9
+ position: number;
10
+ black: boolean;
11
+ finger: PianoFinger;
12
+ endSeconds: number;
13
+ };
14
+ /**
15
+ * The cost of moving a hand `units` beyond where it can reach, with
16
+ * `gapSeconds` to do it in.
17
+ *
18
+ * @internal
19
+ */
20
+ export declare function shiftCost_(units: number, gapSeconds: number): number;
21
+ /**
22
+ * The cost of a key for the finger on it, alone: a weak finger, the thumb on a
23
+ * black key.
24
+ *
25
+ * @internal
26
+ */
27
+ export declare function keyCost_(key: PlacedKey_): number;
28
+ /**
29
+ * The cost of holding keys down together in one hand, `keys` sorted by
30
+ * position: the stretch and span of each pair of neighbours. `Infinity` when
31
+ * any two keys, neighbours or not, are beyond the practical span, unless
32
+ * `isLenient`, which prices the excess as a shift instead.
33
+ *
34
+ * @internal
35
+ */
36
+ export declare function chordCost_(hand: PianoHand, handSize: PianoHandSize, keys: readonly PlacedKey_[], isLenient: boolean): number;
37
+ /**
38
+ * Playing `to` after `from` in the same hand: the cost of doing it in one hand
39
+ * position, or, when it cannot be — beyond the practical span, or the same
40
+ * finger on another key — the units the hand must shift instead, priced by the
41
+ * caller once for the whole hand with {@link shiftCost_}.
42
+ *
43
+ * @internal
44
+ */
45
+ export declare function transition_(hand: PianoHand, handSize: PianoHandSize, from: PlacedKey_, to: PlacedKey_): {
46
+ cost: number;
47
+ shiftUnits: number;
48
+ };
49
+ //# sourceMappingURL=_costs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_costs.d.ts","sourceRoot":"","sources":["../../src/cost/_costs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEzE;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,WAAW,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;CACnB,CAAC;AA0CF;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAMpE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAShD;AAqBD;;;;;;;GAOG;AACH,wBAAgB,UAAU,CACzB,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,aAAa,EACvB,IAAI,EAAE,SAAS,UAAU,EAAE,EAC3B,SAAS,EAAE,OAAO,GAChB,MAAM,CAsBR;AAuBD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAC1B,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,aAAa,EACvB,IAAI,EAAE,UAAU,EAChB,EAAE,EAAE,UAAU,GACZ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAuCtC"}
@@ -0,0 +1,177 @@
1
+ import { fingerSpan_, reachOf_ } from "../span/_finger-spans.js";
2
+ // Parncutt et al. 1997 rule 1 (Stretch), kept by Balliauw rule 1: per unit
3
+ // beyond the comfortable span.
4
+ const STRETCH_PER_UNIT_ = 2;
5
+ // Balliauw rule 2 (Small-Span): per unit below the relaxed span, any pair.
6
+ const SMALL_SPAN_PER_UNIT_ = 1;
7
+ // Parncutt rule 3 (Large-Span) with Jacobs 2001's one point for every pair:
8
+ // per unit beyond the relaxed span. Balliauw drops it; without it a triad
9
+ // costs the same on 1-2-3 as on 1-3-5.
10
+ const LARGE_SPAN_PER_UNIT_ = 1;
11
+ // Jacobs's weak finger (Balliauw rule 5): each use of finger 4.
12
+ const WEAK_FINGER_ = 1;
13
+ // Parncutt rule 8 (Three-to-Four), Balliauw rule 6.
14
+ const THREE_TO_FOUR_ = 1;
15
+ // Parncutt rule 9 (Four-on-Black), Balliauw rule 7: 3 on white next to 4 on black.
16
+ const FOUR_ON_BLACK_ = 1;
17
+ // Parncutt rule 10 (Thumb-on-Black) at Balliauw's weight of 0.5: 1 for the
18
+ // thumb on a black key, 2 more per white neighbour.
19
+ const THUMB_ON_BLACK_ = 0.5;
20
+ const THUMB_ON_BLACK_BESIDE_WHITE_ = 1;
21
+ // Parncutt rule 11 (Five-on-Black) at Balliauw's weight of 0.5: 2 per white
22
+ // neighbour of finger 5 on a black key.
23
+ const FIVE_ON_BLACK_BESIDE_WHITE_ = 1;
24
+ // Parncutt rule 12 (Thumb-Passing): a pass on one level, and the thumb passing
25
+ // onto a black key from a finger on white. pydactyl wires Parncutt's 3 for
26
+ // Balliauw too; the paper reportedly uses 2 (unverified).
27
+ const THUMB_PASSING_ = 1;
28
+ const THUMB_PASSING_ONTO_BLACK_ = 3;
29
+ // Balliauw rule 15: the same key struck again with another finger.
30
+ const REPEATED_KEY_OTHER_FINGER_ = 1;
31
+ // Balliauw rule 13 (Impractical): per unit beyond the practical span. Here it
32
+ // prices a hand shift, scaled by how hurried the shift is.
33
+ const SHIFT_PER_UNIT_ = 10;
34
+ // A shift with this much time between the last key-up and the next key-down
35
+ // costs its full price; one with more costs proportionally less, a fifth of it
36
+ // with a quarter of a second. Set on the fixtures: no published value.
37
+ const SHIFT_UNHURRIED_SECONDS_ = 0.05;
38
+ // However long the hand has, moving it is never free, so an idle hand does not
39
+ // wander. Set on the fixtures.
40
+ const MIN_SHIFT_URGENCY_ = 0.05;
41
+ /**
42
+ * The cost of moving a hand `units` beyond where it can reach, with
43
+ * `gapSeconds` to do it in.
44
+ *
45
+ * @internal
46
+ */
47
+ export function shiftCost_(units, gapSeconds) {
48
+ const urgency = gapSeconds <= SHIFT_UNHURRIED_SECONDS_
49
+ ? 1
50
+ : Math.max(MIN_SHIFT_URGENCY_, SHIFT_UNHURRIED_SECONDS_ / gapSeconds);
51
+ return SHIFT_PER_UNIT_ * units * urgency;
52
+ }
53
+ /**
54
+ * The cost of a key for the finger on it, alone: a weak finger, the thumb on a
55
+ * black key.
56
+ *
57
+ * @internal
58
+ */
59
+ export function keyCost_(key) {
60
+ let cost = 0;
61
+ if (key.finger === 4) {
62
+ cost += WEAK_FINGER_;
63
+ }
64
+ if (key.finger === 1 && key.black) {
65
+ cost += THUMB_ON_BLACK_;
66
+ }
67
+ return cost;
68
+ }
69
+ // Stretch and span of a reach within the practical limits.
70
+ function spanCost_(span, reach) {
71
+ let cost = 0;
72
+ if (reach > span.maxComfortable) {
73
+ cost += STRETCH_PER_UNIT_ * (reach - span.maxComfortable);
74
+ }
75
+ else if (reach < span.minComfortable) {
76
+ cost += STRETCH_PER_UNIT_ * (span.minComfortable - reach);
77
+ }
78
+ if (reach < span.minRelaxed) {
79
+ cost += SMALL_SPAN_PER_UNIT_ * (span.minRelaxed - reach);
80
+ }
81
+ else if (reach > span.maxRelaxed) {
82
+ cost += LARGE_SPAN_PER_UNIT_ * (reach - span.maxRelaxed);
83
+ }
84
+ return cost;
85
+ }
86
+ /**
87
+ * The cost of holding keys down together in one hand, `keys` sorted by
88
+ * position: the stretch and span of each pair of neighbours. `Infinity` when
89
+ * any two keys, neighbours or not, are beyond the practical span, unless
90
+ * `isLenient`, which prices the excess as a shift instead.
91
+ *
92
+ * @internal
93
+ */
94
+ export function chordCost_(hand, handSize, keys, isLenient) {
95
+ let cost = 0;
96
+ for (let upper = 1; upper < keys.length; upper++) {
97
+ for (let lower = upper - 1; lower >= 0; lower--) {
98
+ const pair = reachOf_(hand, keys[lower], keys[upper]);
99
+ const span = fingerSpan_(handSize, pair.lower, pair.upper);
100
+ const excess = Math.max(span.minPractical - pair.reach, pair.reach - span.maxPractical, 0);
101
+ if (excess > 0) {
102
+ if (!isLenient) {
103
+ return Number.POSITIVE_INFINITY;
104
+ }
105
+ cost += SHIFT_PER_UNIT_ * excess;
106
+ }
107
+ else if (lower === upper - 1) {
108
+ cost += spanCost_(span, pair.reach);
109
+ }
110
+ }
111
+ }
112
+ return cost;
113
+ }
114
+ // Parncutt rule 12, from `from` to `to` in one hand.
115
+ function thumbPassingCost_(hand, from, to) {
116
+ const rising = to.position > from.position;
117
+ // The left hand passes the other way: its thumb lies above its fingers.
118
+ const thumbUnder = to.finger === 1 && (hand === "right" ? rising : !rising);
119
+ const fingerOver = from.finger === 1 && (hand === "right" ? !rising : rising);
120
+ if (!thumbUnder && !fingerOver) {
121
+ return 0;
122
+ }
123
+ const thumb = thumbUnder ? to : from;
124
+ const finger = thumbUnder ? from : to;
125
+ if (thumb.black === finger.black) {
126
+ return THUMB_PASSING_;
127
+ }
128
+ return thumb.black ? THUMB_PASSING_ONTO_BLACK_ : 0;
129
+ }
130
+ /**
131
+ * Playing `to` after `from` in the same hand: the cost of doing it in one hand
132
+ * position, or, when it cannot be — beyond the practical span, or the same
133
+ * finger on another key — the units the hand must shift instead, priced by the
134
+ * caller once for the whole hand with {@link shiftCost_}.
135
+ *
136
+ * @internal
137
+ */
138
+ export function transition_(hand, handSize, from, to) {
139
+ if (from.finger === to.finger) {
140
+ return { cost: 0, shiftUnits: Math.abs(to.position - from.position) };
141
+ }
142
+ const { lower, upper, reach } = reachOf_(hand, from, to);
143
+ const span = fingerSpan_(handSize, lower, upper);
144
+ if (reach < span.minPractical) {
145
+ return { cost: 0, shiftUnits: span.minPractical - reach };
146
+ }
147
+ if (reach > span.maxPractical) {
148
+ return { cost: 0, shiftUnits: reach - span.maxPractical };
149
+ }
150
+ let cost = spanCost_(span, reach);
151
+ if (from.position === to.position) {
152
+ cost += REPEATED_KEY_OTHER_FINGER_;
153
+ }
154
+ if (from.finger === 3 && to.finger === 4) {
155
+ cost += THREE_TO_FOUR_;
156
+ }
157
+ const [three, four] = from.finger === 3 ? [from, to] : [to, from];
158
+ if (three.finger === 3 && four.finger === 4 && !three.black && four.black) {
159
+ cost += FOUR_ON_BLACK_;
160
+ }
161
+ for (const [key, neighbour] of [
162
+ [from, to],
163
+ [to, from],
164
+ ]) {
165
+ if (key.black && !neighbour.black) {
166
+ if (key.finger === 1) {
167
+ cost += THUMB_ON_BLACK_BESIDE_WHITE_;
168
+ }
169
+ else if (key.finger === 5) {
170
+ cost += FIVE_ON_BLACK_BESIDE_WHITE_;
171
+ }
172
+ }
173
+ }
174
+ cost += thumbPassingCost_(hand, from, to);
175
+ return { cost, shiftUnits: 0 };
176
+ }
177
+ //# sourceMappingURL=_costs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_costs.js","sourceRoot":"","sources":["../../src/cost/_costs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,0BAA0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AK,CAAC,EAAE,CAAC;gBAC7B,IAAI,IAAI,2BAA2B,CAAC;YACrC,CAAC;QACF,CAAC;IACF,CAAC;IACD,IAAI,IAAI,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IAC1C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;AAChC,CAAC"}
@@ -0,0 +1,40 @@
1
+ import type { PianoFinger, PianoFingeringNote, PianoHand } from "../types.js";
2
+ /**
3
+ * One key of a slice, every note sounding it at once merged.
4
+ *
5
+ * @internal
6
+ */
7
+ export type SliceKey_ = {
8
+ pitch: number;
9
+ position: number;
10
+ black: boolean;
11
+ /** The latest key-up of the notes merged into it. */
12
+ endSeconds: number;
13
+ /** The indices of the notes it plays, in the caller's array. */
14
+ notes: number[];
15
+ /** A hand the result must keep. */
16
+ hand: PianoHand | undefined;
17
+ /** A finger the result must keep. */
18
+ finger: PianoFinger | undefined;
19
+ };
20
+ /**
21
+ * The keys going down together, sorted by pitch.
22
+ *
23
+ * @internal
24
+ */
25
+ export type Slice_ = {
26
+ onsetSeconds: number;
27
+ keys: SliceKey_[];
28
+ /** Keys beyond ten, each with the index in `keys` of the key it follows. */
29
+ extras: {
30
+ key: SliceKey_;
31
+ nearest: number;
32
+ }[];
33
+ };
34
+ /**
35
+ * Groups notes into slices of keys going down within 30 ms, in time order.
36
+ *
37
+ * @internal
38
+ */
39
+ export declare function composeSlices_(notes: readonly PianoFingeringNote[]): Slice_[];
40
+ //# sourceMappingURL=_slices.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_slices.d.ts","sourceRoot":"","sources":["../../src/estimate/_slices.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAS9E;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;IACf,qDAAqD;IACrD,UAAU,EAAE,MAAM,CAAC;IACnB,gEAAgE;IAChE,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,mCAAmC;IACnC,IAAI,EAAE,SAAS,GAAG,SAAS,CAAC;IAC5B,qCAAqC;IACrC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;CAChC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,MAAM,GAAG;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,SAAS,EAAE,CAAC;IAClB,4EAA4E;IAC5E,MAAM,EAAE;QAAE,GAAG,EAAE,SAAS,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAC9C,CAAC;AAYF;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,kBAAkB,EAAE,GAAG,MAAM,EAAE,CAgD7E"}
@@ -0,0 +1,77 @@
1
+ import { isBlackKey, keyPosition } from "@ac-kit/model-music";
2
+ // Notes whose keys go down within this of the first are one chord (Nakamura,
3
+ // Saito & Yoshii 2020, §5.2.3, after the onset spread of played chords).
4
+ const CHORD_ONSET_SECONDS_ = 0.03;
5
+ // Two hands hold ten keys at most; the inner keys of a larger chord share the
6
+ // finger of their nearest outer neighbour.
7
+ const MAX_KEYS_PER_SLICE_ = 10;
8
+ function fingerOf_(fingering) {
9
+ return fingering === 1 ||
10
+ fingering === 2 ||
11
+ fingering === 3 ||
12
+ fingering === 4 ||
13
+ fingering === 5
14
+ ? fingering
15
+ : undefined;
16
+ }
17
+ /**
18
+ * Groups notes into slices of keys going down within 30 ms, in time order.
19
+ *
20
+ * @internal
21
+ */
22
+ export function composeSlices_(notes) {
23
+ const order = [...notes.keys()].toSorted((indexA, indexB) => notes[indexA].startSeconds - notes[indexB].startSeconds ||
24
+ notes[indexA].pitch - notes[indexB].pitch);
25
+ const slices = [];
26
+ let start = 0;
27
+ while (start < order.length) {
28
+ const onsetSeconds = notes[order[start]].startSeconds;
29
+ const byPitch = new Map();
30
+ let end = start;
31
+ while (end < order.length &&
32
+ notes[order[end]].startSeconds - onsetSeconds <= CHORD_ONSET_SECONDS_) {
33
+ const index = order[end];
34
+ const note = notes[index];
35
+ const pitch = Math.round(note.pitch);
36
+ const key = byPitch.get(pitch);
37
+ if (key === undefined) {
38
+ byPitch.set(pitch, {
39
+ pitch,
40
+ position: keyPosition(pitch),
41
+ black: isBlackKey(pitch),
42
+ endSeconds: note.endSeconds,
43
+ notes: [index],
44
+ hand: note.hand,
45
+ finger: fingerOf_(note.fingering),
46
+ });
47
+ }
48
+ else {
49
+ key.endSeconds = Math.max(key.endSeconds, note.endSeconds);
50
+ key.notes.push(index);
51
+ key.hand ??= note.hand;
52
+ key.finger ??= fingerOf_(note.fingering);
53
+ }
54
+ end++;
55
+ }
56
+ slices.push(splitExtras_(onsetSeconds, [...byPitch.values()].toSorted((keyA, keyB) => keyA.pitch - keyB.pitch)));
57
+ start = end;
58
+ }
59
+ return slices;
60
+ }
61
+ // Keeps the five lowest and five highest keys of a chord too large for two hands.
62
+ function splitExtras_(onsetSeconds, sorted) {
63
+ if (sorted.length <= MAX_KEYS_PER_SLICE_) {
64
+ return { onsetSeconds, keys: sorted, extras: [] };
65
+ }
66
+ const half = MAX_KEYS_PER_SLICE_ / 2;
67
+ const keys = [...sorted.slice(0, half), ...sorted.slice(-half)];
68
+ const extras = sorted.slice(half, -half).map((key) => ({
69
+ key,
70
+ nearest: key.pitch - sorted[half - 1].pitch <=
71
+ sorted[sorted.length - half].pitch - key.pitch
72
+ ? half - 1
73
+ : half,
74
+ }));
75
+ return { onsetSeconds, keys, extras };
76
+ }
77
+ //# sourceMappingURL=_slices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_slices.js","sourceRoot":"","sources":["../../src/estimate/_slices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,qBA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F;AAClF,SAAS,YAAY,CAAC,YAAoB,EAAE,MAAmB;IAC9D,IAAI,MAAM,CAAC,MAAM,IAAI,mBAAmB,EAAE,CAAC;QAC1C,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;IACnD,CAAC;IACD,MAAM,IAAI,GAAG,mBAAmB,GAAG,CAAC,CAAC;IACrC,MAAM,IAAI,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAChE,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACtD,GAAG;QACH,OAAO,EACN,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,IAAI,GAAG,CAAC,CAAE,CAAC,KAAK;YACnC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,IAAI,CAAE,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK;YAC9C,CAAC,CAAC,IAAI,GAAG,CAAC;YACV,CAAC,CAAC,IAAI;KACR,CAAC,CAAC,CAAC;IACJ,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AACvC,CAAC"}
@@ -0,0 +1,39 @@
1
+ import type { PianoFingering, PianoFingeringNote, PianoFingeringOptions } from "../types.js";
2
+ /**
3
+ * The hand and finger of each piano note: which keys each hand takes in every
4
+ * chord, and with which fingers, minimising an ergonomic cost — stretches
5
+ * between fingers, thumb passing, the thumb and little finger on black keys,
6
+ * hand shifts against the time available for them, and the hands crossing.
7
+ *
8
+ * Reach for it when a source does not say which hand plays what, such as a MIDI
9
+ * file or a single-staff part, or to suggest fingers for a score. Given notes
10
+ * with a `hand` (a score's staff) or a `fingering` (a printed finger), it keeps
11
+ * them and fills in the rest.
12
+ *
13
+ * A chord no two hands can play — more than ten keys, or spans beyond reach —
14
+ * still gets an answer: the inner keys of a chord over ten share their nearest
15
+ * neighbour's finger, and a slice nothing fits is placed as close to playable
16
+ * as the costs allow, releasing held keys and given fingers if it must.
17
+ *
18
+ * The result is a judgement, not a rule: two pianists agree on about seven
19
+ * fingers in ten.
20
+ *
21
+ * Time complexity: O(S · B · C) for `S` chords, at most 64 states `B` kept per
22
+ * chord and `C` candidate fingerings per chord (a few to a few hundred, growing
23
+ * with the chord's size): about a second for a thousand notes. Memory: O(S ·
24
+ * B).
25
+ *
26
+ * @example
27
+ * estimatePianoFingering([
28
+ * { pitch: 60, startSeconds: 0, endSeconds: 1 },
29
+ * { pitch: 64, startSeconds: 0, endSeconds: 1 },
30
+ * { pitch: 67, startSeconds: 0, endSeconds: 1 },
31
+ * ]);
32
+ * // [{ hand: "right", finger: 1 }, { hand: "right", finger: 3 }, { hand: "right", finger: 5 }]
33
+ *
34
+ * @param notes - The notes of everything one pianist plays, in any order.
35
+ * @param options - See {@link PianoFingeringOptions}.
36
+ * @returns One fingering per note, in the order of `notes`.
37
+ */
38
+ export declare function estimatePianoFingering(notes: readonly PianoFingeringNote[], options?: PianoFingeringOptions): PianoFingering[];
39
+ //# sourceMappingURL=estimate-piano-fingering.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"estimate-piano-fingering.d.ts","sourceRoot":"","sources":["../../src/estimate/estimate-piano-fingering.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAEX,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EAGrB,MAAM,aAAa,CAAC;AAqDrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,sBAAsB,CACrC,KAAK,EAAE,SAAS,kBAAkB,EAAE,EACpC,OAAO,CAAC,EAAE,qBAAqB,GAC7B,cAAc,EAAE,CAuClB"}
@@ -0,0 +1,296 @@
1
+ import { combinations, viterbi } from "@ac-kit/algo";
2
+ import { keyPosition } from "@ac-kit/model-music";
3
+ import { chordCost_, keyCost_, shiftCost_, transition_, } from "../cost/_costs.js";
4
+ import { composeSlices_ } from "./_slices.js";
5
+ // A key still down this long after the next key-down is held; one released
6
+ // sooner is finger legato, which overlaps notes without pinning the finger.
7
+ // Set on the fixtures.
8
+ const LEGATO_SECONDS_ = 0.05;
9
+ // Lifting a held key early, per second cut, counting a second at most. A MIDI
10
+ // note's length is often the written one, sustained by the pedal rather than
11
+ // the finger, so a hold is only a preference, and cutting a note by a
12
+ // fraction of a beat costs little. Set on the fixtures.
13
+ const EARLY_RELEASE_PER_SECOND_ = 4;
14
+ const EARLY_RELEASE_MAX_SECONDS_ = 1;
15
+ // The hands crossing: the left playing above a key the right holds or has just
16
+ // played. Rare on purpose in the repertoire, so it costs as much as a
17
+ // three-unit hurried shift. Set on the fixtures.
18
+ const HANDS_CROSSING_ = 30;
19
+ // A hand that played this recently still occupies its keys for the other hand.
20
+ const HAND_PRESENCE_SECONDS_ = 1;
21
+ // A line handed over to the other hand: a slice one hand plays alone, within an
22
+ // octave of the slice just before it that the other hand played alone, while
23
+ // that hand is still present. Pianists keep a line in one hand; without this a
24
+ // thumb pass costs more than giving the next note to the idle hand. Set on the
25
+ // fixtures, below a thumb pass with its small span.
26
+ const HAND_SWITCH_ = 5;
27
+ const HAND_SWITCH_REACH_ = keyPosition(72) - keyPosition(60);
28
+ // Where each hand rests before it first plays, C3 and G4: its first keys cost
29
+ // an unhurried shift from there, so the first notes of a line go to the hand
30
+ // nearer them, middle C to the right. Set on the fixtures.
31
+ const HOME_POSITIONS_ = { left: keyPosition(48), right: keyPosition(67) };
32
+ // Added to every candidate of a slice no strict candidate fits: held keys
33
+ // released, printed fingers and spans beyond reach allowed. Far above any
34
+ // playable cost, so it is the last resort.
35
+ const LENIENT_PENALTY_ = 1000;
36
+ // The most states kept per slice. Penner & Hindle (2026) found a beam improves
37
+ // hand separation over exact search; this bounds the work on dense chords.
38
+ // On a 917-note pop arrangement, 64 gives the hands of 256 in a third of the
39
+ // time (1 s against 3).
40
+ const BEAM_WIDTH_ = 64;
41
+ const FINGERS_ = [1, 2, 3, 4, 5];
42
+ /**
43
+ * The hand and finger of each piano note: which keys each hand takes in every
44
+ * chord, and with which fingers, minimising an ergonomic cost — stretches
45
+ * between fingers, thumb passing, the thumb and little finger on black keys,
46
+ * hand shifts against the time available for them, and the hands crossing.
47
+ *
48
+ * Reach for it when a source does not say which hand plays what, such as a MIDI
49
+ * file or a single-staff part, or to suggest fingers for a score. Given notes
50
+ * with a `hand` (a score's staff) or a `fingering` (a printed finger), it keeps
51
+ * them and fills in the rest.
52
+ *
53
+ * A chord no two hands can play — more than ten keys, or spans beyond reach —
54
+ * still gets an answer: the inner keys of a chord over ten share their nearest
55
+ * neighbour's finger, and a slice nothing fits is placed as close to playable
56
+ * as the costs allow, releasing held keys and given fingers if it must.
57
+ *
58
+ * The result is a judgement, not a rule: two pianists agree on about seven
59
+ * fingers in ten.
60
+ *
61
+ * Time complexity: O(S · B · C) for `S` chords, at most 64 states `B` kept per
62
+ * chord and `C` candidate fingerings per chord (a few to a few hundred, growing
63
+ * with the chord's size): about a second for a thousand notes. Memory: O(S ·
64
+ * B).
65
+ *
66
+ * @example
67
+ * estimatePianoFingering([
68
+ * { pitch: 60, startSeconds: 0, endSeconds: 1 },
69
+ * { pitch: 64, startSeconds: 0, endSeconds: 1 },
70
+ * { pitch: 67, startSeconds: 0, endSeconds: 1 },
71
+ * ]);
72
+ * // [{ hand: "right", finger: 1 }, { hand: "right", finger: 3 }, { hand: "right", finger: 5 }]
73
+ *
74
+ * @param notes - The notes of everything one pianist plays, in any order.
75
+ * @param options - See {@link PianoFingeringOptions}.
76
+ * @returns One fingering per note, in the order of `notes`.
77
+ */
78
+ export function estimatePianoFingering(notes, options) {
79
+ const handSize = options?.handSize ?? "medium";
80
+ const slices = composeSlices_(notes);
81
+ const path = viterbi({ left: [], right: [] }, {
82
+ nextStatesOf: (state, layer) => successorsOf_(state, slices[layer], layer, handSize),
83
+ keyOf: stateKey_,
84
+ }, slices.length, { beamWidth: BEAM_WIDTH_ });
85
+ // The "free" mode always yields a candidate, so every layer has a state.
86
+ const states = path.states;
87
+ const result = Array.from({ length: notes.length });
88
+ for (const [index, slice] of slices.entries()) {
89
+ const state = states[index];
90
+ const placed = [];
91
+ for (const hand of ["left", "right"]) {
92
+ for (const key of state[hand]) {
93
+ if (key.slice === index) {
94
+ placed[key.key] = { hand, finger: key.finger };
95
+ }
96
+ }
97
+ }
98
+ for (const [keyIndex, key] of slice.keys.entries()) {
99
+ for (const note of key.notes) {
100
+ result[note] = { ...placed[keyIndex] };
101
+ }
102
+ }
103
+ for (const { key, nearest } of slice.extras) {
104
+ for (const note of key.notes) {
105
+ result[note] = { ...placed[nearest] };
106
+ }
107
+ }
108
+ }
109
+ return result;
110
+ }
111
+ function stateKey_(state) {
112
+ const handKey = (keys) => keys.map(({ slice, key, finger }) => `${slice}.${key}.${finger}`).join();
113
+ return `${handKey(state.left)}|${handKey(state.right)}`;
114
+ }
115
+ // The candidates of a slice, relaxing its constraints only when none fits.
116
+ function successorsOf_(state, slice, sliceIndex, handSize) {
117
+ for (const mode of ["strict", "lenient", "free"]) {
118
+ const candidates = candidatesOf_(state, slice, sliceIndex, handSize, mode);
119
+ if (candidates.length > 0) {
120
+ return candidates;
121
+ }
122
+ }
123
+ return [];
124
+ }
125
+ function candidatesOf_(state, slice, sliceIndex, handSize, mode) {
126
+ const penalty = mode === "strict" ? 0 : LENIENT_PENALTY_;
127
+ const candidates = [];
128
+ for (let split = 0; split <= slice.keys.length; split++) {
129
+ const leftKeys = slice.keys.slice(0, split);
130
+ const rightKeys = slice.keys.slice(split);
131
+ if (mode !== "free" &&
132
+ (leftKeys.some((key) => key.hand === "right") ||
133
+ rightKeys.some((key) => key.hand === "left"))) {
134
+ continue;
135
+ }
136
+ const context = { slice, sliceIndex, handSize, mode };
137
+ const leftOptions = handOptionsOf_("left", state.left, 0, leftKeys, context);
138
+ if (leftOptions.length === 0) {
139
+ continue;
140
+ }
141
+ const rightOptions = handOptionsOf_("right", state.right, split, rightKeys, context);
142
+ for (const left of leftOptions) {
143
+ for (const right of rightOptions) {
144
+ candidates.push({
145
+ state: { left: left.keys, right: right.keys },
146
+ cost: left.cost +
147
+ right.cost +
148
+ crossingCost_(left.keys, right.keys, slice.onsetSeconds) +
149
+ switchCost_(state, leftKeys, rightKeys, slice, sliceIndex) +
150
+ penalty,
151
+ });
152
+ }
153
+ }
154
+ }
155
+ return candidates;
156
+ }
157
+ // HAND_SWITCH_ when this slice continues, in the other hand, a line the
158
+ // previous slice played in one hand.
159
+ function switchCost_(state, leftKeys, rightKeys, slice, sliceIndex) {
160
+ if ((leftKeys.length === 0) === (rightKeys.length === 0)) {
161
+ return 0;
162
+ }
163
+ const [playing, other] = leftKeys.length > 0 ? [leftKeys, state.right] : [rightKeys, state.left];
164
+ const ownPrevious = (leftKeys.length > 0 ? state.left : state.right).some((key) => key.slice === sliceIndex - 1);
165
+ const previous = other.filter((key) => key.slice === sliceIndex - 1);
166
+ if (ownPrevious || previous.length === 0) {
167
+ return 0;
168
+ }
169
+ const present = previous.some((key) => key.endSeconds >= slice.onsetSeconds - HAND_PRESENCE_SECONDS_);
170
+ const near = playing.some((key) => previous.some((before) => Math.abs(before.position - key.position) <= HAND_SWITCH_REACH_));
171
+ return present && near ? HAND_SWITCH_ : 0;
172
+ }
173
+ // The keys a hand occupies at `onsetSeconds`: those down, or played recently.
174
+ function occupiedKeys_(keys, onsetSeconds) {
175
+ return keys.filter((key) => key.endSeconds >= onsetSeconds - HAND_PRESENCE_SECONDS_);
176
+ }
177
+ function crossingCost_(left, right, onsetSeconds) {
178
+ const leftKeys = occupiedKeys_(left, onsetSeconds);
179
+ const rightKeys = occupiedKeys_(right, onsetSeconds);
180
+ if (leftKeys.length === 0 || rightKeys.length === 0) {
181
+ return 0;
182
+ }
183
+ const highestLeft = Math.max(...leftKeys.map((key) => key.position));
184
+ const lowestRight = Math.min(...rightKeys.map((key) => key.position));
185
+ return highestLeft >= lowestRight ? HANDS_CROSSING_ : 0;
186
+ }
187
+ // Whether fingers follow positions: rising in the right hand, falling in the left.
188
+ function isOrdered_(hand, keys) {
189
+ for (let index = 1; index < keys.length; index++) {
190
+ const rising = keys[index].finger > keys[index - 1].finger;
191
+ if (rising !== (hand === "right")) {
192
+ return false;
193
+ }
194
+ }
195
+ return true;
196
+ }
197
+ // Every way one hand can take `added`, the slice's keys from `firstKey` on.
198
+ function handOptionsOf_(hand, previous, firstKey, added, context) {
199
+ if (added.length === 0) {
200
+ return [{ keys: previous, cost: 0 }];
201
+ }
202
+ const { slice, mode } = context;
203
+ const onsetSeconds = slice.onsetSeconds;
204
+ const addedPositions = new Set(added.map((key) => key.position));
205
+ const held = mode === "strict"
206
+ ? previous.filter((key) => key.endSeconds > onsetSeconds + LEGATO_SECONDS_ &&
207
+ !addedPositions.has(key.position))
208
+ : [];
209
+ const options = [];
210
+ for (const kept of held.length > 0 ? [held, []] : [held]) {
211
+ let earlyRelease = 0;
212
+ if (kept.length === 0) {
213
+ for (const key of held) {
214
+ earlyRelease +=
215
+ EARLY_RELEASE_PER_SECOND_ *
216
+ Math.min(key.endSeconds - onsetSeconds, EARLY_RELEASE_MAX_SECONDS_);
217
+ }
218
+ }
219
+ for (const option of fingeringsOf_(hand, previous, kept, kept.length === 0 ? held : [], firstKey, added, context)) {
220
+ options.push({ keys: option.keys, cost: option.cost + earlyRelease });
221
+ }
222
+ }
223
+ return options;
224
+ }
225
+ // Every fingering of `added` with the `held` keys staying down; `lifted` are
226
+ // keys held until now and lifted early for it.
227
+ function fingeringsOf_(hand, previous, held, lifted, firstKey, added, context) {
228
+ const { slice, sliceIndex, handSize, mode } = context;
229
+ const onsetSeconds = slice.onsetSeconds;
230
+ if (held.length + added.length > FINGERS_.length) {
231
+ return [];
232
+ }
233
+ const released = previous.filter((key) => !held.includes(key));
234
+ const freeFingers = FINGERS_.filter((finger) => !held.some((key) => key.finger === finger));
235
+ const options = [];
236
+ for (const chosen of combinations(freeFingers, added.length)) {
237
+ // Ascending fingers on ascending keys in the right hand, descending in the left.
238
+ const fingers = hand === "right" ? chosen : chosen.toReversed();
239
+ if (mode === "strict" &&
240
+ added.some((key, index) => key.finger !== undefined && key.finger !== fingers[index])) {
241
+ continue;
242
+ }
243
+ const played = added.map((key, index) => ({
244
+ position: key.position,
245
+ black: key.black,
246
+ finger: fingers[index],
247
+ endSeconds: key.endSeconds,
248
+ startSeconds: onsetSeconds,
249
+ slice: sliceIndex,
250
+ key: firstKey + index,
251
+ }));
252
+ const down = [...held, ...played].toSorted((keyA, keyB) => keyA.position - keyB.position);
253
+ if (!isOrdered_(hand, down)) {
254
+ continue;
255
+ }
256
+ const chord = chordCost_(hand, handSize, down, mode !== "strict");
257
+ if (!Number.isFinite(chord)) {
258
+ continue;
259
+ }
260
+ let cost = chord;
261
+ for (const key of played) {
262
+ cost += keyCost_(key);
263
+ }
264
+ if (previous.length === 0) {
265
+ let distance = 0;
266
+ for (const key of played) {
267
+ distance += Math.abs(key.position - HOME_POSITIONS_[hand]);
268
+ }
269
+ cost += shiftCost_(distance / played.length, Number.POSITIVE_INFINITY);
270
+ }
271
+ if (released.length > 0) {
272
+ // Al Kasimi et al. (2007): the move between chords is the average of
273
+ // its pairwise moves, so a chord does not cost more for its size
274
+ // alone. A shift moves the whole hand, so it is priced once, by its
275
+ // largest pair, against the time since the hand's last key came up.
276
+ let moves = 0;
277
+ let shiftUnits = 0;
278
+ let releasedSeconds = Number.NEGATIVE_INFINITY;
279
+ for (const from of released) {
280
+ // A key lifted early can leave as soon as it has been struck.
281
+ releasedSeconds = Math.max(releasedSeconds, lifted.includes(from) ? from.startSeconds : from.endSeconds);
282
+ for (const to of played) {
283
+ const move = transition_(hand, handSize, from, to);
284
+ moves += move.cost;
285
+ shiftUnits = Math.max(shiftUnits, move.shiftUnits);
286
+ }
287
+ }
288
+ cost +=
289
+ moves / (released.length * played.length) +
290
+ shiftCost_(shiftUnits, onsetSeconds - releasedSeconds);
291
+ }
292
+ options.push({ keys: down, cost });
293
+ }
294
+ return options;
295
+ }
296
+ //# sourceMappingURL=estimate-piano-fingering.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"estimate-piano-fingering.js","sourceRoot":"","sources":["../../src/estimate/estimate-piano-fingering.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EACN,UAAU,EACV,QAAQ,EAER,UAAU,EACV,WAAW,GACX,MAAM,mBAAmB,CAAC;AAS3B,OAAO,EAAE,cAAc,EAA+B,MAAM,cAAc,CAAC;AAE3E,2EAA2E;AAC3E,4EAA4E;AAC5E,uBAAuB;AACvB,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,8EAA8E;AAC9E,6EAA6E;AAC7E,sEAAsE;AACtE,wDAAwD;AACxD,MAAM,yBAAyB,GAAG,CAAC,CAAC;AACpC,MAAM,0BAA0B,GAAG,CAAC,CAAC;AACrC,+EAA+E;AAC/E,sEAAsE;AACtE,iDAAiD;AACjD,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,+EAA+E;AAC/E,MAAM,sBAAsB,GAAG,CAAC,CAAC;AACjC,gFAAgF;AAChF,6EAA6E;AAC7E,+EAA+E;AAC/E,+EAA+E;AAC/E,oDAAoD;AACpD,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,kBAAkB,GAAG,WAAW,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;AAC7D,8EAA8E;AAC9E,6EAA6E;AAC7E,2DAA2D;AAC3D,MAAM,eAAe,GAAG,EAAE,IAAI,EAAE,WAAW,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC;AAC1E,0EAA0E;AAC1E,0EAA0E;AAC1E,2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AC;oBACzC,UAAU,CAAC,UAAU,EAAE,YAAY,GAAG,eAAe,CAAC,CAAC;QACzD,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACpC,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./estimate/estimate-piano-fingering.js";
2
+ export * from "./types.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./estimate/estimate-piano-fingering.js";
2
+ export * from "./types.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,cAAc,YAAY,CAAC"}
@@ -0,0 +1,47 @@
1
+ import type { PianoFinger, PianoHand, PianoHandSize } from "../types.js";
2
+ /**
3
+ * The six limits of a finger pair's reach, in `keyPosition` units, from the
4
+ * lower-numbered finger's key to the other's (see `reachOf_`). A negative reach
5
+ * is the thumb passing under, or a finger crossing over it.
6
+ *
7
+ * Practical limits bound what a hand can do at all; comfortable ones what it
8
+ * does without strain; relaxed ones what it does at rest.
9
+ *
10
+ * @internal
11
+ */
12
+ export type FingerSpan_ = {
13
+ minPractical: number;
14
+ minComfortable: number;
15
+ minRelaxed: number;
16
+ maxRelaxed: number;
17
+ maxComfortable: number;
18
+ maxPractical: number;
19
+ };
20
+ /**
21
+ * The span of two different fingers, `lower` below `upper` in number.
22
+ *
23
+ * @internal
24
+ */
25
+ export declare function fingerSpan_(handSize: PianoHandSize, lower: PianoFinger, upper: PianoFinger): FingerSpan_;
26
+ /**
27
+ * The reach between two keys played by two different fingers of one hand, as
28
+ * the span tables measure it: from the lower-numbered finger's key towards the
29
+ * little finger's side, which is up the keyboard for the right hand and down
30
+ * for the left. Both orders of the same two keys give the same answer, which is
31
+ * the table's "reversed pair negated and swapped" and the left hand's mirror in
32
+ * one step.
33
+ *
34
+ * @internal
35
+ */
36
+ export declare function reachOf_(hand: PianoHand, first: {
37
+ position: number;
38
+ finger: PianoFinger;
39
+ }, second: {
40
+ position: number;
41
+ finger: PianoFinger;
42
+ }): {
43
+ lower: PianoFinger;
44
+ upper: PianoFinger;
45
+ reach: number;
46
+ };
47
+ //# sourceMappingURL=_finger-spans.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_finger-spans.d.ts","sourceRoot":"","sources":["../../src/span/_finger-spans.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEzE;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;CACrB,CAAC;AA2EF;;;;GAIG;AACH,wBAAgB,WAAW,CAC1B,QAAQ,EAAE,aAAa,EACvB,KAAK,EAAE,WAAW,EAClB,KAAK,EAAE,WAAW,GAChB,WAAW,CAUb;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CACvB,IAAI,EAAE,SAAS,EACf,KAAK,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,EAChD,MAAM,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,GAC/C;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAQ3D"}
@@ -0,0 +1,97 @@
1
+ // Balliauw, Herremans, Palhazi Cuervo & Sörensen (2017), the small, medium and
2
+ // large hands, right hand, ascending pairs, in the 14-per-octave distance
3
+ // `keyPosition` measures. Transcribed from pydactyl (MIT,
4
+ // https://github.com/dvdrndlph/pydactyl, `BALLIAUW_*_FINGER_SPANS`), whose
5
+ // reversed and left-hand entries are exactly the mirror `reachOf_` applies;
6
+ // not yet checked against the paper, which is not openly available.
7
+ // Indexed by the lower finger, then by the upper finger above it.
8
+ const SPANS_ = {
9
+ small: [
10
+ [
11
+ [-7, -5, 1, 3, 8, 10],
12
+ [-6, -4, 3, 6, 10, 12],
13
+ [-4, -2, 5, 8, 11, 13],
14
+ [-2, 0, 7, 10, 12, 14],
15
+ ],
16
+ [
17
+ [1, 1, 1, 2, 4, 6],
18
+ [1, 1, 3, 4, 6, 8],
19
+ [2, 2, 5, 6, 8, 10],
20
+ ],
21
+ [
22
+ [1, 1, 1, 2, 2, 4],
23
+ [1, 1, 3, 4, 6, 8],
24
+ ],
25
+ [[1, 1, 1, 2, 4, 6]],
26
+ ],
27
+ medium: [
28
+ [
29
+ [-8, -6, 1, 5, 8, 10],
30
+ [-7, -5, 3, 9, 12, 14],
31
+ [-5, -3, 5, 11, 13, 15],
32
+ [-2, 0, 7, 12, 14, 16],
33
+ ],
34
+ [
35
+ [1, 1, 1, 2, 5, 7],
36
+ [1, 1, 3, 4, 6, 8],
37
+ [2, 2, 5, 6, 10, 12],
38
+ ],
39
+ [
40
+ [1, 1, 1, 2, 2, 4],
41
+ [1, 1, 3, 4, 6, 8],
42
+ ],
43
+ [[1, 1, 1, 2, 4, 6]],
44
+ ],
45
+ large: [
46
+ [
47
+ [-10, -8, 1, 6, 9, 11],
48
+ [-8, -6, 3, 9, 13, 15],
49
+ [-6, -4, 5, 11, 14, 16],
50
+ [-2, 0, 7, 12, 16, 18],
51
+ ],
52
+ [
53
+ [1, 1, 1, 2, 5, 7],
54
+ [1, 1, 3, 4, 6, 8],
55
+ [2, 2, 5, 6, 10, 12],
56
+ ],
57
+ [
58
+ [1, 1, 1, 2, 2, 4],
59
+ [1, 1, 3, 4, 6, 8],
60
+ ],
61
+ [[1, 1, 1, 2, 4, 6]],
62
+ ],
63
+ };
64
+ /**
65
+ * The span of two different fingers, `lower` below `upper` in number.
66
+ *
67
+ * @internal
68
+ */
69
+ export function fingerSpan_(handSize, lower, upper) {
70
+ const row = SPANS_[handSize][lower - 1][upper - lower - 1];
71
+ return {
72
+ minPractical: row[0],
73
+ minComfortable: row[1],
74
+ minRelaxed: row[2],
75
+ maxRelaxed: row[3],
76
+ maxComfortable: row[4],
77
+ maxPractical: row[5],
78
+ };
79
+ }
80
+ /**
81
+ * The reach between two keys played by two different fingers of one hand, as
82
+ * the span tables measure it: from the lower-numbered finger's key towards the
83
+ * little finger's side, which is up the keyboard for the right hand and down
84
+ * for the left. Both orders of the same two keys give the same answer, which is
85
+ * the table's "reversed pair negated and swapped" and the left hand's mirror in
86
+ * one step.
87
+ *
88
+ * @internal
89
+ */
90
+ export function reachOf_(hand, first, second) {
91
+ const [low, high] = first.finger < second.finger ? [first, second] : [second, first];
92
+ const reach = hand === "right"
93
+ ? high.position - low.position
94
+ : low.position - high.position;
95
+ return { lower: low.finger, upper: high.finger, reach };
96
+ }
97
+ //# sourceMappingURL=_finger-spans.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_finger-spans.js","sourceRoot":"","sources":["../../src/span/_finger-spans.ts"],"names":[],"mappings":"AA8BA,+EAA+E;AAC/E,0EAA0E;AAC1E,0DAA0D;AAC1D,2EAA2E;AAC3E,4EAA4E;AAC5E,oEAAoE;AACpE,kEAAkE;AAClE,MAAM,MAAM,GAAG;IACd,KAAK,EAAE;QACN;YACC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,C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AAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;AACzD,CAAC"}
@@ -0,0 +1,39 @@
1
+ /** A hand at the piano. */
2
+ export type PianoHand = "left" | "right";
3
+ /**
4
+ * A finger, numbered as in printed fingering: 1 the thumb to 5 the little
5
+ * finger.
6
+ */
7
+ export type PianoFinger = 1 | 2 | 3 | 4 | 5;
8
+ /**
9
+ * The span of the player's hands, as Balliauw et al. (2017) measure it: a
10
+ * `small` hand reaches an octave between thumb and little finger, a `medium`
11
+ * one a ninth, a `large` one a tenth.
12
+ */
13
+ export type PianoHandSize = "small" | "medium" | "large";
14
+ /**
15
+ * A note as a key press. A `@ac-kit/model-music` `SongNote` is one as it
16
+ * stands.
17
+ */
18
+ export type PianoFingeringNote = {
19
+ /** In semitones, middle C at 60; rounded to the nearest key. */
20
+ pitch: number;
21
+ /** When the key goes down. */
22
+ startSeconds: number;
23
+ /** When the key comes up — not when the sound stops under the pedal. */
24
+ endSeconds: number;
25
+ /** A hand the result must keep, such as the staff a score writes it on. */
26
+ hand?: PianoHand | undefined;
27
+ /** A finger the result must keep, such as a printed fingering, 1 to 5. */
28
+ fingering?: number | undefined;
29
+ };
30
+ /** Which hand and which finger play a note. */
31
+ export type PianoFingering = {
32
+ hand: PianoHand;
33
+ finger: PianoFinger;
34
+ };
35
+ export type PianoFingeringOptions = {
36
+ /** The player's hand span; `medium` by default. */
37
+ handSize?: PianoHandSize;
38
+ };
39
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,OAAO,CAAC;AAEzC;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAE5C;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEzD;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAChC,gEAAgE;IAChE,KAAK,EAAE,MAAM,CAAC;IACd,8BAA8B;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,wEAAwE;IACxE,UAAU,EAAE,MAAM,CAAC;IACnB,2EAA2E;IAC3E,IAAI,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAC7B,0EAA0E;IAC1E,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B,CAAC;AAEF,+CAA+C;AAC/C,MAAM,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,WAAW,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IACnC,mDAAmD;IACnD,QAAQ,CAAC,EAAE,aAAa,CAAC;CACzB,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@ac-kit/media-piano-fingering",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "The hand and finger of each piano note: an ergonomic cost of finger stretches, thumb passing, black keys and hand shifts, minimised chord by chord.",
5
+ "keywords": [
6
+ "fingering",
7
+ "hand separation",
8
+ "midi",
9
+ "music",
10
+ "piano"
11
+ ],
12
+ "homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/media-piano-fingering/",
13
+ "bugs": {
14
+ "url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
15
+ },
16
+ "license": "BSD-3-Clause",
17
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
21
+ "directory": "packages/media-piano-fingering"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./dist/index.js",
29
+ "exports": {
30
+ ".": "./dist/index.js"
31
+ },
32
+ "publishConfig": {
33
+ "main": "./dist/index.js",
34
+ "exports": {
35
+ ".": "./dist/index.js"
36
+ },
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
41
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
42
+ "clean": "rimraf dist",
43
+ "build": "yarn clean && yarn compile",
44
+ "compare:pig": "node --import tsx ./scripts/compare-pig.ts",
45
+ "test": "yarn run -T vitest run",
46
+ "prepack": "yarn build"
47
+ },
48
+ "dependencies": {
49
+ "@ac-kit/algo": "^0.3.0",
50
+ "@ac-kit/core": "^0.7.0",
51
+ "@ac-kit/model-music": "^0.6.0",
52
+ "@types/web": "^0.0.365"
53
+ },
54
+ "devDependencies": {
55
+ "@ac-kit/app-system": "0.3.0",
56
+ "@ac-kit/format-zip": "0.1.1",
57
+ "@ac-kit/math-random": "0.1.6",
58
+ "@ac-kit/tsconfig": "0.1.0",
59
+ "@ac-kit/typedoc-config": "0.1.0",
60
+ "@ac-kit/vitest-config": "0.1.1",
61
+ "@types/node": "^26.5.1",
62
+ "@typescript/native": "typescript@7.0.2",
63
+ "rimraf": "6.1.4",
64
+ "tsx": "4.23.15",
65
+ "typedoc": "0.28.20",
66
+ "typescript": "@typescript/typescript6@6.0.2"
67
+ },
68
+ "engines": {
69
+ "node": ">=24"
70
+ }
6
71
  }