Lesson goal: Playing notes

Previous: Introduction | Home | Next: Play a sine sum

Frequencies for sounds can be inconvenient to figure out, as in the last lesson. So, we also have a function called music() that allows you to play sequences of notes, referenced by their name, like C, A, or Bb (B "flat").
music("note1,note2,...,noteN")
Move the mouse over a dotted box for more information.

In sum, just put a bunch of notes in a string, and separate each by a comma. There are a few special cases of the "notes" too:
  • Ln, where n is a number (like 0.1, 1, 0.05, etc.) specifying how long (in seconds) the notes that follow should be held for. Example: music("L0.1,A,B,C,L0.5,A,B,C")
    play()
  • On, where n is a number (0, 1, 2..8), and "O" is an "oh" (not a zero). The number you put in for n stands for an octave that all subsequent notes should be drawn from. Middle C is in octave 4. Example:
    music("O4,C,O5,C,O6,C")
    play()
    .
  • Pn, where n is the number of seconds to pause for (this would be a "rest" in music). Example:
    music("A,P0.5,A,P0.5,A,P0.5")
    play()

Now you try. Try writing some simple songs!

Type your code here:


See your results here:

See example #1 for "Ode to Joy." Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.