Skip to content

Commit 46ccbb5

Browse files
authored
music keyboard fix (#4296)
* follow BPM * update * Note Value * final fix * removed console log * BPM block added
1 parent 019ae57 commit 46ccbb5

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

js/widgets/musickeyboard.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,14 @@ function MusicKeyboard(activity) {
872872
}
873873

874874
this._stopOrCloseClicked = false;
875-
this._playChord(notes, selectedNotes[0].duration, selectedNotes[0].voice);
876-
const maxWidth = Math.max.apply(Math, selectedNotes[0].duration);
877-
this.playOne(1, maxWidth, playButtonCell);
875+
876+
// Convert durations to seconds based on BPM
877+
const durationInSeconds = selectedNotes[0].duration.map(
878+
(beatDuration) => (beatDuration * 60 * 4) / (this.bpm)
879+
);
880+
this._playChord(notes, durationInSeconds, selectedNotes[0].voice);
881+
const maxDuration = Math.max(...durationInSeconds);
882+
this.playOne(1, maxDuration, playButtonCell);
878883
} else {
879884
if (!this.keyboardShown) {
880885
this._createTable();
@@ -898,14 +903,14 @@ function MusicKeyboard(activity) {
898903

899904
/**
900905
* Plays a sequence of musical notes recursively with a specified time delay.
901-
*
906+
*
902907
* @param {number} counter - The index of the current note in the sequence.
903908
* @param {number} time - The time duration of the current note.
904909
* @param {HTMLElement} playButtonCell - The HTML element representing the play button.
905910
*/
906911
this.playOne = function (counter, time, playButtonCell) {
907912
setTimeout(() => {
908-
let cell, eleid, ele, notes, zx, res, maxWidth;
913+
let cell, eleid, ele, notes, zx, res, maxDuration;
909914
if (counter < selectedNotes.length) {
910915
if (this._stopOrCloseClicked) {
911916
return;
@@ -952,15 +957,24 @@ function MusicKeyboard(activity) {
952957
}
953958

954959
if (this.playingNow) {
960+
const durationInSeconds = selectedNotes[counter].duration.map(
961+
(beatDuration) => (beatDuration * 60 * 4) / this.bpm
962+
);
963+
955964
this._playChord(
956965
notes,
957-
selectedNotes[counter].duration,
966+
durationInSeconds,
958967
selectedNotes[counter].voice
959968
);
960969
}
961970

962-
maxWidth = Math.max.apply(Math, selectedNotes[counter].duration);
963-
this.playOne(counter + 1, maxWidth, playButtonCell);
971+
maxDuration = Math.max(
972+
...selectedNotes[counter].duration.map(
973+
(beatDuration) => (beatDuration * 60 * 4) / this.bpm
974+
)
975+
);
976+
977+
this.playOne(counter + 1, maxDuration, playButtonCell);
964978
} else {
965979
playButtonCell.innerHTML =
966980
`&nbsp;&nbsp;<img
@@ -979,12 +993,12 @@ function MusicKeyboard(activity) {
979993
this._createKeyboard();
980994
}
981995
}
982-
}, time * 1000 + 125);
996+
}, time * 1000);
983997
};
984998

985999
/**
9861000
* Plays a chord (multiple notes simultaneously) using a synthesizer.
987-
*
1001+
*
9881002
* @param {string[]} notes - Array of note names to be played as a chord.
9891003
* @param {number[]} noteValue - Array of note values (durations) for each note in the chord.
9901004
* @param {string[]} instruments - Array of instrument names or identifiers for each note.
@@ -1040,7 +1054,7 @@ function MusicKeyboard(activity) {
10401054

10411055
/**
10421056
* Fills chromatic gaps in a given list of notes by padding with missing notes.
1043-
*
1057+
*
10441058
* @param {Object[]} noteList - List of notes represented as dictionaries containing `noteName` and `noteOctave`.
10451059
* @returns {Object[]} A new list of notes with chromatic gaps filled.
10461060
*/
@@ -1557,7 +1571,7 @@ function MusicKeyboard(activity) {
15571571
cell.style.minWidth = Math.floor(MATRIXSOLFEWIDTH * this._cellScale) * 1.5 + "px";
15581572
cell.style.maxWidth = cell.style.minWidth;
15591573
cell.className = "headcol"; // This cell is fixed horizontally.
1560-
cell.innerHTML = _("duration (MS)");
1574+
cell.innerHTML = _("Note Value");
15611575
cell.style.position = "sticky";
15621576
cell.style.left = "0px";
15631577
cell.style.zIndex = "1";
@@ -2993,7 +3007,17 @@ function MusicKeyboard(activity) {
29933007
[2, "hidden", 0, 0, [0, selectedNotes.length == 0 ? null : 3]]
29943008
];
29953009

2996-
let prevId = 2;
3010+
// Add BPM
3011+
newStack.push(
3012+
[3, "setmasterbpm2", 0, 0, [2, 4, 5, 8]],
3013+
[4, ["number", { value: this.bpm }], 0, 0, [3]],
3014+
[5, "divide", 0, 0, [3, 6, 7]],
3015+
[6, ["number", { value: 1 }], 0, 0, [5]],
3016+
[7, ["number", { value: 4 }], 0, 0, [5]],
3017+
[8, "vspace", 0, 0, [3, 9]]
3018+
);
3019+
3020+
let prevId = 8;
29973021
let endOfStackIdx, id;
29983022

29993023
for (let noteGrp = 0; noteGrp < newNotes.length; noteGrp++) {

0 commit comments

Comments
 (0)