Przeglądaj źródła

PlaybackManager: add playFromMs() and getTimestampFromMs() methods

sschmidTU 3 lat temu
rodzic
commit
80ae32999d
1 zmienionych plików z 20 dodań i 1 usunięć
  1. 20 1
      src/Playback/PlaybackManager.ts

+ 20 - 1
src/Playback/PlaybackManager.ts

@@ -394,6 +394,26 @@ export class PlaybackManager implements IPlaybackParametersListener {
 
     }
 
+    /** Starts play() from a time in milliseconds.
+     *  Note that sometimes all notes still active at the timestamp will be played.
+     *  This is due to the playback system and could potentially be improved,
+     *  but note that OSMD is not an mp3 player that can start in the middle of a sound,
+     *  so please don't overuse this feature.
+     */
+    public async playFromMs(timeInMs: number): Promise<void> {
+        await this.pause();
+        const timestamp: Fraction = this.getTimestampFromMs(timeInMs);
+        const previousSelectionStart: Fraction = this.musicPartManager.MusicSheet.SelectionStart.clone();
+        this.musicPartManager.MusicSheet.SelectionStart = timestamp;
+        this.reset();
+        this.play();
+        this.musicPartManager.MusicSheet.SelectionStart = previousSelectionStart; // restore previous start point
+    }
+
+    public getTimestampFromMs(timeInMs: number): Fraction {
+        return this.timingSource.Settings.getDurationAsNoteDuration(timeInMs);
+    }
+
     public Dispose(): void {
         // lock(this.playbackThreadSyncObject) {
             this.listeners = [];
@@ -555,7 +575,6 @@ export class PlaybackManager implements IPlaybackParametersListener {
             // stop the notes that are already over now:
             this.stopFinishedNotes();
 
-            /***** process tempo instructions: *****/
             this.processTempoInstructions();
 
             if (this.RunningState === PlaybackState.Running) { // needed when resetting when in pause