|
1 | 1 | // https://github.com/grimmdude/MidiPlayerJS |
2 | | -// https://github.com/danigb/soundfont-player |
| 2 | +// https://github.com/danigb/smplr |
3 | 3 |
|
4 | 4 | import MidiPlayer from 'midi-player-js'; |
5 | | -import Soundfont from 'soundfont-player'; |
| 5 | +import { Soundfont } from 'smplr'; |
6 | 6 | import { getOrigMidi } from '../../../app'; |
7 | 7 | import { setAttributes } from '../../utils/dom'; |
8 | 8 | import { clamp, round } from '../../utils/math'; |
@@ -165,33 +165,38 @@ class Player { |
165 | 165 | } |
166 | 166 |
|
167 | 167 | init() { |
168 | | - Soundfont.instrument(audioContext, './instruments/acoustic-grand-piano-mp3.js').then( |
169 | | - (instrument) => { |
170 | | - this.instrument = instrument; |
171 | | - midiPlayer = new MidiPlayer.Player((event) => { |
172 | | - const noteKey = event.noteName + event.track; |
173 | | - |
174 | | - if (event.name == 'Note on' && event.velocity > 0) { |
175 | | - this.activeNotes[noteKey] = instrument.play( |
176 | | - event.noteNumber, |
177 | | - audioContext.currentTime, |
178 | | - { |
179 | | - gain: event.velocity / 127, |
180 | | - } |
181 | | - ); |
182 | | - } else if ( |
183 | | - event.name == 'Note off' || |
184 | | - (event.name == 'Note on' && event.velocity == 0) // synonym of `Note off` |
185 | | - ) { |
186 | | - this.activeNotes[noteKey]?.stop(); |
| 168 | + // Create a new Soundfont instance from smplr |
| 169 | + this.instrument = new Soundfont(audioContext, { |
| 170 | + instrument: 'acoustic_grand_piano', |
| 171 | + }); |
| 172 | + |
| 173 | + // Wait for the instrument to load |
| 174 | + this.instrument.load.then(() => { |
| 175 | + midiPlayer = new MidiPlayer.Player((event) => { |
| 176 | + const noteKey = event.noteName + event.track; |
| 177 | + |
| 178 | + if (event.name == 'Note on' && event.velocity > 0) { |
| 179 | + // Start playing a note using smplr's API |
| 180 | + this.activeNotes[noteKey] = this.instrument.start({ |
| 181 | + note: event.noteNumber, |
| 182 | + velocity: event.velocity, |
| 183 | + }); |
| 184 | + } else if ( |
| 185 | + event.name == 'Note off' || |
| 186 | + (event.name == 'Note on' && event.velocity == 0) // synonym of `Note off` |
| 187 | + ) { |
| 188 | + // Stop the note if it exists |
| 189 | + if (this.activeNotes[noteKey]) { |
| 190 | + this.activeNotes[noteKey](); // Call the returned stop function |
| 191 | + delete this.activeNotes[noteKey]; |
187 | 192 | } |
| 193 | + } |
188 | 194 |
|
189 | | - this.updateProgress(); |
190 | | - }); |
| 195 | + this.updateProgress(); |
| 196 | + }); |
191 | 197 |
|
192 | | - midiPlayer.on('endOfFile', () => this.stop()); |
193 | | - } |
194 | | - ); |
| 198 | + midiPlayer.on('endOfFile', () => this.stop()); |
| 199 | + }); |
195 | 200 |
|
196 | 201 | this.updateControls('stopped'); |
197 | 202 | this.progressBar = new ProgressBar('player'); |
|
0 commit comments