The Syntax of Strudel

The Syntax of Strudel

Let's take a moment to understand the syntax of Strudel.

In Strudel, we always find commands in this form: something(). For example, sound(), stack(), cat(). This is called functions. However, since that word may sound very technical, you can call them machines or programs.

For example, think of sound() as a machine that will transform whatever you put inside the parentheses into sound: sound("bd bd bd bd"). Similarly, think of cat() as another machine. Whatever you put inside the parentheses will concatenate it so that one thing sounds after another. In summary, they are machines that do the work for us.

When we pass more than one thing to these machines (like in stack or cat), we separate each of them with commas.

stack(
  s("bd - bd -"),
  s("- sd - sd"), 
  s("[hh hh] [hh hh] [hh hh] [hh hh]")
 )

It doesn't matter if you use line breaks or tabs. You can organize the code in whichever way makes it clearer for you. We could write everything in a single line if we wanted to.

stack(s("bd - bd -"), s("- sd - sd"), s("[hh hh] [hh hh] [hh hh] [hh hh]"))

Visualizing Your Creations:

Do you want to see your beat in a more visual way? Add _pianoroll({ labels: 1 }) on any line.

What does labels: 1 do? It displays the instrument name in the visualization. If you use labels: 0 or simply _pianoroll(), you will hide the names.

You can also add a color to each instrument:

The Other Strudel Syntax

In the previous examples, you have seen the other syntax of Strudel: the "functions" with a dot (.).

You have seen .color() and ._pianoroll(). They are the same "machines" that do things for us, just written in a slightly different way. Sometimes this form is more convenient.

It's the same to write sound("bd bd bd bd") as to write "bd bd bd bd".sound().

This syntax helps us in complex situations. For example, imagine we want to apply several transformations. Without the dot, we would have to nest functions within each other: pianoroll(color(sound("bd"), "blue")) - hard to read.

With the dot, we can write:
sound("bd").color("blue").pianoroll() - much clearer, right?
It's like an assembly line, what comes out of one machine goes to the next: "Play the bd sound, then assign it the color blue, and visualize it in the piano roll.

Simply explained: Instead of passing everything inside the parentheses, the dot (.) tells the machine: "Take what is before the dot and work with that.

In the end, it's all the same: machines that work with the information we give them, whether it's inside the parentheses or before the dot.

When things go wrong...

Later, we will see how to simplify this syntax. For now, it's easy to forget a comma or a parenthesis. When this happens, nothing plays when you hit play. Don't worry! Strudel helps you with a hint.

Strudel displays an error message at the bottom of the screen with the line number where the problem is: Error: Unexpected token (7:1).

The message isn't very clear, I know. But the last two numbers (7:1) indicate where to look: line 7, character 1.

In the next lesson, we return to drum rhythms and will see some real examples. 👉>>