Skip to content

Commit

Permalink
add key modifier QoL
Browse files Browse the repository at this point in the history
  • Loading branch information
spessasus committed Nov 25, 2024
1 parent cf7ff93 commit fc280b5
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 77 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SpessaSynth",
"version": "3.23.2",
"version": "3.23.3",
"type": "module",
"scripts": {
"start": "node src/website/server/server.js",
Expand Down
31 changes: 30 additions & 1 deletion src/spessasynth_lib/synthetizer/key_modifier_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export class KeyModifierManager
constructor(synth)
{
this.synth = synth;
/**
* The velocity override mappings for MIDI keys
* @type {KeyModifier[][]}
* @private
*/
this._keyModifiers = [];
}

/**
Expand Down Expand Up @@ -44,12 +50,29 @@ export class KeyModifierManager
const velocity = options?.velocity ?? -1;
const program = options?.patch?.program ?? -1;
const bank = options?.patch?.bank ?? -1;
const mod = new KeyModifier(velocity, bank, program);
if (this._keyModifiers[channel] === undefined)
{
this._keyModifiers[channel] = [];
}
this._keyModifiers[channel][midiNote] = mod;
this._sendToWorklet(
workletKeyModifierMessageType.addMapping,
[channel, midiNote, new KeyModifier(velocity, bank, program)]
[channel, midiNote, mod]
);
}

/**
* Gets a key modifier
* @param channel {number} the channel affected. Usually 0-15
* @param midiNote {number} the MIDI note to change. 0-127
* @returns {KeyModifier|undefined}
*/
getModifier(channel, midiNote)
{
return this._keyModifiers?.[channel]?.[midiNote];
}

/**
* Deletes a key modifier
* @param channel {number} the channel affected. Usually 0-15
Expand All @@ -61,6 +84,11 @@ export class KeyModifierManager
workletKeyModifierMessageType.deleteMapping,
[channel, midiNote]
);
if (this._keyModifiers[channel]?.[midiNote] === undefined)
{
return;
}
this._keyModifiers[channel][midiNote] = undefined;
}

/**
Expand All @@ -69,5 +97,6 @@ export class KeyModifierManager
clearModifiers()
{
this._sendToWorklet(workletKeyModifierMessageType.clearMappings, undefined);
this._keyModifiers = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export class KeyModifier
* @enum {number}
*/
export const workletKeyModifierMessageType = {
addMapping: 0, // [channel<number, midiNote<number>, mapping<KeyModifier>]
deleteMapping: 1, // [channel<number, midiNote<number>]
clearMappings: 2 // <no data>
addMapping: 0, // [channel<number>, midiNote<number>, mapping<KeyModifier>]
deleteMapping: 1, // [channel<number>, midiNote<number>]
clearMappings: 2 // <no data>
};

export class WorkletKeyModifierManager
Expand Down
14 changes: 11 additions & 3 deletions src/website/js/synthesizer_ui/methods/key_modifier_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ async function doModifyKey(synth, locale, keyboard, presetList)
{
presetOptions[p.presetName] = p.presetName;
}
showNotification(
const mod = synth.keyModifierManager.getModifier(keyboard.channel, key);
const vel = mod?.velocity ?? -1;
const n = showNotification(
locale.getLocaleString(LOCALE_PATH + "modifyKey.title"),
[
{
Expand All @@ -96,7 +98,7 @@ async function doModifyKey(synth, locale, keyboard, presetList)
{
type: "input",
translatePathTitle: LOCALE_PATH + "modifyKey.velocity",
attributes: getInput("vel", 0, 127, -1)
attributes: getInput("vel", 0, 127, vel)
},
{
type: "select",
Expand Down Expand Up @@ -135,6 +137,12 @@ async function doModifyKey(synth, locale, keyboard, presetList)
true,
locale
);
const prog = mod?.patch?.program ?? -1;
const bank = mod?.patch?.bank ?? -1;
if (bank !== -1 && prog !== -1)
{
n.div.querySelector("select[preset-selector]").value = presetList.find(p => p.bank === bank && p.program === prog).presetName;
}
}

/**
Expand All @@ -159,7 +167,7 @@ async function doRemoveModification(synth, locale, keyboard)
onClick: async n =>
{
closeNotification(n.id);
await doModifyKey(synth, locale, keyboard);
await doRemoveModification(synth, locale, keyboard);
}
},
{
Expand Down
70 changes: 35 additions & 35 deletions src/website/minified/demo_main.min.js

Large diffs are not rendered by default.

68 changes: 34 additions & 34 deletions src/website/minified/local_main.min.js

Large diffs are not rendered by default.

0 comments on commit fc280b5

Please sign in to comment.