Color Piano Theory is now available on the Chrome Webstore. There haven’t been any major UI overhauls since last reported, but there has been a lot of work going on the back-end! Most importantly moving from the Java interface to native HTML5 <audio> tag (as Java isn’t supported in the Chrome Webstore). Although this sounds like a simple task, there’s a lot of steps involved; hopefully this will save someone else a bit of trouble!
Generating your own soundfont files;
- JSMIDI will allow you to generate MIDI files with the MidiWriter package;
var key = 0x45; // the note A4
var noteEvents = [];
Array.prototype.push.apply(noteEvents, MidiEvent.createNote(key));
var track = new MidiTrack({ events: noteEvents});
var song = MidiWriter({ tracks: [track] });
console.log(song.b64);
- Saving the MIDI files to disk; File Writer API allows you to save those generated MIDI files to your hard-disk, or, alternatively (and a bit more simple in terms of programming), you could POST the base64 from an embedded <iframe> to .PHP, and write to the file-system;
var iframe = document.createElement("iframe");
iframe.src = "index.php?midi=" + (song.b64) + "&key=" + key;
document.body.appendChild(iframe);
if ($_REQUEST['midi']) {
$myFile = "./midi/".$_REQUEST['key'].".mid";
$fh = fopen($myFile, "w") or die("can't open file");
fwrite($fh, base64_decode(str_replace(' ','+',$_REQUEST['midi'])));
fclose($fh);
return;
}
- Getting out of MIDI format; At this point, we have a bunch of MIDI files. We need to eventually get these MIDI’s -> OGG format, by mapping it to a high-quality SoundFont;
- Older versions of iTunes allows you to batch convert from MIDI’s -> MP4′s. That was very nice feature that seems to have disappeared…
- Online app, such as SolMire, allow you to convert from MIDI’s -> MP3′s and other formats, one at a time. I especially like that SolMire allows you to choose the desired SoundFont to use on the .MIDI.
- MIDI2MP3 is a command line application available for Window and Mac OSX that enables you to use specific SoundFonts in your encodings, and allows you to use the command line… and therefore the ability for batch MIDI -> WAV conversion! FluidSynth Soundfont GM is a good .SF2 file to get you started
- Getting into the OGG format;
- Converting the OGG’s -> base64, and storing them in .js or .jgz file(s):
- Read this amazing Tutorial by the Grinning Gecko!
The following code will allow you to take those MIDI files we created with the JSMIDI package (step #1) and convert them from WAV to OGG to JS to JGZ in seconds! Presenting a solution for the batch conversions of multiple MIDI’s into base64 soundfonts;
#!/bin/bash
# gzip - http://www.gzip.org/
# base64 - http://www.fourmilab.ch/webtools/base64/
# oggenc - http://www.rarewares.org/ogg-oggenc.php
# midi2mp3 - http://www.audiosoftstore.com/downloads.html
# from MIDI to WAV to OGG to JS to JGZ, and beyond!
find ./directory -name '*.mid' -print0 | while read -d $'\0' file
do
# from MIDI to WAV
./inc/midi2mp3 $file -sf ./sf2/FluidSynth_1.43.sf2 -e wave
# from WAV to OGG
./inc/oggenc -m 64 -M 128 $file.wav
# from OGG to base64 embedded in Javascript
echo "if (typeof(Soundfont) === 'undefined') Soundfont = {};" > $file.js
echo "Soundfont['`basename $file`'] = 'data:audio/mpeg;base64,`base64 -i $file.ogg -o -`';" >> $file.js
# gzipped version
gzip $file.js -c > $file.jgz
done
Now you’re ready to create your own custom Soundfont =)

Midibridge using base64 Soundfonts, a proof of concept « Abumarkub
Dec 17, 2011 @ 18:13:22
[...] In fact he is not really using the Soundfonts itself, but rendered .ogg files that are loaded as base64 files into the html5 element instead. He describes his toolchain for rendering audio files from Soundfonts in this blogpost. [...]
Jul 19, 2012 @ 01:05:22
Hey! Do you guys have samples? Like a piano sample?
I’m trying something with the MIDI.js library but still couldn’t make my soundfont…
Jul 24, 2012 @ 20:30:11
@viniciuspires Example soundfont can be found in the repo; https://github.com/mudcube/MIDI.js/tree/master/soundfont
Jul 24, 2012 @ 07:23:17
Great tutorial, really helped me out.
A couple of notes for windows users. Midi2Mp3 has no command line as far as I am aware on windows. I tried it out unsuccessfully. Instead I used timidity (http://timidity.sourceforge.net/). In the same directory as timidity.exe you need to put a file named timidity.cfg with something like this inside to specify the sound font:
dir “D:mysoundfonts”
soundfont FluidR3_GM.sf2
Then to build:
$EXE/timidity $file -Ow -o $file.wav
One thing I found is that this added an extra second or two of no noise to the end of the .wav (I’m not sure why this is), but you can download sox (http://sox.sourceforge.net/) to trim it. For example,
$EXE/sox $file.wav $file.short.wav trim 0 1.15
Will just keep the first 1.15 seconds. For more dynamic trimming try the silence command, although it’s quite difficult to use correctly you can detect just area of silence and cut them.
Jul 24, 2012 @ 20:32:56
@ForrestCoward Very cool
You should share your project here once you’re ready. Sorry for not updating this article, but the latest version of MIDI.js doesn’t require Midi2Mp3 anymore, and only relies on open-source projects. You can find the latest shell script here; https://github.com/mudcube/MIDI.js/blob/master/soundfont-generator/sf2-piano.sh
Oct 19, 2012 @ 15:58:49
There is something i don’t understand:
* “Presenting a solution for the batch conversions of multiple MIDI’s into base64 soundfonts;” Why do we need these base64 soundfonts? The ogg files are generated on the fly and converted to base64 on the fly if i understood you correctly then what is this bash script for?
*Is there any reason for converting to ogg instead of wav?
Oct 19, 2012 @ 18:29:25
The OGG files are not generated on the fly, they are created with the bash script. They are encoded into Base64 in order to encapsulate all the files into one file and cut down on HTTP requests. OGG and MP3 are used as the compression is much higher than WAV, so you end up saving literally 10s of megabytes of space.
Oct 20, 2012 @ 20:50:40
thank you very much for your quick answer.
i still have some difficulty understanding what exactly is going on inside the MIDI.js project.
*there is a folder containing the atob/btoa base64 module but i don’t see any code inside the MIDI.minimal.js code using it? is gen-midi.js legacy code?
*so you converted some .mid files beforehand using the bash script
window.btoa & window.atob are used to encode/decode ogg files to base64
Oct 22, 2012 @ 19:34:13
There are two projects in MIDI.js, one is for the front-end (playing the soundfiles) the other is the back-end that can be run to compile a new set of sounds. The atob/btoa are used in the soundfont-generator—this can be used to encode new sounds like a Guitar or Drums. The Soundfont-Generator dynamically generates .MIDI files, that are converted into .OGG or .MP3, and then to Base64 .JS
Mar 19, 2013 @ 16:37:40
Check out https://github.com/SHMEDIALIMITED/SoundFontJS
It exports MIDI.js compatible sound fonts from raw audio