Back Original

Bad Counterpoint, and MIDI and HTML5 in 2025

Today I spent some toy throwing together a toy music generator in Python, Bad Bach. Counterpoint is a musical style with roots in the Renaissance which follows a strict set out of rules. I set out to deliberately write bad counterpoint, or what I later learned is called dissonant counterpoint, which breaks as many of those rules as possible. I initially approached this as a puzzle solver problem, with the goal of maximizing under a set of constraints. However, I realized this music would be too deterministic for my taste. So instead I constructed a set of weights for (breaking) each rule, and then randomly generate rule-breaking harmonies based on those weights.

You can see a little demo here: Awful Twinkle. This takes a variation of Twinkle, Twinkle, Little Star and provides an example of one of the counterpoint lines that my code generated.

I was surprised to learn when throwing this demo together that HTML5 seems to have removed easy support for embedding MIDI on a webpage. A long time ago I remember doing something like this:

<embed src="file.mid" autostart=true repeat=true>

This no longer works (although this abuse of autostart and repeat definitely brings back memories)! HTML5 does have an audio tag, but it does not seem to support MIDI.

Fortunately, there’s this really cool project https://github.com/cifkao/html-midi-player which embeds a nice JS player and visualizer for MIDI files. Check it out in my demo or on their home page!