Skip to content

Instantly share code, notes, and snippets.

@DareFox
Forked from snakecase/Beep on replay buffer save.lua
Last active December 1, 2022 05:23
Show Gist options
  • Save DareFox/fd91bab3b8b5f26601f5e47ae2cf9f07 to your computer and use it in GitHub Desktop.
Save DareFox/fd91bab3b8b5f26601f5e47ae2cf9f07 to your computer and use it in GitHub Desktop.
OBS Lua: Sound notification on keybind [Windows]

A simple Lua script which plays a .wav sound whenever specified keybind was activated.

Originally script was specified only for Replay Buffer save event and it works not on Save button press, but when replay was saved in file. So there was delay between press and sound effect.

Now you can launch sound not on event, but on keybind, which solves delay problem.

Installation

  1. Download script and change PROP_AUDIO_FILEPATH to a path to the sound of your choosing. By default it's Windows Hardware Remove sfx

  2. Launch OBS -> Tools -> Scripts -> +

This Lua script was downloaded from https://gist.github.com/DareFox/fd91bab3b8b5f26601f5e47ae2cf9f07

Credits:

Thank you guys!

local obs = obslua
local ffi = require("ffi")
local winmm = ffi.load("Winmm")
hotkey_id = obs.OBS_INVALID_HOTKEY_ID
-- Put a path to the sound of your choosing
PROP_AUDIO_FILEPATH = "C:\\Windows\\Media\\Windows Hardware Remove.wav"
ffi.cdef[[
bool PlaySound(const char *pszSound, void *hmod, uint32_t fdwSound);
]]
function playsound(filepath)
winmm.PlaySound(filepath, nil, 0x00020003)
end
function onPress(pressed)
if pressed then
playsound(PROP_AUDIO_FILEPATH)
end
end
function script_load(settings)
-- Register keybind
hotkey_id = obs.obs_hotkey_register_frontend("audioPlayOnKeybind", "Play audio on this keybind", onPress)
-- Load saved keybind
local hotkey_save_array = obs.obs_data_get_array(settings, "audioBind.trigger")
obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
end
function script_save(settings)
-- Save keybind
local hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
obs.obs_data_set_array(settings, "audioBind.trigger", hotkey_save_array)
obs.obs_data_array_release(hotkey_save_array)
end
-- This Lua script was downloaded from https://gist.github.com/DareFox/fd91bab3b8b5f26601f5e47ae2cf9f07
-- Credits:
-- snakecase (https://gist.github.com/snakecase/e816384a071cec31efbb4b9e429c108d)
-- upgradeQ (https://gist.github.com/upgradeQ/b2412242d76790d7618d6b0996c4562f),
-- gima (https://gitlab.com/gima/obsnotification)
-- Thank you guys!
@LtothePay
Copy link

It does not work for me. Not with default settings or anything other. ?_?

@DareFox
Copy link
Author

DareFox commented Jun 8, 2022

It does not work for me. Not with default settings or anything other. ?_?

what os do you have, @LtothePay ?

@dreamdenizen
Copy link

dreamdenizen commented Nov 30, 2022

Thanks for putting this together. Unfortunately it's not working for me either on Win11 64-bit 22H2. Tried the original sound path shown above and another custom one, but no luck. Any ideas? I'm on OBS 28.1.2

@DareFox
Copy link
Author

DareFox commented Nov 30, 2022

Thanks for putting this together. Unfortunately it's not working for me either on Win11 64-bit 22H2. Tried the original sound path shown above and another custom one, but no luck. Any ideas? I'm on OBS 28.1.2

Do you have any logs related to the script? Perhaps there are error messages, which will help solve the problem
image

@dreamdenizen
Copy link

Thanks for putting this together. Unfortunately it's not working for me either on Win11 64-bit 22H2. Tried the original sound path shown above and another custom one, but no luck. Any ideas? I'm on OBS 28.1.2

Do you have any logs related to the script? Perhaps there are error messages, which will help solve the problem image

hey there. thanks for getting back to me. I see the script is loading and unloading in the log. If I mistype the path to the sound file it gives me:
[Lua: onKeybindSound.lua] Error loading file: R:/Videos/OBS/onKeybindSound.lua:7: invalid escape sequence near '"R:\Videos\OBS'

And when it's typed correctly, I see it load fine in the log, but when I hit the keybind, nothing shows up in the log other than the saving of the buffer:
[ffmpeg muxer: 'Replay Buffer'] Wrote replay buffer to 'R:/Replay 2022-11-30 23-10-12.mkv'

There's no mention of onKeybindSound.lua anywhere else in the log other than load an unload. I double checked to make sure the file was good, and tried a couple other wavs too, no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment