voicemeeter-api-python/voicemeeterlib/strip.py

610 lines
14 KiB
Python
Raw Normal View History

2022-06-16 14:07:12 +01:00
import time
from abc import abstractmethod
from math import log
from typing import Union
from . import kinds
2022-06-16 14:07:12 +01:00
from .iremote import IRemote
from .meta import bool_prop, device_prop, float_prop
2022-06-16 14:07:12 +01:00
class Strip(IRemote):
"""
Implements the common interface
Defines concrete implementation for strip
"""
@abstractmethod
def __str__(self):
pass
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'strip[{self.index}]'
2022-06-16 14:07:12 +01:00
@property
def mono(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('mono') == 1
2022-06-16 14:07:12 +01:00
@mono.setter
def mono(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('mono', 1 if val else 0)
2022-06-16 14:07:12 +01:00
@property
def solo(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('solo') == 1
2022-06-16 14:07:12 +01:00
@solo.setter
def solo(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('solo', 1 if val else 0)
2022-06-16 14:07:12 +01:00
@property
def mute(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('mute') == 1
2022-06-16 14:07:12 +01:00
@mute.setter
def mute(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('mute', 1 if val else 0)
2022-06-16 14:07:12 +01:00
@property
def limit(self) -> int:
2025-01-15 12:40:31 +00:00
return int(self.getter('limit'))
2022-06-16 14:07:12 +01:00
@limit.setter
def limit(self, val: int):
2025-01-15 12:40:31 +00:00
self.setter('limit', val)
2022-06-16 14:07:12 +01:00
@property
def label(self) -> str:
2025-01-15 12:40:31 +00:00
return self.getter('Label', is_string=True)
2022-06-16 14:07:12 +01:00
@label.setter
def label(self, val: str):
2025-01-15 12:40:31 +00:00
self.setter('Label', str(val))
2022-06-16 14:07:12 +01:00
@property
def gain(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('gain'), 1)
2022-06-16 14:07:12 +01:00
@gain.setter
def gain(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('gain', val)
2022-06-16 14:07:12 +01:00
def fadeto(self, target: float, time_: int):
2025-01-15 12:40:31 +00:00
self.setter('FadeTo', f'({target}, {time_})')
2022-07-02 02:00:52 +01:00
time.sleep(self._remote.DELAY)
2022-06-16 14:07:12 +01:00
def fadeby(self, change: float, time_: int):
2025-01-15 12:40:31 +00:00
self.setter('FadeBy', f'({change}, {time_})')
2022-07-02 02:00:52 +01:00
time.sleep(self._remote.DELAY)
2022-06-16 14:07:12 +01:00
class PhysicalStrip(Strip):
@classmethod
def make(cls, remote, i, is_phys):
"""
Factory method for PhysicalStrip.
Returns a PhysicalStrip class.
"""
EFFECTS_cls = _make_effects_mixins(is_phys)[remote.kind.name]
return type(
2025-01-15 12:40:31 +00:00
'PhysicalStrip',
(cls, EFFECTS_cls),
{
2025-01-15 12:40:31 +00:00
'comp': StripComp(remote, i),
'gate': StripGate(remote, i),
'denoiser': StripDenoiser(remote, i),
'eq': StripEQ(remote, i),
'device': StripDevice.make(remote, i),
},
)
2022-06-16 14:07:12 +01:00
def __str__(self):
2025-01-15 12:40:31 +00:00
return f'{type(self).__name__}{self.index}'
2022-06-16 14:07:12 +01:00
@property
def audibility(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('audibility'), 1)
@audibility.setter
def audibility(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('audibility', val)
2022-06-16 14:07:12 +01:00
class StripComp(IRemote):
2022-06-16 14:07:12 +01:00
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}].comp'
2022-06-16 14:07:12 +01:00
@property
def knob(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter(''), 1)
@knob.setter
def knob(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('', val)
2022-06-16 14:07:12 +01:00
@property
def gainin(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('GainIn'), 1)
2022-06-16 14:07:12 +01:00
@gainin.setter
def gainin(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('GainIn', val)
@property
def ratio(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Ratio'), 1)
@ratio.setter
def ratio(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Ratio', val)
@property
def threshold(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Threshold'), 1)
@threshold.setter
def threshold(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Threshold', val)
@property
def attack(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Attack'), 1)
@attack.setter
def attack(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Attack', val)
@property
def release(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Release'), 1)
@release.setter
def release(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Release', val)
@property
def knee(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Knee'), 2)
@knee.setter
def knee(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Knee', val)
@property
def gainout(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('GainOut'), 1)
@gainout.setter
def gainout(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('GainOut', val)
@property
def makeup(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('makeup') == 1
@makeup.setter
def makeup(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('makeup', 1 if val else 0)
class StripGate(IRemote):
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}].gate'
@property
def knob(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter(''), 1)
@knob.setter
def knob(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('', val)
@property
def threshold(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Threshold'), 1)
@threshold.setter
def threshold(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Threshold', val)
@property
def damping(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Damping'), 1)
@damping.setter
def damping(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Damping', val)
@property
def bpsidechain(self) -> int:
2025-01-15 12:40:31 +00:00
return int(self.getter('BPSidechain'))
@bpsidechain.setter
def bpsidechain(self, val: int):
2025-01-15 12:40:31 +00:00
self.setter('BPSidechain', val)
@property
def attack(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Attack'), 1)
@attack.setter
def attack(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Attack', val)
@property
def hold(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Hold'), 1)
@hold.setter
def hold(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Hold', val)
@property
def release(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('Release'), 1)
@release.setter
def release(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('Release', val)
class StripDenoiser(IRemote):
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}].denoiser'
@property
def knob(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter(''), 1)
@knob.setter
def knob(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('', val)
class StripEQ(IRemote):
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}].eq'
@property
def on(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('on') == 1
@on.setter
def on(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('on', 1 if val else 0)
@property
def ab(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('ab') == 1
@ab.setter
def ab(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('ab', 1 if val else 0)
2022-06-16 14:07:12 +01:00
class StripDevice(IRemote):
@classmethod
def make(cls, remote, i):
"""
Factory function for strip.device.
Returns a StripDevice class of a kind.
"""
DEVICE_cls = type(
2025-01-15 12:40:31 +00:00
f'StripDevice{remote.kind}',
(cls,),
{
**{
param: device_prop(param)
for param in [
2025-01-15 12:40:31 +00:00
'wdm',
'ks',
'mme',
'asio',
]
},
},
)
return DEVICE_cls(remote, i)
2022-06-16 14:07:12 +01:00
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}].device'
@property
def name(self) -> str:
2025-01-15 12:40:31 +00:00
return self.getter('name', is_string=True)
2022-06-16 14:07:12 +01:00
@property
def sr(self) -> int:
2025-01-15 12:40:31 +00:00
return int(self.getter('sr'))
2022-06-16 14:07:12 +01:00
class VirtualStrip(Strip):
@classmethod
def make(cls, remote, i, is_phys):
"""
Factory method for VirtualStrip.
Returns a VirtualStrip class.
"""
EFFECTS_cls = _make_effects_mixins(is_phys)[remote.kind.name]
return type(
2025-01-15 12:40:31 +00:00
'VirtualStrip',
(cls, EFFECTS_cls),
{},
)
2022-06-16 14:07:12 +01:00
def __str__(self):
2025-01-15 12:40:31 +00:00
return f'{type(self).__name__}{self.index}'
2022-06-16 14:07:12 +01:00
@property
def mc(self) -> bool:
2025-01-15 12:40:31 +00:00
return self.getter('mc') == 1
2022-06-16 14:07:12 +01:00
@mc.setter
def mc(self, val: bool):
2025-01-15 12:40:31 +00:00
self.setter('mc', 1 if val else 0)
2022-06-16 14:07:12 +01:00
mono = mc
@property
def k(self) -> int:
2025-01-15 12:40:31 +00:00
return int(self.getter('karaoke'))
2022-06-16 14:07:12 +01:00
@k.setter
def k(self, val: int):
2025-01-15 12:40:31 +00:00
self.setter('karaoke', val)
2022-06-16 14:07:12 +01:00
@property
2023-07-20 11:11:34 +01:00
def bass(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('EQGain1'), 1)
2022-06-16 14:07:12 +01:00
@bass.setter
def bass(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('EQGain1', val)
2022-06-16 14:07:12 +01:00
@property
2023-07-20 11:11:34 +01:00
def mid(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('EQGain2'), 1)
2022-06-16 14:07:12 +01:00
@mid.setter
def mid(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('EQGain2', val)
2022-06-16 14:07:12 +01:00
med = mid
@property
2023-07-20 11:11:34 +01:00
def treble(self) -> float:
2025-01-15 12:40:31 +00:00
return round(self.getter('EQGain3'), 1)
2022-06-16 14:07:12 +01:00
high = treble
2022-06-16 14:07:12 +01:00
@treble.setter
def treble(self, val: float):
2025-01-15 12:40:31 +00:00
self.setter('EQGain3', val)
2022-06-16 14:07:12 +01:00
def appgain(self, name: str, gain: float):
2025-01-15 12:40:31 +00:00
self.setter('AppGain', f'("{name}", {gain})')
2022-06-16 14:07:12 +01:00
def appmute(self, name: str, mute: bool = None):
2025-01-15 12:40:31 +00:00
self.setter('AppMute', f'("{name}", {1 if mute else 0})')
2022-06-16 14:07:12 +01:00
class StripLevel(IRemote):
def __init__(self, remote, index):
super().__init__(remote, index)
self.range = _make_strip_level_maps[remote.kind.name][self.index]
2022-06-16 14:07:12 +01:00
def getter(self, mode):
"""
Returns a tuple of level values for the channel.
If observables thread running and level updates are subscribed to, fetch values from cache
Otherwise call CAPI func.
"""
2022-06-16 14:07:12 +01:00
def fget(x):
return round(20 * log(x, 10), 1) if x > 0 else -200.0
if not self._remote.stopped() and self._remote.event.ldirty:
2025-01-15 12:40:31 +00:00
vals = self._remote.cache['strip_level'][self.range[0] : self.range[-1]]
else:
vals = [self._remote.get_level(mode, i) for i in range(*self.range)]
2022-06-16 14:07:12 +01:00
return tuple(fget(val) for val in vals)
2022-06-16 14:07:12 +01:00
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}]'
2022-06-16 14:07:12 +01:00
@property
def prefader(self) -> tuple:
self._remote.strip_mode = 0
return self.getter(0)
2022-06-16 14:07:12 +01:00
@property
def postfader(self) -> tuple:
self._remote.strip_mode = 1
2022-06-16 14:07:12 +01:00
return self.getter(1)
@property
def postmute(self) -> tuple:
self._remote.strip_mode = 2
2022-06-16 14:07:12 +01:00
return self.getter(2)
@property
def isdirty(self) -> bool:
"""
Returns dirty status for this specific channel.
Expected to be used in a callback only.
"""
if not self._remote.stopped():
return any(self._remote._strip_comp[self.range[0] : self.range[-1]])
2022-06-16 14:07:12 +01:00
is_updated = isdirty
def make_strip_level_map(kind):
phys_map = tuple((i, i + 2) for i in range(0, kind.phys_in * 2, 2))
virt_map = tuple(
(i, i + 8)
for i in range(
kind.phys_in * 2,
kind.phys_in * 2 + kind.virt_in * 8,
8,
)
)
return phys_map + virt_map
_make_strip_level_maps = {kind.name: make_strip_level_map(kind) for kind in kinds.all}
2022-06-16 14:07:12 +01:00
class GainLayer(IRemote):
def __init__(self, remote, index, i):
super().__init__(remote, index)
self._i = i
@property
def identifier(self) -> str:
2025-01-15 12:40:31 +00:00
return f'Strip[{self.index}]'
2022-06-16 14:07:12 +01:00
@property
def gain(self):
2025-01-15 12:40:31 +00:00
return self.getter(f'GainLayer[{self._i}]')
2022-06-16 14:07:12 +01:00
@gain.setter
def gain(self, val):
2025-01-15 12:40:31 +00:00
self.setter(f'GainLayer[{self._i}]', val)
2022-06-16 14:07:12 +01:00
def _make_gainlayer_mixin(remote, index):
"""Creates a GainLayer mixin"""
return type(
2025-01-15 12:40:31 +00:00
'GainlayerMixin',
2022-06-16 14:07:12 +01:00
(),
{
2025-01-15 12:40:31 +00:00
'gainlayer': tuple(
2022-06-16 14:07:12 +01:00
GainLayer(remote, index, i) for i in range(remote.kind.num_bus)
)
},
)
def _make_channelout_mixin(kind):
"""Creates a channel out property mixin"""
return type(
2025-01-15 12:40:31 +00:00
f'ChannelOutMixin{kind}',
2022-06-16 14:07:12 +01:00
(),
{
2025-01-15 12:40:31 +00:00
**{f'A{i}': bool_prop(f'A{i}') for i in range(1, kind.phys_out + 1)},
**{f'B{i}': bool_prop(f'B{i}') for i in range(1, kind.virt_out + 1)},
2022-06-16 14:07:12 +01:00
},
)
2022-06-18 11:12:33 +01:00
_make_channelout_mixins = {
kind.name: _make_channelout_mixin(kind) for kind in kinds.all
2022-06-16 14:07:12 +01:00
}
def _make_effects_mixin(kind, is_phys):
"""creates an effects mixin for a kind"""
def _make_xy_cls():
2025-01-15 12:40:31 +00:00
pan = {param: float_prop(param) for param in ['pan_x', 'pan_y']}
color = {param: float_prop(param) for param in ['color_x', 'color_y']}
fx = {param: float_prop(param) for param in ['fx_x', 'fx_y']}
if is_phys:
return type(
2025-01-15 12:40:31 +00:00
'XYPhys',
(),
{
**pan,
**color,
**fx,
},
)
return type(
2025-01-15 12:40:31 +00:00
'XYVirt',
(),
{**pan},
)
def _make_fx_cls():
if is_phys:
return type(
2025-01-15 12:40:31 +00:00
'FX',
(),
{
**{
param: float_prop(param)
2025-01-15 12:40:31 +00:00
for param in ['reverb', 'delay', 'fx1', 'fx2']
},
**{
2025-01-15 12:40:31 +00:00
f'post{param}': bool_prop(f'post{param}')
for param in ['reverb', 'delay', 'fx1', 'fx2']
},
},
)
2025-01-15 12:40:31 +00:00
return type('FX', (), {})
2025-01-15 12:40:31 +00:00
if kind.name == 'basic':
steps = (_make_xy_cls,)
2025-01-15 12:40:31 +00:00
elif kind.name == 'banana':
steps = (_make_xy_cls,)
2025-01-15 12:40:31 +00:00
elif kind.name == 'potato':
steps = (_make_xy_cls, _make_fx_cls)
2025-01-15 12:40:31 +00:00
return type(f'Effects{kind}', tuple(step() for step in steps), {})
def _make_effects_mixins(is_phys):
return {kind.name: _make_effects_mixin(kind, is_phys) for kind in kinds.all}
2022-06-16 14:07:12 +01:00
def strip_factory(is_phys_strip, remote, i) -> Union[PhysicalStrip, VirtualStrip]:
"""
Factory method for strips
Mixes in required classes
Returns a physical or virtual strip subclass
"""
STRIP_cls = (
PhysicalStrip.make(remote, i, is_phys_strip)
if is_phys_strip
else VirtualStrip.make(remote, i, is_phys_strip)
)
2022-06-18 11:12:33 +01:00
CHANNELOUTMIXIN_cls = _make_channelout_mixins[remote.kind.name]
2022-06-16 14:07:12 +01:00
_kls = (STRIP_cls, CHANNELOUTMIXIN_cls)
2025-01-15 12:40:31 +00:00
if remote.kind.name == 'potato':
2022-06-16 14:07:12 +01:00
GAINLAYERMIXIN_cls = _make_gainlayer_mixin(remote, i)
_kls += (GAINLAYERMIXIN_cls,)
return type(
2025-01-15 12:40:31 +00:00
f'{STRIP_cls.__name__}{remote.kind}',
2022-06-16 14:07:12 +01:00
_kls,
{
2025-01-15 12:40:31 +00:00
'levels': StripLevel(remote, i),
2022-06-16 14:07:12 +01:00
},
)(remote, i)
def request_strip_obj(is_phys_strip, remote, i) -> Strip:
"""
Strip entry point. Wraps factory method.
Returns a reference to a strip subclass of a kind
"""
return strip_factory(is_phys_strip, remote, i)