# Your first pattern (10 minutes)

You have a sine wave DSP from § *Your first DSP*. It plays a steady 440 Hz tone. In this chapter we'll make it MIDI-responsive, then drive it from a sequencer pattern so it plays a melodic 4-bar loop instead.

## Step 1 — Make the DSP MIDI-responsive

Open your Builder from the previous chapter (Documents rail → click your DSP).

Right-click the canvas, search `midi_note`, and add it. The `midi_note` node has three outputs:

- `freq` — Hz, computed from the current note.
- `gate` — 1 when a note is on, 0 when off.
- `vel` — note velocity, 0..1.

Disconnect the cable between your osc and the Output node (right-click the cable → *Delete connection*). Now wire:

- `midi_note.freq` → `osc.freq` (purple cable — this drives the oscillator's frequency input).
- `osc.out` → a `gain` node → multiply by `midi_note.gate`.
- The `gain` output → `Output.in_l`.

This is the minimum MIDI-responsive shape: the gate gives a hard on/off, the freq picks the pitch, the velocity is available for later wiring. We're skipping an envelope for now to keep the graph small — see **FaustWave — Builder** for the full poly-synth recipe.

> 🟦 **NEW TO MIDI?** A MIDI note has three properties: a pitch (translated to frequency in Hz), a velocity (how hard the key was struck), and a gate state (key down / key up). The `midi_note` node exposes all three as Faust signals so your DSP can react. You don't need a physical MIDI keyboard — FaustWave's on-screen Keyboard counts as a MIDI source.

## Step 2 — Test it with the on-screen Keyboard

```yaml
actions:
  - action_id: surface.open
    input: { surface: keyboard-default }
    label: "▶ Open the Keyboard panel"
  - action_id: patchbay.toggle
    input: { open: true }
    label: "▶ Open the Patchbay"
```

Open the **Keyboard** rail panel (left rail, piano-keys icon under the MIDI sources area — or the first button above). You see a virtual keyboard.

Before clicking keys, route the keyboard to your Builder so it actually receives the notes:

1. Open the **Patchbay** rail panel (cable icon, left rail — or the second button above).
2. The Patchbay matrix shows Sources as rows and Sinks as columns, split into Audio + MIDI blocks.
3. Find your Builder's MIDI sink in the MIDI block — it's named after the Builder's `module_id`. (Don't see it? MIDI sinks register **while the DSP runs** — press Play or the Builder's RUN button once and the row appears.) Find the Keyboard source row — it's `keyboard-default`.
4. Click the matrix cell where they intersect. The cell fills purple — the connection is live.

Now click keys in the Keyboard panel. Each note plays your sine at that pitch as long as the key is held. If you don't hear anything: make sure the master transport is playing (the Play button in the topbar Transport Widget) — your Builder is a transport-follower by default and only runs when the master transport runs.

> 🟦 **WHY ROUTING IS EXPLICIT**: FaustWave's routing table is the single source of truth for who talks to whom. There's no implicit "all MIDI goes everywhere" — each connection is a deliberate cable in the Patchbay. This is what lets you have ten Builders open, each listening to a different sequencer track, without crosstalk. There's no bundled instrument and nothing is pre-routed for you — a fresh project starts with no MIDI sinks, so you always draw the line from a source (Keyboard / Sequencer) to your own Builder yourself. See **FaustWave — Patchbay** for the model.

## Step 3 — Open the Sequencer

```yaml
actions:
  - action_id: surface.open
    input: { surface: sequencer-default#roll }
    label: "▶ Open the Sequencer panel"
```

Open the **Sequencer** rail panel (grid-with-dot icon on the left rail under the MIDI sources section — or the button above).

You land on a multi-track piano-roll grid. The top row is a **chip row** — one chip per track with name, MIDI channel, and pattern-slot launcher (A / B / C / D). Below is the **grid** itself, with pitch rows on the left (gutter) and step columns running right.

A fresh sequencer starts with one track at MIDI channel 1, in C major. Pattern length is 16 steps (1 bar at 4/4).

## Step 4 — Pick a key and scale

Below the chip row, find the **Key + Scale** picker. Click to open it:

- **Key** — pick a tonic. Try `A` for a minor-key feel, or stick with `C`.
- **Scale** — pick a scale. `Aeolian` (= natural minor) is friendly for first-pattern experiments; `Pentatonic Minor` is even friendlier (no clashing intervals).

The grid now highlights in-scale rows in cyan. Out-of-scale rows are dimmer. You can still click them, but the visual hint is a real working aid: every note you draw on a highlighted row is "in key" by construction.

> 🟦 **NEW TO MUSIC THEORY?** A scale is a set of allowed pitches relative to a tonic — for example, A minor uses A, B, C, D, E, F, G (no sharps or flats). If you draw notes only on the highlighted rows, every note you play will sit in that scale. FaustWave's KB has a `theory` corpus with scale + chord references the AI can search; see **FaustWave — AI Assistant** for asking it *"what's a good progression in A minor"*.

## Step 5 — Draw your first 4 notes

The default mode is the **Paint tool** (pencil). Click on a step cell to place a note. Click an existing note to remove it. Drag vertically while painting to set velocity.

Place four notes:

- Step 1, row A (the tonic).
- Step 5, row C (the minor third).
- Step 9, row E (the fifth).
- Step 13, row A (an octave up if you like, or back to the same A).

You've written an arpeggio outlining the A minor chord across 16 steps.

> 🟦 **CABLE COLOURS RECAP**: in the sequencer grid the cells you paint aren't cables — they're step events. The cables (cyan, purple, orange) are the ones inside the Builder canvas you saw in the previous chapter.

## Step 6 — Route the sequencer to your Builder

Back to the Patchbay panel. Find `sequencer-default` on the MIDI Source rows (the sequencer's MIDI output) and your Builder's MIDI sink on the MIDI Sink columns. Click the cell where they meet.

Now the sequencer's MIDI events flow to your Builder. The Keyboard cable from earlier can stay; the Builder happily takes notes from either source.

## Step 7 — Play it

Press **Play** in the master Transport Widget (top centre). Three things happen at once:

1. The master transport flips to playing.
2. The shared Faust master clock starts ticking at the current BPM (default 120). You'll see the **Bar.Beat.Sixteenth** display in the widget count 1.1.1 → 1.1.2 → 1.1.3 → 1.1.4 → 1.2.1 → …
3. The sequencer's cursor walks across the steps; every time it hits a note you painted, it dispatches a MIDI event to your Builder, which plays the corresponding pitch.

You should hear your arpeggio looping. Drag the BPM display in the widget (click the number → type a new value → Enter) to change the tempo on the fly; the loop follows.

> 🟦 **THE FAUST MASTER CLOCK**: every sequencer (and the topbar's Bar.Beat readout) reads from one shared Faust DSP clock running at audio-thread priority. This is why there's no drift between multiple sequencers, no JS-timer jitter, and why the Bar.Beat counter is sample-accurate. See **FaustWave — Sequencer** § 1 for the full clock story.

## Step 8 — Try the chord palette

Stop playback (the same Play button toggles to Stop while playing).

To the right of the key/scale picker is the **Chord Palette** — a row of diatonic chord buttons. In A minor it shows: `i (Am)`, `ii° (Bdim)`, `III (C)`, `iv (Dm)`, `v (Em)`, `VI (F)`, `VII (G)`. Click any chord; the next click on the grid drops it as a stack of three steps (root + 3rd + 5th) all on the same step column.

Place a few chord stabs across the bar and play again. You've got a polychord progression in 30 seconds.

## Step 9 — Save the pattern

The sequencer auto-saves to your project's `project.json` on every change. There's no Save button — close the rail panel, close the app, reopen, and your pattern is exactly where you left it.

If you want the pattern as a portable file (to send to a friend or import into another project), use the AI Assistant or the MCP `sequencer.state` action to dump the pattern as JSON — community-shareable, also useful for git history.

## What you've built

- A Builder DSP that plays MIDI-driven sine tones.
- A routed signal flow: sequencer → Builder → master → speakers.
- A scale-aware 4-step arpeggio + a few chord stabs in A minor.
- A grasp of the global transport, the shared Faust clock, and routing as deliberate cables.

## Where to go from here

- Add a second track at a different MIDI channel for a bass line; learn solo/mute. See **FaustWave — Sequencer** § 2.
- Build a proper poly-synth (envelope, filter, multi-voice) so the notes don't sound like a square wave with no attack. See **FaustWave — Builder**.
- Capture the loop to a WAV file. Continue to § *Your first recording* in this book.
- Ask the AI to suggest a progression in your key, or to mutate the pattern. See **FaustWave — AI Assistant**.
