Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Play MIDI files #107

Open
page200 opened this issue Jan 2, 2023 · 9 comments
Open

Play MIDI files #107

page200 opened this issue Jan 2, 2023 · 9 comments

Comments

@page200
Copy link

page200 commented Jan 2, 2023

Since there's no "Discussions" tab in this repo (and this discussion affects potential add-ons and bugfixes), I'm opening this issue to discuss how to play MIDI files. This discussion started at mudcube/MIDI.js#268 (comment) and mudcube/MIDI.js#268 (comment) with @urobot2011 in the repo of MIDI.js, whose SoundFont loader soundfont-player replaces.

The readme of soundfont-player says "It is a much simpler and lightweight replacement for MIDI.js soundfont loader (MIDI.js is much bigger, capable of play midi files, for example, but it weights an order of magnitude more).". Therefore:

  • By implementing the capability of playing MIDI files, we might partially revert to what MIDI.js does and thus again run into the problem that the first notes play too late. As a quick test, if you use the time argument of play() or schedule() in soundfont-player to program a melody where many instruments play fast in the beginning (for example playing fast scales), does the melody play with the correct rhythm?
  • What's lacking in soundfont-player is the loading of MIDI files. We could borrow that functionality from other projects than MIDI.js. For example:

Another thing: When I tried the official demo of MidiPlayerJS months ago, which uses soundfont-player, it had unstable playback, for example when switching quickly between windows. When I tried today, it seemed to use wrong instruments and the rhythm didn't seem perfect. I wonder whether that problem is due to MidiPlayerJS or soundfont-player. It might be due to soundfont-player because that webpage says "This MIDI player was built using MidiPlayerJS to read the file, and soundfont-player to load and play the sounds". I also wonder whether those are the fatal flaws that @urobot2011 mentioned at mudcube/MIDI.js#268 (comment) and grimmdude/MidiPlayerJS#94, or whether @urobot2011 meant another one.

I wonder what the change was compared to MIDI.js that affected the timing.

@mk-pmb
Copy link
Contributor

mk-pmb commented Jan 2, 2023

Thank you for summarizing!

@page200
Copy link
Author

page200 commented Jan 3, 2023

To sum up several discussions: The only approach that works well so far (correct instruments, perfect rhythm, can play in the background) is the latest official branch of MIDI.js (named "abcjs"), except for the first seconds of the first time a song is played. That problem is described at mudcube/MIDI.js#268 (comment). Solving that seems like the most promising plan. Feel free to have a look into that.

@urobot2011
Copy link

Thanks for your help!

@urobot2011
Copy link

But when i click the 'play' button on my Kalimba-sheet-music-writer project the sound is fine (there is a little sound jitter but i don't mind that).
What I'm wondering is if it's possible to know the length of a note in advance before the 'note off' event fires. I didn't get the answer from MidiPlayerJS's official documentation.
Here is a test page for my Kalimba-sheet-music-writer project:
Test Page

@page200
Copy link
Author

page200 commented Jan 3, 2023

@urobot2011 Hm, I'd like to play MIDI files without jitter. And regarding note durations, I suspect that some JavaScript MIDI players don't care about matching "Note On" MIDI messages with the respective "Note Off" messages. If you don't want to "reinvent" that matching, then apps that visualize that matching (for example as piano rolls) might serve as inspiration. Tag me at https://github.com/urobot2011/Kalimba-sheet-music-writer/discussions or so if you want me to list them and some of their properties. I don't know whether MidiPlayerJS performs that matching. https://musiclab.chromeexperiments.com/Piano-Roll/ (https://github.com/googlecreativelab/chrome-music-lab/tree/master/pianoroll) uses an interval tree to rewind the song efficiently.

@urobot2011
Copy link

@page200 I looked at Color Piano and thought this might be possible with MIDI.JS.
Is this possible with MIDI.JS?

@page200
Copy link
Author

page200 commented Jan 4, 2023

@urobot2011 Color Piano uses MIDI.js, but probably an old branch of MIDI.js that doesn't play all instruments correctly. (Color Piano only plays the piano.) As discussed at mudcube/MIDI.js#268, it seems more promosing to fix the first seconds of the new "abcjs" branch than the entire instrument support in the old "master" branch.

@urobot2011

This comment was marked as resolved.

@urobot2011

This comment was marked as resolved.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants