Music + Programming = Awesome!

07 Sep 2025

Source Code Strudel REPL

Making Music with Code Using Strudel

As both a software developer and a musician, I’ve always been fascinated by the overlap between programming and music. On the surface, they may seem very different, but both rely heavily on patterns, structure, and building blocks that can be combined in creative ways.

Music, for example, is often structured into sections: intro, verse, chorus, bridge, and so on. Each of those sections is made up of harmonies, rhythms, and melodies, which are themselves built from individual notes and instruments. In other words, music is modular: a song is constructed from smaller, reusable pieces.

Software works much the same way. Applications are built from reusable components, functions, and abstractions that can be arranged and layered together to form features.

So, what if you could use the expressive power of programming to compose music, breaking it down into smaller parts and reusing them with functions, variables, and patterns?

That’s exactly what Strudel makes possible.

What is Strudel?

Strudel is a JavaScript port of Tidal Cycles, a language for algorithmic music composition. With Strudel, you can write music as code live, in your browser, and hear the results in real time.

Let’s walk through how it works by building a simple beat, then layering chords, melodies, and song sections.

Building a Beat

Here’s what’s happening:

  • s() defines a sound. We’re using a hi-hat (hh), bass drum (bd), and snare (sd).
  • hh!8 means the hi-hat plays 8 times in a cycle. Think of a cycle like a musical measure.
  • gain() adjusts the volume of each sound.
  • beat("0,7,10", 16) tells the bass drum to hit on beats 0, 7, and 10 in a 16-beat cycle.

Already, you can hear a simple drum pattern taking shape.

Adding Variation

  • The angle brackets <> allow us to vary when a sound plays.
  • Decimals (like 7.1 or 10.2) slightly shift a beat off-grid, making the rhythm feel less mechanical.
  • ~ represents a rest (silence). For example, the snare skips the first 3 cycles before hitting on 14.

This is how we start moving from robotic repetition to something more human-sounding.

Introducing Notes

$: note("<f#3 g#3 a#3 g#3>").s("piano")
  • note() plays specific pitches.
  • <> cycles through notes one per measure. Without brackets, each note is played as a quarter note within the cycle.
  • Commas group notes into chords. For example:
$: note("<[f#3,a#3] [g#3,c4] [a#3,c#4] [g#3,c4]>").s("piano")

Now we’ve added harmonic texture to the rhythm.

Using Scales for Simplicity

Typing every note manually can get messy. Instead, we can use scales:

Now we can just use the numbers on the scale rather than having to try and remember the key. It also means we can switch our song between different keys.

In fact, we use the same notation to change the scale after each note is played!

We are now using *2 to play each chord twice each cycle and each time we are changing the octaves.

Reusing Parts with Variables

Because Strudel runs on JavaScript, we can define parts as variables and reuse them:

  • stack() plays multiple parts together.
  • arrange() sequences them over time.

This lets you build full song sections the same way you’d structure software modules.

A Full Song Example

Here’s a complete track I made in Strudel:

Closing Thoughts

Strudel shows just how naturally programming and music fit together. By thinking of music in terms of reusable components, functions, and transformations, you can create dynamic, evolving pieces and perform them live with code.

If you’re a programmer who loves music (or a musician who loves code), I highly recommend giving Strudel a try. You may be surprised at how expressive programming can be when applied to sound.

Source Code Strudel REPL

Recent Blogs

Fitness Fox

02 Sep 2025