News for hackers. Stuff that matters.
Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by feross on Thursday September 06, 2018 @07:57PM (#17930728)
A bit more info about how BitMidi is built...

First step, I needed to build a MIDI player in JavaScript. At first, I was determinted to write one from scratch in JavaScript and use the Web Audio API to synthesize all the instruments in code. I thought this would yield the the smallest possible JavaScript file size.

However, I didn’t really have the audio engineering skills to pull this off. So ended up settling for an approach that uses SoundFonts, which are basically instrument voices or files that contain all the possible notes an instrument can play.

BitMidi uses the instrument voices from the General MIDI sound set released by FreePats.

Then I compiled the best MIDI player written in C (libtimidity) to WebAssembly using Emscripten. I put in lots of effort to optimize the built size and include the minimal amount of code. The result of my efforts are available in the npm package timidity (https://github.com/feross/timidity). It’s quite lightweight – just 34 KB of JavaScript and 23 KB of lazy-loaded WebAssembly, smaller than anything I’ve seen on any other site.

Then I put a frontend on it, so it’s easy to browse all the files. BitMidi uses all the best techniques that I know about to make it super fast and snappy. The site gets perfect 100s on all categories on Chrome’s Lighthouse Performance benchmark, which is extremely non-trivial in my experience.

I plan to ingest a lot more MIDI files in the future, from sources like the Geocities MIDI archive on the Internet Archive and elsewhere.

Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by jimbo1qaz on Friday September 07, 2018 @01:05AM (#17932007)
>the best MIDI player written in C (libtimidity)

Ouch. Has timidity fixed the issue where playing 2 of the same note in sequence cuts off the release tail of the first note when the second starts? Fluidsynth is written in C and doesn't have that issue and sounds better (I assume this behavior matches good MIDI hardware, but not sure about the crappy Windows GS Wavetable Synth). fluidsynth-lite is a Fluidsynth fork with dependencies removed.

Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by feross on Friday September 07, 2018 @01:21AM (#17932055)
Okay, I stand corrected. I did a bunch of research, but I clearly missed some superior MIDI players. I'll take a look at FluidSynth-lite and see if there's some way to use it.

One issue I can already see is they use a SoundFont file (.sf2) instead of instrument patch files, which means the whole 100MB+ file probably needs to be loaded before playback. My current approach uses GUS patch files which have one file per instrument, so I can load just what's needed for a particular song and start playing much sooner.

Edit: It looks the GeneralUser SoundFont is only 31MB and it sounds almost as good as the 100MB+ one. So, while this will still be slower than my current approach of loading just the needed instruments, perhaps if I start loading it in the background with ServiceWorker and cache it forever, this could work. Thoughts welcome.

Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by SmellyGeekBoy on Friday September 07, 2018 @04:23AM (#17932690)
Perhaps give users the choice of which MIDI playback "engine" to use? It seems a shame to throw away all of your hard work (on the player) so far.
Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by j0wy on Thursday September 06, 2018 @09:29PM (#17931224)
Awesome job - I’m about to cry from the memories this brings back. I really appreciate the effort put into porting a proper MIDI player to the browser while keeping page optimization in mind.

Dragging and dropping to play a midi is a great touch!

Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by feross on Thursday September 06, 2018 @10:02PM (#17931364)
It's heartwarming to get feedback like this. Thank you for this.
Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by jek0 on Friday September 07, 2018 @12:47AM (#17931941)
Great job putting all this together! Brings back memories.

Out of curiosity I downloaded a .mid file to compare it with my local timidity install. The quality of the later is way better (I installed it from brew)... It sounds like it either use a better SoundFont or midi effects (like reverb) are not working properly on the site?

Is it just me? Not complaining, just mentioning it.

Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by feross on Friday September 07, 2018 @01:05AM (#17932004)
You're right. Reverb sounds way better with the version of timidity available on Homebrew. I noticed that brew is installing TiMidity++ (http://timidity.sourceforge.net/) instead of libTiMidity (http://libtimidity.sourceforge.net/) which is what I ported to WebAssembly. I'll look into the differences between the two, but if anyone knows more please share.
Re:Show HN: BitMidi – Wayback machine for old-school MIDI files by coding123 on Thursday September 06, 2018 @09:33PM (#17931244)
That's a pretty amazing technical feat!