Audio
Tags: webaudio, thesession, midi, abc
Published: 2020-12-19 09:37:06

---
title: Audio
published: true
date: 2020-12-16 09:31:54 UTC
tags: webaudio,thesession,midi,abc
canonical_url: https://adactio.com/journal/17698
---

I spent the last couple of weekends [rolling out a new feature on The Session](https://thesession.org/discussions/45644). It involves playing audio in a web page. No big deal these days, right? But the history involves some old file formats…

The first venerable format is [ABC notation](https://abcnotation.com/). File extension: `.abc`, mime type: `text/vnd.abc`. It’s an ingenious text format for musical notation using ASCII. The metadata of the piece of music is defined in JSON-like key/value pairs. Then the contents are encoded with letters: A, B, C, etc. Uppercase and lowercase denote different octaves. Numbers can be used for note lengths.

[The format](https://en.wikipedia.org/wiki/ABC_notation) was created by Chris Walshaw in 1997 when dial-up was the norm. With ABC, people were able to swap tunes on email lists or bulletin boards without transferring weighty image or sound files. If you had ABC software on your computer, you could convert that lightweight text file into sheet music …or audio.

That brings me to the second old format: midi files. File extension: `.mid`, mime-type: `audio/midi`. Like ABC, it’s a lightweight format for encoding the instructions for music instead of the music itself.

Think of it like SVG: instead of storing the final pixels of an image, SVG stores the instructions for drawing the image instead. The instructions in a midi file are like “play this note for this long on this instrument.” Again, as with ABC, you need some software to turn the instructions into sound.

There was a time when lots of software could play midi files. Quicktime on the Mac, for example. You could even embed midi files in web pages. I mean literally embed them …with [the `embed` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed). No Geocities page was complete without an autoplaying midi file.

On [The Session](https://thesession.org/), people submit tunes in ABC format. Then, using the amazing [ABCJS](https://paulrosen.github.io/abcjs/) JavaScript library, the ABC is turned into SVG on the fly! For years I’ve also offered midi files, generated on the server from the ABC notation.

But times have changed. These days it’s hard to find software that plays midi files. Quicktime doesn’t do it anymore. And you’d need to go to the app store on iOS to find a midi file player. It’s time to phase out the midi files on The Session.

I still want to provide automatically-generated audio though. Fortunately ABCJS gives me a way to do this. But instead of using the old technology of midi files, it uses a more modern browser feature: the [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API).

The end result _sounds_ like a midi file, but the underlying technique is more like a synthesiser. There’s a separate mp3 file for each note. The JavaScript figures out how long each “sample” needs to be played for, strings them all together, and outputs them with Web Audio. So you’ve got cutting-edge browser technology recreating a much older file format. [Paul Rosen](https://paulrosen.net/)—the creator of ABCJS—has [a presentation explaining how it all works under the hood](https://web-audio-slides.abcjs.net/).

Not only is there a separate short mp3 file for each note in seven octaves, but if you want the sound of a different instrument, you need samples for all seven octaves in that instrument. They’re called soundfonts.

[Paul provides soundfonts for ABCJS](https://github.com/paulrosen/midi-js-soundfonts). It’s a repo that was forked from [this repo from Benjamin Gleitzman](https://github.com/gleitz/midi-js-soundfonts). And here’s where it gets small worldy…

The reason why [Benjamin](https://blog.gleitzman.com/) has a repo of soundfonts is because he needed to create midi-like audio in the browser. He wanted to do this for a project on September 28th and 29th, 2013 …at [Science Hack Day San Francisco](http://sciencehackday.pbworks.com/w/page/69019453/sfhacks2013)!

I was there too—working on my own [audio-related hack](https://adactio.com/journal/6525)—and I remember the excellent (and winning) hack that Benjamin worked on. It was called [Symphony of Satellites](http://gleitzman.com/apps/spaceharp/) and it’s still online along with [the promo video](https://www.youtube.com/watch?v=dijUnpR2SLE). Here’s [Benjamin’s post-hackday write-up](https://blog.gleitzman.com/post/63283830260/midi-js-playing-audio-in-the-browser-with-javascript) from seven years ago.

It’s rare that the worlds of the web and Irish music cross over. When I got to meet [Paul](https://paulrosen.net/)—creator of ABCJS—at a web conference a couple of years ago it kind of blew my mind. Last weekend when I set out to dabble with a feature on The Session, I certainly didn’t expect to stumble on a connection to Science Hack Day! (Aside: [the first Science Hack Day was ten years ago](https://adactio.com/journal/1674)—yowzers!)

Anyway, I was able to get that audio playback working on The Session. Except for some weirdness on iOS that I had to fix. But that’s a hack for another day.