# Writing music with code

Admit it: You’ve been searching for a way to write music using a text editor!

You’ve used musical notation software long enough to be sick of its slow, sprawling screens of endless notes. And even if you haven’t, it must strike you as odd that we slavishly attempt to emulate pen and paper notation with our keyboards and micies.

Recently, I came across one language which bridges the gap between computing and musical notation.

Setup instructions

Meet Alda

ocarina: o4 f8 > a b4 o4 f8 > a b4
<!-- TRANSLATION: ocarina set to 4th octave, play f in set octave for 1/8th (quaver),
shift up one octave, play b in set octave for 1/4th (crotchet). Repeat -->

Alda transforms the text we type into audible music for our ear holes!

In the simplest case, we can write a series of notes in a file to quickly hear how an idea sounds. When we want to get trickier, we can do fun things like setting markers in our notation to loop back to, and setting the tempo, feel, or key of a piece of music.

Piano, drums, ocarina (for those who want to rewrite Zelda songs), it can do it all.

For those who would like to read about its history, the creator of Alda has written an extensive blog post describing it and its design goals.

## An example song using Alda

percussion "drums": (volume 100)
piano: (volume 0)
ocarina: (volume 0)

drums: o3 c2 c2
piano: o2 f1/>a/c
ocarina: o4 f8 > a b4 o4 f8 > a b4

drums: o3 c2 c2
piano: o2 f1/>a/c
ocarina: o4 f8 > a b4 o4 f8 > a b4

## Shaking it up with Bitcoin

I’ve always dreamed of generating music using live data. The problem was that the creative pipeline was difficult to imagine until I heard of languages like Alda.
It’s possible to play sounds by interacting with your computer’s audio stack from code, but there is no notation created, and while you could create midi files programmatically, there’s a lot of overhead.

Now, using Alda and a little knowledge of chaining programs together, it can be a reality.

To do this, I have started with some python code. Python has some easy-to-use libraries for fetching currency data. Once we have the data in memory, we sample it at a few different timescales, to create sets of numbers to drive our pitch, duration and volume.

Then, we convert the raw numeric data into meaningful musical values and spew the results into a text file.

'''
insufficient
'''

# Function below handles turning prices into notes
def generate_instrument(prices, sample_count, duration):
rate = len(prices) // sample_count
samples = [prices[i * rate] for i in range(sample_count)]
low, high = min(samples), max(samples)
diff = high - low
pitches = [
int(((s - low) / diff) * KEY_COUNT)
for s in samples ]
return [ pitch_to_note(p, duration) for p in pitches ]

# Using the above function multiple times with different sample rates
piano = generate_instrument(prices, 32, 16)
bass = generate_instrument(prices, 8, 4)
drums = generate_instrument(prices, 8, 4)

# Print the results to standard output or write them to a file

The result:

piano: o2 c16 o2 c16 o2 c16 o2 c16 o2 c16 o2 d16 o2 d16 o2 c16 o2 d16 o2 f-16 o2 a16 o2 a16 o2 a16 o3 e-16 o3 d16 o3 g16 o4 f-16 o5 f-16 o7 c16 o6 c16 o4 b-16 o5 c16 o4 e-16 o5 e-16 o4 g16 o4 e-16 o4 c16 o4 e-16 o4 f-16 o4 d16 o4 d16 o3 b-16
midi-bird-tweet: o2 c4 o2 d4 o2 e-4 o3 e-4 o6 d4 o7 c4 o6 e-4 o6 d4
percussion: o2 c4 o2 d4 o2 e-4 o3 e-4 o6 d4 o7 c4 o6 e-4 o6 d4

We can then pass this file into Alda to hear our crypto-cacophony.

## Try it yourself!

• Follow the Basic setup instructions