Skip to content

Sound and music

A mod can make the game sound different in four ways: ship fight music for a battle or an arena, attach sound effects to moves and visual effects, play a sound from Lua at a moment you choose, and replace a sound the game already plays. This guide covers all four.

New to mods? Read How mods work first.

Put audio files in your mod’s assets/audio/ folder, then ask for a handle while your mod loads. A handle is a value that stands for a file; you pass it to the functions that play it.

local sf2 = require("sf2")
-- assets/audio/arena.ogg (or arena.wav) in your mod
local arena_theme = sf2.assets.audio("audio/arena")

Write the path without the file extension. sf2.assets.audio only finds the file; it does not play anything. A missing file, or one that is not audio, is an error that names the path.

Format Extension Notes
Ogg Vorbis .ogg Recommended, about a tenth the size of WAV. Clips longer than 20 seconds stream while they play, so use them for music rather than for a sound that may overlap itself.
PCM WAV .wav 16-bit only. Loaded fully into memory.

MP3, M4A and FLAC are not supported. To convert a file with FFmpeg: ffmpeg -i arena.wav -c:a libvorbis -q:a 6 arena.ogg. Trim silence from the start of short effects, because it delays the sound.

To use something the game already ships, pass its ID: sf2.assets.audio("core:gamedata/sounds/snd_gong"). The core catalog lists every built-in sound and music track. Many places also accept a native sound name directly ("snd_hit1").

A fight’s music comes from, in this order: the location’s own music, then the battle’s music, then the game’s default. Give music to:

  • A battle or fight, with the music field of sf2.battles.register. It takes an audio handle (your own track, or a built-in one such as sf2.assets.audio("core:gamedata/music/fuji")) or a native track’s file name, such as "fight18_fuji". A name no music file has plays the default track (fight1_samurai_spirit) and logs a warning. sf2.battles.patch changes the music of an existing fight.
  • A location, with music (one handle) or music_choices (up to 16 handles; the game picks one at random each time a fight starts and it loops). A location’s music takes priority over the fight’s. See Menus and settings.
local sf2 = require("sf2")
local boss_theme = sf2.assets.audio("audio/boss_theme")
-- Inside a fight of sf2.battles.register { ... }:
-- music = boss_theme,
-- or switch an existing fight to it:
sf2.battles.patch {
fight = "core:fights/zone_1/tournament/3",
music = boss_theme,
}

A random playlist for an arena:

local sf2 = require("sf2")
sf2.world.register {
id = "festival",
layers = { { fighters = { player_x = 868, enemy_x = 1068 } } }, -- add your pictures to a layer
music_choices = {
sf2.assets.audio("core:gamedata/music/fight2_blade_dance"),
sf2.assets.audio("core:gamedata/music/fight11_ronin"),
},
}

Each choice has equal odds and a track may repeat. This is not a sequential playlist.

To change a track the game already plays everywhere it plays, rather than one fight at a time, swap it with music on sf2.assets.replace. A settings switch makes it the player’s choice, for example a remastered soundtrack that is off until they turn it on:

local remastered = sf2.settings.toggle {
id = "remastered_music", label = "Remastered music", default = false,
}
sf2.assets.replace {
music = "fight10_black_warrior",
replacement = sf2.assets.audio("audio/black_warrior_remastered"),
setting = remastered,
}

Name the track by the file the game plays, exactly as stages and fights give it ("fight10_black_warrior", not "black_warrior"); letter case does not matter. Turning the switch on or off applies from the next track that starts.

To offer several versions of one track, such as three dojo themes, use one choice instead of a switch per version: register it with sf2.settings.choice and give each swap setting = theme, choice = "<id>". Let players pick one of several options shows the whole pattern.

You do not need Lua to play sounds at the right moment:

  • In a move, on plays a sound at a frame or an event. See Edit and create moves.

    on = {
    [3] = { sound = "snd_swish7" }, -- on frame 3
    strike = { sound = { "snd_hit1", "snd_hit2" } }, -- a random pick when the move hits
    }

    A list picks one at random (repeat a name to weight it). Native names only; to play your own file from a move, register a trigger.

  • A trigger plays a sound or hit effect when an event happens, without defining a move:

    sf2.moves.register {
    kind = "trigger", id = "step_sound",
    events = { { start = opening_step } }, -- opening_step is a move handle
    actions = { { type = "sound", audio = sf2.assets.audio("audio/step"), volume = 0.7 } },
    }
  • A screen effect plays a sound when its trigger fires. The sound field of sf2.fx.screen takes a native name, an audio handle, { sound = ..., volume = 0..1 }, or a list of these to pick from at random:

    sf2.fx.screen {
    id = "crit_sting", trigger = "critical",
    sound = { sound = sf2.assets.audio("audio/crit_sting"), volume = 0.6 },
    }

    Give each choice its own volume to even out loud and quiet files. See the visual effects cookbook.

For sounds that are not tied to a move or an effect (a warning beep, a looping hum, a cue when a spell fires), use sf2.audio.try_play. It returns an instance handle for that one playback, or nil, reason if the game cannot play it (for example when the voice limit is reached). The try_ in the name marks a call that can fail for reasons outside your control, so check the result.

local sf2 = require("sf2")
-- Resolve the handle while loading.
local hum = sf2.assets.audio("audio/hum")
local ambient = {
on_round_begin = function(_, fighter)
-- Start the loop when a round begins.
local sound, reason = sf2.audio.try_play(hum, { volume = 0.4, loop = true })
if not sound then sf2.log.warn(reason) end
end,
}

The options table is optional:

Option Default Meaning
volume 1 0 to 1. Multiplied by the player’s own sound volume and mute setting.
loop false Repeat until you stop it or its lifetime ends.
clock "game" "game" pauses when combat is paused. "real" keeps playing through pause.
owner None An open UI view from sf2.ui.open. Closing that view stops the sound.

Once you have an instance handle, sf2.audio.is_playing, sf2.audio.set_volume and sf2.audio.stop work on it.

local sf2 = require("sf2")
local alarm = sf2.assets.audio("audio/alarm")
local playing
local warning = {
on_tick = function(_, fighter)
local view = fighter:snapshot()
if not view then return end
local low = view.self.health / view.self.max_health < 0.25
if low and not (playing and sf2.audio.is_playing(playing)) then
playing = sf2.audio.try_play(alarm, { volume = 0.5, loop = true })
elseif not low and playing then
sf2.audio.stop(playing)
playing = nil
end
end,
on_round_end = function()
if playing then sf2.audio.stop(playing); playing = nil end
end,
}

Things to know:

  • Start sounds from a runtime callback (combat, story or UI), not while the mod is still loading.
  • Sounds are non-spatial and have no pitch, pan, position or seeking controls.
  • A sound ends when it finishes, you stop it, the scene changes or the mod shuts down. The end of a round does not stop it unless you do (or an owner view closes), so stop looping sounds in on_round_end.
  • A mod may have 16 playing sounds and the whole game 64. At the limit new requests fail; existing sounds are never cut off. Paused and muted sounds count.
  • Never save an instance handle. Keep the handle in a Lua variable only.

sf2.assets.replace swaps one asset for another you own. With a sound field it swaps a single sound that moves play, optionally for chosen weapon types and only while a settings switch is on. That is how a mod offers an “old knife sounds” option without replacing the original recording for everybody:

local sf2 = require("sf2")
local old_knives = sf2.settings.toggle {
id = "old_knife_sounds", label = "Old knife sounds", default = false,
}
for number = 1, 3 do
sf2.assets.replace {
sound = "snd_swish_sword" .. number,
replacement = sf2.assets.audio("audio/old_swish" .. number),
subtypes = { "Knives", "Daggers" },
setting = old_knives,
}
end

replacement here is your own file (or another core sound). The swap applies to the one-shot sounds that moves play, judged by the weapon the fighter has equipped; looping move sounds, sounds played by scripts, screen effects, music and the UI are not affected. A mod can register up to 64 swaps, and two mods swapping the same sound for overlapping weapon types conflict (the second fails to load). Disabling the mod and restarting restores the original. The general rules for swapping sprites, models and other assets are in the reskin guide.

Every melee strike in the game plays one of snd_hit1 to snd_hit6 when it connects, whether or not the opponent blocks. To give clean hits and blocked hits different sounds, quiet those six with a swap to a short silent file, then play your own sounds from two screen effects: one on hit (an unblocked hit) and one on block. Both hang off one setting, so a player can switch back to the original sounds:

local sf2 = require("sf2")
local heavy = sf2.settings.toggle {
id = "heavy_impacts", label = "Heavy impact sounds", default = false,
}
-- assets/audio/silence.ogg: a few milliseconds of silence.
local silence = sf2.assets.audio("audio/silence")
for number = 1, 6 do
sf2.assets.replace { sound = "snd_hit" .. number, replacement = silence, setting = heavy }
end
local function pick(names, volume)
local list = {}
for _, name in ipairs(names) do
list[#list + 1] = { sound = sf2.assets.audio("audio/" .. name), volume = volume }
end
return list
end
sf2.fx.screen { id = "heavy_hit_sound", setting = heavy, trigger = "hit",
sound = pick({ "punch1", "punch2", "kick1" }, 0.9) }
sf2.fx.screen { id = "heavy_block_sound", setting = heavy, trigger = "block",
sound = pick({ "guard1", "guard2" }, 0.7) }

A third effect on guard_break can add a sound on top when a perk pushes a hit through the guard; add perks = "PERK_BLOCK_BREAKER" to play it for Block Breaker only. A screen effect with only a sound changes nothing on screen. Sounds have no pitch control, so for variety ship a few copies of a file pitched slightly up and down and list them. One screen effect takes at most 16 sounds, and a longer list stops the whole mod from loading. The swap covers armed strikes too, so the new sounds play for weapons as well; a subtypes list on each swap limits it to chosen weapon types.

  • Audio is not spatial, and there is no public control for pitch, pan or streaming.
  • Music chosen by a location or fight is not saved with a playback position.
  • Test audible volume, mute and scene changes on a real device. Numbers in the file cannot tell you whether a mix sounds right.