PICO-8 Wiki
Explore
Main Page
Discuss
All Pages
Community
Interactive Maps
Recent Blog Posts
Popular pages
Most visited articles
Spr
Splore
P8SCII
Sspr
Fget
Pal
Line
Pico-8 tutorials
Useful Pico-8 code
Community
Recent blog posts
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
PICO-8 Wiki
244
pages
Explore
Main Page
Discuss
All Pages
Community
Interactive Maps
Recent Blog Posts
Popular pages
Most visited articles
Spr
Splore
P8SCII
Sspr
Fget
Pal
Line
Pico-8 tutorials
Useful Pico-8 code
Community
Recent blog posts
Editing
Music
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Music
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{ApiReference |name=music |shortdesc=Plays a music pattern, or stops playing. |n||The pattern number to start playing (0-63), or -1 to stop playing music. |fadems|optional|If not 0, fade in (or out) the music volume over a duration, given as a number of milliseconds. |channelmask|optional|A bitfield indicating which of the four sound channels should be reserved for music. The default is 0 (no channels reserved). }} A music pattern describes a part of a song to play as background music. The pattern assigns sound effects to one or more of the PICO-8's four sound channels, with instructions to proceed to the next pattern, loop, or end when the sounds end. Typically, sound effects associated with music patterns are a collection of notes played at the intended tempo. A call to <code>music()</code> can include a ''channel mask'' to declare which sound channels should not be interrupted by a sound effect played by a call to <code>[[Sfx|sfx()]]</code>. The channel mask is a bitfield, where channels 0, 1, 2, and 3 are represented by the corresponding bits, with values 1, 2, 4, and 8, respectively. A call to <code>[[Sfx|sfx()]]</code> may still take over a channel reserved by the music pattern if it specifies an explicit channel number. When using music and sound effects together, it is a good idea to set a channel mask that reserves some channels and leaves others open for effects. PICO-8 will prioritize the use of the unmasked channels for sound effects, giving both music and sound a chance to play. It should be noted that it is only necessary to call <code>music()</code> once, to trigger playback. PICO-8 will continue playback independently afterwards. It is not necessary to call it every frame with the current music ID--in fact, doing so will cause the track to be stuck on the first note perpetually, as the music restarts over and over. == Examples == <syntaxhighlight lang="lua"> -- start music from pattern 0 music(0) -- stop the music with a 300 ms fade out music(-1, 300) -- start music from pattern 4, reserving channels 2 and 3 -- by setting bits 2 and 3, whose values are 4 and 8, totaling 12 music(4, 0, 12) </syntaxhighlight> == See also == * <code>[[Sfx|sfx()</code>]]</code> [[Category:Reference]] [[Category:API]]
Summary:
Please note that all contributions to the PICO-8 Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Template used on this page:
Template:ApiReference
(
view source
)
Follow on IG
TikTok
Join Fan Lab