
The Strudel Syntax
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 too technical, you can refer to them as machines or programs.
For example, think of sound()
as a machine that transforms whatever you put inside the parentheses into sound: sound("bd bd bd bd")
. Similarly, think of cat()
as another machine. What 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 one 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 spaces, tabs, or line breaks. You can organize the code however you prefer. We could write everything on 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 name of the instrument 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 differently. 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. Instead of having something like a(b(c(d())))
, we can write a().b().c().d()
. It's easier to read, isn't it?
Explained simply: Instead of passing everything inside the parentheses, the dot (.
) tells the function: "Take what is before the dot and work with that.
Example: Instead of passing our entire beat to ._pianoroll()
within the parentheses, the dot (.
) tells it: "Take the entire beat that is before the dot and make me a graph with it.
The same happens with s("bd - bd -").color("blue")
: "Take what is before the dot and assign it the color blue.
Compare these two forms:
s("bd - bd -").color("blue").pianoroll()
It's like an assembly line, what comes out of one machine goes to the next.pianoroll(color(s("bd - bd -"), "blue"))
In the case that we could write it this way, we see that it is much less readable.
In the end, it's all the same: machines that work with the information we provide, whether it's inside the parentheses or before the dot.
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. 👉>>