Skip to content

Instantly share code, notes, and snippets.

@snakecase
Forked from upgradeQ/beep.lua
Last active May 25, 2024 19:03
Show Gist options
  • Save snakecase/e816384a071cec31efbb4b9e429c108d to your computer and use it in GitHub Desktop.
Save snakecase/e816384a071cec31efbb4b9e429c108d to your computer and use it in GitHub Desktop.
OBS Lua: Sound notification on replay buffer save [Windows]

A simple Lua script which plays a .wav sound whenever replay buffer is saved.

Installation

  1. Download Half-Life 2 City Scanner NPC's camera shutter sound: https://github.com/sourcesounds/hl2/blob/master/sound/npc/scanner/scanner_photo1.wav

    • Or use any .wav sound but make sure to match its name either in "Beep on replay buffer save.lua" (edit with any text editor) or rename your .wav file to "sound_npc_scanner_scanner_photo1.wav".
  2. Put it in the same location with "Beep on replay buffer save.lua"

    • A good way is to create a separate "scripts" folder in %AppData%\obs-studio\ and keep all your scripts there in case you need to backup your OBS Settings.
  3. OBS → Tools → Scripts → +

This Lua script was downloaded from https://gist.github.com/snakecase/e816384a071cec31efbb4b9e429c108d

Credits:

Thank you guys!

local obs = obslua
local ffi = require("ffi")
local winmm = ffi.load("Winmm")
-- Put a sound of your choosing next to "Beep on replay save.lua" and don't forget to match its name either in code below or rename your file.
PROP_AUDIO_FILEPATH = script_path() .. "sound_npc_scanner_scanner_photo1.wav"
ffi.cdef[[
bool PlaySound(const char *pszSound, void *hmod, uint32_t fdwSound);
]]
function playsound(filepath)
winmm.PlaySound(filepath, nil, 0x00020000)
end
function on_event(event)
if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_SAVED
then playsound(PROP_AUDIO_FILEPATH)
end
end
function script_load(settings)
obs.obs_frontend_add_event_callback(on_event)
end
-- This Lua script was downloaded from https://gist.github.com/snakecase/e816384a071cec31efbb4b9e429c108d
-- Credits: upgradeQ (https://gist.github.com/upgradeQ/b2412242d76790d7618d6b0996c4562f), gima (https://gitlab.com/gima/obsnotification)
-- Thank you guys!
@NateLapT
Copy link

NateLapT commented Mar 6, 2022

Is there an easy way to adjust the audio level with the script? This plays super loud.
I could just edit the audio, but I assume there's a simple bit of code that would work...

@snakecase
Copy link
Author

Is there an easy way to adjust the audio level with the script? This plays super loud. I could just edit the audio, but I assume there's a simple bit of code that would work...

I googled a little and unfortunately didn't find any solution which I can implement. I guess editing the audio file is the easiest way.
Here is a -10 dB edit (approximately -70%). Don't forget to rename .mp4 to .wav (GitHub doesn't support .wav in comments).

sound_npc_scanner_scanner_photo1.mp4

@kabewm
Copy link

kabewm commented Mar 23, 2022

thank you so much :)

@Leopleurodon
Copy link

Great and simple script! Thank you. I've found another Half Life 2 sound effect that fits much better and is also conveniently quieter: It's called "bugreporter_succeeded.wav" in the "common" folder of the HL2 sound folder, which can be downloaded online somewhere (took me a few seconds to google it).

@snakecase
Copy link
Author

snakecase commented Mar 31, 2022

Great and simple script! Thank you. I've found another Half Life 2 sound effect that fits much better and is also conveniently quieter: It's called "bugreporter_succeeded.wav" in the "common" folder of the HL2 sound folder, which can be downloaded online somewhere (took me a few seconds to google it).

This one is being used by SteamVR natively for screenshots. I didn't want to confuse them thus decided to go with what came to mind first. But I'm glad you found what suits you better.

@Leopleurodon
Copy link

oh that makes sense! It does sound familiar. I don't use SteamVR but it might even be Steam's default screenshot sound effect, now that I think about it...

@mcdavsco
Copy link

mcdavsco commented Apr 9, 2022

Thanks for this. Works great and was exactly what I was looking for!

@Vaarelsauce
Copy link

Worked great! Although the revised -10db sound is still just a tad too loud, so here's a revised quieter version of that revision (-15db in total, I think). Other than that, the script worked flawless :)

(attached it as .mp4 because non-authors can't send .mp3 for some reason. just make sure to rename it to .wav)
https://user-images.githubusercontent.com/36707936/197033922-51507751-bab9-468f-88f5-18f1b36af11d.mp4

@alexzh3
Copy link

alexzh3 commented Feb 9, 2023

Does it still work for OBS version 29.0.2? Does not seem to work for me.

@alexzh3
Copy link

alexzh3 commented Feb 9, 2023

Does it still work for OBS version 29.0.2? Does not seem to work for me.

Using Windows 11, no errors in the log, my suspicion is this part of the code:
winmm.PlaySound(filepath, nil, 0x00020003)
The winmm for the soundsystem has probably changed on Windows 11?

@Vaarelsauce
Copy link

Does it still work for OBS version 29.0.2? Does not seem to work for me.

Using Windows 11, no errors in the log, my suspicion is this part of the code: winmm.PlaySound(filepath, nil, 0x00020003) The winmm for the soundsystem has probably changed on Windows 11?

I'm not sure about Windows 11, but it's been working perfectly fine for me on Windows 10 (OBS 29.0.2).

@Alsh7
Copy link

Alsh7 commented Feb 10, 2023

I'm on the same specs but it's not working for me either... :/

@mcdavsco
Copy link

Working fine for me with OBS 29.0.2 on Windows 11 (22H2). Does the script still show for you under Tools->Scripts?

@Alsh7
Copy link

Alsh7 commented Feb 12, 2023

Working fine for me with OBS 29.0.2 on Windows 11 (22H2). Does the script still show for you under Tools->Scripts?

Yep

@markodevinci
Copy link

Hey is anyone else having an issue where the sound effect is very delayed after hitting the replay buffer hotkey? The sound plays about 10-15 seconds after I hit the key. Don't know how to fix it, also I'm on Windows 11, OBS 29.02.

@NateLapT
Copy link

NateLapT commented Mar 5, 2023

Hey is anyone else having an issue where the sound effect is very delayed after hitting the replay buffer hotkey? The sound plays about 10-15 seconds after I hit the key. Don't know how to fix it, also I'm on Windows 11, OBS 29.02.

How long is your replay?

@markodevinci
Copy link

Hey is anyone else having an issue where the sound effect is very delayed after hitting the replay buffer hotkey? The sound plays about 10-15 seconds after I hit the key. Don't know how to fix it, also I'm on Windows 11, OBS 29.02.

How long is your replay?

My replay is 30 seconds long.

@Nebula-Developer
Copy link

Does it still work for OBS version 29.0.2? Does not seem to work for me.

Using Windows 11, no errors in the log, my suspicion is this part of the code: winmm.PlaySound(filepath, nil, 0x00020003) The winmm for the soundsystem has probably changed on Windows 11?

Figured out how to fix it, replace that line with:
winmm.PlaySound(filepath, nil, 0x0001) -- The 0x0001 flag is for SND_FILENAME

Works like a charm. Thanks copilot!

@snakecase
Copy link
Author

snakecase commented Apr 10, 2023

Does it still work for OBS version 29.0.2? Does not seem to work for me.

Using Windows 11, no errors in the log, my suspicion is this part of the code: winmm.PlaySound(filepath, nil, 0x00020003) The winmm for the soundsystem has probably changed on Windows 11?

Figured out how to fix it, replace that line with: winmm.PlaySound(filepath, nil, 0x0001) -- The 0x0001 flag is for SND_FILENAME

Works like a charm. Thanks copilot!

Though Copilot provided a working solution, 0x0001 is actually SND_ASYNC. And you won't believe but 0x00020003 that was set in this script before (which was blindly copy pasted from one of the credited scripts) doesn't have any aliases and was working only because

If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a file name.

🤣

Fixed with 0x00020000, should be working for everyone.

@Nebula-Developer
Copy link

Does it still work for OBS version 29.0.2? Does not seem to work for me.

Using Windows 11, no errors in the log, my suspicion is this part of the code: winmm.PlaySound(filepath, nil, 0x00020003) The winmm for the soundsystem has probably changed on Windows 11?

Figured out how to fix it, replace that line with: winmm.PlaySound(filepath, nil, 0x0001) -- The 0x0001 flag is for SND_FILENAME
Works like a charm. Thanks copilot!

Though Copilot provided a working solution, 0x0001 is actually SND_ASYNC. And you won't believe but 0x00020003 that was set in this script before (which was blindly copy pasted from one of the credited scripts) doesn't have any aliases and was working only because

If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a file name.

🤣

Fixed with 0x00020000, should be working for everyone.

Thanks for the correction. Seemed to work, but will also try 0x00020000 if it seems a better option.

@GTMoraes
Copy link

GTMoraes commented Jun 8, 2023

Hello!

Ha, I enjoyed this script, so I made some additions to it. I made some sounds based off on the Black Mesa's turret sounds (we're still using Half Life related effects, right?).
As I toggle buffer on and off, I wanted a way to know whether it was toggled on or off.

here's how the script would be

local obs = obslua
local ffi = require("ffi")
local winmm = ffi.load("Winmm")

-- Put a sound of your choosing next to "Beep on replay save.lua" and don't forget to match its name either in code below or rename your file.
PROP_AUDIO_SAVE = script_path() .. "Cam_SAVE.wav"
PROP_AUDIO_START = script_path() .. "Cam_ON.wav"
PROP_AUDIO_SHUTDOWN = script_path() .. "Cam_OFF.wav"

ffi.cdef[[
    bool PlaySound(const char *pszSound, void *hmod, uint32_t fdwSound);
]]

function playsound(filepath)
    winmm.PlaySound(filepath, nil, 0x00020000)
end

function on_event(event) 
  if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTED 
    then playsound(PROP_AUDIO_START) 
  end 
  if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_SAVED 
    then playsound(PROP_AUDIO_SAVE) 
  end 
  if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPED
    then playsound(PROP_AUDIO_SHUTDOWN) 
  end 
end

function script_load(settings)
  obs.obs_frontend_add_event_callback(on_event)
end

-- This Lua script was downloaded from https://gist.github.com/snakecase/e816384a071cec31efbb4b9e429c108d

-- Credits: upgradeQ (https://gist.github.com/upgradeQ/b2412242d76790d7618d6b0996c4562f), gima (https://gitlab.com/gima/obsnotification)
-- Thank you guys!

bear in mind I'm no programmer, but I think I heard something about case switch or something that would be better here? idk, it's just basic code. It works.

These are the wav files
click here

Same installation procedure as the original script. I used the lowered volume WAV files that someone supplied here (with I guess 75% lower volume?) File was also actually an MP3, but I'm sending it over as a correct .wav file anyway.

And there you have it. Volume is pretty quiet, I guess. Sounds OK from the TV speakers, which is what I'm listening from.

@daski000
Copy link

wasnt working until i corrected to 0x00020000 but now its does the windows background sound instead of the wav sound idk why

@snakecase
Copy link
Author

It's already set to 0x00020000 in the sctipt. 🤔

@sutex
Copy link

sutex commented May 11, 2024

Hello!

Ha, I enjoyed this script, so I made some additions to it. I made some sounds based off on the Black Mesa's turret sounds (we're still using Half Life related effects, right?). As I toggle buffer on and off, I wanted a way to know whether it was toggled on or off.

here's how the script would be

local obs = obslua
local ffi = require("ffi")
local winmm = ffi.load("Winmm")

-- Put a sound of your choosing next to "Beep on replay save.lua" and don't forget to match its name either in code below or rename your file.
PROP_AUDIO_SAVE = script_path() .. "Cam_SAVE.wav"
PROP_AUDIO_START = script_path() .. "Cam_ON.wav"
PROP_AUDIO_SHUTDOWN = script_path() .. "Cam_OFF.wav"

ffi.cdef[[
    bool PlaySound(const char *pszSound, void *hmod, uint32_t fdwSound);
]]

function playsound(filepath)
    winmm.PlaySound(filepath, nil, 0x00020000)
end

function on_event(event) 
  if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTED 
    then playsound(PROP_AUDIO_START) 
  end 
  if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_SAVED 
    then playsound(PROP_AUDIO_SAVE) 
  end 
  if event == obs.OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPED
    then playsound(PROP_AUDIO_SHUTDOWN) 
  end 
end

function script_load(settings)
  obs.obs_frontend_add_event_callback(on_event)
end

-- This Lua script was downloaded from https://gist.github.com/snakecase/e816384a071cec31efbb4b9e429c108d

-- Credits: upgradeQ (https://gist.github.com/upgradeQ/b2412242d76790d7618d6b0996c4562f), gima (https://gitlab.com/gima/obsnotification)
-- Thank you guys!

bear in mind I'm no programmer, but I think I heard something about case switch or something that would be better here? idk, it's just basic code. It works.

These are the wav files click here

Same installation procedure as the original script. I used the lowered volume WAV files that someone supplied here (with I guess 75% lower volume?) File was also actually an MP3, but I'm sending it over as a correct .wav file anyway.

And there you have it. Volume is pretty quiet, I guess. Sounds OK from the TV speakers, which is what I'm listening from.

Hi when I use this script and turn on the replay buffer, it turns off seconds later. In the original, this didn't happen.

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