Difference between revisions of "Sound.play"
m (added links) |
m |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<c>[[Sound]].play</c> plays a sound. Call this on a [[Sound]] object initialized either | <c>[[Sound]].play</c> plays a sound. Call this on a [[Sound]] object initialized either | ||
− | Sound.init, or loaded via [[file.loadSound]]. | + | [[Sound.init]], or loaded via [[file.loadSound]]. |
=== Arguments === | === Arguments === | ||
Line 17: | Line 17: | ||
=== Usage Notes === | === Usage Notes === | ||
The sound will automatically stop if [[Sound.loop|.loop]] is false, but will repeat until explicitly | The sound will automatically stop if [[Sound.loop|.loop]] is false, but will repeat until explicitly | ||
− | stopped if [[Sound.loop|.loop]] is true. Note that ''speed'' will change both the duration and pitch of the sound, due to changing the speed of the waveforms being played. | + | stopped if [[Sound.loop|.loop]] is true. Note that ''speed'' will change both the duration and pitch of the sound, due to changing the speed of the waveforms being played. ''speed'' is capped at 90 or so, and a speed of 0 or less is treated as a speed of 1 (standard speed/pitch). |
== Example == | == Example == | ||
+ | Here is a simple example of loading and playing a sound. | ||
<ms>s = file.loadSound("/sys/sounds/bonus.wav") | <ms>s = file.loadSound("/sys/sounds/bonus.wav") | ||
s.play</ms> | s.play</ms> | ||
+ | The next example loads some background music from a file, sets it to loop, and then plays it at volume 0.2. (Note: to actually run this example, you would need to have a ''background_loop.wav'' file on your user disk in a ''/usr/sound'' directory.) | ||
+ | <ms> | ||
+ | background_loop_track = file.loadSound("/usr/sound/background_loop.wav") | ||
+ | background_loop_track.loop = true | ||
+ | background_loop_track.play 0.2 | ||
+ | </ms> | ||
+ | |||
+ | |||
+ | The following example (taken from [https://youtu.be/EPMGMsSbT3g this video]) shows how to adjust the stereo pan of a sound based on the X position of a mouse click. | ||
+ | |||
+ | <ms>clear | ||
+ | snd = file.loadSound("/sys/sounds/bongo.wav") | ||
+ | |||
+ | while true // repeat forever (press Control-C to exit) | ||
+ | if mouse.button then | ||
+ | // play a sound, panning left/right according to mouse X position | ||
+ | pan = mouse.x / 480 - 1 | ||
+ | snd.play 1, pan | ||
+ | |||
+ | // wait for the mouse button to be released | ||
+ | while mouse.button; yield; end while | ||
+ | end if | ||
+ | yield | ||
+ | end while</ms> | ||
[[Category:Mini Micro]] | [[Category:Mini Micro]] |
Latest revision as of 20:18, 6 May 2024
Sound.play
plays a sound. Call this on a Sound object initialized either
Sound.init, or loaded via file.loadSound.
Arguments
Parameter Name | Type | Default Value | Meaning |
---|---|---|---|
volume | number | 1 | volume (0-1) of the sound |
pan | number | 0 | left (-1) to right (1) stereo pan of the sound |
speed | number | 1 | speed multiplier (1 == standard speed) |
Usage Notes
The sound will automatically stop if .loop is false, but will repeat until explicitly stopped if .loop is true. Note that speed will change both the duration and pitch of the sound, due to changing the speed of the waveforms being played. speed is capped at 90 or so, and a speed of 0 or less is treated as a speed of 1 (standard speed/pitch).
Example
Here is a simple example of loading and playing a sound.
s = file.loadSound("/sys/sounds/bonus.wav")
s.play
The next example loads some background music from a file, sets it to loop, and then plays it at volume 0.2. (Note: to actually run this example, you would need to have a background_loop.wav file on your user disk in a /usr/sound directory.)
background_loop_track = file.loadSound("/usr/sound/background_loop.wav")
background_loop_track.loop = true
background_loop_track.play 0.2
The following example (taken from this video) shows how to adjust the stereo pan of a sound based on the X position of a mouse click.
clear
snd = file.loadSound("/sys/sounds/bongo.wav")
while true // repeat forever (press Control-C to exit)
if mouse.button then
// play a sound, panning left/right according to mouse X position
pan = mouse.x / 480 - 1
snd.play 1, pan
// wait for the mouse button to be released
while mouse.button; yield; end while
end if
yield
end while