From 813480d439488de2e1d5abe12a076c2b0140fd8f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 20 Jun 2014 16:03:31 +0200 Subject: add alsa plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- DEPENDENCIES | 2 + Makefile | 4 +- TODO | 1 - examples/plugins/alsa | 32 ++++++++++++++++ src/plugins/alsa.py | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 examples/plugins/alsa create mode 100644 src/plugins/alsa.py diff --git a/DEPENDENCIES b/DEPENDENCIES index 474af2a..b791e6a 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -9,6 +9,8 @@ RUNTIME DEPENDENCIES: OPTIONAL RUNTIME DEPENDENCIES: linux: most of the monitors require Linux's procfs or sysfs + wget: for Internet services + python-pyalsaaudio: for ALSA volume control BUILD DEPENDENCIES: diff --git a/Makefile b/Makefile index bba2721..bc8b56d 100644 --- a/Makefile +++ b/Makefile @@ -19,11 +19,11 @@ SRC = __main__ util x PLUGINS = chase clock cpuifo cpuonline cpu df discstats ipaddress \ kmsg leapsec linereader loadavg lunar mem moc network \ pacman snmp snmp6 softirqs solar uname uptime users \ - vmstat weather xdisplay xkb + vmstat weather xdisplay xkb alsa PLUGIN_EXAMPLES = chase clock cpu cpuinfo cpuonline df discstats \ ipaddress kmsg loadavg lunar mem moc network \ - pacman uname uptime users xdisplay xkb + pacman uname uptime users xdisplay xkb slsa EXAMPLES = clock mixed moderate plugin-test test xmonad diff --git a/TODO b/TODO index 63e501b..fc4bf91 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,6 @@ List of plugins to implement: iostat [-x] [-d] [-N] -zk [-p | ...] SMART monitoring Battery - Volume control /proc/acpi (mute) System tray Eyes diff --git a/examples/plugins/alsa b/examples/plugins/alsa new file mode 100644 index 0000000..f11183b --- /dev/null +++ b/examples/plugins/alsa @@ -0,0 +1,32 @@ +# -*- python -*- + +# A xpybar configuration example testing the features of plugins.alsa + +import time +import threading + +from plugins.alsa import ALSA +from plugins.clock import Clock + + +OUTPUT, HEIGHT, YPOS, TOP = 0, 12 * len(ALSA.get_cards()), 24, True + + +clock = Clock(sync_to = 0.25 * Clock.SECONDS) +alsa_ = [[ALSA(ci, m) for m in ALSA.get_mixers(ci)] for ci in range(len(ALSA.get_cards()))] +cardnames = ALSA.get_cards() + +start_ = start +def start(): + start_() + async(lambda : clock.continuous_sync(lambda : bar.invalidate())) + +text_v = lambda v : '---' if v is None else ('%2i%%' % v)[:3] +read_m = lambda m : '%s: %s' % (m.mixername, ' '.join(text_v(v) for v in m.get_volume())) +read_c = lambda c : '%s │ %s' % (c[0].cardname, ' │ '.join(read_m(m) for m in c)) + +def redraw(): + text = '\n'.join(read_c(c) for c in alsa_) + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + diff --git a/src/plugins/alsa.py b/src/plugins/alsa.py new file mode 100644 index 0000000..35d23c4 --- /dev/null +++ b/src/plugins/alsa.py @@ -0,0 +1,101 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +''' + +import alsaaudio + + +class ALSA: + ''' + ALSA volume controller + + @variable cardindex:int The index of the audio card + @variable cardname:str The name of the audio card + @variable mixername:str The name of the mixer + @variable mixer:alsaaudio.Mixer The mixer object used internally by this class + ''' + + + ALL_CHANNELS = -1 + ''' + :int Channel index that selects all available channels + ''' + + + def __init__(self, cardindex = 0, mixername = 'Master'): + ''' + Constructor + + @param cardindex:int The index of the audio card + @param mixername:str The name of the mixer + ''' + self.cardindex = cardindex + self.cardname = alsaaudio.cards()[cardindex] + self.mixername = mixername + self.mixer = alsaaudio.Mixer(self.mixername, 0, self.cardindex) + + + def get_volume(self): + ''' + Get the volume for each channel on the mixer + + @return :list The [0, 100] volume for each channel, `None` on a channel indicate that it is muted + ''' + self.mixer = alsaaudio.Mixer(self.mixername, 0, self.cardindex) + vs = self.mixer.getvolume() + try: + ms = self.mixer.getmute() + except: + ms = [0] * len(vs) + return [v if m == 0 else None for (v, m) in zip(vs, ms)] + + + def set_volume(self, volume, channel = -1): + ''' + set the volume for a channel on the mixer + + @param volume:int? The [0, 100] volume for the channel, `None` to mute the channel + @param channel:int The index of the channel, `ALSA.ALL_CHANNELS` (-1) for all channels + ''' + if volume is None: + self.mixer.setmute(1, channel) + else: + self.mixer.setvolume(volume, channel) + self.mixer.setmute(0, channel) + + + @staticmethod + def get_cards(): + ''' + Get the names of all available audio cards + + @return :list The names of all available audio cards + ''' + return alsaaudio.cards() + + + @staticmethod + def get_mixers(cardindex = 0): + ''' + Get the names of all available mixers for an audio card + + @param cardindex:int The index of the audio card + @return :list The names of all available mixers for an audio card + ''' + return alsaaudio.mixers(cardindex) + -- cgit v1.2.3-70-g09d2