# Genuary 2023.10: Generative Music

Spend a month making one beautiful thing per day, given a bunch of prompts. A month late, but as they say, ’the second best time is now'.

Let’s do it!

## 10) Generative music

Wikipedia: Generative music

NOTE: Because of limitations in JavaScript autoplaying sound, you must click to start.

NOTE: This currently doesn’t work in Safari. Something funny with the p5.sound addon. I’m working on it.

# AoC 2017 Day 18: Duetvm

### Source: Duet

Part 1: Create a virtual machine with the following instruction set:

• snd X plays a sound with a frequency equal to the value of X
• set X Y sets register X to Y
• add X Y set register X to X + Y
• mul X Y sets register X to X * Y
• mod X Y sets register X to X mod Y
• rcv X recovers the frequency of the last sound played, if X is not zero
• jgz X Y jumps with an offset of the value of Y, iff X is greater than zero

In most cases, X and Y can be either an integer value or a register.

What is the value recovered by rcv the first time X is non-zero?

# Making music, part 3: Making noise

Last week we parsed some music. That post was in a bit of a hurry, so we had to leave off a fair few important pieces (like ties and slurs for one; chords for a rather bigger one). We’ll get to them soon, but for now we want to actually get something playing back.

# Making music, part 2: Taking shape

It’s been a bit, but as you may have noticed life is a bit mad at the moment. But I’ve still made some progress.

When we left off last time, we’d finished the first step towards making some lovely music with Racket: tokenization. Now we want to take those songs and form them into something actually approaching music.

# 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.