From b3dec8b569eafcad6225dbcfdb20166b214b2807 Mon Sep 17 00:00:00 2001 From: Danielku15 Date: Sat, 26 Nov 2022 13:59:15 +0100 Subject: [PATCH] Ensure we respect repeats on getting beat playback position --- src/AlphaTabApiBase.ts | 14 ++++++-------- src/midi/MidiTickLookup.ts | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/AlphaTabApiBase.ts b/src/AlphaTabApiBase.ts index 31eca2b22..3067082c1 100644 --- a/src/AlphaTabApiBase.ts +++ b/src/AlphaTabApiBase.ts @@ -805,9 +805,7 @@ export class AlphaTabApiBase { let currentBeat = this._currentBeat; let tickCache = this._tickCache; if (currentBeat && tickCache) { - this.player!.tickPosition = - tickCache.getMasterBarStart(currentBeat.currentBeat.voice.bar.masterBar) + - currentBeat.currentBeat.playbackStart; + this.player!.tickPosition = tickCache.getBeatStart(currentBeat.currentBeat); } } }); @@ -1145,8 +1143,8 @@ export class AlphaTabApiBase { if (this.settings.player.enableUserInteraction) { // for the selection ensure start < end if (this._selectionEnd) { - let startTick: number = this._selectionStart!.beat.absolutePlaybackStart; - let endTick: number = this._selectionEnd!.beat.absolutePlaybackStart; + let startTick: number = this._tickCache?.getBeatStart(this._selectionStart!.beat) ?? this._selectionStart!.beat.absolutePlaybackStart; + let endTick: number = this._tickCache?.getBeatStart(this._selectionEnd!.beat) ?? this._selectionEnd!.beat.absolutePlaybackStart; if (endTick < startTick) { let t: SelectionInfo = this._selectionStart!; this._selectionStart = this._selectionEnd; @@ -1162,7 +1160,7 @@ export class AlphaTabApiBase { // move to selection start this._currentBeat = null; // reset current beat so it is updating the cursor if (this._playerState === PlayerState.Paused) { - this.cursorUpdateTick(this._selectionStart.beat.absolutePlaybackStart, false); + this.cursorUpdateTick(this._tickCache.getBeatStart(this._selectionStart.beat), false); } this.tickPosition = realMasterBarStart + this._selectionStart.beat.playbackStart; // set playback range @@ -1314,8 +1312,8 @@ export class AlphaTabApiBase { if (!endBeat.bounds) { endBeat.bounds = cache.findBeat(endBeat.beat); } - let startTick: number = startBeat.beat.absolutePlaybackStart; - let endTick: number = endBeat.beat.absolutePlaybackStart; + let startTick: number = this._tickCache?.getBeatStart(startBeat.beat) ?? startBeat.beat.absolutePlaybackStart; + let endTick: number = this._tickCache?.getBeatStart(endBeat.beat) ?? endBeat.beat.absolutePlaybackStart; if (endTick < startTick) { let t: SelectionInfo = startBeat; startBeat = endBeat; diff --git a/src/midi/MidiTickLookup.ts b/src/midi/MidiTickLookup.ts index 58770f112..147680123 100644 --- a/src/midi/MidiTickLookup.ts +++ b/src/midi/MidiTickLookup.ts @@ -278,6 +278,20 @@ export class MidiTickLookup { return this.masterBarLookup.get(bar.index)!.start; } + + /** + * Gets the start time in midi ticks for a given beat at which the masterbar is played the first time. + * @param beat The beat to find the time period for. + * @returns The time in midi ticks at which the beat is played the first time or 0 if the beat is not contained + */ + public getBeatStart(beat:Beat): number { + if (!this.masterBarLookup.has(beat.voice.bar.index)) { + return 0; + } + + return this.masterBarLookup.get(beat.voice.bar.index)!.start + beat.playbackStart; + } + /** * Adds a new {@link MasterBarTickLookup} to the lookup table. * @param masterBar The item to add.