-- Bjorndroid talk button, for OBS Studio (Tools -> Scripts -> + -> this file).
--
-- OBS catches its hotkeys whichever window is in front. This script adds one,
-- "Bjorndroid: talk (hold)", set to F18 (what the talk button sends) unless you change it
-- in Settings -> Hotkeys. Each press and release goes into Bjorndroid's OBS overlay as a page
-- event; the overlay tells the Bjorndroid server, and the console on this PC listens while
-- the button is held.
--
-- Nothing is installed and nothing is recorded here: OBS runs this script itself, and the
-- microphone is used only by the Bjorndroid console, only while the button is held.
obs = obslua
local HOTKEY_NAME = "bjorndroid_talk"
local hotkey_id = obs.OBS_INVALID_HOTKEY_ID
local press_id = ""
-- One id per press (its release carries the same one), so a press that reaches the server
-- through more than one browser source counts once.
local function new_press_id()
return string.format("%08x%06x", os.time(), math.random(0, 0xffffff))
end
local function tell_browser_sources(down)
if down then press_id = new_press_id() end
local json = string.format('{"down":%s,"press":"%s"}', down and "true" or "false", press_id)
local reached = 0
local sources = obs.obs_enum_sources()
if sources ~= nil then
for _, src in ipairs(sources) do
if obs.obs_source_get_unversioned_id(src) == "browser_source" then
local ph = obs.obs_source_get_proc_handler(src)
local cd = obs.calldata_create()
obs.calldata_set_string(cd, "eventName", "bjorndroid-talk")
obs.calldata_set_string(cd, "jsonString", json)
if obs.proc_handler_call(ph, "javascript_event", cd) then reached = reached + 1 end
obs.calldata_destroy(cd)
end
end
obs.source_list_release(sources)
end
if reached == 0 then
obs.script_log(obs.LOG_WARNING, "Talk button pressed, but no browser source got it. Is the Bjorndroid overlay in OBS?")
end
end
function script_description()
return "Bjorndroid talk button
Hold your talk button (F18) and talk to the cast, whichever window is in front. " ..
"To use another key: Settings → Hotkeys → \"Bjorndroid: talk (hold)\"."
end
function script_load(settings)
math.randomseed(os.time())
hotkey_id = obs.obs_hotkey_register_frontend(HOTKEY_NAME, "Bjorndroid: talk (hold)", tell_browser_sources)
local saved = obs.obs_data_get_array(settings, HOTKEY_NAME)
if obs.obs_data_array_count(saved) == 0 then
-- First run: F18, what the talk button is set to.
obs.obs_data_array_release(saved)
local d = obs.obs_data_create_from_json('{"keys":[{"key":"OBS_KEY_F18"}]}')
saved = obs.obs_data_get_array(d, "keys")
obs.obs_data_release(d)
end
obs.obs_hotkey_load(hotkey_id, saved)
obs.obs_data_array_release(saved)
end
function script_save(settings)
local a = obs.obs_hotkey_save(hotkey_id)
obs.obs_data_set_array(settings, HOTKEY_NAME, a)
obs.obs_data_array_release(a)
end