obsws-python/examples/levels/__main__.py

48 lines
1.1 KiB
Python
Raw Normal View History

2022-10-26 11:08:53 +01:00
from enum import IntEnum
from math import log
import obsws_python as obs
LEVELTYPE = IntEnum(
"LEVELTYPE",
"VU POSTFADER PREFADER",
start=0,
)
DEVICE = "Desktop Audio"
2022-10-26 11:08:53 +01:00
def on_input_mute_state_changed(data):
2022-10-26 12:13:32 +01:00
"""An input's mute state has changed."""
2022-10-26 11:19:18 +01:00
if data.input_name == DEVICE:
print(f"{DEVICE} mute toggled")
2022-10-26 11:08:53 +01:00
def on_input_volume_meters(data):
2022-10-26 12:13:32 +01:00
"""volume level update every 50 milliseconds"""
2022-10-26 11:08:53 +01:00
def fget(x):
return round(20 * log(x, 10), 1) if x > 0 else -200.0
for device in data.inputs:
name = device["inputName"]
2022-10-26 11:19:18 +01:00
if name == DEVICE and device["inputLevelsMul"]:
2022-10-26 11:08:53 +01:00
left, right = device["inputLevelsMul"]
print(
f"{name} [L: {fget(left[LEVELTYPE.POSTFADER])}, R: {fget(right[LEVELTYPE.POSTFADER])}]",
)
def main():
with obs.EventClient(
subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS)
) as client:
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
2022-10-26 11:08:53 +01:00
while _ := input("<Enter> to exit>\n"):
pass
2022-10-26 11:08:53 +01:00
if __name__ == "__main__":
main()