-
Notifications
You must be signed in to change notification settings - Fork 1
/
mixer.py
38 lines (30 loc) · 1.12 KB
/
mixer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import alsaaudio
from config import ALSA_CARD_INDEX
class AlsaError(Exception):
pass
class Mixer:
def __init__(self):
self.__name = 'Master'
self.__kwargs = {'cardindex': ALSA_CARD_INDEX}
# muting at the beginning
self.mute()
def mute(self):
# unfortunately on my soundcard muting mutes all playback controls, while unmuting unmutes only the specific control.
# only setting volume to 0 instead
self.setVolume(0)
def getVolume(self) -> int:
try:
mixer = alsaaudio.Mixer(self.__name, **self.__kwargs)
volumes = mixer.getvolume()
# only the first channel, should be the same for all
return volumes[0]
except alsaaudio.ALSAAudioError as e:
raise AlsaError(str(e))
def setVolume(self, volume: int):
try:
mixer = alsaaudio.Mixer(self.__name, **self.__kwargs)
channel = alsaaudio.MIXER_CHANNEL_ALL
mixer.setmute(0, channel)
mixer.setvolume(volume, channel)
except alsaaudio.ALSAAudioError as e:
raise AlsaError(str(e))