瀏覽代碼

fix drums playback - load correct soundfont, use correct channels and pitches. add PlaybackManager.IgnoreFixedInstrumentKeysForPercussion option

previously, woodblock was used as the instrument (115), not sure why.
also, the subinstrument.fixedKey seems to be wrong for drums, it seems to be always one higher than is correct.
also, the percussion channel is 10 for general midi according to wikipedia, not 9.
sschmidTU 2 年之前
父節點
當前提交
5380f37f1c
共有 2 個文件被更改,包括 10 次插入4 次删除
  1. 6 1
      src/Playback/BasicAudioPlayer.ts
  2. 4 3
      src/Playback/PlaybackManager.ts

+ 6 - 1
src/Playback/BasicAudioPlayer.ts

@@ -86,9 +86,14 @@ export class BasicAudioPlayer implements IAudioPlayer<SoundfontPlayer.Player> {
       return this.memoryLoadedSoundFonts.get(soundId);
       return this.memoryLoadedSoundFonts.get(soundId);
     }
     }
 
 
+    let nameOrUrl: any = midiNames[soundId].toLowerCase();
+    if (soundId === MidiInstrument.Percussion) {
+      // percussion unfortunately doesn't exist in the original soundfonts
+      nameOrUrl = "https://paulrosen.github.io/midi-js-soundfonts/FluidR3_GM/percussion-mp3.js";
+    }
     const player: SoundfontPlayer.Player = await SoundfontPlayer.instrument(
     const player: SoundfontPlayer.Player = await SoundfontPlayer.instrument(
       this.ac as unknown as AudioContext,
       this.ac as unknown as AudioContext,
-      midiNames[soundId].toLowerCase() as any,
+      nameOrUrl,
       this.SoundfontInstrumentOptions
       this.SoundfontInstrumentOptions
     );
     );
     this.memoryLoadedSoundFonts.set(soundId, player);
     this.memoryLoadedSoundFonts.set(soundId, player);

+ 4 - 3
src/Playback/PlaybackManager.ts

@@ -46,7 +46,7 @@ export class PlaybackManager implements IPlaybackParametersListener {
     public addListener(listener: IPlaybackListener): void {
     public addListener(listener: IPlaybackListener): void {
         this.listeners.push(listener);
         this.listeners.push(listener);
     }
     }
-    private readonly percussionChannel: number = 9; // this is a definition of the midi interface (cannot be changed)
+    private readonly percussionChannel: number = 10; // this is a definition of the midi interface (cannot be changed)
     private messageViewer: IMessageViewer;
     private messageViewer: IMessageViewer;
     private audioMetronomePlayer: IAudioMetronomePlayer;
     private audioMetronomePlayer: IAudioMetronomePlayer;
     private audioPlayer: IAudioPlayer<any>;
     private audioPlayer: IAudioPlayer<any>;
@@ -115,6 +115,7 @@ export class PlaybackManager implements IPlaybackParametersListener {
     public MoveCursorDuringPrecount: boolean = true;
     public MoveCursorDuringPrecount: boolean = true;
     private fermataActive: boolean;
     private fermataActive: boolean;
     private doPreCount: boolean = true;
     private doPreCount: boolean = true;
+    public IgnoreFixedInstrumentKeysForPercussion: boolean = true;
 
 
     constructor (timingSource: ITimingSource, audioMetronomePlayer: IAudioMetronomePlayer, audioPlayer: IAudioPlayer<any>, messageViewer: IMessageViewer) {
     constructor (timingSource: ITimingSource, audioMetronomePlayer: IAudioMetronomePlayer, audioPlayer: IAudioPlayer<any>, messageViewer: IMessageViewer) {
         const metronomeLabel: string = TextTranslation.translateText("Playback/LabelMetronome", "Metronome");
         const metronomeLabel: string = TextTranslation.translateText("Playback/LabelMetronome", "Metronome");
@@ -282,7 +283,7 @@ export class PlaybackManager implements IPlaybackParametersListener {
 
 
                 this.audioPlayer.open(uniqueMidiInstruments, 16);
                 this.audioPlayer.open(uniqueMidiInstruments, 16);
                 // set drums:
                 // set drums:
-                this.audioPlayer.setSound(this.percussionChannel, 115);
+                this.audioPlayer.setSound(this.percussionChannel, MidiInstrument.Percussion);
             }
             }
 
 
             this.currentReferenceBPM = this.sheetStartBPM = musicSheet.getExpressionsStartTempoInBPM();
             this.currentReferenceBPM = this.sheetStartBPM = musicSheet.getExpressionsStartTempoInBPM();
@@ -746,7 +747,7 @@ export class PlaybackManager implements IPlaybackParametersListener {
                                     const notePlaybackInstrument: SubInstrument =
                                     const notePlaybackInstrument: SubInstrument =
                                         instrument.getSubInstrument(note.ParentNote.PlaybackInstrumentId);
                                         instrument.getSubInstrument(note.ParentNote.PlaybackInstrumentId);
                                     if (notePlaybackInstrument !== undefined) {
                                     if (notePlaybackInstrument !== undefined) {
-                                        if (notePlaybackInstrument.fixedKey >= 0) {
+                                        if (notePlaybackInstrument.fixedKey >= 0 && !(isPercussion && this.IgnoreFixedInstrumentKeysForPercussion)) {
                                             key = notePlaybackInstrument.fixedKey;
                                             key = notePlaybackInstrument.fixedKey;
                                         }
                                         }
                                     }
                                     }