Skip to content

Commit

Permalink
<SamplePlayer/> shouldn't generate and <WebMidi/> shouldn't send out-…
Browse files Browse the repository at this point in the history
…of-bounds note_on velocities
  • Loading branch information
broadwell committed Oct 15, 2021
1 parent bfa7b92 commit 959a3d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/components/SamplePlayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,20 @@
}
},
);
const modifiedVelocity =
const modifiedVelocity = Math.min(
baseVelocity *
(($softOnOff && SOFT_PEDAL_RATIO) || 1) *
(($accentOnOff && ACCENT_BUMP) || 1) *
$volumeCoefficient *
(noteNumber < HALF_BOUNDARY
? $bassVolumeCoefficient
: $trebleVolumeCoefficient);
(($softOnOff && SOFT_PEDAL_RATIO) || 1) *
(($accentOnOff && ACCENT_BUMP) || 1) *
$volumeCoefficient *
(noteNumber < HALF_BOUNDARY
? $bassVolumeCoefficient
: $trebleVolumeCoefficient),
1,
);
if (modifiedVelocity) {
piano.keyDown({
midi: noteNumber,
velocity: Math.min(modifiedVelocity, 1),
velocity: modifiedVelocity,
});
}
if (!fromMidi) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/WebMidi.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { notify } from "../ui-components/Notification.svelte";
import { onMount } from "svelte";
import { webMidiEnabled } from "../stores";
import { clamp } from "../lib/utils";
export let startNote;
export let stopNote;
Expand All @@ -20,7 +21,7 @@
const sendMidiMsg = (msgType, entity, value) => {
let msg = null;
if (msgType == "note_on") {
msg = [MIDI_NOTE_ON, entity, parseInt(value * 127, 10)];
msg = [MIDI_NOTE_ON, entity, parseInt(clamp(0, 1, value) * 127, 10)];
} else if (msgType == "note_off") {
msg = [MIDI_NOTE_OFF, entity, value];
} else if (msgType == "controller") {
Expand Down

0 comments on commit 959a3d0

Please sign in to comment.