The Online Encyclopedia of Integer Sequences (OEIS) was “founded in 1964 by N. J. A. Sloane”, which is a rather startling claim to make about a website, but in 1964 it wasn’t “Online”. Anyway, it’s an initially file cards, later punched cards, later published books, and now online database of sequences of integers, 345,080 of them at this writing, from number A000001, “Number of groups of order n” (0, 1, 1, 1, 2, 1, 2, 1, 5…) through A000040, “The prime numbers” (2, 3, 5, 7, 11, 13, 17…) and A000045, “Fibonacci numbers” (0, 1, 1, 2, 3, 5, 8, 13…) to A345267, “Conjectural order of the torsion subgroup of the group K_n(Z) (the algebraic K-theory groups of the integers)” (1, 2, 2, 48, 1, 1, 1, 240, 1, 2, 2, 1008…) and beyond. Sloane’s still running the OEIS, nearly 60 years later. He turns up frequently in videos on the Numberphile YouTube channel. Here’s one recent video:

(This WordPress theme’s handling of embedded videos is buggy so I have to insert some space here:)

.

.

.

.

.

And why I’m writing about this here on the Analog Output blog becomes evident at 7:00 when he shows what happens when you take the integer sequence he was talking about and turn it into a melodic sequence.

As he mentions, there’s a link on each OEIS page that allows you to turn the sequence into a MIDI file you can listen to. That got me thinking about a module. You could easily have an Arduino Nano take an integer sequence and turn it into control voltages. What you couldn’t do so easily is get the OIES sequence into the Nano in the first place… Well, what about a Raspberry Pi with a WiFi adapter? It could link to the OEIS and download any sequence on demand. If your WiFi’s working, anyway. Hmm.

Well, that’d be fun, maybe, but doing it on an external computer and sending it out on MIDI is easier.

After resisting the temptation to waste time on this for a while, I gave in and started looking for Python libraries. I found MIDO, a MIDI library, and oeispy, an OEIS library. The rest was easy. Well, kind of, but I did find some things I didn’t like about oeispy. I submitted an issue on GitHub but got no response, so I forked the project, calling my version oeislib. Besides addressing a couple of misfeatures, it fills a significant gap with a new method, `getBFileData`

. There was already a `getData`

method which returns the sequence values shown on the main page for the sequence (as a character string, parsing it’s your problem), but typically no more than a few dozen entries are included there. But for each sequence there’s also a “b-file”, like https://oeis.org/A000040/b000040.txt, which gives more entries, 10,000 of them in this case. oeispy didn’t have a method to access the b-file. oeislib does; `getBFileData(res-indexed)`

returns a generator object that yields successive pairs `[i, a(i)]`

.

Armed with that, `playoeis.py`

takes a specified sequence, or the best result of a sequence search, and turns it into MIDI events. There are options to loop or not, to truncate the sequence to a given number of events, to reduce the integers like that 1008 you saw up there modulo something (modulo 88 to match a piano keyboard range, for instance), to add an offset to each (MIDI note 0 being awfully low, an offset of 24 or more is useful), to interpret negative numbers or zeros or nonpositive numbers or whatever as rests, and more.

It’s fun. A lot of sequences are either boring or random sounding but some are pretty interesting. I agree with Sloane about A318921, that one makes a nice melodic sequence indeed. And so does A123456, ‘Ludwig van Beethoven, Bagatelle No. 25, “Für Elise”‘ (-20, 56, 55, 56, 55, 56, 51, 54…).

Repos:

- oeislib
- playoeis.py (requires oeislib)