Your first song

Play a note, line up a few more, shape their rhythm and set the tempo. Every example on this page is the code that plays.

  • Lesson 1
  • 10 minutes

You will learn

  • Play a note with pitch and fn('note', …)
  • Put notes in a row with sequence
  • Shape rhythm with weight and rest, and how that differs from repeat
  • Set the speed with cpm

Play one note

A song is built from patterns, and the smallest pattern is a single note. Start with middle C:

export const oneNote = chain(
  fn('note', pitch('c4')), // Middle C.
  fn('instrument', sample('triangle')), // A soft built-in synth.
  fn('gain', 0.3), // Loudness: keep it low while you experiment.
)

One note

Ready

Read it from the inside out:

  • pitch('c4') names the note C in the fourth octave. Lower numbers are lower octaves.
  • fn('note', …) turns that name into a pattern Strudel can play. It plays once per cycle — one bar, for now.
  • chain(…) adds settings to the pattern: instrument picks a built-in synth, and gain sets loudness, from 0 (silent) upward.

fn is checked by TypeScript as you type, so a misspelled function name shows up in your editor instead of as silence.

Put notes in a row

sequence(…) plays its entries one after another and splits the bar equally between them. Four notes means each gets a quarter of the bar.

export const fourNotes = chain(
  fn('note', sequence(
    pitch('c4'),
    pitch('e4'),
    pitch('g4'),
    pitch('b4'),
  )),
  fn('instrument', sample('triangle')),
  fn('gain', 0.3),
)

Four notes

Ready

Add a fifth note and every note gets shorter, because the bar stays the same length. A sequence divides time; it does not add time.

Hold a note, then rest

Equal shares make every note the same length. weight(…, 2) gives one entry two shares instead of one, and rest() takes a share and stays silent.

export const longAndRest = chain(
  fn('note', sequence(
    weight(pitch('c4'), 2), // Two shares of the bar: a longer note.
    pitch('e4'), // One share.
    rest(), // One share of silence.
  )),
  fn('instrument', sample('triangle')),
  fn('gain', 0.3),
)

Long note and rest

Ready

Count the shares: 2 + 1 + 1 = 4. C lasts half the bar, E a quarter, and the last quarter is silence. Weights are relative, not seconds: weight(…, 2) means “twice as long as a share”, whatever a share happens to be.

// C gets two of four shares — half the bar — and plays once.
export const held = chain(
  fn('note', sequence(weight(pitch('c4'), 2), pitch('e4'), pitch('g4'))),
  fn('instrument', sample('triangle')),
  fn('gain', 0.3),
)

One held C

Ready
// Same half bar for C, but repeat() fits two strikes inside it.
export const repeated = chain(
  fn('note', sequence(weight(repeat(pitch('c4'), 2), 2), pitch('e4'), pitch('g4'))),
  fn('instrument', sample('triangle')),
  fn('gain', 0.3),
)

Two quick Cs

Ready

Set the tempo

Every example so far has played at the same speed: one bar every two seconds. That comes from cpm, cycles per minute. On this page one cycle is one bar.

// cpm = cycles per minute. One cycle is one bar here,
// so 30 cpm plays one bar every two seconds.
export const withTempo = chain(fourNotes, fn('cpm', 30))

Musicians count beats per minute (BPM) instead. With four beats in a bar, divide BPM by 4 to get cpm: 120 BPM is 30 cpm. Raise cpm and the whole pattern speeds up; the shares stay in proportion.

Make it yours

Here everything you just learned is one function. Pick four notes or rests, a sound and a tempo. Changes apply while it plays; open Strudel output to see the values that are playing now.

Your tune

Four slots, one bar, looping.

Ready
SlowerFaster
export const myTune = ({ slots, sound, bpm }: TuneSettings) => chain(
  fn('note', sequence(
    // Each slot is a note name or 'rest'.
    ...slots.map((slot) => slot === 'rest' ? rest() : pitch(slot)),
  )),
  fn('instrument', sample(sound)),
  fn('gain', 0.3),
  fn('cpm', bpm / 4), // Four beats per bar: BPM ÷ 4.
)

Recap

  • A cycle is the unit of time. Here, one cycle is one bar.
  • sequence splits a cycle into equal shares; weight changes how many shares an entry gets.
  • rest is a silent share. repeat fits more strikes into one share.
  • cpm sets how many cycles play per minute: BPM ÷ 4 for four beats per bar.

Practise with A simple melody, which adds cat to play several bars in a row. When you’re ready for drums, start with the Kick in the Sound Cookbook and the Four on the floor example. Dance Monkey puts all of it together.