Skip to content

Commit

Permalink
Merge volume and mute modules, add all command
Browse files Browse the repository at this point in the history
  • Loading branch information
HEnquist committed Apr 9, 2024
1 parent f2d7711 commit af284de
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 52 deletions.
67 changes: 36 additions & 31 deletions camilladsp/camilladsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -522,33 +522,52 @@ 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.
"""
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.
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -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.
Expand All @@ -604,34 +623,28 @@ 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.
"""
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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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:
"""
Expand Down
3 changes: 1 addition & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
7 changes: 0 additions & 7 deletions docs/mute.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/volume.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ nav:
- status.md
- config.md
- volume.md
- mute.md
- levels.md
- rate.md
- settings.md
Expand Down
39 changes: 29 additions & 10 deletions tests/test_camillaws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}}
),
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit af284de

Please sign in to comment.