From ad14e426be24023e4b0f8fc42d88f32e86261198 Mon Sep 17 00:00:00 2001 From: Ken Hayber Date: Sat, 13 Dec 2008 18:56:36 -0800 Subject: [PATCH] Support changed API in alsaaudio 0.4 Changed device/card selector from a string to an index. Use new cards() call to find user input device name and convert to the index. Default to 0. --- mixer.py | 32 ++++++++++++++++++++------------ volume.py | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/mixer.py b/mixer.py index 423b70b..94c5a7a 100644 --- a/mixer.py +++ b/mixer.py @@ -44,21 +44,29 @@ SHOW_CONTROLS = Option('controls', -1) MASK_LOCK = Option('lock_mask', -1) MASK_MUTE = Option('mute_mask', 0) +# support two different alsaaudio APIs +if hasattr(alsaaudio, 'cards'): + try: + mixer_device = alsaaudio.cards().index(MIXER_DEVICE.value) + except ValueError: + mixer_device = 0 +else: + mixer_device = MIXER_DEVICE.value try: ALSA_CHANNELS = [] - for channel in alsaaudio.mixers(MIXER_DEVICE.value): + for channel in alsaaudio.mixers(mixer_device): id = 0 while (channel,id) in ALSA_CHANNELS: id += 1 try: - mixer = alsaaudio.Mixer(channel, id, MIXER_DEVICE.value) + mixer = alsaaudio.Mixer(channel, id, mixer_device) except alsaaudio.ALSAAudioError: continue if len(mixer.volumecap()): ALSA_CHANNELS.append((channel,id)) except: - pass + pass def build_mixer_controls(box, node, label, option): """Custom Option widget to allow hide/display of each mixer control""" @@ -81,7 +89,7 @@ def build_mixer_controls(box, node, label, option): n = 0 for channel, id in ALSA_CHANNELS: - mixer = alsaaudio.Mixer(channel, id, MIXER_DEVICE.value) + mixer = alsaaudio.Mixer(channel, id, mixer_device) if not len(mixer.volumecap()): continue checkbox = controls[n] = gtk.CheckButton(label=channel) @@ -125,12 +133,12 @@ class Mixer(rox.Window): n = 0 for channel, id in ALSA_CHANNELS: #if the mixer supports a channel add it - mixer = alsaaudio.Mixer(channel, id, MIXER_DEVICE.value) + mixer = alsaaudio.Mixer(channel, id, mixer_device) option_mask = option_value = 0 if not len(mixer.volumecap()): continue - + if len(mixer.getvolume()) > 1: option_mask |= volumecontrol._STEREO option_mask |= volumecontrol._LOCK @@ -153,7 +161,7 @@ class Mixer(rox.Window): option_value |= volumecontrol._MUTE except: pass - + volume = VolumeControl(n, option_mask, option_value, SHOW_VALUES.int_value, channel) volume.set_level(self.get_volume(n)) @@ -187,8 +195,8 @@ class Mixer(rox.Window): def setting_toggled(self, vol, channel, button, val): """Handle checkbox toggles""" ch, id = ALSA_CHANNELS[channel] - mixer = alsaaudio.Mixer(ch, id, MIXER_DEVICE.value) - + mixer = alsaaudio.Mixer(ch, id, mixer_device) + if button == volumecontrol._MUTE: mixer.setmute(val) @@ -210,8 +218,8 @@ class Mixer(rox.Window): def set_volume(self, volume, channel): """Set the playback volume""" ch, id = ALSA_CHANNELS[channel] - mixer = alsaaudio.Mixer(ch, id, MIXER_DEVICE.value) - + mixer = alsaaudio.Mixer(ch, id, mixer_device) + try: mixer.setvolume(volume[0], 0) mixer.setvolume(volume[1], 1) @@ -221,7 +229,7 @@ class Mixer(rox.Window): def get_volume(self, channel): """Get the current sound card setting for specified channel""" ch, id = ALSA_CHANNELS[channel] - mixer = alsaaudio.Mixer(ch, id, MIXER_DEVICE.value) + mixer = alsaaudio.Mixer(ch, id, mixer_device) vol = mixer.getvolume() if len(vol) == 1: return (vol[0], vol[0]) diff --git a/volume.py b/volume.py index 5274d24..81a656b 100644 --- a/volume.py +++ b/volume.py @@ -44,14 +44,23 @@ SHOW_ICON = Option('show_icon', True) SHOW_BAR = Option('show_bar', False) THEME = Option('theme', 'gtk-theme') +# support two different alsaaudio APIs +if hasattr(alsaaudio, 'cards'): + try: + mixer_device = alsaaudio.cards().index(MIXER_DEVICE.value) + except ValueError: + mixer_device = 0 +else: + mixer_device = MIXER_DEVICE.value + try: ALSA_CHANNELS = [] - for channel in alsaaudio.mixers(MIXER_DEVICE.value): + for channel in alsaaudio.mixers(mixer_device): id = 0 while (channel,id) in ALSA_CHANNELS: id += 1 try: - mixer = alsaaudio.Mixer(channel, id, MIXER_DEVICE.value) + mixer = alsaaudio.Mixer(channel, id, mixer_device) except alsaaudio.ALSAAudioError: continue if len(mixer.volumecap()): @@ -85,7 +94,7 @@ def build_channel_list(box, node, label, option): label = item.get_text() if label == option.value: button.set_history(i) - + def read_channel(): return button.child.get_text() box.handlers[option] = (read_channel, update_channel) button.connect('changed', lambda w: box.check_widget(option)) @@ -98,8 +107,8 @@ rox.app_options.notify() class Volume(applet.Applet): icons = [] size = 24 - - + + """An applet to control a sound card Master or PCM volume""" def __init__(self, filename): applet.Applet.__init__(self, filename) @@ -115,7 +124,7 @@ class Volume(applet.Applet): bar_orient = gtk.PROGRESS_BOTTOM_TO_TOP self.add(self.box) - + self.load_icons() self.image = gtk.Image() self.box.pack_start(self.image) @@ -145,16 +154,16 @@ class Volume(applet.Applet): self.thing = None try: - self.mixer = alsaaudio.Mixer(VOLUME_CONTROL.value, 0, MIXER_DEVICE.value) + self.mixer = alsaaudio.Mixer(VOLUME_CONTROL.value, 0, mixer_device) except: - rox.info(_('Failed to open Mixer device "%s". Please select a different device.\n') % MIXER_DEVICE.value) + rox.info(_('Failed to open Mixer device "%s". Please select a different device.\n') % mixer_device) return - + self.get_volume() self.update_ui() self.show_all() self.show() - + if not SHOW_ICON.int_value: self.image.hide() if not SHOW_BAR.int_value: @@ -303,7 +312,7 @@ class Volume(applet.Applet): vol = self.mixer.getvolume() self.level = vol return (vol[0], vol[1]) - + def mute(self): try: mute = self.mixer.getmute()[0] @@ -314,12 +323,12 @@ class Volume(applet.Applet): self.update_ui() except: rox.info(_('Device does not support Muting.')) - + def update_ui(self): vol = self.level try: mute = self.mixer.getmute()[0] except: mute = False - + if (vol[0] <= 0) or mute: self.pixbuf = self.icons[0] elif vol[0] >= 66: @@ -331,14 +340,14 @@ class Volume(applet.Applet): self.resize_image(self.size) self.tips.set_tip(self, _('Volume control') + ': %d%%' % min(vol[0], vol[1])) - if self.thing: + if self.thing: self.volume.set_level((vol[0], vol[1])) - self.bar.set_fraction(max(vol[0], vol[1])/100.0) + self.bar.set_fraction(max(vol[0], vol[1])/100.0) def get_options(self): """Used as the notify callback when options change""" if VOLUME_CONTROL.has_changed: - self.mixer = alsaaudio.Mixer(VOLUME_CONTROL.value, 0, MIXER_DEVICE.value) + self.mixer = alsaaudio.Mixer(VOLUME_CONTROL.value, 0, mixer_device) self.get_volume() self.update_ui() @@ -353,7 +362,7 @@ class Volume(applet.Applet): self.image.show() else: self.image.hide() - + if THEME.has_changed: self.load_icons() self.update_ui() -- 2.11.4.GIT