JSModPlayer – a Javascript .MOD player

The epic Pacman 30th anniversary Google Doodle, along with Ben Firshman’s dynamicaudio.js library for dynamically generating audio, collectively persuaded me that I haven’t done any mad Javascript hacking for far too long. My response to this state of affairs is JSModPlayer, a player for .MOD music files (the mainstay of Amiga and PC sample-based music circa 1990).

So far it only implements a subset of the possible sample effects, and it demands a very fast Javascript engine – luckily all the new breed of browsers are pretty competitive at that now. Even so, unless your CPU is an absolute behemoth, it’ll probably struggle to keep up – the audio output is fixed at 44100Hz, and that’s rather a lot of numbers for Javascript to crunch, especially when the MOD file gets up to 16 or more channels. Which, amusingly enough, is exactly the situation we had back when we were using Gravis Ultrasounds on 386es. Hurrah for progress!

Update 2010-06-08: Oops. In the process of testing how Safari 5 shapes up, I discovered a rather silly oversight: the audio buffering routine was set up to never use more than 10% of CPU. Now that I’ve fixed it, it turns out that Chrome and Safari (at least) have no trouble at all playing Jugi’s Dope theme in its 28-channel glory. (However, taking the brakes off the buffering does mean that we can’t reliably pause the audio any more. A small price to pay, I think you’ll agree.)

11 Responses to “JSModPlayer – a Javascript .MOD player”

  1. sole says:

    I told you once, repeat it know: you’re insane! Mental! Awesomely mental!
    I have looked at the code and it’s even readable. Love it.

    I was watching a fragment of a Google IO conference past week and the developers said they were going to add spatial audio capabilities and that OpenAL-ish sort of thing to Google Chrome. It looked awfully like Chrome interfacing with OpenAL (just as NaCl looks a lot like a layer over SDL).

    Things might get quite interesting in the future in the sound field :)

  2. Megus says:

    Matthew, you’re a maniac!

  3. Cool :) Didn’t run very well in FF 3.6 but it’s javascript, so it’s to be expected. What I wondered though was, why not stick to 22khz? The amiga didn’t output sound above that anyway and humans can’t hear it. Most samples in mods are 8bit as well. More modern formats, like fasttracker’s XM format had 16bit/44khz samples indeed, but for playing 4 channel .mod files, 22khz, 8 bit samples is enough. Protracker FTW! ;)

  4. matt says:

    Otis: Using 44kHz was purely because that’s what dynamicaudio.js gave me to play with, and I haven’t yet dug into the Flash audio API to see if it’s possible to override that.

    It’s not true to say that sample rates above 22kHz are unnecessary given the limits of human hearing, though – while you couldn’t hear a 44kHz audio wave, you can certainly hear the aliasing effects from sample rate not being high enough. And even if the original samples are limited to 8 bit, once you start adding them together and applying effects / volume levels, having 16-bit output can make a difference…

  5. Zipo says:

    The Amiga audio DMA chip had a limit of 28Khz on PAL (slightly higher on NTSC if I remember correctly), which could be got around with a bit of effort (you don’t have to use the DMA chip).
    A lot of the percussion instruments where sampled at that rate (or close to it).

    A bigger limit was that the Amiga by default only had 512K of RAM, to upgrade was hideously expensive at the time.

    The Gravis Ultrasound would only let you use 14 sound channels or less at 44.1Khz (which is why a lot of Fast Tracker and Scream Tracker mods only used 14 sound channels). After than the more channels you use the lower the sound output frequency. At 28 sound channels output was 22Khz (which is why DOPE “only” uses 28 Channels). Again RAM was an issue most Ultrasounds came with 512K that people upgraded to 1Mega byte (the most it could have), worse still was that sound cards of the time did not support ADPCM sample compression (a few of the arcade machines did).

    Dope (a DOS demo released by Komplex in 1995) was fantastic and the music is still great!
    * http://www.komplex.org/pcdemos.html
    No doubt there is a video of it on the web somewhere.

  6. Any chance you could add this code into the Chrome “Light Player” plugin? Would be great to be able to play .mod’s right from browsing sites like: http://cdmirror.textfiles.com/10000soundssongs/MOD/

    Here’s the project page: https://chrome.google.com/webstore/detail/chkhjlbdflppmaddpjmjecgdagdljimc

    I think it’s all in JavaScript so would adding it be really difficult?
    https://github.com/itchyny/ChromePlayer

  7. [...] as we can tell. There is the odd exception though: for our most recent demo, we used Popcorn.js and jsmodplayer because they’re the useful result of two interesting JavaScript communities. I was introduced [...]

  8. chris says:

    Hi works good in chrome, but when i download source from github and click a song it remains on loading. any ideas? I’ve maintained the folder structure when extracting.

  9. matt says:

    chris: The song data is loaded using XmlHttpRequest, so as it stands it won’t work if you just open the page directly off disk – it needs to be served over HTTP, through a local instance of Apache or similar.

  10. Aaron says:

    I was just about to try this myself and then I came across your page. Awesome work!

  11. YoY says:

    need a port on html5 audio api :)

Leave a Reply