Skip to content

Commit e06b7ff

Browse files
authored
Implement Sound.get_volume() (#266)
See #259
1 parent 281101f commit e06b7ff

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

ev3dev/core.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,8 @@ class Sound:
29662966
29672967
"""
29682968

2969+
channel = None
2970+
29692971
@staticmethod
29702972
def beep(args=''):
29712973
"""
@@ -3052,15 +3054,8 @@ def speak(text, espeak_opts='-a 200 -s 130'):
30523054
return Popen(cmd_line, stdout=n, shell=True)
30533055

30543056
@staticmethod
3055-
def set_volume(pct, channel=None):
3056-
"""
3057-
Sets the sound volume to the given percentage [0-100] by calling
3058-
``amixer -q set <channel> <pct>%``.
3059-
If the channel is not specified, it tries to determine the default one
3060-
by running ``amixer scontrols``. If that fails as well, it uses the
3061-
``Playback`` channel, as that is the only channel on the EV3.
3062-
"""
3063-
if channel is None:
3057+
def _get_channel():
3058+
if Sound.channel is None:
30643059
# Get default channel as the first one that pops up in
30653060
# 'amixer scontrols' output, which contains strings in the
30663061
# following format:
@@ -3070,9 +3065,44 @@ def set_volume(pct, channel=None):
30703065
out = check_output(['amixer', 'scontrols']).decode()
30713066
m = re.search("'(?P<channel>[^']+)'", out)
30723067
if m:
3073-
channel = m.group('channel')
3068+
Sound.channel = m.group('channel')
30743069
else:
3075-
channel = 'Playback'
3070+
Sound.channel = 'Playback'
3071+
3072+
return Sound.channel
3073+
3074+
@staticmethod
3075+
def set_volume(pct, channel=None):
3076+
"""
3077+
Sets the sound volume to the given percentage [0-100] by calling
3078+
``amixer -q set <channel> <pct>%``.
3079+
If the channel is not specified, it tries to determine the default one
3080+
by running ``amixer scontrols``. If that fails as well, it uses the
3081+
``Playback`` channel, as that is the only channel on the EV3.
3082+
"""
3083+
3084+
if channel is None:
3085+
channel = Sound._get_channel()
30763086

30773087
cmd_line = '/usr/bin/amixer -q set {0} {1:d}%'.format(channel, pct)
30783088
Popen(cmd_line, shell=True).wait()
3089+
3090+
@staticmethod
3091+
def get_volume(channel=None):
3092+
"""
3093+
Gets the current sound volume by parsing the output of
3094+
``amixer get <channel>``.
3095+
If the channel is not specified, it tries to determine the default one
3096+
by running ``amixer scontrols``. If that fails as well, it uses the
3097+
``Playback`` channel, as that is the only channel on the EV3.
3098+
"""
3099+
3100+
if channel is None:
3101+
channel = Sound._get_channel()
3102+
3103+
out = check_output(['amixer', 'get', channel]).decode()
3104+
m = re.search('\[(?P<volume>\d+)%\]', out)
3105+
if m:
3106+
return int(m.group('volume'))
3107+
else:
3108+
raise Exception('Failed to parse output of `amixer get {}`'.format(channel))

0 commit comments

Comments
 (0)