Skip to content

rq.sound ​

since 1.4.0

Plays sounds: a game sound number, a file in the mod folder, or a sound inside a game Flash file.

They play on the game's sound effect mixer, so Config's sound volume applies, and at real time: rq.speed does not change them. A file or Flash sound is read once and kept in memory, so a sound played on every hit reads the disk once. At most 32 mod sounds play at once; one more raises an error. These play beside the game's music; rq.music puts a file in place of a music track.

lua
-- Berserk's sound, from its Flash file
rq.sound.play_flash("Data/Flash/Battle/狂戦士発動.swf")
-- A sound of your own, looped, then faded out
local h = rq.sound.play("sfx/rain.ogg", { loop = 0, volume = 60 })
rq.sound.stop(h, 500)

Functions ​

NameSummary
rq.sound.playPlays a game sound or a sound file from the mod folder.
rq.sound.play_flashPlays a sound kept inside a Flash file.
rq.sound.flash_infoLists the sounds in a Flash file: a mod's file under that key first (rq.image.add_flash), as for play_flash.
rq.sound.stopStops a sound, at once or with a fade out.
rq.sound.playingReturns whether a sound still plays.
rq.sound.set_volumeChanges a sound's volume, at once or over a fade.

rq.sound.play ​

lua
rq.sound.play(src, opts) -> integer|nil

since 1.4.0

Plays a game sound or a sound file from the mod folder.

A file can be WAV, OGG, FLAC, Opus or MP3, up to 64 MB. Raises an error for a missing file, a path out of the mod folder, or when 32 mod sounds already play.

lua
local h = rq.sound.play("sfx/hit.wav", { volume = 80 })
ParameterTypeDescription
srcinteger|stringA game sound number (as for rq.scene.sound) or a path in the mod folder.
opts (optional)rq.SoundOptsHow to play it.
ReturnsTypeDescription
handleinteger|nilThe handle, or nil when the number does not exist or the file is not a sound.

See also: rq.sound.stop

rq.sound.play_flash ​

lua
rq.sound.play_flash(key, opts) -> integer|nil

since 1.4.0 changed 2.63.0

Plays a sound kept inside a Flash file.

Some game sounds have no number, such as Berserk's in Data/Flash/Battle/狂戦士発動.swf. A bare key means Data/Flash/System/<key>.swf, as in rq.image.add_gif, and a file a mod added under that key (rq.image.add_flash, or a GIF) is looked at first, as the game plays it. Only uncompressed (PCM) sounds play. Raises an error when the file or that sound is missing, the sound is compressed, index is below 1, or 32 mod sounds already play.

A file's sound is read once and kept; it is read again after a mod adds, replaces or removes the file under that name.

under its name changed (a bare key and the full path are one file).

ParameterTypeDescription
keystringThe Flash file's key or path.
opts (optional)rq.SoundOptsHow to play it; index picks the sound.
ReturnsTypeDescription
handleinteger|nilThe handle, or nil when the sound did not start.

Changed in 2.63.0: A mod's own file from rq.image.add_flash, and a sound read again after the file

See also: rq.sound.flash_info

rq.sound.flash_info ​

lua
rq.sound.flash_info(key) -> rq.FlashSound[]|nil

since 1.4.0 changed 2.63.0

Lists the sounds in a Flash file: a mod's file under that key first (rq.image.add_flash), as for play_flash.

lua
for i, s in ipairs(rq.sound.flash_info("Data/Flash/Battle/狂戦士発動.swf") or {}) do
  rq.log(i, s.ms, s.playable)
end
ParameterTypeDescription
keystringThe Flash file's key or path, as for play_flash.
ReturnsTypeDescription
soundsrq.FlashSound []|nilThe sounds, in the order index counts them; nil when there is no such file.

Changed in 2.63.0: A mod's own file from rq.image.add_flash.

rq.sound.stop ​

lua
rq.sound.stop(handle, fade_ms) -> boolean

since 1.4.0

Stops a sound, at once or with a fade out.

ParameterTypeDescription
handleintegerA handle from play or play_flash.
fade_ms (optional)integerThe fade out, in milliseconds. Default 0: stop at once.
ReturnsTypeDescription
stoppedbooleanfalse when the handle already ended.

rq.sound.playing ​

lua
rq.sound.playing(handle) -> boolean

since 1.4.0

Returns whether a sound still plays.

An ended handle stays false, even when its slot plays something new.

ParameterTypeDescription
handleintegerA handle from play or play_flash.
ReturnsTypeDescription
playingbooleantrue while it plays.

rq.sound.set_volume ​

lua
rq.sound.set_volume(handle, volume, fade_ms) -> boolean

since 1.4.0

Changes a sound's volume, at once or over a fade.

ParameterTypeDescription
handleintegerA handle from play or play_flash.
volumeintegerThe new volume, 0 to 100, clamped.
fade_ms (optional)integerThe fade, in milliseconds. Default 0: at once.
ReturnsTypeDescription
changedbooleanfalse when the handle already ended.