Skip to content

Commit

Permalink
Merge pull request #260 from cuthbertLab/fix-beat-groups
Browse files Browse the repository at this point in the history
Fix beatGroups in 5/4, 6/4, 7/4, 9/4, 12/4
  • Loading branch information
mscuthbert authored Aug 21, 2024
2 parents 5c5211a + 6634618 commit 1c4420d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,7 @@ export class TimeSignature extends base.Music21Object {
computeBeatGroups(): number[][] {
const tempBeatGroups = [];
let numBeats = this.numerator;
let beatValue = this.denominator;
if (beatValue < 8 && numBeats >= 5) {
const beatsToEighthNoteRatio = 8 / beatValue;
// hopefully beatValue is an int -- right Brian Ferneyhough?
beatValue = 8;
numBeats *= beatsToEighthNoteRatio;
}

const beatValue = this.denominator;
if (beatValue >= 8) {
if ([4, 2].includes(numBeats)) { // 4/8 and 2/8 should have eighth beats
tempBeatGroups.push([1, beatValue]);
Expand Down
37 changes: 37 additions & 0 deletions tests/moduleTests/meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,41 @@ export default function tests() {
);
assert.equal(m.beatDuration.dots, 1, 'beatDuration has dot');
});

test('music21.meter.TimeSignature.beatGroups', assert => {
function assertBeatGroups(meterString: string, expectedBeatGroups: number[][]) {
const m = new music21.meter.TimeSignature(meterString);
assert.deepEqual(
m.beatGroups,
expectedBeatGroups,
`${meterString} should have beat groups: ${expectedBeatGroups}`,
);
}
assertBeatGroups('2/2', [[1, 2]]);
assertBeatGroups('3/2', [[1, 2]]);
assertBeatGroups('4/2', [[1, 2]]);
assertBeatGroups('2/4', [[2, 8]]);
assertBeatGroups('3/4', [[2, 8]]);
assertBeatGroups('4/4', [[2, 8]]);
assertBeatGroups('5/4', [[2, 8]]);
assertBeatGroups('6/4', [[2, 8]]);
assertBeatGroups('7/4', [[2, 8]]);
assertBeatGroups('9/4', [[2, 8]]);
assertBeatGroups('12/4', [[2, 8]]);
assertBeatGroups('2/8', [[1, 8]]);
assertBeatGroups('3/8', [[3, 8]]);
assertBeatGroups('4/8', [[1, 8]]);
assertBeatGroups('5/8', [[3, 8], [2, 8]]);
assertBeatGroups('6/8', [[3, 8], [3, 8]]);
assertBeatGroups('7/8', [[3, 8], [2, 8], [2, 8]]);
assertBeatGroups('9/8', [[3, 8], [3, 8], [3, 8]]);
assertBeatGroups('12/8', [[3, 8], [3, 8], [3, 8], [3, 8]]);
assertBeatGroups('3/16', [[3, 16]]);
assertBeatGroups('4/16', [[1, 16]]);
assertBeatGroups('5/16', [[3, 16], [2, 16]]);
assertBeatGroups('6/16', [[3, 16], [3, 16]]);
assertBeatGroups('7/16', [[3, 16], [2, 16], [2, 16]]);
assertBeatGroups('9/16', [[3, 16], [3, 16], [3, 16]]);
assertBeatGroups('12/16', [[3, 16], [3, 16], [3, 16], [3, 16]]);
});
}

0 comments on commit 1c4420d

Please sign in to comment.