Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/midi/MidiTickLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,15 @@ export class MidiTickLookup {
if (currentMasterBar) {
// pre-beat grace notes at the start of the bar we also add the beat to the previous bar
if (start < 0 && currentMasterBar.previousMasterBar) {
const previousStart = currentMasterBar.previousMasterBar!.end + start;
const relativeMasterBarEnd = currentMasterBar.previousMasterBar!.end - currentMasterBar.previousMasterBar!.start;
const previousStart = relativeMasterBarEnd + start;
const previousEnd = previousStart + duration;

// add to previous bar
currentMasterBar.previousMasterBar!.addBeat(beat, previousStart, previousStart, currentMasterBar.previousMasterBar!.end - previousStart);
currentMasterBar.previousMasterBar!.addBeat(beat, previousStart, previousStart, duration);

// overlap to current bar?
if(previousEnd > currentMasterBar.previousMasterBar!.end) {
if(previousEnd > relativeMasterBarEnd) {
// the start is negative and representing the overlap to the previous bar.
const overlapDuration = duration + start;
currentMasterBar.addBeat(beat, start, 0, overlapDuration);
Expand Down
35 changes: 33 additions & 2 deletions test/audio/MidiTickLookup.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AlphaTexImporter } from '@src/importer/AlphaTexImporter';
import { ScoreLoader } from '@src/importer/ScoreLoader';
import { ByteBuffer } from '@src/io/ByteBuffer';
import { Logger } from '@src/Logger';
Expand Down Expand Up @@ -574,8 +575,6 @@ describe('MidiTickLookupTest', () => {
})




it('cursor-snapping', async () => {
const buffer = await TestPlatform.loadFile('test-data/audio/cursor-snapping.gp');
const settings = new Settings();
Expand Down Expand Up @@ -616,6 +615,38 @@ describe('MidiTickLookupTest', () => {
expect(secondBeat!.beatLookup.duration).to.equal(960);
});


it('before-beat-grace-later-bars', () => {
const settings = new Settings();
const importer = new AlphaTexImporter();
importer.initFromString(`\\ts 2 4 1.1.2 | 2.1.4 3.1 | 4.1{gr} 5.1{gr} 6.1.2 | 7.1.4 8.1`, settings);
const score = importer.readScore();
const lookup = buildLookup(score, settings);

// bar 2 contains the grace notes which stole duration from fret 3 beat.
const bar2 = lookup.masterBars[1];

let current = bar2.firstBeat;
expect(current!.highlightedBeats.map(b => b.beat.notes[0].fret).join(',')).to.equal("2");
expect(current!.start).to.equal(0);
expect(current!.duration).to.equal(960);

current = current!.nextBeat;
expect(current!.highlightedBeats.map(b => b.beat.notes[0].fret).join(',')).to.equal("3");
expect(current!.start).to.equal(960);
expect(current!.duration).to.equal(840); // 120 ticks stolen by grace beats

current = current!.nextBeat;
expect(current!.highlightedBeats.map(b => b.beat.notes[0].fret).join(',')).to.equal("4");
expect(current!.start).to.equal(960 + 840);
expect(current!.duration).to.equal(60);

current = current!.nextBeat;
expect(current!.highlightedBeats.map(b => b.beat.notes[0].fret).join(',')).to.equal("5");
expect(current!.start).to.equal(960 + 840 + 60);
expect(current!.duration).to.equal(60);
});


function lookupTest(
tex: string,
Expand Down