|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +const midi_mixer_plugin_1 = require("midi-mixer-plugin"); |
| 7 | +const obs_websocket_js_1 = __importDefault(require("obs-websocket-js")); |
| 8 | +const obs = new obs_websocket_js_1.default(); |
| 9 | +let sources = {}; |
| 10 | +let scenes = {}; |
| 11 | +const settingsP = $MM.getSettings(); |
| 12 | +let currentScene = ""; |
| 13 | +const connect = async () => { |
| 14 | + const settings = await settingsP; |
| 15 | + return obs.connect({ |
| 16 | + address: settings.address ?? "localhost:4444", |
| 17 | + password: settings.password ?? "", |
| 18 | + }); |
| 19 | +}; |
| 20 | +const registerListeners = () => { |
| 21 | + obs.on("SourceVolumeChanged", (data) => { |
| 22 | + const source = sources[data.sourceName]; |
| 23 | + if (!source) |
| 24 | + return; |
| 25 | + source.volume = data.volume; |
| 26 | + }); |
| 27 | + obs.on("SourceMuteStateChanged", (data) => { |
| 28 | + const source = sources[data.sourceName]; |
| 29 | + if (!source) |
| 30 | + return; |
| 31 | + source.muted = data.muted; |
| 32 | + }); |
| 33 | + obs.on("SwitchScenes", (data) => { |
| 34 | + currentScene = data["scene-name"]; |
| 35 | + Object.values(scenes).forEach((button) => { |
| 36 | + button.active = data["scene-name"] === button.id; |
| 37 | + }); |
| 38 | + }); |
| 39 | +}; |
| 40 | +const mapSources = async () => { |
| 41 | + const data = await obs.send("GetSourcesList"); |
| 42 | + data.sources?.forEach(async (source) => { |
| 43 | + const [volume, muted] = await Promise.all([ |
| 44 | + obs |
| 45 | + .send("GetVolume", { |
| 46 | + source: source.name, |
| 47 | + }) |
| 48 | + .then((res) => res.volume), |
| 49 | + obs |
| 50 | + .send("GetMute", { |
| 51 | + source: source.name, |
| 52 | + }) |
| 53 | + .then((res) => res.muted), |
| 54 | + ]); |
| 55 | + const assignment = new midi_mixer_plugin_1.Assignment(source.name, { |
| 56 | + name: source.name, |
| 57 | + muted, |
| 58 | + volume, |
| 59 | + }); |
| 60 | + assignment.on("volumeChanged", (level) => { |
| 61 | + obs.send("SetVolume", { |
| 62 | + source: source.name, |
| 63 | + volume: level, |
| 64 | + }); |
| 65 | + }); |
| 66 | + assignment.on("mutePressed", () => { |
| 67 | + obs.send("SetMute", { |
| 68 | + source: source.name, |
| 69 | + mute: !assignment.muted, |
| 70 | + }); |
| 71 | + }); |
| 72 | + sources[source.name] = assignment; |
| 73 | + }); |
| 74 | +}; |
| 75 | +const mapScenes = async () => { |
| 76 | + const data = await obs.send("GetSceneList"); |
| 77 | + currentScene = data["current-scene"]; |
| 78 | + data.scenes.forEach((scene) => { |
| 79 | + const button = new midi_mixer_plugin_1.ButtonType(scene.name, { |
| 80 | + name: `OBS: Switch to "${scene.name}" scene`, |
| 81 | + active: scene.name === currentScene, |
| 82 | + }); |
| 83 | + button.on("pressed", () => { |
| 84 | + obs.send("SetCurrentScene", { |
| 85 | + "scene-name": scene.name, |
| 86 | + }); |
| 87 | + button.active = true; |
| 88 | + }); |
| 89 | + scenes[scene.name] = button; |
| 90 | + }); |
| 91 | +}; |
| 92 | +const init = async () => { |
| 93 | + obs.disconnect(); |
| 94 | + sources = {}; |
| 95 | + scenes = {}; |
| 96 | + try { |
| 97 | + $MM.setSettingsStatus("status", "Connecting..."); |
| 98 | + await connect(); |
| 99 | + registerListeners(); |
| 100 | + await Promise.all([mapSources(), mapScenes()]); |
| 101 | + $MM.setSettingsStatus("status", "Connected"); |
| 102 | + } |
| 103 | + catch (err) { |
| 104 | + console.warn("OBS error:", err); |
| 105 | + $MM.setSettingsStatus("status", err.description || err.message || err); |
| 106 | + } |
| 107 | +}; |
| 108 | +$MM.onSettingsButtonPress("reconnect", init); |
| 109 | +init(); |
0 commit comments