MIDI.js icon indicating copy to clipboard operation
MIDI.js copied to clipboard

Play different instruments in a flexible way

Open Owwwl9 opened this issue 8 years ago • 7 comments

I'd like to play several midi-files after one another, like sort of a playlist. According to the midi-file I'd like to get all its instruments to play. Sofar I only achieve that like this:

var player;
var instruments;

window.onload = function () {
    MIDI.loadPlugin({
        soundfontUrl: './soundfont/',
        onsuccess: function () {
            player = MIDI.Player;
            player.timeWrap = 1;
            player.loadFile('test.mid', function () {
                instruments = player.getFileInstruments();
                for (var i = 0; i < instruments.length; i++) {
                    MIDI.programChange(i, MIDI.GM.byName[instruments[i]].number);
                }
                player.start();
            });
        }
    });
};

However they either seem to play on the wrong channel or somehow the wrong soundfont is assigned. So for example, the clarinet plays the piano's melody. I couldn't figure out how to solve that problem.

I'd be really happy about some advice or ideas :)

Owwwl9 avatar Nov 26 '17 10:11 Owwwl9

On 26/11/17 21:37, Owwwl9 wrote:

instruments = player.getFileInstruments(); for (var i = 0; i < instruments.length; i++) { MIDI.programChange(i, MIDI.GM.byName[instruments[i]].number); }

Don't do this, it's completely wrong. getFileInstruments just gives you a list of instruments that are used in the file, so that you can load the appropriate sound fonts.

The MIDI file itself will set the program (instrument) on the appropriate channel. There is no need for you to make any calls to MIDI.programChange yourself.

hmoffatt avatar Nov 26 '17 22:11 hmoffatt

Thanks, you are right about that, I didn't feel comfortable with that solution as well.

Without it however, the soundfonts aren't loaded, except from "acoustic_grand_piano". I've got all the soundfonts needed in my soundfont-folder, but it doesn't work. Somehow midi.getFileInstruments fails to assign the right instruments, I guess.

The only way it works sofar really is by using programChange as discussed in this previous issue https://github.com/mudcube/MIDI.js/issues/158

What do I miss?

Owwwl9 avatar Nov 27 '17 09:11 Owwwl9

@Owwwl9 I'm currently having the exact same problem... did you finally find a solution for this issue?

jeniffer9 avatar Nov 24 '18 14:11 jeniffer9

Same Problem, happens every year I guess ^^

profiluefter avatar Dec 06 '19 20:12 profiluefter

Same. We skipped a few years.

embryCODE avatar Mar 20 '22 15:03 embryCODE

@embryCODE Maybe it's related to this line being commented out? https://github.com/mudcube/MIDI.js/blob/a8a84257afa70721ae462448048a87301fc1554a/js/midi/player.js#L116

Boscop avatar Nov 03 '22 20:11 Boscop

Same problem. Has anyone found a solution? Or any example of MIDI.js playing MIDI files with the correct instruments apart from piano?

The commented-out line is probably not the only bug, as discussed in issue #268

page200 avatar Dec 27 '22 21:12 page200