Making Music with Code: A Beginner’s Guide to Strudel
Hello friends, and welcome to another creative coding tutorial!
Most of the time on this blog we explore generative art and creative coding with visuals — using tools like p5.js to draw shapes, animate patterns, or generate abstract compositions.
But code isn’t limited to images.
It can also generate sound and music.
In this tutorial, we’re going to explore Strudel, a creative coding library that lets you create music directly in the browser using code.
By the end of this article you will:
- understand the core ideas behind Strudel
- create simple drum rhythms with code
- generate melodies using patterns
- experiment with tempo, silence, and variation
Just like generative visuals, the goal here isn’t just to write music — it’s to design systems that produce music.
Let’s dive in.
What Is Strudel?
Strudel is a browser-based music live coding environment.
You can find the online editor together with documentation at https://strudel.cc/
Instead of opening a traditional music production program and arranging notes on a timeline, you describe music using patterns written in code.
These patterns generate sound events over time.
If you’ve explored generative art before, the idea will feel very familiar:
Instead of drawing shapes manually, you write rules that generate shapes.
With Strudel, instead of placing notes manually, you write rules that generate music.
The result is a very playful workflow:
- Write a short piece of code
- Run it
- Hear the result instantly
- Modify it and experiment
Let’s start with the simplest possible example.
Your First Sound
The easiest way to produce sound in Strudel is with the sound() function.
Here is a minimal example:
When you run this, Strudel plays a built-in sound sample called casio.
Strudel includes a library of sounds and instruments that you can trigger with simple text labels.
Try replacing "casio" with another sound.
For example:
Already we can see the philosophy behind Strudel: simple code that produces immediate feedback.
Understanding Patterns
The most important concept in Strudel is the pattern.
A pattern describes how events happen over time.
In music, events could be:
- a drum hit
- a note
- a sample
- a sound effect
In Strudel, patterns are written as space-separated sequences.
For example:
Let’s break this down.
The string inside sound() contains four elements, each one triggers a sound.
Common drum abbreviations include:
bd→ bass drumsd→ snare drumhh→ hi-hat
Strudel compresses the entire sequence into something called a cycle.
A cycle is simply a repeating unit of time.
Once the sequence finishes, it loops again.
This means a single line of code can generate a repeating rhythm indefinitely.
Changing the Rhythm
One of the fun parts about working with patterns is how easy they are to modify.
If we add more elements to the sequence, the rhythm becomes denser.
More events in the same cycle means the rhythm plays faster and more complex.
This is one of the core ideas behind algorithmic music:
Small textual changes can create very different musical results.
Controlling Tempo
So far our rhythms are looping automatically, but what if we want to control how fast they play?
In Strudel, tempo is measured in cycles per minute.
We control this with the setcpm() function.
Example:
Here we slow the rhythm down slightly.
Because Strudel organizes music around cycles instead of traditional BPM, it becomes very easy to stretch or compress patterns.
Think of cycles like the grid that music sits on.
Everything in the system follows the same timing structure.
Adding Silence
Music isn’t just about sound, silence is equally important.
In Strudel we can insert pauses using ~.
Example:
Here the third step in the pattern is silent.
These small gaps create breathing room in the rhythm and make patterns feel more musical.
Nested Rhythms
Strudel also allows nested patterns.
This means we can place smaller patterns inside larger ones.
For example:
The brackets tell Strudel to play the enclosed elements faster inside the same step.
So instead of a single hi-hat hit, we get two.
This is a very powerful idea because it lets us build complex rhythmic structures in a very compact way.
Playing Notes Instead of Samples
So far we’ve been triggering drum sounds and samples.
But Strudel can also generate pitched musical notes, which allows us to create melodies and harmonic patterns.
To do this, we use the note() function.
Here is a simple example:
Let’s break down what is happening here.
The numbers represent MIDI note values.
In the MIDI system, every musical note corresponds to a number:
60→ Middle C48→ C in a lower octave67→ G
So this sequence creates a small melodic pattern.
The .sound() function tells Strudel which instrument should play those notes. In this case, we chose a piano sound.
Using Note Names Instead of Numbers
While MIDI numbers are useful, they can feel a bit abstract if you’re used to thinking about music in terms of note names.
The good news is that Strudel also lets you write notes using musical notation.
For example:
Here we’re describing notes the same way musicians usually do, each note contains two pieces of information:
- the note name (C, D, E, F, G, A, B)
- the octave number - this is optional
For example:
C4→ middle CC5→ the C one octave higher
This approach is often easier to read when writing melodies because you can recognize musical relationships more quickly.
Try modifying the pattern and see what happens.
Maybe something like:
C4 D4 E4 G4 or A3 C4 E4 A4
Small changes like this can produce completely different musical moods.
Layering Instruments
Another nice feature of Strudel is that we can layer multiple instruments.
Example:
Here two instruments play the same notes at the same time.
Layering sounds like this is a simple way to create richer textures.
A Small Generative Groove
Now that we’ve explored rhythms, tempo, and melodies, let’s combine everything into a small generative groove.
We’ll create three musical layers:
- a drum rhythm
- a bass line
- a simple melody
Each layer is just a pattern, and Strudel plays them together in sync.
Let’s break this down.
1️⃣ The Drum Pattern
sound("bd ~ sd ~ bd ~ sd ~")
This creates a simple kick and snare rhythm.
bd→ bass drumsd→ snare drum~→ a pause
The pauses give the rhythm space and make it feel more musical.
2️⃣ The Bass Line
note("C2 ~ C2 G1").sound("gm_acoustic_bass")
Here we generate a repeating bass pattern.
We use lower notes (C2, G1) so the bass sits underneath the drums.
3️⃣ The Melody
note("C4 E4 G4 B4").sound("gm_lead_6_voice")
This creates a simple melodic sequence using higher notes.
Because Strudel loops patterns automatically, the melody keeps repeating and interacting with the bass and drums.
Why This Works
The key function here is stack().
stack() tells Strudel to play multiple patterns at the same time.
You can think of it like layering tracks in a music production program:
- one track for drums
- one for bass
- one for melody
Except here we’re describing the entire groove in just a few lines of code.
Try Experimenting
This is where creative coding becomes fun.
Try changing things like:
• the drum pattern
• the bass notes
• the melody
• the tempo
Even small tweaks can create completely new musical ideas.
For example:
- change the melody to
C4 D4 E4 G4 - add more hi-hats to the drums
- slow the tempo down
You might be surprised how quickly new grooves start to emerge.
Why Strudel Is Interesting for Creative Coding
If you enjoy generative art, Strudel fits naturally into the same mindset.
Both systems rely on similar ideas:
• patterns
• repetition
• randomness
• small rules producing complex results
Just like generative visuals, the most interesting pieces often emerge from simple systems interacting with each other.
And that’s what makes creative coding so fun.
You’re not just composing music.
You’re designing a system that composes music for you.
Wrapping Up
Congratulations! 🎉
In this tutorial we explored the basics of Strudel and musical creative coding.
We looked at:
- triggering sounds
- building rhythmic patterns
- controlling tempo
- adding silence
- generating melodies
If you enjoyed this tutorial, try expanding the examples and building your own generative music sketches.
You can combine these ideas with other creative coding tools — imagine pairing Strudel music with p5.js generative visuals for a full audiovisual piece.
Experiment, tweak the patterns, and see what you can create.
Happy coding, and see you in the next tutorial! 🚀