Skip to content

Instantly share code, notes, and snippets.

@upgradeQ
Created November 22, 2020 18:13
Show Gist options
  • Save upgradeQ/8b353942af76e135b70f2a5567ebfb23 to your computer and use it in GitHub Desktop.
Save upgradeQ/8b353942af76e135b70f2a5567ebfb23 to your computer and use it in GitHub Desktop.
audio volume level. RIP source_volume_level
local obs = obslua
local ffi = require 'ffi'
local jit = require 'jit'
local obsffi
if ffi.os == "OSX" then
obsffi = ffi.load("obs.0.dylib")
else
obsffi = ffi.load("obs")
end
ffi.cdef[[
typedef struct obs_source obs_source_t;
obs_source_t *obs_get_source_by_name(const char *name);
void obs_source_release(obs_source_t *source);
enum obs_fader_type {
OBS_FADER_CUBIC,
OBS_FADER_IEC,
OBS_FADER_LOG
};
typedef struct obs_volmeter obs_volmeter_t;
bool obs_volmeter_attach_source(obs_volmeter_t *volmeter,
obs_source_t *source);
int MAX_AUDIO_CHANNELS;
obs_volmeter_t *obs_volmeter_create(enum obs_fader_type type);
typedef void (*obs_volmeter_updated_t)(
void *param, const float magnitude[MAX_AUDIO_CHANNELS],
const float peak[MAX_AUDIO_CHANNELS],
const float input_peak[MAX_AUDIO_CHANNELS]);
void obs_volmeter_add_callback(obs_volmeter_t *volmeter,
obs_volmeter_updated_t callback,
void *param);
]]
local lvl -- (1)
function callback_meter(data,mag,peak,input)
lvl = 'Volume lvl is :' .. tostring(tonumber(peak[0]))
end
jit.off(callback_meter) -- (2)
function volume_lvl() -- (3)
if lvl == nil then print('error lvl is nil') else print(lvl) end
end
function launch_callback() -- (4)
local source = obsffi.obs_get_source_by_name("audio")
local volmeter = obsffi.obs_volmeter_create(obsffi.OBS_FADER_LOG)
-- https://github.com/WarmUpTill/SceneSwitcher/blob/214821b69f5ade803a4919dc9386f6351583faca/src/switch-audio.cpp#L194-L207
local cb = ffi.cast("obs_volmeter_updated_t",callback_meter)
obsffi.obs_volmeter_add_callback(volmeter,cb,data)
obsffi.obs_volmeter_attach_source(volmeter,source)
obsffi.obs_source_release(source)
end
local lock
function htk_3_cb(pressed)
if pressed then
if not lock then
print('launching volmeter callback')
launch_callback()
lock = true
end
end
end
function htk_1_cb(pressed)
if pressed then
volume_lvl()
end
end
function htk_2_cb(pressed)
if pressed then
print('press 1 to view volume level of source with name audio, 3 to start ')
end
end
key_1 = '{"htk_1": [ { "key": "OBS_KEY_1" } ],'
key_3 = '"htk_3": [ { "key": "OBS_KEY_3" } ],'
key_2 = '"htk_2": [ { "key": "OBS_KEY_2" } ]}'
json_s = key_1 .. key_3 .. key_2
default_hotkeys = {
{id='htk_1',des='lvl',callback=htk_1_cb},
{id='htk_2',des='info',callback=htk_2_cb},
{id='htk_3',des='launch' ,callback=htk_3_cb},
}
function script_load(settings)
s = obs.obs_data_create_from_json(json_s)
for _,v in pairs(default_hotkeys) do
local a = obs.obs_data_get_array(s,v.id)
h = obs.obs_hotkey_register_frontend(v.id,v.des,v.callback)
obs.obs_hotkey_load(h,a)
obs.obs_data_array_release(a)
end
obs.obs_data_release(s)
end
@upgradeQ
Copy link
Author

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