Making music, part 1: Reading ABC notation

It’s been a bit since I’ve had time to post1, but I’ve got an interesting new project that I’ve been working on. It’s a bit more complicated, ergo spread out over a few posts, but those tend to be the more interesting posts anyway, eh?

The basic idea is that I want to be able to write and play music in Racket. One end goal would be to make a library available for the C211 class to give them something else to work with (in addition to <a href="//blog.jverkamp.com"/wombat-ide/c211-image-api/">images and <a href="//blog.jverkamp.com"/wombat-ide/c211-turtle-api/">turtles). To that end, here’s my current plan of attack2:

  • Write a lexer for ABC notation to turn raw text into a list of tokens
  • Write a parser to turn those tokens into a song (for example dealing with the interactions between key signature/accidentals and meter/note duration)
  • Use the rsound library on Planet2 / pkg to play back individuals notes and chords
  • Tie it all together to play a parsed song in ABC notation using the rsound library
  • (maybe): Use the rsound library to save ABC files as WAV audio
  • (maybe): Figure out the format and save ABC files as MIDI
  • (maybe): Render songs as music sheets/li>

Sounds like fun! Let’s get started.

read more...


Functions as lists

Yesterday’s challenge from Programming Praxis challenges us to rebuild a data structure near and dear to any Lisper’s/Schemer’s/Racketer’s1/functional programmer’s heart: lists. The idea presented in their sample solution uses two element vectors, directly mimicking the general internal structure of Scheme’s lists. How about we do something a bit stranger? 😄

read more...


Extending Racket structs to bitfields

Keen eyed observers may have noticed that last Friday when I posted about converting my various Racket libraries to Planet 2 packages, that there was a new package there I haven’t otherwise talked about: bit-struct. Today seems like a good time to talk about that. Theoretically, I’ll also have another post or two this week showing exactly what I’m doing with it (spoilers: it involves sending on the order of billions of DNS requests1).

read more...


Deploy Racket libraries to Planet 2

Over the last few years, I’ve managed to write a fair bit of Racket code. It’s a lovely language and an even better ecosystem for writing quick/clean code (batteries included! ). Several times throughout that, I’ve written code that I thought could be useful to others, including several libraries that I used to write my Racket Roguelike and code that I’ve used working with the C211 class at Indiana University. I’ve always said that I should turn these into packages that others can use and… I’ve finally figured it out.

read more...


Smallest consecutive four-factor composites

Another post from Programming Praxis, from this past Tuesday:

The smallest pair of consecutive natural numbers that each have two distinct prime factors are 14 = 2 * 7 and 15 = 3 * 5. The smallest triplet of consecutive natural numbers that each have three distinct prime factors are 644 = 2^2 * 7 * 23, 645 = 3 * 5 * 43 and 646 = 2 * 17 * 19. What is the smallest set of four consecutive natural numbers that each have four distinct prime factors?

read more...


Visualizing the Monkey Grid

I’m a bit behind the times, but this post from Programming Praxis intrigued me enough that I kept it in my todo list for rather a while. So let’s get around to it.

I’ll just copy the description straight from the Programming Praxis website (although there are at least two previous version:[1][2]):

There is a monkey which can walk around on a planar grid. The monkey can move one space at a time left, right, up or down. That is, from (x, y) the monkey can go to (x+1, y), (x-1, y), (x, y+1), and (x, y-1). Points where the sum of the digits of the absolute value of the x coordinate plus the sum of the digits of the absolute value of the y coordinate are lesser than or equal to 19 are accessible to the monkey. For example, the point (59, 79) is inaccessible because 5 + 9 + 7 + 9 = 30, which is greater than 19. Another example: the point (-5, -7) is accessible because abs(-5) + abs(-7) = 5 + 7 = 12, which is less than 19. How many points can the monkey access if it starts at (0, 0), including (0, 0) itself?

read more...