浏览代码

player: cursor: fix cursor.NotesUnderCursor() not returning the notes at current playback position

it always returned the first notes, because the cursor's iterator was never updated from playback,
it just set the visible position.
this was a request from a developer, to be able to use NotesUnderCursor().
sschmidTU 3 年之前
父节点
当前提交
41329c424a
共有 1 个文件被更改,包括 15 次插入17 次删除
  1. 15 17
      src/OpenSheetMusicDisplay/Cursor.ts

+ 15 - 17
src/OpenSheetMusicDisplay/Cursor.ts

@@ -55,14 +55,13 @@ export class Cursor implements IPlaybackListener {
     this.container.appendChild(curs);
     this.container.appendChild(curs);
   }
   }
   public cursorPositionChanged(timestamp: Fraction, data: CursorPosChangedData): void {
   public cursorPositionChanged(timestamp: Fraction, data: CursorPosChangedData): void {
-    // if (this.iterator.CurrentEnrolledTimestamp.lt(timestamp)) {
-    //   this.iterator.moveToNext();
-    //   while (this.iterator.CurrentEnrolledTimestamp.lt(timestamp)) {
-    //     this.iterator.moveToNext();
-    //   }
-    // } else if (this.iterator.CurrentEnrolledTimestamp.gt(timestamp)) {
-    //   this.iterator = new MusicPartManagerIterator(this.manager.MusicSheet, timestamp);
-    // }
+    // update iterator so cursor.NotesUnderCursor() etc works
+    while (this.iterator.CurrentEnrolledTimestamp.lt(timestamp)) {
+      this.iterator.moveToNext();
+    }
+    if (this.iterator.CurrentEnrolledTimestamp.gt(timestamp)) {
+      this.iterator = new MusicPartManagerIterator(this.manager.MusicSheet, timestamp);
+    }
 
 
     this.updateWithTimestamp(data.PredictedPosition);
     this.updateWithTimestamp(data.PredictedPosition);
   }
   }
@@ -207,12 +206,7 @@ export class Cursor implements IPlaybackListener {
     this.updateCurrentPage(); // attach cursor to new page DOM if necessary
     this.updateCurrentPage(); // attach cursor to new page DOM if necessary
 
 
     // this.graphic?.Cursors?.length = 0;
     // this.graphic?.Cursors?.length = 0;
-    let iterator: MusicPartManagerIterator;
-    if (this.openSheetMusicDisplay.PlaybackManager) {
-      iterator = this.openSheetMusicDisplay.PlaybackManager.CursorIterator;
-    } else {
-      iterator = this.iterator;
-    }
+    const iterator: MusicPartManagerIterator = this.Iterator;
 
 
     // TODO when measure draw range (drawUpToMeasureNumber) was changed, next/update can fail to move cursor. but of course it can be reset before.
     // TODO when measure draw range (drawUpToMeasureNumber) was changed, next/update can fail to move cursor. but of course it can be reset before.
 
 
@@ -347,7 +341,7 @@ export class Cursor implements IPlaybackListener {
    * Go to next entry
    * Go to next entry
    */
    */
   public next(): void {
   public next(): void {
-    this.iterator.moveToNextVisibleVoiceEntry(false); // moveToNext() would not skip notes in hidden (visible = false) parts
+    this.Iterator.moveToNextVisibleVoiceEntry(false); // moveToNext() would not skip notes in hidden (visible = false) parts
     this.update();
     this.update();
   }
   }
 
 
@@ -395,6 +389,10 @@ export class Cursor implements IPlaybackListener {
   }
   }
 
 
   public get Iterator(): MusicPartManagerIterator {
   public get Iterator(): MusicPartManagerIterator {
+    // if (this.openSheetMusicDisplay.PlaybackManager) {
+    //   return this.openSheetMusicDisplay.PlaybackManager.CursorIterator;
+    // }
+    // PlaybackManager.CursorIterator is often not at the visible cursor position.
     return this.iterator;
     return this.iterator;
   }
   }
 
 
@@ -404,7 +402,7 @@ export class Cursor implements IPlaybackListener {
 
 
   /** returns voices under the current Cursor position. Without instrument argument, all voices are returned. */
   /** returns voices under the current Cursor position. Without instrument argument, all voices are returned. */
   public VoicesUnderCursor(instrument?: Instrument): VoiceEntry[] {
   public VoicesUnderCursor(instrument?: Instrument): VoiceEntry[] {
-    return this.iterator.CurrentVisibleVoiceEntries(instrument);
+    return this.Iterator.CurrentVisibleVoiceEntries(instrument);
   }
   }
 
 
   public NotesUnderCursor(instrument?: Instrument): Note[] {
   public NotesUnderCursor(instrument?: Instrument): Note[] {
@@ -429,7 +427,7 @@ export class Cursor implements IPlaybackListener {
    *  This is only necessary if using PageFormat (multiple pages).
    *  This is only necessary if using PageFormat (multiple pages).
    */
    */
   public updateCurrentPage(): number {
   public updateCurrentPage(): number {
-    const timestamp: Fraction = this.iterator.currentTimeStamp;
+    const timestamp: Fraction = this.Iterator.currentTimeStamp;
     for (const page of this.graphic.MusicPages) {
     for (const page of this.graphic.MusicPages) {
       const lastSystemTimestamp: Fraction = page.MusicSystems.last().GetSystemsLastTimeStamp();
       const lastSystemTimestamp: Fraction = page.MusicSystems.last().GetSystemsLastTimeStamp();
       if (lastSystemTimestamp.gt(timestamp)) {
       if (lastSystemTimestamp.gt(timestamp)) {