Skip to content

Instantly share code, notes, and snippets.

@karlvr
Last active August 11, 2018 22:04
Show Gist options
  • Save karlvr/34e66d2090942dbf9c9bb3c5b2b87260 to your computer and use it in GitHub Desktop.
Save karlvr/34e66d2090942dbf9c9bb3c5b2b87260 to your computer and use it in GitHub Desktop.
mopidy-alsamixer 1.0.3 patch for logarithmic volume
diff --git a/mopidy_alsamixer/__init__.py b/mopidy_alsamixer/__init__.py
index 8f4312a..8916efa 100644
--- a/mopidy_alsamixer/__init__.py
+++ b/mopidy_alsamixer/__init__.py
@@ -22,6 +22,9 @@ class Extension(ext.Extension):
schema = super(Extension, self).get_config_schema()
schema['card'] = config.Integer(minimum=0)
schema['control'] = config.String()
+ schema['min_volume'] = config.Integer(minimum=0, maximum=100)
+ schema['max_volume'] = config.Integer(minimum=0, maximum=100)
+ schema['volume_scale'] = config.String(choices=('linear', 'cubic'))
return schema
def setup(self, registry):
diff --git a/mopidy_alsamixer/ext.conf b/mopidy_alsamixer/ext.conf
index 2a95443..c365f59 100644
--- a/mopidy_alsamixer/ext.conf
+++ b/mopidy_alsamixer/ext.conf
@@ -2,3 +2,6 @@
enabled = true
card = 0
control = Master
+min_volume = 0
+max_volume = 100
+volume_scale = linear
diff --git a/mopidy_alsamixer/mixer.py b/mopidy_alsamixer/mixer.py
index c133347..a0acf44 100644
--- a/mopidy_alsamixer/mixer.py
+++ b/mopidy_alsamixer/mixer.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import logging
import select
import threading
+import math
import alsaaudio
@@ -23,6 +24,9 @@ class AlsaMixer(pykka.ThreadingActor, mixer.Mixer):
self.config = config
self.card = self.config['alsamixer']['card']
self.control = self.config['alsamixer']['control']
+ self.min_volume = self.config['alsamixer']['min_volume']
+ self.max_volume = self.config['alsamixer']['max_volume']
+ self.volume_scale = self.config['alsamixer']['volume_scale']
known_cards = alsaaudio.cards()
if self.card >= len(known_cards):
@@ -70,15 +74,28 @@ class AlsaMixer(pykka.ThreadingActor, mixer.Mixer):
if not channels:
return None
elif channels.count(channels[0]) == len(channels):
- return int(channels[0])
+ return self.mixer_volume_to_volume(channels[0])
else:
# Not all channels have the same volume
return None
def set_volume(self, volume):
- self._mixer.setvolume(volume)
+ self._mixer.setvolume(self.volume_to_mixer_volume(volume))
return True
+ def mixer_volume_to_volume(self, mixer_volume):
+ volume = mixer_volume
+ if self.volume_scale == "cubic":
+ volume = 100.0 * math.pow(volume / 100.0, 3.0)
+ volume = (volume - self.min_volume) * 100.0 / (self.max_volume - self.min_volume)
+ return int(volume)
+
+ def volume_to_mixer_volume(self, volume):
+ mixer_volume = self.min_volume + volume * (self.max_volume - self.min_volume) / 100.0
+ if self.volume_scale == "cubic":
+ mixer_volume = 100.0 * math.pow(mixer_volume / 100.0, 1.0 / 3.0)
+ return int(mixer_volume)
+
def get_mute(self):
try:
channels_muted = self._mixer.getmute()
@karlvr
Copy link
Author

karlvr commented Aug 11, 2018

Used to patch Mopidy-Alsamixer 1.0.3 to include logarithmic volume scale, without requiring an upgrade that comes with other changes that make it incompatible with Pi Musicbox 0.7.

(cd /usr/local/lib/python2.7/dist-packages/mopidy_alsamixer && curl -L https://gist.github.com/karlvr/34e66d2090942dbf9c9bb3c5b2b87260/raw/85edd590bea140b87b3cf2023ecf740e3fd77646/mopidy-alsamixer-patch.diff | patch -p2)
rm /usr/local/lib/python2.7/dist-packages/mopidy_alsamixer/*.pyc

You can then add the following to the [alsamixer] section in your mopidy settings:

volume_scale = cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment