Skip to content

Commit

Permalink
Add volume range config
Browse files Browse the repository at this point in the history
This helps with mopidy#3
  • Loading branch information
karlvr committed Aug 11, 2018
1 parent 3ff5ceb commit 1485ffa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.rst
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mopidy_alsamixer/__init__.py
Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions mopidy_alsamixer/mixer.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 1485ffa

Please sign in to comment.