Merge pull request 'mode-above-fix' (#2062) from eefano/strudel:mode-above-fix into main

Reviewed-on: https://codeberg.org/uzu/strudel/pulls/2062
This commit is contained in:
froos
2026-08-13 22:40:41 +02:00
4 changed files with 30 additions and 6 deletions
+9 -2
View File
@@ -2352,11 +2352,18 @@ export const { offset } = registerControl('offset');
**/
export const { octaves } = registerControl('octaves');
/**
* Remove anchor note from the voicing. Useful for melody harmonization
* How the voicing is aligned to the anchor
* - `below`: top note <= anchor
* - `duck`: top note <= anchor, anchor excluded
* - `above`: bottom note >= anchor
* - `root`: bottom note is the lowest root of the chord >= anchor
*
* - `oldabove` : old (buggy) behavior of above, kept for legacy reason
* - `oldroot` : old (buggy) behavior of root, kept for legacy reason
*
* @name mode
* @tags tonal
* @param {string | Pattern} modeName one of {below | above | duck | root}
* @param {string | Pattern} modeName one of {below | above | duck | root | oldabove | oldroot}
* @example
* mode("<below above duck root>").chord("C").voicing()
*
+14 -3
View File
@@ -134,6 +134,16 @@ let modeTarget = {
duck: (v) => v.slice(-1)[0],
above: (v) => v[0],
root: (v) => v[0],
oldabove: (v) => v[0],
oldroot: (v) => v[0],
};
let modeMult = {
below: 1,
duck: 1,
above: -1,
root: -1,
oldabove: 1,
oldroot: 1,
};
export function renderVoicing({ chord, dictionary, offset = 0, n, mode = 'below', anchor = 'c5', octaves = 1 }) {
@@ -144,19 +154,20 @@ export function renderVoicing({ chord, dictionary, offset = 0, n, mode = 'below'
const voicings = dictionary[symbol].map((voicing) =>
(typeof voicing === 'string' ? voicing.split(' ') : voicing).map(step2semitones),
);
const mult = modeMult[mode];
let minDistance, bestIndex;
// calculate distances up from voicing top notes
let chromaDiffs = voicings.map((v, i) => {
const targetStep = modeTarget[mode](v);
const diff = _mod(anchorChroma - targetStep - rootChroma, 12);
const diff = _mod((anchorChroma - targetStep - rootChroma) * mult, 12);
if (minDistance === undefined || diff < minDistance) {
minDistance = diff;
bestIndex = i;
}
return diff;
return diff * mult;
});
if (mode === 'root') {
if (mode === 'root' || mode === 'oldroot') {
bestIndex = 0;
}
+3
View File
@@ -185,6 +185,9 @@ export const rootNotes = register('rootNotes', function (octave, pat) {
* - `below`: top note <= anchor
* - `duck`: top note <= anchor, anchor excluded
* - `above`: bottom note >= anchor
* - `root`: bottom note is the lowest root of the chord >= anchor
* - `oldabove` : old (buggy) behavior of above, kept for legacy reason
* - `oldroot` : old (buggy) behavior of root, kept for legacy reason
* - `offset`: whole number that shifts the voicing up or down to the next voicing
* - `n`: if set, the voicing is played like a scale. Overshooting numbers will be octaved
*
+4 -1
View File
@@ -287,7 +287,10 @@ The modes are:
- `below`: the top note of the voicing is lower than or equal to the anchor (default)
- `above`: the bottom note of the voicing is higher than or equal to the anchor
- `duck`: the top note of the voicing is lower than the anchor
- `root`: the bottom note of the voicing is always the root note closest to the anchor
- `root`: the bottom note of the voicing is always the lowest root note higher than or equal to the anchor
- `oldabove` : old (buggy) behavior of above, kept for legacy reasons
- `oldroot` : old (buggy) behavior of root, kept for legacy reasons
The `anchor` can also be set from within the `mode` function: