A simple melody

“Twinkle, Twinkle, Little Star”, written bar by bar. Four bars, each its own sequence, played one after another.

  • Beginner
  • C major
  • 100 BPM
  • 4 bars

You will learn

  • Write a melody longer than one bar with sequence, weight and fn('cat', …)

Write each bar

Each bar is a sequence of four beats. Bars 2 and 4 end on a note that lasts two beats, so it gets weight(…, 2) and the bar has three entries instead of four — still four shares in total.

const bar1 = sequence(pitch('c4'), pitch('c4'), pitch('g4'), pitch('g4')) // Twin-kle twin-kle
const bar2 = sequence(pitch('a4'), pitch('a4'), weight(pitch('g4'), 2)) // lit-tle star (held)
const bar3 = sequence(pitch('f4'), pitch('f4'), pitch('e4'), pitch('e4')) // how I won-der
const bar4 = sequence(pitch('d4'), pitch('d4'), weight(pitch('c4'), 2)) // what you are (held)

Twinkle melody

Ready

Play the bars in order

fn('cat', …) plays its patterns one after another, one per cycle. Four bars take four cycles, then the melody loops. Without cat, putting all four bars in one sequence would squeeze them into a single cycle.

export const twinkle = chain(
  // cat plays one pattern per cycle: bar 1, then 2, 3 and 4.
  fn('cat', fn('note', bar1), fn('note', bar2), fn('note', bar3), fn('note', bar4)),
  fn('instrument', sample('triangle')),
  fn('gain', 0.3),
  fn('cpm', 25), // 100 BPM ÷ 4 beats per bar.
)

Twinkle, Twinkle

Ready

Long notes or short ones

The held notes can become a short note followed by silence. The bar keeps four shares, so nothing after it moves — compare the two players and listen to the space before bars 3 and 1.

// Bars 2 and 4 again: the held note becomes a short note and a rest.
// Each bar still has four shares, so the timing of the next bar is unchanged.
const bar2Detached = sequence(pitch('a4'), pitch('a4'), pitch('g4'), rest())
const bar4Detached = sequence(pitch('d4'), pitch('d4'), pitch('c4'), rest())

export const twinkleDetached = chain(
  fn('cat',
    fn('note', bar1),
    fn('note', bar2Detached),
    fn('note', bar3),
    fn('note', bar4Detached),
  ),
  fn('instrument', sample('triangle')),
  fn('gain', 0.3),
  fn('cpm', 25),
)

Detached ending

Ready