Skip to content

Commit

Permalink
Add precision volume controls for Themes (in addition to Master)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Dec 28, 2022
1 parent 5846a1f commit 71433a9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
61 changes: 49 additions & 12 deletions amniotic/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cachetools.func import ttl_cache
from datetime import datetime
from itertools import cycle
from numbers import Number
from pathlib import Path
from random import choice
from typing import Union, Optional, List, Dict
Expand Down Expand Up @@ -80,6 +81,18 @@ def get_devices(player: Optional[vlc.MediaPlayer] = None, device_names: dict[str
return devices


def sanitize_volume(value: Number) -> int:
"""
Ensure a volume is an `int` between 0 and 100
"""
value = round(value)
value = min(value, 100)
value = max(value, 0)
return value


class Amniotic:
VOLUME_DEFAULT = 50
THEME_NAME_DEFAULT = 'Default Theme'
Expand Down Expand Up @@ -182,14 +195,16 @@ def set_volume(self, value: int):
Set Master Volume, and propagate to all Themes.
"""
value = min(value, 100)
value = max(value, 0)
value = sanitize_volume(value)
self.volume = value
for theme in self.themes.values():
theme.set_volume(self.volume)
theme.volume_master = self.volume
theme.set_volume()

def set_volume_adjust_threshold(self, value: int):
self.volume_adjust_threshold = value
for theme in self.themes.values():
theme.volume_adjust_threshold = value

def set_volume_down(self):
self.set_volume(self.volume - self.volume_adjust_threshold)
Expand All @@ -203,7 +218,7 @@ def set_volume_theme(self, value):
Set the current Theme Volume.
"""
self.theme_current.set_volume(self.volume, value)
self.theme_current.set_volume(value)

@property
def status(self) -> dict:
Expand Down Expand Up @@ -240,6 +255,7 @@ def __init__(self, path: Path, device_names: Optional[dict[str, str]] = None):
Fetch paths from Theme audio directory,and set a default audio output device.
"""

self.path = path
self.name = path.stem
self.paths = self.get_paths()
Expand All @@ -257,8 +273,12 @@ def __init__(self, path: Path, device_names: Optional[dict[str, str]] = None):
self.device = None

self.set_device(device=None)

self.volume_master = 0
self.volume = self.VOLUME_DEFAULT
self.volume_scaled = self.volume
self.volume_scaled = 0
self.volume_adjust_threshold = 2
self.set_volume(self.VOLUME_DEFAULT)

def load_players(self):
"""
Expand Down Expand Up @@ -451,19 +471,32 @@ def enabled(self, value: bool):
else:
self.stop()

def set_volume(self, volume_master: int, volume: Optional[int] = None):
def set_volume(self, value: Optional[int] = None):
"""
Set the scaled volume by multiplying the master volume with the theme volume.
"""

if volume is not None:
self.volume = volume
if value is not None:
value = sanitize_volume(value)
self.volume = value

volume_scaled_old = self.volume_scaled
volume_scaled = round(self.volume * (self.volume_master / 100))
logging.info(f'Changing scaled volume for theme "{self.name}": from {volume_scaled_old} to {volume_scaled}')
self.set_volume_scaled(volume_scaled)

def set_volume_down(self):
self.set_volume(self.volume - self.volume_adjust_threshold)

def set_volume_up(self):
self.set_volume(self.volume + self.volume_adjust_threshold)

def set_volume_scaled(self, volume_scaled):

logging.info(f'Changing scaled volume for theme "{self.name}": to {volume_scaled}')

volume_old = self.volume_scaled
volume_scaled = round(self.volume * (volume_master / 100))
logging.info(f'Changing scaled volume for theme "{self.name}": from {volume_old} to {volume_scaled}')
self.volume_scaled = volume_scaled
if self.enabled:
self.player.audio_set_volume(volume_scaled)
Expand Down Expand Up @@ -495,7 +528,8 @@ def status(self):
'device': {'id': self.device, 'name': self.devices.get(self.device)},
'enabled': self.enabled,
'track_count': len(self.paths),
'volume': {'theme': self.volume, 'scaled': self.volume_scaled},
'volume': {'theme': self.volume, 'scaled': self.volume_scaled,
'adjust_threshold': self.volume_adjust_threshold},
'position': position,
'position_percentage': round(position * 100) if position else None,
'elapsed': elapsed,
Expand All @@ -518,3 +552,6 @@ def status_text(self):
"""
return f'{self.name} @ {self.volume}%'

def set_volume_adjust_threshold(self, value):
pass
30 changes: 28 additions & 2 deletions amniotic/mqtt/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def handle_incoming(self, value: Any):
"""
self.amniotic.enabled = False

class ButtonVolumeDown(Button):
class ButtonVolumeDownMaster(Button):
NAME = 'Master Volume Down'
ICON_SUFFIX = 'volume-minus'

Expand All @@ -417,7 +417,7 @@ def handle_incoming(self, value: Any):
self.amniotic.set_volume_down()


class ButtonVolumeUp(Button):
class ButtonVolumeUpMaster(Button):
NAME = 'Master Volume Up'
ICON_SUFFIX = 'volume-plus'

Expand All @@ -430,6 +430,32 @@ def handle_incoming(self, value: Any):
self.amniotic.set_volume_up()


class ButtonVolumeDownTheme(Button):
NAME = 'Theme Volume Down'
ICON_SUFFIX = 'volume-minus'

def handle_incoming(self, value: Any):
"""
Decrement volume
"""
self.amniotic.theme_current.set_volume_down()


class ButtonVolumeUpTheme(Button):
NAME = 'Theme Volume Up'
ICON_SUFFIX = 'volume-plus'

def handle_incoming(self, value: Any):
"""
Increment volume
"""
self.amniotic.theme_current.set_volume_up()


class ButtonUpdateCheck(Button):
"""
Expand Down
6 changes: 4 additions & 2 deletions amniotic/mqtt/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,16 @@ def entity_classes(self) -> list[Type]:
controls = [
control.VolumeMaster,
control.VolumeAdjustThreshold,
control.ButtonVolumeDown,
control.ButtonVolumeUp,
control.ButtonVolumeDownMaster,
control.ButtonVolumeUpMaster,
control.ButtonDisableAllThemes,

control.SelectTheme,
control.VolumeTheme,
control.ToggleTheme,
control.DeviceTheme,
control.ButtonVolumeDownTheme,
control.ButtonVolumeUpTheme,

control.Downloader,
control.NewTheme,
Expand Down

0 comments on commit 71433a9

Please sign in to comment.