Skip to content

Commit

Permalink
Suggested implementation for midi-mixer#12
Browse files Browse the repository at this point in the history
  • Loading branch information
lwYeo authored Dec 9, 2022
1 parent 62761d3 commit efb869f
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const connect = async () => {
return obs.connect(address, settings.password ?? "", { eventSubscriptions: EventSubscription.All | EventSubscription.InputVolumeMeters })
};

const streamButton = new ButtonType("streamButton", {
name: "OBS: Stream Button",
});

const recordButton = new ButtonType("recordButton", {
name: "OBS: Record Button",
});

const registerListeners = () => {
obs.on("InputVolumeChanged", (data) => {
Expand Down Expand Up @@ -63,6 +70,14 @@ const registerListeners = () => {
});
});

obs.on("StreamStateChanged", (data) => {
streamButton.active = data.outputActive;
});

obs.on("RecordStateChanged", (data) => {
recordButton.active = data.outputActive;
});

obs.on("ExitStarted", () => {
disconnect();
init();
Expand Down Expand Up @@ -149,6 +164,33 @@ const mapScenes = async () => {
});
};

const mapButtons = async () => {
streamButton.on("pressed", async () => {
const status = await obs.call("GetStreamStatus");

if (status.outputActive) {
obs.call("StopStream");
}
else {
obs.call("StartStream");
}
});

recordButton.on("pressed", async () => {
const status = await obs.call("GetRecordStatus");

if (status.ouputPaused) {
obs.call("ResumeRecord");
}
else if (status.outputActive) {
obs.call("StopRecord");
}
else {
obs.call("StartRecord");
}
});
};

function disconnect() {
console.log("Disconnecting");
obs.disconnect();
Expand All @@ -175,15 +217,20 @@ const init = async () => {

await connect();
registerListeners();
await Promise.all([mapSources(), mapScenes()]);
await Promise.all([mapSources(), mapScenes(), mapButtons()]);

$MM.setSettingsStatus("status", "Connected");
wsConnected = true;
} catch (err: any) {
console.warn("OBS error:", err);
$MM.setSettingsStatus("status", err.description || err.message || err);
}
};

const delay = (delayMs: number) => {
return new Promise(resolve => setTimeout(resolve, delayMs));
}

$MM.onSettingsButtonPress("reconnect", init);

init();

0 comments on commit efb869f

Please sign in to comment.