From d4ae72dee5371ce841fce307a3643e08e6487ba2 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:08:24 +0100 Subject: [PATCH 1/7] Subs moved into enum.py Subs now exported --- obsws_python/__init__.py | 3 ++- obsws_python/enum.py | 43 ++++++++++++++++++++++++++++++++++++++++ obsws_python/events.py | 24 ++-------------------- 3 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 obsws_python/enum.py diff --git a/obsws_python/__init__.py b/obsws_python/__init__.py index 6ab08c5..d54af06 100644 --- a/obsws_python/__init__.py +++ b/obsws_python/__init__.py @@ -1,4 +1,5 @@ +from .enum import Subs from .events import EventClient from .reqs import ReqClient -__ALL__ = ["ReqClient", "EventClient"] +__ALL__ = ["ReqClient", "EventClient", "Subs"] diff --git a/obsws_python/enum.py b/obsws_python/enum.py new file mode 100644 index 0000000..b3c4b7e --- /dev/null +++ b/obsws_python/enum.py @@ -0,0 +1,43 @@ +from enum import IntFlag + + +class Subs(IntFlag): + GENERAL = 1 << 0 + CONFIG = 1 << 1 + SCENES = 1 << 2 + INPUTS = 1 << 3 + TRANSITIONS = 1 << 4 + FILTERS = 1 << 5 + OUTPUTS = 1 << 6 + SCENEITEMS = 1 << 7 + MEDIAINPUTS = 1 << 8 + VENDORS = 1 << 9 + UI = 1 << 10 + + LOW_VOLUME = ( + GENERAL + | CONFIG + | SCENES + | INPUTS + | TRANSITIONS + | FILTERS + | OUTPUTS + | SCENEITEMS + | MEDIAINPUTS + | VENDORS + | UI + ) + + INPUTVOLUMEMETERS = 1 << 16 + INPUTACTIVESTATECHANGED = 1 << 17 + INPUTSHOWSTATECHANGED = 1 << 18 + SCENEITEMTRANSFORMCHANGED = 1 << 19 + + HIGH_VOLUME = ( + INPUTVOLUMEMETERS + | INPUTACTIVESTATECHANGED + | INPUTSHOWSTATECHANGED + | SCENEITEMTRANSFORMCHANGED + ) + + ALL = LOW_VOLUME | HIGH_VOLUME diff --git a/obsws_python/events.py b/obsws_python/events.py index a2f07bc..0771e26 100644 --- a/obsws_python/events.py +++ b/obsws_python/events.py @@ -1,11 +1,11 @@ import json import logging import time -from enum import IntEnum from threading import Thread from .baseclient import ObsClient from .callback import Callback +from .enum import Subs """ A class to interact with obs-websocket events @@ -13,33 +13,13 @@ defined in official github repo https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events """ -Subs = IntEnum( - "Subs", - "general config scenes inputs transitions filters outputs sceneitems mediainputs vendors ui", - start=0, -) - class EventClient: logger = logging.getLogger("events.eventclient") DELAY = 0.001 def __init__(self, **kwargs): - defaultkwargs = { - "subs": ( - (1 << Subs.general) - | (1 << Subs.config) - | (1 << Subs.scenes) - | (1 << Subs.inputs) - | (1 << Subs.transitions) - | (1 << Subs.filters) - | (1 << Subs.outputs) - | (1 << Subs.sceneitems) - | (1 << Subs.mediainputs) - | (1 << Subs.vendors) - | (1 << Subs.ui) - ) - } + defaultkwargs = {"subs": Subs.LOW_VOLUME} kwargs = defaultkwargs | kwargs self.base_client = ObsClient(**kwargs) if self.base_client.authenticate(): From a7d53dce74579ca9bb44bb83c68dd615220af9b6 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:08:53 +0100 Subject: [PATCH 2/7] levels example added --- examples/levels/__main__.py | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/levels/__main__.py diff --git a/examples/levels/__main__.py b/examples/levels/__main__.py new file mode 100644 index 0000000..701b1d9 --- /dev/null +++ b/examples/levels/__main__.py @@ -0,0 +1,43 @@ +from enum import IntEnum +from math import log + +import obsws_python as obs + +LEVELTYPE = IntEnum( + "LEVELTYPE", + "VU POSTFADER PREFADER", + start=0, +) + + +def on_input_mute_state_changed(data): + """The current program scene has changed.""" + print(f"{data.input_name} mute toggled") + + +def on_input_volume_meters(data): + def fget(x): + return round(20 * log(x, 10), 1) if x > 0 else -200.0 + + for device in data.inputs: + name = device["inputName"] + if name == INPUT_DEVICE and device["inputLevelsMul"]: + left, right = device["inputLevelsMul"] + print( + f"{name} [L: {fget(left[LEVELTYPE.POSTFADER])}, R: {fget(right[LEVELTYPE.POSTFADER])}]", + ) + + +def main(): + client = obs.EventClient(subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS)) + client.callback.register([on_input_volume_meters, on_input_mute_state_changed]) + + while cmd := input(" to exit>\n"): + if not cmd: + break + + +if __name__ == "__main__": + INPUT_DEVICE = "Desktop Audio" + + main() From 592be28469a2f71d6806605b1b930f89841f7c57 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:19:18 +0100 Subject: [PATCH 3/7] README added for levels example --- examples/levels/README.md | 16 ++++++++++++++++ examples/levels/__main__.py | 7 ++++--- 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 examples/levels/README.md diff --git a/examples/levels/README.md b/examples/levels/README.md new file mode 100644 index 0000000..9e0012b --- /dev/null +++ b/examples/levels/README.md @@ -0,0 +1,16 @@ +## About + +Prints PREFADER level values for audio device `Desktop Audio`. If mute toggled prints mute state changed notification. + +## Use + +This example assumes the existence of a `config.toml`, placed next to `__main__.py`: + +```toml +[connection] +host = "localhost" +port = 4455 +password = "mystrongpass" +``` + +Press `` to exit from the script. diff --git a/examples/levels/__main__.py b/examples/levels/__main__.py index 701b1d9..7457433 100644 --- a/examples/levels/__main__.py +++ b/examples/levels/__main__.py @@ -12,7 +12,8 @@ LEVELTYPE = IntEnum( def on_input_mute_state_changed(data): """The current program scene has changed.""" - print(f"{data.input_name} mute toggled") + if data.input_name == DEVICE: + print(f"{DEVICE} mute toggled") def on_input_volume_meters(data): @@ -21,7 +22,7 @@ def on_input_volume_meters(data): for device in data.inputs: name = device["inputName"] - if name == INPUT_DEVICE and device["inputLevelsMul"]: + if name == DEVICE and device["inputLevelsMul"]: left, right = device["inputLevelsMul"] print( f"{name} [L: {fget(left[LEVELTYPE.POSTFADER])}, R: {fget(right[LEVELTYPE.POSTFADER])}]", @@ -38,6 +39,6 @@ def main(): if __name__ == "__main__": - INPUT_DEVICE = "Desktop Audio" + DEVICE = "Desktop Audio" main() From ddee4f7e3e7accb7729893b47e7b7b7fe6a81314 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:35:16 +0100 Subject: [PATCH 4/7] add setup.py to hotkeys example --- examples/hotkeys/setup.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 examples/hotkeys/setup.py diff --git a/examples/hotkeys/setup.py b/examples/hotkeys/setup.py new file mode 100644 index 0000000..06a1afe --- /dev/null +++ b/examples/hotkeys/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup + +setup( + name="hotkeys", + description="hotkeys example", + install_requires=["obsws-python", "keyboard"], +) From 4a94af45174cbe322195253d054ba8e6d1e8045c Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:37:31 +0100 Subject: [PATCH 5/7] minor version bump --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f25013f..48cd041 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import find_packages, setup HERE = pathlib.Path(__file__).parent -VERSION = "1.1.1" +VERSION = "1.2.0" PACKAGE_NAME = "obsws-python" AUTHOR = "Adem Atikturk" AUTHOR_EMAIL = "aatikturk@gmail.com" From 41d7de2cb12e7842d464aadf86c7b5131f03788b Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:55:39 +0100 Subject: [PATCH 6/7] fix error in readme --- examples/levels/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/levels/README.md b/examples/levels/README.md index 9e0012b..5780767 100644 --- a/examples/levels/README.md +++ b/examples/levels/README.md @@ -1,6 +1,6 @@ ## About -Prints PREFADER level values for audio device `Desktop Audio`. If mute toggled prints mute state changed notification. +Prints POSTFADER level values for audio device `Desktop Audio`. If mute toggled prints mute state changed notification. ## Use From 9551173590decc6d43b50e66b4c8ec8e5ffa9704 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 26 Oct 2022 12:13:32 +0100 Subject: [PATCH 7/7] fix docstring, add docstring. --- examples/levels/__main__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/levels/__main__.py b/examples/levels/__main__.py index 7457433..62e03a8 100644 --- a/examples/levels/__main__.py +++ b/examples/levels/__main__.py @@ -11,12 +11,14 @@ LEVELTYPE = IntEnum( def on_input_mute_state_changed(data): - """The current program scene has changed.""" + """An input's mute state has changed.""" if data.input_name == DEVICE: print(f"{DEVICE} mute toggled") def on_input_volume_meters(data): + """volume level update every 50 milliseconds""" + def fget(x): return round(20 * log(x, 10), 1) if x > 0 else -200.0