05-03-26

Multi-Key Soundboard Lua Script

Create a custom soundboard with this Lua script for your Rebind hardware. Assign audio files to hotkeys, control volume, and play multiple sounds simultaneously. Includes file validation and a stop-al

rebindlua scriptsoundboardaudiohotkeyshardware inputscript

Question

Can you provide a Rebind Lua script for a soundboard with multiple audio triggers?

Rebind AI

Here's a Rebind Lua script for a soundboard with multiple audio triggers:

lua
-- rebind: min_sdk=0.1.0
-- rebind: name=Soundboard
-- rebind: description=Multi-key audio trigger panel with volume control

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable Soundboard" }),
    volume = UI.Slider(0.7, { min = 0, max = 1, step = 0.05, label = "Master Volume" }),

    key1 = UI.Keybind("F1", { label = "Sound 1 Key", group = "Hotkeys" }),
    sound1 = UI.Text("sound1.wav", { label = "Sound 1 File", placeholder = "filename.wav", group = "Sounds" }),

    key2 = UI.Keybind("F2", { label = "Sound 2 Key", group = "Hotkeys" }),
    sound2 = UI.Text("sound2.wav", { label = "Sound 2 File", placeholder = "filename.wav", group = "Sounds" }),

    key3 = UI.Keybind("F3", { label = "Sound 3 Key", group = "Hotkeys" }),
    sound3 = UI.Text("sound3.wav", { label = "Sound 3 File", placeholder = "filename.wav", group = "Sounds" }),

    key4 = UI.Keybind("F4", { label = "Sound 4 Key", group = "Hotkeys" }),
    sound4 = UI.Text("sound4.wav", { label = "Sound 4 File", placeholder = "filename.wav", group = "Sounds" }),

    key5 = UI.Keybind("F5", { label = "Sound 5 Key", group = "Hotkeys" }),
    sound5 = UI.Text("sound5.wav", { label = "Sound 5 File", placeholder = "filename.wav", group = "Sounds" }),

    key6 = UI.Keybind("F6", { label = "Sound 6 Key", group = "Hotkeys" }),
    sound6 = UI.Text("sound6.wav", { label = "Sound 6 File", placeholder = "filename.wav", group = "Sounds" }),

    polyphony = UI.Toggle(true, { label = "Play Multiple Sounds", group = "Settings", tooltip = "When off, new sounds stop previous ones" }),
    stop_key = UI.Keybind("F10", { label = "Stop All Sounds", group = "Settings" }),
})

-- Map keybinds to sound files
local soundMap = {}

local function buildSoundMap()
    soundMap = {
        [cfg.key1] = cfg.sound1,
        [cfg.key2] = cfg.sound2,
        [cfg.key3] = cfg.sound3,
        [cfg.key4] = cfg.sound4,
        [cfg.key5] = cfg.sound5,
        [cfg.key6] = cfg.sound6,
    }
end

function OnStart()
    buildSoundMap()
    Log.Info("Soundboard loaded with " .. Log.Info("6 sounds available"))
    if not File.Exists("sounds") then
        File.MkDir("sounds")
        Log.Info("Created 'sounds' directory for audio files")
    end
end

function OnDown(key)
    if not cfg.enabled then
        return true
    end

    if key == cfg.stop_key then
        Audio.StopAll()
        UI.Notify("All sounds stopped", "info")
        return false
    end

    local soundFile = soundMap[key]
    if soundFile and soundFile ~= "" then
        local fullPath = "sounds/" .. soundFile
        if File.Exists(fullPath) then
            if not cfg.polyphony then
                Audio.StopAll()
            end
            Audio.Play(fullPath, { volume = cfg.volume })
            return false
        else
            UI.Notify("Sound file not found: " .. soundFile, "warning")
            return false
        end
    end

    return true
end

function OnStop()
    Audio.StopAll()
end

function OnBlur()
    Audio.StopAll()
end

This script allows you to assign audio files to hotkeys and play them with a single keystroke. It includes:

  • 6 configurable hotkeys (F1-F6 by default).
  • A master volume slider.
  • A polyphony toggle to allow multiple sounds to play simultaneously.
  • A stop key (F10) to stop all sounds.
  • File validation to warn if a sound file doesn't exist.
  • Auto-directory creation for easy asset organization.

To use it:

  1. Place your audio files (e.g., .wav, .mp3, .ogg, or .flac) in a sounds/ subfolder next to your script.
  2. Enter the filename (e.g., alert.wav) in the "Sound File" fields.
  3. Press the corresponding hotkey to play the sound.
  4. Adjust volume with the Master Volume slider.
  5. Press F10 to stop all sounds.

Example file structure:

lua
my-scripts/
  soundboard.luau
  sounds/
    alert.wav
    notification.mp3
    chime.ogg
    error.flac

Then set:

  • Sound 1 File: alert.wav
  • Sound 2 File: notification.mp3
  • Sound 3 File: chime.ogg
  • etc.