pxt-calliope/docs/reference/music/start-melody.md
Amerlander 918af4f3ac
Bump V3.0.22 (#110)
* change simulator svg

* change radio image

* Remove google fonts cdn

* change color of 'advanced' button

* font fix

* font fix 2

* display fix

* change fullsceen simulator bg

* Continuous servo

* handle continuous state

* adding shims

* update rendering for continuous servos

* fixing sim

* fix sig

* typo

* fix sim

* bump pxt

* bump pxt

* rerun travis

* Input blocks revision

- add Button and Pin event types
- merge onPinPressed & onPinReleased in new onPinEvent function
- create new onButtonEvent function

* update input blocks in docs and tests

* remove device_pin_release block

* Hide DAL.x behind Enum

* bring back deprecated blocks, but hide them

* shims and locales files

* fix input.input. typing

* remove buildpr

* bump V3

* update simulator aspect ratio

* add Loudness Block

* revoke loudness block

* Adds soundLevel

To be replaced by pxt-common-packages when DAL is updated.

* Remove P0 & P3 from AnalogPin

Co-authored-by: Juri <gitkraken@juriwolf.de>
2020-09-08 02:04:25 -07:00

3.2 KiB

start Melody

Start playing a musical melody through pin P0 of the @boardname@.

music.startMelody(music.builtInMelody(Melodies.Entertainer), MelodyOptions.Once)

~ hint

Simulator: This function only works on the @boardname@ and in some browsers.

~

There are built-in melodies that you can choose from the ||start melody|| block. These are already composed for you and are easy to use by just selecting the one you want. If you want to play your own melody, you can compose one and use it instead of one of the built-in ones.

Melodies are a sequence of notes, each played for some small amount time, one after the other. The notes in a melody are held in an array of strings. Each string in the array is a note of the melody. You make a melody by assembling the notes along with the duration that the note plays for. The melody is formed like this:

NOTE[octave][:duration] eg: ['g5:1']

music.startMelody(['g4:1', 'c5', 'e', 'g:2', 'e:1', 'g:3'], MelodyOptions.Once)

Melodies are played either in the foreground or background. This allows more than one melody to be active at once. If a melody is set to play in the background, it can be interrupeted, or paused, temporarily while a melody set for the foreground is played. If the foreground melody is not set to play forever, then the background melody resumes when the foreground melody is finished.

You can set options for how you want the melody to play. You can ask that the melody plays just one time, once, or have it keep repeating, forever. With these options the melody will play in the foreground either once or continue to repeat. Of course, if you set forever, any melody that was started in background will never play unless you stop the foreground melody. To make a background melody, set the option to once in background or forever in background.

Parameters

  • melody: A built-in melody or an array representation of a melody you wish to play.
  • options: the play option for the melody:
  • once: play the melody in the foreground one time
  • forever: play the melody in the foreground and keep repeating it
  • once in background: play the melody in the background one time
  • forever in background: play the melody in the background and keep repeating it

Examples

Play the "Entertainer"

This example plays the Entertainer built-in melody.

music.startMelody(music.builtInMelody(Melodies.Entertainer), MelodyOptions.Once)

Play a composed melody forever

Play a made-up melody in the background forever.

music.startMelody(['g4:1', 'c5', 'e', 'g:2', 'e:1', 'g:3'], MelodyOptions.ForeverInBackground)

See also

stop melody, play tone, rest, ring tone, tempo, set tempo, change tempo by

Making Melodies