|
1 | 1 | # play
|
2 | 2 |
|
3 |
| -Play a sound expression. |
| 3 | +Play a song, melody, tone, or a sound effect from a playable music source. |
4 | 4 |
|
5 | 5 | ```sig
|
6 |
| -soundExpression.giggle.play() |
| 6 | +music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone) |
7 | 7 | ```
|
8 | 8 |
|
9 |
| -A sound expression is a preformatted set of tones that create a certain sound. There are several sounds to choose from. The sound is started and your program then continues. |
| 9 | +Music is played for a simple tone, a melody, or a song. Each of these music sources is called a [playable](/types/playable) object. The ``||music:play||`` block can take any of these playable objects and play them as sound output for your game. |
10 | 10 |
|
11 | 11 | ### ~ reminder
|
12 | 12 |
|
| 13 | +#### For micro:bit v2 only |
| 14 | + |
13 | 15 | 
|
14 | 16 |
|
15 | 17 | This block requires the [micro:bit V2](/device/v2) hardware. If you use this block with a micro:bit v1 board, you will see the **927** error code on the screen.
|
16 | 18 |
|
17 | 19 | ### ~
|
18 | 20 |
|
19 |
| -## Parameters |
| 21 | +The simplest music source is a **tone**, on note play for a duration of time: |
| 22 | + |
| 23 | +```block |
| 24 | +music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone) |
| 25 | +``` |
20 | 26 |
|
21 |
| -In blocks, the sound is selected from the list in the ``||music:play sound||`` block. |
| 27 | +Then, there is the **melody** which is a series of notes played at a certain speed, or `tempo`. You can create your own melody of choose a built-in one to play: |
22 | 28 |
|
23 | 29 | ```block
|
24 |
| -soundExpression.giggle.play() |
| 30 | +music.play(music.stringPlayable("D F E A E A C B ", 120), music.PlaybackMode.UntilDone) |
| 31 | +music.play(music.builtInPlayableMelody(Melodies.BaDing), music.PlaybackMode.UntilDone) |
25 | 32 | ```
|
26 | 33 |
|
27 |
| -When coding in JavaScript or Python, the sound is a ``soundExpression`` object which from which you run the ``play()`` function from. For example, to play the ``soaring`` sound, select ``soaring`` from the ``soundExpression`` namespace and run ``play()``: |
| 34 | +The most complex playable object is a **sound expression**. [Sound expressions](/reference/music/create-sound-expression) are composed in the [Sound Editor](/types/sound#sound-editing) using different parameters for making sound waves and effects.. |
28 | 35 |
|
29 |
| -```typescript |
30 |
| -soundExpression.soaring.play() |
| 36 | +```block |
| 37 | +music.play(music.createSoundExpression(WaveShape.Sine, 5000, 0, 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), music.PlaybackMode.UntilDone) |
31 | 38 | ```
|
32 | 39 |
|
33 |
| -## Example |
| 40 | +## Parameters |
| 41 | + |
| 42 | +* **toPlay**: the [playable](/types/playable) object, or music source, to play. |
| 43 | +* **playbackMode**: the playback mode for continuing the program: |
| 44 | +>* `play until done`: play the music source in **toPlay** but wait to run the next part of the program until music play is done. |
| 45 | +>* `in background`: play the music source in **toPlay** but continue with the rest of the program before music play is done. |
| 46 | +>* `looping in background`: play the music source in **toPlay** but continue with the rest of the program before music play is done. The music will remain playing, returning to the first note of the music after its duration. |
| 47 | +
|
| 48 | +### ~ hint |
| 49 | + |
| 50 | +#### Stop the music! |
| 51 | + |
| 52 | +You can stop any music currently playing with the ``||music:stop all sounds||`` block. This is useful if **playbackMode** is set to `in background looping` and you wish to stop the music for a scene change or respond to an event with a different sound. |
| 53 | + |
| 54 | +### ~ |
| 55 | + |
| 56 | +## Examples #example |
| 57 | + |
| 58 | +### Play a melody |
| 59 | + |
| 60 | +Play a short melody created in the Melody Editor. |
| 61 | + |
| 62 | +```blocks |
| 63 | +music.play(music.stringPlayable("D F E A E A C B ", 120), music.PlaybackMode.UntilDone) |
| 64 | +``` |
| 65 | + |
| 66 | +### Different music sources, one block to play them all |
| 67 | + |
| 68 | +Put 4 different playable music sources in an array. Play one after the other. |
| 69 | + |
| 70 | +```blocks |
| 71 | +let playables = [ |
| 72 | +music.tonePlayable(262, music.beat(BeatFraction.Whole)), |
| 73 | +music.stringPlayable("D F E A E A C B ", 120), |
| 74 | +music.builtInPlayableMelody(Melodies.BaDing), |
| 75 | +music.createSoundExpression(WaveShape.Sine, 5000, 0, 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear) |
| 76 | +] |
| 77 | +for (let someMusic of playables) { |
| 78 | + music.play(someMusic, music.PlaybackMode.UntilDone) |
| 79 | + basic.pause(500) |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Looping music play |
| 84 | + |
| 85 | +Play a simple song in the background. When the @boardname@ is shaken, stop the song an play the `power down` melody. |
| 86 | + |
| 87 | +```blocks |
| 88 | +music.play(music.stringPlayable("C5 A B G A F A C5 ", 120), music.PlaybackMode.LoopingInBackground) |
| 89 | +input.onGesture(Gesture.Shake, function () { |
| 90 | + music.stopAllSounds() |
| 91 | + music.play(music.builtInPlayableMelody(Melodies.PowerDown), music.PlaybackMode.InBackground) |
| 92 | +}) |
| 93 | +``` |
| 94 | +### Play a sound effect |
34 | 95 |
|
35 |
| -Play the ``twinkle`` sound on the speaker. |
| 96 | +Play a sine wave sound effect for `5` seconds. |
36 | 97 |
|
37 | 98 | ```blocks
|
38 |
| -soundExpression.twinkle.play() |
| 99 | +music.play(music.createSoundExpression(WaveShape.Sine, 5000, 0, 255, 0, 5000, SoundExpressionEffect.None, InterpolationCurve.Linear), music.PlaybackMode.UntilDone) |
39 | 100 | ```
|
40 | 101 |
|
41 | 102 | ## See also
|
42 | 103 |
|
43 |
| -[play until done](/reference/music/play-until-done) |
| 104 | +[tone playable](/reference/music/tone-playable), |
| 105 | +[string playable](/reference/music/string-playable), |
| 106 | +[melody playable](/reference/music/built-in-melody-playable), |
| 107 | +[create song](/reference/music/create-song), |
| 108 | +[stop all sounds](/reference/music/stop-all-sounds), |
| 109 | +[sound editor](/reference/types/sound#sound-editing), |
| 110 | +[create sound expression](/reference/music/create-sound-expression) |
0 commit comments