Skip to main content

State Machines

To start making music with Flow, you need to grasp the core idea behind it: a state machine. It's a simple but very powerful concept that you will love.

Flow uses an advanced state machine kind that has three special features: it's probabilistic, it has hierarchical structure, and it is parallel. These features make Flow versatile and expressive for creating generative music.

Don't worry if this sounds complicated. We will explain everything in detail below.

What is a State Machine?

Widely used in many fields such as philosophy, biology, computer science, engineering, linguistics and more, a state machine is a way of modeling various objects and systems that can be in different states and transition between these states.

A simple example of a system that can be represented with a state machine is a toaster. A toaster has two main states: Idle and Toasting. When a user presses the lever, the toaster transitions from Idle to Toasting. When the timer expires, the toaster transitions back to Idle. The diagram below shows how this process can be modeled with a state machine:

State machine representation

A state machine can be represented with two different ways:

  • A graphical diagram that shows the states and the transitions between them. Each rectangle represents a state, and each arrow represents a transition from one state to another. The arrow can have a label that indicates the input or event that triggers the transition.

  • A matrix in which each row and column represent a state, and each cell represents a transition from the row state to the column state. The cell can have a value that indicates a transition from one state to another, or it can be empty if there is no transition.

You can switch between these views by clicking the Diagram or Matrix tabs.

State machine representing a toaster

This state machine is pretty simple: it has only two states, and each state has exactly one possible transition. Its next state is uniquely determined by its current state and input, which makes a state machine completely predictable, or deterministic.

Deterministic state machines can have more than one possible transition for each state, and follow one of them depending on the events or inputs.

For example, an alarm clock with the snooze function has three states: Idle, Ringing, and Snooze. The transitions between these states depend on the input signals from the clock, the buttons, or the buzzer. A single state can have multiple transitions leading to or from it, depending on the input signals and the conditions. The following state machine captures the possible transitions for different alarm clock scenarios:

State machine representing an alarm clock

This state machine is still deterministic because its next state is completely determined by its current state and inputs.

Nondeterminism and Probabilities

Some objects and systems may behave in unpredictable ways, but still can be modeled with state machines. A state machine can define more than one transition for a given state and follow any transition randomly: such state machines are called nondeterministic. A state machine can also have probabilities associated with each transition, and choose between them according to the probability distribution. Such state machines are called probabilistic.

One example of a system that can be modeled with a probabilistic state machine is the weather. The weather can have different states, such as Sunny, Cloudy, Rainy, and so on. The transitions between these states can depend on various factors, such as temperature, humidity, wind, etc. However, these factors are not always predictable or observable, so we can simply assign some probabilities to the transitions based on historical data or meteorological models. Such state machine can be used to predict the likelihood of different weather conditions at a given time and location in the future:

Probabilistic sate machine representing a weather

This state machine captures that the weather can change in unpredictable ways, leading to different possible outcomes. For example, a sunny day can turn into an overcast evening:

Sunny day with some clouds in the evening

Another day might begin with sunshine, but soon turn cloudy and rainy:

Cloudy and rainy day

Nondeterministic and probabilistic state machines are powerful tools for modeling complex behaviors that involve uncertainty or randomness. However, the examples we have seen so far are simple and flat, meaning that each state is a single unit, atomic and indivisible.

Hierarchical Structure

A state machine can have complex high-level states that are also state machines themselves. This creates a hierarchy of states within the system.

For example, think of a vending machine. It can have several high-level states, and one of its states, Payment Processing, can be modeled with the state machine itself:

Hierarchical state machine representing a vending machine
Initial and final states

The high-level Payment Processing state is a state machine itself, which has its own initial and final states. The initial state is marked with a solid circle (⬤), and the final state is marked with a hollow circle (⦿). These markers tell the parent state machine which state to enter when it transitions to the nested Payment Processing state, and which state to exit from when it leaves the nested state.

This structure can be further refined by using more nested state machine layers — for example, the Payment Validation state itself can be a state machine that depends on the payment method: coins, bills, or card.

A hierarchical state machine can be in more than one state at the same time, at different levels of the hierarchy. For example, the highest level state might be Payment Processing, the next level state is Payment Validation, and the lowest level state is something like Checking Bank Account.

Hierarchical state machines are more expressive and succinct than non-hierarchical ones. However, we can improve them even further.

Parallelism

A parallel state machine is a state machine that can have multiple parts, each of which is a state machine itself operating independently and simultaneously. A parallel state machine can be in more than one state at a time, one for each part.

For example, consider the following parallel state machine that models a car and its subsystems — engine, transmission and brakes:

Parallel state machine with three individual parts representing a car driving process

The state machine can be in any combination of states from the different parts, such as

  • Engine: Off;
  • Transmission: Park;
  • Brakes: Released,

or

  • Engine: Running;
  • Transmission: Drive;
    • Drive: D2;
  • Brakes: Applied.

Parallel state machines can be useful to model complex systems that have multiple aspects or modes of behavior that are independent of each other.

Summary

That's it! You've learned what a state machine is, and how it can have nondeterministic and probabilistic features, hierarchical structure and multiple parts running in parallel.

You may be wondering, how does this state machine idea relate to creating music? Let's find out!