Skip to content

Commit 7d1422e

Browse files
committed
fix: replace soundfont-player with smplr
- Fixed crash on loading files refs: #246
1 parent 880e1fc commit 7d1422e

File tree

3 files changed

+42
-80
lines changed

3 files changed

+42
-80
lines changed

package-lock.json

Lines changed: 10 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"midi-player-js": "^2.0.14",
3636
"normalize.css": "^8.0",
3737
"pagemap": "^1.4.0",
38-
"soundfont-player": "^0.12.0",
38+
"smplr": "^0.16.1",
3939
"verovio": "~3.8.1"
4040
},
4141
"devDependencies": {

src/js/new/modules/Player/index.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// https://github.com/grimmdude/MidiPlayerJS
2-
// https://github.com/danigb/soundfont-player
2+
// https://github.com/danigb/smplr
33

44
import MidiPlayer from 'midi-player-js';
5-
import Soundfont from 'soundfont-player';
5+
import { Soundfont } from 'smplr';
66
import { getOrigMidi } from '../../../app';
77
import { setAttributes } from '../../utils/dom';
88
import { clamp, round } from '../../utils/math';
@@ -165,33 +165,38 @@ class Player {
165165
}
166166

167167
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];
187192
}
193+
}
188194

189-
this.updateProgress();
190-
});
195+
this.updateProgress();
196+
});
191197

192-
midiPlayer.on('endOfFile', () => this.stop());
193-
}
194-
);
198+
midiPlayer.on('endOfFile', () => this.stop());
199+
});
195200

196201
this.updateControls('stopped');
197202
this.progressBar = new ProgressBar('player');

0 commit comments

Comments
 (0)