From 1485ffa36cf4334b7126aa3026bfc1a52e0da1e7 Mon Sep 17 00:00:00 2001 From: Karl von Randow Date: Wed, 1 Feb 2017 09:36:49 +1300 Subject: [PATCH] Add volume range config This helps with https://github.com/mopidy/mopidy-alsamixer/issues/3 --- README.rst | 8 ++++++++ mopidy_alsamixer/__init__.py | 2 ++ mopidy_alsamixer/mixer.py | 8 ++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 71f8610..533042f 100644 --- a/README.rst +++ b/README.rst @@ -55,11 +55,19 @@ The following configuration values are available: Other typical values includes ``PCM``. Run the command ``amixer scontrols`` to list available controls on your system. +- ``alsamixer/volmin`` and ``alsamixer/volmax``: Map the Mopidy volume control range to + a different range. Values are in the range 0-100. Use this if the default range (0 - 100) + is too wide, resulting in a small usable range for Mopidy's volume control. + For example try ``volmin = 30`` and ``volmin = 70`` to map Mopidy's volume control to the middle + of ALSA's volume range. + Example ``alsamixer`` section from the Mopidy configuration file:: [alsamixer] card = 1 control = PCM + volmin = 0 + volmin = 100 Project resources diff --git a/mopidy_alsamixer/__init__.py b/mopidy_alsamixer/__init__.py index 8f4312a..f298d9f 100644 --- a/mopidy_alsamixer/__init__.py +++ b/mopidy_alsamixer/__init__.py @@ -22,6 +22,8 @@ def get_config_schema(self): schema = super(Extension, self).get_config_schema() schema['card'] = config.Integer(minimum=0) schema['control'] = config.String() + schema['volmin'] = config.Integer(minimum=0, maximum=100) + schema['volmax'] = config.Integer(minimum=0, maximum=100) return schema def setup(self, registry): diff --git a/mopidy_alsamixer/mixer.py b/mopidy_alsamixer/mixer.py index c133347..d0a3919 100644 --- a/mopidy_alsamixer/mixer.py +++ b/mopidy_alsamixer/mixer.py @@ -23,6 +23,8 @@ def __init__(self, config): self.config = config self.card = self.config['alsamixer']['card'] self.control = self.config['alsamixer']['control'] + self.volmin = self.config['alsamixer']['volmin'] + self.volmax = self.config['alsamixer']['volmax'] known_cards = alsaaudio.cards() if self.card >= len(known_cards): @@ -70,13 +72,15 @@ def get_volume(self): if not channels: return None elif channels.count(channels[0]) == len(channels): - return int(channels[0]) + unadjusted_volume = (channels[0] - self.volmin) * 100.0 / (self.volmax - self.volmin) + return int(unadjusted_volume) else: # Not all channels have the same volume return None def set_volume(self, volume): - self._mixer.setvolume(volume) + adjusted_volume = self.volmin + volume * (self.volmax - self.volmin) / 100.0 + self._mixer.setvolume(int(adjusted_volume)) return True def get_mute(self):