From af284de68da13254ad2e4358b59d57441d8c20ed Mon Sep 17 00:00:00 2001 From: Henrik Date: Tue, 9 Apr 2024 21:20:31 +0200 Subject: [PATCH] Merge volume and mute modules, add all command --- camilladsp/camilladsp.py | 67 +++++++++++++++++++++------------------- docs/index.md | 3 +- docs/mute.md | 7 ----- docs/volume.md | 2 +- mkdocs.yml | 1 - tests/test_camillaws.py | 39 +++++++++++++++++------ 6 files changed, 67 insertions(+), 52 deletions(-) delete mode 100644 docs/mute.md diff --git a/camilladsp/camilladsp.py b/camilladsp/camilladsp.py index a41b1a1..f2a97ed 100644 --- a/camilladsp/camilladsp.py +++ b/camilladsp/camilladsp.py @@ -14,7 +14,7 @@ import json import math -from typing import Dict, Tuple, List, Optional, Union +from typing import Dict, Tuple, List, Optional, Union, TypedDict from threading import Lock import yaml from websocket import create_connection, WebSocket # type: ignore @@ -522,15 +522,34 @@ def description(self) -> Optional[str]: return desc +class Fader(TypedDict): + """ + Class for type annotation of fader volume and mute settings. + """ + + volume: float + mute: bool + + class Volume(_CommandGroup): """ Collection of methods for volume and mute control """ - def main(self) -> float: + def all(self) -> List[Fader]: + """ + Get volume and mute for all faders with a single call. + + Returns: + List[Fader]: A list of one object per fader, each with `volume` and `mute` properties. + """ + faders = self.client.query("GetFaders") + return faders + + def main_volume(self) -> float: """ Get current main volume setting in dB. - Equivalent to calling `get_fader_volume()` with `fader=0`. + Equivalent to calling `volume(0)`. Returns: float: Current volume setting. @@ -538,17 +557,17 @@ def main(self) -> float: vol = self.client.query("GetVolume") return float(vol) - def set_main(self, value: float): + def set_main_volume(self, value: float): """ Set main volume in dB. - Equivalent to calling `set_fader()` with `fader=0`. + Equivalent to calling `set_volume(0)`. Args: value (float): New volume in dB. """ self.client.query("SetVolume", arg=float(value)) - def fader(self, fader: int) -> float: + def volume(self, fader: int) -> float: """ Get current volume setting for the given fader in dB. @@ -562,7 +581,7 @@ def fader(self, fader: int) -> float: _fader, vol = self.client.query("GetFaderVolume", arg=int(fader)) return float(vol) - def set_fader(self, fader: int, vol: float): + def set_volume(self, fader: int, vol: float): """ Set volume for the given fader in dB. @@ -573,7 +592,7 @@ def set_fader(self, fader: int, vol: float): """ self.client.query("SetFaderVolume", arg=(int(fader), float(vol))) - def set_fader_external(self, fader: int, vol: float): + def set_volume_external(self, fader: int, vol: float): """ Special command for setting the volume when a "Loudness" filter is being combined with an external volume control (without a "Volume" filter). @@ -586,7 +605,7 @@ def set_fader_external(self, fader: int, vol: float): """ self.client.query("SetFaderExternalVolume", arg=(int(fader), float(vol))) - def adjust_fader(self, fader: int, value: float) -> float: + def adjust_volume(self, fader: int, value: float) -> float: """ Adjust volume for the given fader in dB. Positive values increase the volume, negative decrease. @@ -604,16 +623,10 @@ def adjust_fader(self, fader: int, value: float) -> float: ) return float(new_vol) - -class Mute(_CommandGroup): - """ - Collection of methods for mute control - """ - - def main(self) -> bool: + def main_mute(self) -> bool: """ Get current main mute setting. - Equivalent to calling `get_fader()` with `fader=0`. + Equivalent to calling `mute(0)`. Returns: bool: True if muted, False otherwise. @@ -621,17 +634,17 @@ def main(self) -> bool: mute = self.client.query("GetMute") return bool(mute) - def set_main(self, value: bool): + def set_main_mute(self, value: bool): """ Set main mute, true or false. - Equivalent to calling `set_fader()` with `fader=0`. + Equivalent to calling `set_mute(0)`. Args: value (bool): New mute setting. """ self.client.query("SetMute", arg=bool(value)) - def fader(self, fader: int) -> bool: + def mute(self, fader: int) -> bool: """ Get current mute setting for a fader. @@ -645,7 +658,7 @@ def fader(self, fader: int) -> bool: _fader, mute = self.client.query("GetFaderMute", arg=int(fader)) return bool(mute) - def set_fader(self, fader: int, value: bool): + def set_mute(self, fader: int, value: bool): """ Set mute status for a fader, true or false. @@ -656,7 +669,7 @@ def set_fader(self, fader: int, value: bool): """ self.client.query("SetFaderMute", arg=(int(fader), bool(value))) - def toggle_fader(self, fader: int) -> bool: + def toggle_mute(self, fader: int) -> bool: """ Toggle mute status for a fader. @@ -889,7 +902,6 @@ def __init__(self, host: str, port: int): super().__init__(host, port) self._volume = Volume(self) - self._mute = Mute(self) self._rate = RateMonitor(self) self._levels = Levels(self) self._config = Config(self) @@ -901,17 +913,10 @@ def __init__(self, host: str, port: int): @property def volume(self) -> Volume: """ - A `Volume` instance for volume controls. + A `Volume` instance for volume and mute controls. """ return self._volume - @property - def mute(self) -> Mute: - """ - A `Mute` instance for mute controls. - """ - return self._mute - @property def rate(self) -> RateMonitor: """ diff --git a/docs/index.md b/docs/index.md index 2ea604a..ac6edc6 100644 --- a/docs/index.md +++ b/docs/index.md @@ -33,8 +33,7 @@ capture_levels = client.levels.capture_rms() | [General][camilladsp.camilladsp.General] | `general` | Basics, for example starting and stopping processing | | [Status][camilladsp.camilladsp.Status] | `status` | Reading status parameters such as buffer levels | | [Config][camilladsp.camilladsp.Config] | `config` | Managing the configuration | -| [Volume][camilladsp.camilladsp.Volume] | `volume` | Volume controls | -| [Mute][camilladsp.camilladsp.Mute] | `mute` | Mute controls | +| [Volume][camilladsp.camilladsp.Volume] | `volume` | Volume and mute controls | | [Levels][camilladsp.camilladsp.Levels] | `levels` | Reading signal levels | | [RateMonitor][camilladsp.camilladsp.RateMonitor] | `rate` | Reading the sample rate montitor | | [Settings][camilladsp.camilladsp.Settings] | `settings` | Websocket server settings | diff --git a/docs/mute.md b/docs/mute.md deleted file mode 100644 index 298ac85..0000000 --- a/docs/mute.md +++ /dev/null @@ -1,7 +0,0 @@ -# Mute control -This class is accessed via the `mute` property on a `CamillaClient` instance. - -It provides methods for reading and setting the mute control. - -## class: `Mute` -::: camilladsp.camilladsp.Mute \ No newline at end of file diff --git a/docs/volume.md b/docs/volume.md index f1d7f92..109a8e6 100644 --- a/docs/volume.md +++ b/docs/volume.md @@ -1,7 +1,7 @@ # Volume control This class is accessed via the `volume` property on a `CamillaClient` instance. -It provides methods for reading and setting the volume control. +It provides methods for reading and setting the volume and mute controls. ## class: `Volume` ::: camilladsp.camilladsp.Volume \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index ef00341..843e910 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -14,7 +14,6 @@ nav: - status.md - config.md - volume.md - - mute.md - levels.md - rate.md - settings.md diff --git a/tests/test_camillaws.py b/tests/test_camillaws.py index 87e6f3e..46363f2 100644 --- a/tests/test_camillaws.py +++ b/tests/test_camillaws.py @@ -31,6 +31,20 @@ def __init__(self): '"GetCaptureRate"': json.dumps( {"GetCaptureRate": {"result": "Ok", "value": "88250"}} ), + '"GetFaders"': json.dumps( + { + "GetFaders": { + "result": "Ok", + "value": [ + {"volume": -1, "mute": False}, + {"volume": -2, "mute": True}, + {"volume": -3, "mute": False}, + {"volume": -4, "mute": True}, + {"volume": -5, "mute": False}, + ] + } + } + ), '{"GetFaderVolume": 1}': json.dumps( {"GetFaderVolume": {"result": "Ok", "value": [1, -1.23]}} ), @@ -223,10 +237,15 @@ def test_query_mockedws(camilla_mockws): assert camilla_mockws.query("SetSomeValue", arg=123) is None assert camilla_mockws.dummyws.query == json.dumps({"SetSomeValue": 123}) assert camilla_mockws.general.supported_device_types() == (["a", "b"], ["c", "d"]) - assert camilla_mockws.volume.fader(1) == -1.23 - assert camilla_mockws.volume.adjust_fader(1, -2.5) == -3.73 - assert camilla_mockws.mute.fader(1) == False - assert camilla_mockws.mute.toggle_fader(1) == True + assert camilla_mockws.volume.volume(1) == -1.23 + assert camilla_mockws.volume.adjust_volume(1, -2.5) == -3.73 + assert camilla_mockws.volume.mute(1) == False + assert camilla_mockws.volume.toggle_mute(1) == True + faders = camilla_mockws.volume.all() + assert faders[0]["volume"] == -1.0 + assert faders[2]["volume"] == -3.0 + assert faders[0]["mute"] == False + assert faders[1]["mute"] == True def test_queries(camilla_mockquery): @@ -264,17 +283,17 @@ def test_queries(camilla_mockquery): camilla_mockquery.query.assert_called_with("GetBufferLevel") camilla_mockquery.status.clipped_samples() camilla_mockquery.query.assert_called_with("GetClippedSamples") - camilla_mockquery.volume.main() + camilla_mockquery.volume.main_volume() camilla_mockquery.query.assert_called_with("GetVolume") - camilla_mockquery.volume.set_main(-25.0) + camilla_mockquery.volume.set_main_volume(-25.0) camilla_mockquery.query.assert_called_with("SetVolume", arg=-25.0) - camilla_mockquery.volume.set_fader(1, -1.23) + camilla_mockquery.volume.set_volume(1, -1.23) camilla_mockquery.query.assert_called_with("SetFaderVolume", arg=(1, -1.23)) - camilla_mockquery.mute.main() + camilla_mockquery.volume.main_mute() camilla_mockquery.query.assert_called_with("GetMute") - camilla_mockquery.mute.set_main(False) + camilla_mockquery.volume.set_main_mute(False) camilla_mockquery.query.assert_called_with("SetMute", arg=False) - camilla_mockquery.mute.set_fader(1, False) + camilla_mockquery.volume.set_mute(1, False) camilla_mockquery.query.assert_called_with("SetFaderMute", arg=(1, False)) camilla_mockquery.levels.capture_rms() camilla_mockquery.query.assert_called_with("GetCaptureSignalRms")