pxt-common-packages 10.4.5 → 10.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/built/common-sim.d.ts +4 -0
- package/built/common-sim.js +187 -0
- package/libs/azureiot/built/debug/binary.js +461 -461
- package/libs/color/built/debug/binary.js +8 -8
- package/libs/color-sensor/built/debug/binary.js +8 -8
- package/libs/controller/built/debug/binary.js +7118 -7118
- package/libs/controller---none/built/debug/binary.js +7094 -7094
- package/libs/datalogger/built/debug/binary.js +63 -63
- package/libs/edge-connector/built/debug/binary.js +8 -8
- package/libs/esp32/built/debug/binary.js +462 -462
- package/libs/game/_locales/game-jsdoc-strings.json +1 -0
- package/libs/game/built/debug/binary.js +7037 -7037
- package/libs/game/docs/reference/game/set-game-over-effect.md +47 -0
- package/libs/game/docs/reference/game/set-game-over-message.md +47 -0
- package/libs/game/docs/reference/game/set-game-over-playable.md +48 -0
- package/libs/game/docs/reference/game/set-game-over-scoring-type.md +78 -0
- package/libs/game/docs/reference/game.md +8 -0
- package/libs/game/game.ts +24 -0
- package/libs/game/info.ts +18 -8
- package/libs/lcd/built/debug/binary.js +8 -8
- package/libs/light-spectrum-sensor/built/debug/binary.js +8 -8
- package/libs/lora/built/debug/binary.js +8 -8
- package/libs/matrix-keypad/built/debug/binary.js +8 -8
- package/libs/mixer/docs/reference/music/create-song.md +38 -0
- package/libs/mixer/docs/reference/music/melody-playable.md +30 -0
- package/libs/mixer/docs/reference/music/play.md +132 -0
- package/libs/mixer/docs/reference/music/song-editor.md +92 -0
- package/libs/mixer/docs/reference/music/string-playable.md +41 -0
- package/libs/mixer/docs/reference/music/tone-playable.md +30 -0
- package/libs/mixer/docs/reference/music.md +12 -7
- package/libs/mixer/docs/types/playable.md +76 -0
- package/libs/mixer/melody.ts +1 -0
- package/libs/mixer/playable.ts +4 -0
- package/libs/mqtt/built/debug/binary.js +176 -176
- package/libs/multiplayer/player.ts +1 -0
- package/libs/net/built/debug/binary.js +176 -176
- package/libs/net-game/built/debug/binary.js +8626 -8626
- package/libs/palette/built/debug/binary.js +7036 -7036
- package/libs/pixel/built/debug/binary.js +8 -8
- package/libs/power/built/debug/binary.js +8 -8
- package/libs/proximity/built/debug/binary.js +8 -8
- package/libs/radio/built/debug/binary.js +8 -8
- package/libs/radio-broadcast/built/debug/binary.js +8 -8
- package/libs/rotary-encoder/built/debug/binary.js +8 -8
- package/libs/screen/_locales/screen-jsdoc-strings.json +2 -0
- package/libs/screen/built/debug/binary.js +50 -50
- package/libs/screen/image.cpp +226 -0
- package/libs/screen/image.ts +44 -0
- package/libs/screen/sim/image.ts +240 -0
- package/libs/servo/built/debug/binary.js +8 -8
- package/libs/sprite-scaling/built/debug/binary.js +7036 -7036
- package/libs/storyboard/built/debug/binary.js +7036 -7036
- package/package.json +1 -1
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# play
|
|
2
|
+
|
|
3
|
+
Play a song, melody, or tone from a playable music source.
|
|
4
|
+
|
|
5
|
+
```sig
|
|
6
|
+
music.play(music.createSong(hex`00780004080200`), music.PlaybackMode.UntilDone)
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Music is played for a simple tone, a melody, or a song. Each of these music sources is called a [playble](/types/playable) object. The ``||music:play||`` block can take any of these playable objects and play them as sound output for your game.
|
|
10
|
+
|
|
11
|
+
The simpliest music source is a **tone**, on note play for a duration of time:
|
|
12
|
+
|
|
13
|
+
```block
|
|
14
|
+
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
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:
|
|
18
|
+
|
|
19
|
+
```block
|
|
20
|
+
music.play(music.stringPlayable("D F E A E A C B ", 120), music.PlaybackMode.UntilDone)
|
|
21
|
+
music.play(music.melodyPlayable(music.magicWand), music.PlaybackMode.UntilDone)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The most complex playabe object is a **song**. Songs are composed in the [Song Editor](/reference/music/song-editor) using many notes from different instruments.
|
|
25
|
+
|
|
26
|
+
```block
|
|
27
|
+
music.play(music.createSong(hex`0078000408020200001c00010a006400f40164000004000000000000000000000000000500000430000400080001220c001000012514001800011e1c00200001222400280001252c003000012934003800012c3c004000011e03001c0001dc00690000045e010004000000000000000000000564000104000330000400080001290c001000011e1400180001251c002000012924002800011b2c003000012234003800011e3c0040000129`), music.PlaybackMode.UntilDone)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Parameters
|
|
31
|
+
|
|
32
|
+
* **toPlay**: the [playable](/types/playable) object, or music source, to play.
|
|
33
|
+
* **playbackMode**: the playback mode for continuing the program:
|
|
34
|
+
>* `play until done`: play the music source in **toPlay** but wait to run the next part of the program until music play is done.
|
|
35
|
+
>* `in background`: play the music source in **toPlay** but continue with the rest of the program before music play is done.
|
|
36
|
+
>* `in background looping`: 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.
|
|
37
|
+
|
|
38
|
+
### ~ hint
|
|
39
|
+
|
|
40
|
+
#### Stop the music!
|
|
41
|
+
|
|
42
|
+
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.
|
|
43
|
+
|
|
44
|
+
### ~
|
|
45
|
+
|
|
46
|
+
## Examples #example
|
|
47
|
+
|
|
48
|
+
### Play a melody
|
|
49
|
+
|
|
50
|
+
Play a short melody created in the Melody Editor.
|
|
51
|
+
|
|
52
|
+
```blocks
|
|
53
|
+
music.play(music.stringPlayable("D F E A E A C B ", 120), music.PlaybackMode.UntilDone)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Different music sources, one block to play them all
|
|
57
|
+
|
|
58
|
+
Put 4 different playable music sources in an array. Play one after the other.
|
|
59
|
+
|
|
60
|
+
```blocks
|
|
61
|
+
let playables = [
|
|
62
|
+
music.tonePlayable(262, music.beat(BeatFraction.Whole)),
|
|
63
|
+
music.stringPlayable("D F E A E A C B ", 120),
|
|
64
|
+
music.melodyPlayable(music.baDing),
|
|
65
|
+
music.createSong(hex`0078000408020200001c00010a006400f40164000004000000000000000000000000000500000430000400080001220c001000012514001800011e1c00200001222400280001252c003000012934003800012c3c004000011e03001c0001dc00690000045e010004000000000000000000000564000104000330000400080001290c001000011e1400180001251c002000012924002800011b2c003000012234003800011e3c0040000129`)
|
|
66
|
+
]
|
|
67
|
+
for (let someMusic of playables) {
|
|
68
|
+
music.play(someMusic, music.PlaybackMode.UntilDone)
|
|
69
|
+
pause(500)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Looping music play
|
|
74
|
+
|
|
75
|
+
Play a simple song in the background while a monkey moves around the screen. When the monkey hits the bubble in the middle of the screen, stop the song and play a bursting sound.
|
|
76
|
+
|
|
77
|
+
```blocks
|
|
78
|
+
let bubble = sprites.create(img`
|
|
79
|
+
. . . . . b b b b b b . . . . .
|
|
80
|
+
. . . b b 9 9 9 9 9 9 b b . . .
|
|
81
|
+
. . b b 9 9 9 9 9 9 9 9 b b . .
|
|
82
|
+
. b b 9 d 9 9 9 9 9 9 9 9 b b .
|
|
83
|
+
. b 9 d 9 9 9 9 9 1 1 1 9 9 b .
|
|
84
|
+
b 9 d d 9 9 9 9 9 1 1 1 9 9 9 b
|
|
85
|
+
b 9 d 9 9 9 9 9 9 1 1 1 9 9 9 b
|
|
86
|
+
b 9 3 9 9 9 9 9 9 9 9 9 1 9 9 b
|
|
87
|
+
b 5 3 d 9 9 9 9 9 9 9 9 9 9 9 b
|
|
88
|
+
b 5 3 3 9 9 9 9 9 9 9 9 9 d 9 b
|
|
89
|
+
b 5 d 3 3 9 9 9 9 9 9 9 d d 9 b
|
|
90
|
+
. b 5 3 3 3 d 9 9 9 9 d d 5 b .
|
|
91
|
+
. b d 5 3 3 3 3 3 3 3 d 5 b b .
|
|
92
|
+
. . b d 5 d 3 3 3 3 5 5 b b . .
|
|
93
|
+
. . . b b 5 5 5 5 5 5 b b . . .
|
|
94
|
+
. . . . . b b b b b b . . . . .
|
|
95
|
+
`, SpriteKind.Player)
|
|
96
|
+
let monkey = sprites.create(img`
|
|
97
|
+
. . . . f f f f f . . . . . . .
|
|
98
|
+
. . . f e e e e e f . . . . . .
|
|
99
|
+
. . f d d d d e e e f . . . . .
|
|
100
|
+
. c d f d d f d e e f f . . . .
|
|
101
|
+
. c d f d d f d e e d d f . . .
|
|
102
|
+
c d e e d d d d e e b d c . . .
|
|
103
|
+
c d d d d c d d e e b d c . f f
|
|
104
|
+
c c c c c d d d e e f c . f e f
|
|
105
|
+
. f d d d d d e e f f . . f e f
|
|
106
|
+
. . f f f f f e e e e f . f e f
|
|
107
|
+
. . . . f e e e e e e e f f e f
|
|
108
|
+
. . . f e f f e f e e e e f f .
|
|
109
|
+
. . . f e f f e f e e e e f . .
|
|
110
|
+
. . . f d b f d b f f e f . . .
|
|
111
|
+
. . . f d d c d d b b d f . . .
|
|
112
|
+
. . . . f f f f f f f f f . . .
|
|
113
|
+
`, SpriteKind.Enemy)
|
|
114
|
+
monkey.setBounceOnWall(true)
|
|
115
|
+
monkey.x = scene.screenWidth()
|
|
116
|
+
monkey.setVelocity(50, 40)
|
|
117
|
+
music.play(music.stringPlayable("C5 A B G A F A C5 ", 120), music.PlaybackMode.LoopingInBackground)
|
|
118
|
+
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
|
|
119
|
+
music.stopAllSounds()
|
|
120
|
+
sprites.destroy(sprite, effects.blizzard, 500)
|
|
121
|
+
music.play(music.melodyPlayable(music.zapped), music.PlaybackMode.UntilDone)
|
|
122
|
+
})
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## See also #seealso
|
|
126
|
+
|
|
127
|
+
[tone playable](/reference/music/tone-playable),
|
|
128
|
+
[string playable](/reference/music/string-playable),
|
|
129
|
+
[melody playable](/reference/music/melody-playable),
|
|
130
|
+
[create song](/reference/music/create-song),
|
|
131
|
+
[stop all sounds](/reference/music/stop-all-sounds),
|
|
132
|
+
[song editor](/reference/music/song-editor)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Song Editor
|
|
2
|
+
|
|
3
|
+
A song is contained inside the ``||music:song||`` block. The song is created and modified using the Song Editor which opens when you click on the song parameter window of the block.
|
|
4
|
+
|
|
5
|
+
```block
|
|
6
|
+
music.createSong(hex`00780004080200`)
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
More often though, you'll see the ``||music:song||`` block as part of the ``||music:play||`` block.
|
|
10
|
+
|
|
11
|
+
```block
|
|
12
|
+
music.play(music.createSong(hex`00780004080200`), music.PlaybackMode.UntilDone)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Clicking on the song parameter window opens the Song Editor. If the ``||music:song||`` block contains no song information, the Song Editor will display with an empty song.
|
|
16
|
+
|
|
17
|
+
## JavaScript and Python
|
|
18
|
+
|
|
19
|
+
When you edit your code in the JavaScript or Python workspaces, the musical notes symbol (♫) appears in the margin of the line containing the ``||music:song||`` equivalent function, [createSong()](/reference/music/create-song). Clicking on this symbol will open the Song Editor too.
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
1 let mySprite = sprites.create(assets.image`myImage`, SpriteKind.Player)
|
|
23
|
+
♫ 2 music.play(music.createSong(hex`00780004080200`), music.PlaybackMode.InBackground)
|
|
24
|
+
3 mySprite.startEffect(effects.confetti)
|
|
25
|
+
4
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Music staff
|
|
29
|
+
|
|
30
|
+
Notes of a song are placed on the music **staff** which is a set of horizontal lines arranged from low to high. The notes are placed at **grid** positions from left to right. The grid divides a **measure** to determine how may notes the measure contains. A grid of **1/8** has **8** note positons per measure. If a note position has no notes in it, that position is a rest (no sound). Placing notes higher or lower on the staff, of course, sets the pitch of the note.
|
|
31
|
+
|
|
32
|
+
### Measures
|
|
33
|
+
|
|
34
|
+
You can change the length of the song by adding or removing measures. The **Measures** control lets you choose how long your song will play for. In this example, measures is set to **4** to add 2 more measures to the song.
|
|
35
|
+
|
|
36
|
+
### Bass clef
|
|
37
|
+
|
|
38
|
+
You can put notes on the **Bass Clef** too. Just check the **Show base clef** option. The bass clef is displayed in the Song Editor to match the default treble clef.
|
|
39
|
+
|
|
40
|
+
Notes are added to the bass clef in the same way as the treble clef.
|
|
41
|
+
|
|
42
|
+
### Grid
|
|
43
|
+
|
|
44
|
+
The number of note positions (divisions) in each measure is set by the **Grid** control. The grid setting will set the number of note divisions in the measures using a measure fraction. This example is using an grid setting of 1/16 so there are 16 notes shown in the measure.
|
|
45
|
+
|
|
46
|
+
### Example
|
|
47
|
+
|
|
48
|
+
This example contains a song with notes on both clefs having measures at 1/8 divisions.
|
|
49
|
+
|
|
50
|
+
```blocks
|
|
51
|
+
let mySprite = sprites.create(img`
|
|
52
|
+
....................
|
|
53
|
+
....................
|
|
54
|
+
....................
|
|
55
|
+
....2222...2222.....
|
|
56
|
+
...222222.222222....
|
|
57
|
+
..222222222222222...
|
|
58
|
+
..222222222222222...
|
|
59
|
+
..222222222222222...
|
|
60
|
+
..222222222222222...
|
|
61
|
+
..222222222222222...
|
|
62
|
+
..222222222222222...
|
|
63
|
+
...2222222222222....
|
|
64
|
+
....22222222222.....
|
|
65
|
+
.....222222222......
|
|
66
|
+
......2222222.......
|
|
67
|
+
.......22222........
|
|
68
|
+
........222.........
|
|
69
|
+
.........2..........
|
|
70
|
+
.........2..........
|
|
71
|
+
....................
|
|
72
|
+
`, SpriteKind.Player)
|
|
73
|
+
music.play(music.createSong(hex`0078000408040200001c00010a006400f401640000040000000000000000000000000005000004a30000000400012a04000800012708000c0001200c001000012410001400012a14001800012018001c00011d1c002000012020002400012724002800012a28002c0001252c003000012030003400012434003800012738003c00012a3c004000012740004400012044004800012548004c00012a4c005000012050005400012454005800021e2758005c00012a5c00600001206000640001276400680001246c007000012004001c00100500640000041e000004000000000000000000000000000a040004600000000400010804000800011208000c00010c0c001000011210001400010814001800011218001c00010c1c002000011220002400010824002800011228002c00010c2c003000011230003400010834003800011238003c00010c3c0040000112`), music.PlaybackMode.InBackground)
|
|
74
|
+
mySprite.startEffect(effects.confetti, 5000)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Instruments
|
|
78
|
+
|
|
79
|
+
You can play notes for several different **instruments**. You select an instrument from the instrument bar at the top of the editor window.
|
|
80
|
+
|
|
81
|
+
The instruments are represented by various character symbols and make different sounds for the same notes. These are MakeCode intruments and don't exaclty have the same sound as typical instruments like a violin, cello, drums, or guitar.
|
|
82
|
+
|
|
83
|
+
If you need to focus on placing notes for a particular instrument, you can use the **Only show selected instrument** option to temporarily remove the other instruments from the staff. This makes it easier to compose for a single instrument since only the notes for the currently selected instrument will show.
|
|
84
|
+
|
|
85
|
+
## Playing the song
|
|
86
|
+
|
|
87
|
+
While editing a song, you can play, stop, or loop play the music you currently have composed. Also, you can change the tempo (beats per minute) of the song with the **Tempo** setting.
|
|
88
|
+
|
|
89
|
+
## See also
|
|
90
|
+
|
|
91
|
+
[play](/reference/music/play),
|
|
92
|
+
[create song](/reference/music/create-song)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# string Playable
|
|
2
|
+
|
|
3
|
+
Created a short melody of notes composed in a string.
|
|
4
|
+
|
|
5
|
+
```sig
|
|
6
|
+
music.stringPlayable("D F E A E A C B ", 120)
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The **melody** is short series of notes composed in a string. The melody is played at a rate set by the **tempo** value you give. The melody string contains a sequence of notes formatted like this:
|
|
10
|
+
|
|
11
|
+
``"E B C5 A B G A F "``
|
|
12
|
+
|
|
13
|
+
The melody is shown in the ``||music:melody||`` block as note symbols which also appear in the Melody Editor.
|
|
14
|
+
|
|
15
|
+
```block
|
|
16
|
+
music.stringPlayable("E F G F E G B C5 ", 120)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The melodies are most often created in the Melody Editor from the block so that valid notes are chosen and the correct melody length is set.
|
|
20
|
+
|
|
21
|
+
## Parameters
|
|
22
|
+
|
|
23
|
+
* **melody**: a [string](/types/string) which contains the notes of the melody.
|
|
24
|
+
* **tempo**: a [number](/types/number) which is the rate to play the melody at in beats per minute.
|
|
25
|
+
|
|
26
|
+
## Returns
|
|
27
|
+
|
|
28
|
+
* a [playable](/types/playable) object that contains the **melody** and **tempo**.
|
|
29
|
+
|
|
30
|
+
## Example #example
|
|
31
|
+
|
|
32
|
+
Play the ``Mystery`` melody continuously.
|
|
33
|
+
|
|
34
|
+
```blocks
|
|
35
|
+
music.play(music.stringPlayable("E F G F E G B C5 ", 120), music.PlaybackMode.LoopingInBackground)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## See also #seealso
|
|
39
|
+
|
|
40
|
+
[tone playable](/reference/music/tone-playable),
|
|
41
|
+
[melody playable](/reference/music/melody-playable)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# tone Playable
|
|
2
|
+
|
|
3
|
+
Create a musical tone that will play for some amount of time.
|
|
4
|
+
|
|
5
|
+
```sig
|
|
6
|
+
music.tonePlayable(262, music.beat(BeatFraction.Whole))
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Parameters
|
|
10
|
+
|
|
11
|
+
* **note**: is the note frequency as a [number](/types/number) of [Hertz](https://wikipedia.org/wiki/Hertz) (how high or low the tone is, also known as _pitch_). If **note** is less or equal to zero, no sound is played.
|
|
12
|
+
* **duration**: is the [number](/types/number) of milliseconds (one-thousandth of a second) that the tone lasts for. If **duration** is negative or zero, the sound will play continuously.
|
|
13
|
+
|
|
14
|
+
## Returns
|
|
15
|
+
|
|
16
|
+
* a [playable](/types/playable) object that contains the tone.
|
|
17
|
+
|
|
18
|
+
## Example #example
|
|
19
|
+
|
|
20
|
+
Store the musical note 'C' in the variable `note` and play that note for 1000 milliseconds (one second).
|
|
21
|
+
|
|
22
|
+
```blocks
|
|
23
|
+
let note = music.noteFrequency(Note.C);
|
|
24
|
+
music.play(music.tonePlayable(note, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## See also #seealso
|
|
28
|
+
|
|
29
|
+
[melody playable](/reference/music/melody-playable),
|
|
30
|
+
[string playable](/reference/music/string-playable)
|
|
@@ -2,20 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Make music and play tones.
|
|
4
4
|
|
|
5
|
+
## Songs
|
|
6
|
+
|
|
7
|
+
```cards
|
|
8
|
+
music.createSong(hex`00780004080200`)
|
|
9
|
+
```
|
|
10
|
+
|
|
5
11
|
## Melodies
|
|
6
12
|
|
|
7
13
|
```cards
|
|
8
|
-
music.
|
|
9
|
-
music.baDing
|
|
10
|
-
music.baDing.playUntilDone()
|
|
11
|
-
music.baDing.loop()
|
|
12
|
-
music.baDing.stop()
|
|
14
|
+
music.stringPlayable("", 120)
|
|
15
|
+
music.melodyPlayable(music.baDing)
|
|
13
16
|
```
|
|
14
17
|
|
|
15
18
|
## Sound
|
|
16
19
|
|
|
17
20
|
```cards
|
|
18
|
-
music.
|
|
21
|
+
music.play(null, music.PlaybackMode.UntilDone)
|
|
22
|
+
music.tonePlayable(262, music.beat(BeatFraction.Whole))
|
|
19
23
|
music.ringTone(0);
|
|
20
24
|
music.rest(0);
|
|
21
25
|
music.noteFrequency(Note.C);
|
|
@@ -29,7 +33,8 @@ music.stopAllSounds()
|
|
|
29
33
|
|
|
30
34
|
## See Also
|
|
31
35
|
|
|
32
|
-
[play
|
|
36
|
+
[play](/reference/music/play), [tone playable](/reference/music/tone-playable),
|
|
37
|
+
[string playable](/reference/music/string-playable), [melody playable](/reference/music/melody-playable), [create song](/reference/music/create-song),
|
|
33
38
|
[ring tone](/reference/music/ring-tone), [rest](/reference/music/rest),
|
|
34
39
|
[beat](/reference/music/beat), [tempo](/reference/music/tempo),
|
|
35
40
|
[change tempo by](/reference/music/change-tempo-by),[set tempo](/reference/music/set-tempo)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# playable
|
|
2
|
+
|
|
3
|
+
The **playable** data object provides a common format to play tones, melodies, and songs. Each of these music sources are created in different ways but are transformed into playable objects so that a single playback method is used to [play](/refernece/music/play) them.
|
|
4
|
+
|
|
5
|
+
## Music sources for playable objects
|
|
6
|
+
|
|
7
|
+
The blocks used to create playable music soucres are the following:
|
|
8
|
+
|
|
9
|
+
### Tone
|
|
10
|
+
|
|
11
|
+
A tone is a musical note, or a sound frequency, and a duration. The duration is often set as the length of a `beat`.
|
|
12
|
+
|
|
13
|
+
```block
|
|
14
|
+
music.tonePlayable(262, music.beat(BeatFraction.Whole))
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Melody
|
|
18
|
+
|
|
19
|
+
Melodies are a series of notes and a tempo to play them at.
|
|
20
|
+
|
|
21
|
+
```block
|
|
22
|
+
music.stringPlayable("D F E A E A C B ", 120)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Built-in sound
|
|
26
|
+
|
|
27
|
+
A built-in sound is a simple melody already composed for you. There are several you can choose from.
|
|
28
|
+
|
|
29
|
+
```block
|
|
30
|
+
music.melodyPlayable(music.baDing)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Song
|
|
34
|
+
|
|
35
|
+
Songs are complex music sources which have many notes from different instruments. Songs are made in the Song Editor.
|
|
36
|
+
|
|
37
|
+
```block
|
|
38
|
+
music.createSong(hex`0078000408020200001c00010a006400f40164000004000000000000000000000000000500000430000400080001220c001000012514001800011e1c00200001222400280001252c003000012934003800012c3c004000011e03001c0001dc00690000045e010004000000000000000000000564000104000330000400080001290c001000011e1400180001251c002000012924002800011b2c003000012234003800011e3c0040000129`)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Play the music
|
|
42
|
+
|
|
43
|
+
In your programs, you can simply use the ``||music:play||`` blocks for each playable object. Like this one for tone:
|
|
44
|
+
|
|
45
|
+
```block
|
|
46
|
+
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Or, this one for song:
|
|
50
|
+
|
|
51
|
+
```block
|
|
52
|
+
music.play(music.createSong(hex`0078000408020200001c00010a006400f40164000004000000000000000000000000000500000430000400080001220c001000012514001800011e1c00200001222400280001252c003000012934003800012c3c004000011e03001c0001dc00690000045e010004000000000000000000000564000104000330000400080001290c001000011e1400180001251c002000012924002800011b2c003000012234003800011e3c0040000129`), music.PlaybackMode.UntilDone)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Example #example
|
|
56
|
+
|
|
57
|
+
Put 4 different playable music sources in an array. Play one after the other.
|
|
58
|
+
|
|
59
|
+
```blocks
|
|
60
|
+
let playables = [
|
|
61
|
+
music.tonePlayable(262, music.beat(BeatFraction.Whole)),
|
|
62
|
+
music.stringPlayable("D F E A E A C B ", 120),
|
|
63
|
+
music.melodyPlayable(music.baDing),
|
|
64
|
+
music.createSong(hex`0078000408020200001c00010a006400f40164000004000000000000000000000000000500000430000400080001220c001000012514001800011e1c00200001222400280001252c003000012934003800012c3c004000011e03001c0001dc00690000045e010004000000000000000000000564000104000330000400080001290c001000011e1400180001251c002000012924002800011b2c003000012234003800011e3c0040000129`)
|
|
65
|
+
]
|
|
66
|
+
for (let someMusic of playables) {
|
|
67
|
+
music.play(someMusic, music.PlaybackMode.UntilDone)
|
|
68
|
+
pause(500)
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## See also #seealso
|
|
73
|
+
|
|
74
|
+
[play](/reference/music/play), [tone playable](/reference/music/tone-playable),
|
|
75
|
+
[string playable](/reference/music/string-playable), [melody playable](/reference/music/melody-playable),
|
|
76
|
+
[create song](/reference/music/create-song)
|
package/libs/mixer/melody.ts
CHANGED
package/libs/mixer/playable.ts
CHANGED
|
@@ -108,6 +108,7 @@ namespace music {
|
|
|
108
108
|
//% block="play $toPlay $playbackMode"
|
|
109
109
|
//% toPlay.shadow=music_melody_playable
|
|
110
110
|
//% group="Sounds"
|
|
111
|
+
//% help="music/play"
|
|
111
112
|
export function play(toPlay: Playable, playbackMode: PlaybackMode) {
|
|
112
113
|
toPlay.play(playbackMode);
|
|
113
114
|
}
|
|
@@ -119,6 +120,7 @@ namespace music {
|
|
|
119
120
|
//% group="Sounds"
|
|
120
121
|
//% duplicateShadowOnDrag
|
|
121
122
|
//% blockHidden
|
|
123
|
+
//% help=music/melody-playable
|
|
122
124
|
export function melodyPlayable(melody: Melody): Playable {
|
|
123
125
|
return new MelodyPlayable(melody);
|
|
124
126
|
}
|
|
@@ -135,6 +137,7 @@ namespace music {
|
|
|
135
137
|
//% melody.shadow=melody_editor
|
|
136
138
|
//% tempo.min=40 tempo.max=500
|
|
137
139
|
//% tempo.defl=120
|
|
140
|
+
//% help=music/string-playable
|
|
138
141
|
export function stringPlayable(melody: string, tempo: number): Playable {
|
|
139
142
|
let notes: string[] = melody.split(" ").filter(n => !!n);
|
|
140
143
|
let formattedMelody = "";
|
|
@@ -171,6 +174,7 @@ namespace music {
|
|
|
171
174
|
//% note.shadow=device_note
|
|
172
175
|
//% duration.shadow=device_beat
|
|
173
176
|
//% parts="headphone"
|
|
177
|
+
//% help=music/tone-playable
|
|
174
178
|
export function tonePlayable(note: number, duration: number): Playable {
|
|
175
179
|
return new TonePlayable(note, duration);
|
|
176
180
|
}
|