Skip to content

Commit b4ebe69

Browse files
committed
Fix bar line calculation to show measures instead of beats
1 parent e0856d2 commit b4ebe69

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

js/widgets/phrasemaker.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class PhraseMaker {
178178
* @type {number}
179179
* @private
180180
*/
181+
this._globalColumnIndex = 0;
181182
this._notesCounter = 0;
182183

183184
/**
@@ -399,6 +400,7 @@ class PhraseMaker {
399400
* @private
400401
*/
401402
_isBarLine(columnIndex) {
403+
402404
if (columnIndex === 0) {
403405
return true; // First measure always starts with a bar line
404406
}
@@ -408,29 +410,24 @@ class PhraseMaker {
408410
// Calculate the cumulative note value up to this position
409411
let cumulativeValue = 0;
410412
for (let i = 0; i < columnIndex; i++) {
411-
if (this._noteValueRow && this._noteValueRow.cells[i]) {
412-
const noteValue = parseFloat(this._noteValueRow.cells[i].getAttribute("alt")) || 0;
413-
cumulativeValue += noteValue;
414-
} else {
415-
// Fallback for tuplet cases
416-
const noteValue = 1 / (this._notesToPlay[i] && this._notesToPlay[i][1] ? this._notesToPlay[i][1] : 4);
417-
cumulativeValue += noteValue;
418-
}
413+
cumulativeValue += 0.25;
419414
}
420415

421416
// Calculate measure duration in whole note units
422417
// meterBeats * (4 / meterNoteValue) gives us the measure duration
423-
const measureDuration = this._meterBeats * (4 / this._meterNoteValue);
418+
const measureDuration = this._meterBeats * (4 / this._meterNoteValue)/4;
424419

425420
// Convert cumulative value to quarter note units for easier calculation
426-
const positionInQuarters = cumulativeValue * 4;
427-
const measureInQuarters = measureDuration;
421+
// const positionInQuarters = cumulativeValue * 4;
422+
// const measureInQuarters = measureDuration;
423+
// // Check if we're at a measure boundary (with tolerance for floating point errors)
424+
// const remainder = positionInQuarters % measureInQuarters;
425+
// return Math.abs(remainder) < 0.001 || Math.abs(remainder - measureInQuarters) < 0.001;
426+
const measurePosition = cumulativeValue / measureDuration;
427+
const isWholeMeasure = Math.abs(measurePosition - Math.round(measurePosition)) < 0.001;
428428

429-
// Check if we're at a measure boundary (with tolerance for floating point errors)
430-
const remainder = positionInQuarters % measureInQuarters;
431-
432-
433-
return Math.abs(remainder) < 0.001 || Math.abs(remainder - measureInQuarters) < 0.001;
429+
430+
return isWholeMeasure && columnIndex > 0;
434431
}
435432

436433
/**
@@ -555,6 +552,7 @@ class PhraseMaker {
555552
init(activity) {
556553
// Initializes the matrix. First removes the previous matrix
557554
// and then make another one in DOM (document object model)
555+
this._globalColumnIndex = 0;
558556
let tempTable;
559557
this.activity = activity;
560558

@@ -3138,6 +3136,7 @@ class PhraseMaker {
31383136
* @param {number} noteValue - The value of the notes (e.g., 4 for quarter notes, 8 for eighth notes).
31393137
*/
31403138
addNotes(numBeats, noteValue) {
3139+
31413140
let noteValueToDisplay = calcNoteValueToDisplay(noteValue, 1);
31423141

31433142
if (noteValue > 12) {
@@ -3210,11 +3209,13 @@ class PhraseMaker {
32103209
cell.style.color = platformColor.textColor;
32113210
cell.setAttribute("alt", noteValue);
32123211

3213-
if (this._isBarLine(j)) {
3212+
if (this._isBarLine(this._globalColumnIndex)) {
32143213
cell.style.borderLeft = "3px solid #333333";
32153214
cell.style.paddingLeft = "2px";
32163215
}
32173216

3217+
this._globalColumnIndex++;
3218+
32183219
if (this._matrixHasTuplets) {
32193220
// We may need to insert some blank cells in the extra rows
32203221
// added by tuplets.

0 commit comments

Comments
 (0)