obs = obslua local hotkeys = {} local hk = {} function script_description() return [[Control audio source volumes with hotkeys.
Use Stream Deck + to control source volumes by using hotkeys]] end function script_properties() return false end function change_volume(source_name, type) local sources = obs.obs_enum_sources() if sources ~= nil then for _, source in ipairs(sources) do name = obs.obs_source_get_name(source) if name == source_name then local volume = obs.obs_source_get_volume(source) -- Convert volume to decibel local db = (20 * math.log10(volume)) local change = 0.5 if db <= -10 and db > -20 then change = 1 elseif db <= -20 and db > -30 then change = 2 elseif db <= -30 and db > -40 then change = 3 elseif db <= -40 and db > -50 then change = 4 elseif db <= -50 then change = 5 else change = 0.5 end if type == "up" then db = db + change else db = db - change end volume = (10^(db/20)) if volume < 0 then volume = 0 end if volume > 1 then volume = 1 end obs.obs_source_set_volume(source, volume) end end end end function toggle_mute(source_name) local sources = obs.obs_enum_sources() local state if sources ~= nil then for _, source in ipairs(sources) do name = obs.obs_source_get_name(source) if name == source_name then state = obs.obs_source_muted(source) if state == true then obs.obs_source_set_muted(source, false) else obs.obs_source_set_muted(source, true) end end end end end -- A function named script_load will be called on startup function script_load(settings) local sources = get_sources(); local a local i = 0 for k, v in pairs(sources) do hotkeys[i] = obs.OBS_INVALID_HOTKEY_ID -- Create volume up hotkey hk[i] = v..i hotkeys[i] = obs.obs_hotkey_register_frontend(hk[i], v .. " Vol +", function(pressed) if pressed then change_volume(v, "up") end end) a = obs.obs_data_get_array(settings, hk[i]) obs.obs_hotkey_load(hotkeys[i], a) obs.obs_data_array_release(a) i = i + 1 -- Create volume down hotkey hk[i] = v..i hotkeys[i] = obs.obs_hotkey_register_frontend(hk[i], v .. " Vol -", function(pressed) if pressed then change_volume(v, "down") end end) a = obs.obs_data_get_array(settings, hk[i]) obs.obs_hotkey_load(hotkeys[i], a) obs.obs_data_array_release(a) i = i + 1 -- Create volume mute toggle hk[i] = v..i hotkeys[i] = obs.obs_hotkey_register_frontend(hk[i], v .. " Toggle Mute", function(pressed) if pressed then toggle_mute(v) end end) a = obs.obs_data_get_array(settings, hk[i]) obs.obs_hotkey_load(hotkeys[i], a) obs.obs_data_array_release(a) i = i + 1 end end -- A function named script_save will be called when the script is saved -- -- NOTE: This function is usually used for saving extra data (such as in this -- case, a hotkey's save data). Settings set via the properties are saved -- automatically. function script_save(settings) for k, v in pairs(hotkeys) do a = obs.obs_hotkey_save(hotkeys[k]) obs.obs_data_set_array(settings, hk[k], a) obs.obs_data_array_release(a) end end function get_sources() local audio_sources = {} local sources = obs.obs_enum_sources() if sources ~= nil then for _, source in ipairs(sources) do source_id = obs.obs_source_get_id(source) local name = obs.obs_source_get_name(source) local fullname = name if source_id == "wasapi_output_capture" then fullname = name table.insert(audio_sources, fullname) elseif source_id == "wasapi_process_output_capture" then fullname = name table.insert(audio_sources, fullname) elseif source_id == "wasapi_input_capture" then fullname = name table.insert(audio_sources, fullname) end end end obs.source_list_release(sources) table.sort(audio_sources) local sources = {} local i = 0 for _, k in pairs(audio_sources) do sources[i] = k i = i+1 end return sources end