Esempio n. 1
0
def get_volume():
    vol = None
    switch = False
    if nwg_panel.common.dependencies["pyalsa"]:
        mixer = alsamixer.Mixer()
        mixer.attach()
        mixer.load()

        element = alsamixer.Element(mixer, "Master")
        max_vol = element.get_volume_range()[1]
        vol = int(round(element.get_volume() * 100 / max_vol, 0))
        switch = element.get_switch()
        del mixer
    else:
        result = cmd2string(nwg_panel.common.commands["get_volume_alt"])
        if result:
            lines = result.splitlines()
            for line in lines:
                if "Front Left:" in line:
                    try:
                        vol = int(line.split()[4][1:-2])
                    except:
                        pass
                    switch = "on" in line.split()[5]
                    break

    return vol, switch
Esempio n. 2
0
def get_volume(alt_cmd):
    vol = None
    switch = False
    if py_alsa:
        mixer = alsamixer.Mixer()
        mixer.attach()
        mixer.load()

        element = alsamixer.Element(mixer, "Master")
        max_vol = element.get_volume_range()[1]
        vol = int(round(element.get_volume() * 100 / max_vol, 0))
        switch = element.get_switch()
        del mixer
    else:
        result = cmd2string(alt_cmd)
        if result:
            lines = result.splitlines()
            for line in lines:
                if "Front Left:" in line:
                    try:
                        vol = int(line.split()[4][1:-2])
                    except:
                        pass
                    switch = "on" in line.split()[5]
                    break

    return vol, switch
Esempio n. 3
0
def set_volume(percent, alt_cmd):
    if py_alsa:
        mixer = alsamixer.Mixer()
        mixer.attach()
        mixer.load()

        element = alsamixer.Element(mixer, "Master")
        max_vol = element.get_volume_range()[1]
        element.set_volume_all(int(percent * max_vol / 100))
        del mixer
    else:
        cmd = "{} {}% /dev/null 2>&1".format(alt_cmd, percent)
        subprocess.call(cmd, shell=True)
Esempio n. 4
0
 def get_mixers(self, card_index=0):
     """ Returns mixers list """
     mixers = []
     try:
         mixer = alsamixer.Mixer()
         mixer.attach("hw:%d" % card_index)
         mixer.load()
         for mix in mixer.list():
             m = alsamixer.Element(mixer=mixer, name=mix[0], index=0)
             if m.has_volume():
                 if mix[0] not in mixers:
                     mixers.append(mix[0])
     except Exception, err:
         log.exception(str(err))
Esempio n. 5
0
    def init_alsa(self):
        self.mixer = alsamixer.Mixer()
        self.mixer.attach()
        self.mixer.load()
        self.element = alsamixer.Element(self.mixer, VOL_CTRL)
        self.element.set_callback(self.on_element_changed)
        self.volume_range = self.element.get_volume_range()

        # Setup mixer changes event handling
        self.poller = select.poll()
        self.mixer.register_poll(self.poller)
        self.handle = True
        gobject.timeout_add(POLL_TIMEOUT,
                            self.poll)
        self.poller.poll()
Esempio n. 6
0
def set_volume(slider):
    percent = slider.get_value()
    if nwg_panel.common.dependencies["pyalsa"]:
        mixer = alsamixer.Mixer()
        mixer.attach()
        mixer.load()

        element = alsamixer.Element(mixer, "Master")
        max_vol = element.get_volume_range()[1]
        element.set_volume_all(int(percent * max_vol / 100))
        del mixer
    else:
        cmd = "{} {}% /dev/null 2>&1".format(
            nwg_panel.common.commands["set_volume_alt"], percent)
        subprocess.call(cmd, shell=True)
Esempio n. 7
0
 def get_mixer_list(self):
     """ Append to mixer list, mixers with equal names
     are grouped and controlled together. """
     mixerlist = []
     for mixer in self.mixer.list():
         try:
             if mixer[0] not in mixerlist:
                 mixerlist.append(mixer[0])
                 seq = 0
             else:
                 seq += 1
             if self.control == mixer[0]:
                 m = alsamixer.Element(mixer=self.mixer,
                                       name=mixer[0],
                                       index=seq)
                 self.mixerlist.append(m)
         except Exception:
             pass
Esempio n. 8
0
    def init_mixers(self):
        '''Initialize handling of ALSA mixer devices.'''
        self._mixer = {}
        if alsamixer is None:
            self.log.warn('no ALSA mixer support')
            return

        self.log.debug('start init mixers')

        for mixer_name, mixer in self.mixers.items():
            m = alsamixer.Mixer()
            try:
                m.attach(mixer_name)
            except RuntimeError:
                raise MissingALSADevice(mixer_name)

            m.load()
            for element_name, element in mixer.items():
                try:
                    e = alsamixer.Element(m, element_name)
                except IOError:
                    raise MissingALSADevice('%s.%s' % (
                        mixer_name, element_name))

                # create output device controls
                for channel, control in element.get('output', {}).items():
                    tag = '%s.%s.%s.out' % (
                        mixer_name,
                        element_name,
                        channel)

                    self.init_mixer_device(tag, m, e, channel, control)

                # create capture device controls
                for channel, control in element.get('capture', {}).items():
                    tag = '%s.%s.%s.in' % (
                        mixer_name,
                        element_name,
                        channel)

                    self.init_mixer_device(tag, m, e, channel, control,
                                           capture=True)

        self.log.debug('done init mixers')
Esempio n. 9
0
def set_volume(slider):
    percent = slider.get_value()
    if nwg_panel.common.dependencies["pyalsa"]:
        mixer = alsamixer.Mixer()
        mixer.attach()
        mixer.load()
        try:
            element = alsamixer.Element(mixer,
                                        nwg_panel.common.defaults["master"])
            max_vol = element.get_volume_range()[1]
            element.set_volume_all(int(percent * max_vol / 100))
        except Exception as e:
            eprint(e)
        del mixer
    else:
        c = "amixer sset {}".format(nwg_panel.common.defaults["master"])
        cmd = "{} {}% /dev/null 2>&1".format(c, percent)
        try:
            subprocess.call(cmd.split())
        except Exception as e:
            eprint(e)
Esempio n. 10
0
    def _set_mic_boost_control(self, element):
	self.mic_boost = amixer.Element(self.mixer, element[0], element[1])
Esempio n. 11
0
    def _set_in_idx_control(self, element):
	self.in_idx = amixer.Element(self.mixer, element[0], element[1])
Esempio n. 12
0
    def _set_input_src_control(self, element):
	self.input_src = amixer.Element(self.mixer, element[0], element[1])
Esempio n. 13
0
    def _set_capture_control(self, element):
	self.capture = amixer.Element(self.mixer, element[0], element[1])
Esempio n. 14
0
    def _set_pcm_control(self, element):
	self.pcm = amixer.Element(self.mixer, element[0], element[1])
Esempio n. 15
0
    def _set_master_control(self, element):
	self.master = amixer.Element(self.mixer, element[0], element[1])
Esempio n. 16
0
def get_volume():
    vol = 0
    switch = False
    if nwg_panel.common.dependencies["pyalsa"]:
        mixer = alsamixer.Mixer()
        mixer.attach()
        mixer.load()
        # https://github.com/nwg-piotr/nwg-panel/issues/24
        try:
            element = alsamixer.Element(mixer,
                                        nwg_panel.common.defaults["master"])
            max_vol = element.get_volume_range()[1]
            vol = int(round(element.get_volume() * 100 / max_vol, 0))
            switch = element.get_switch()
        except:
            try:
                element = alsamixer.Element(mixer, mixer.list()[0][0])
                # Overwrite user-defined name if caused error
                print("'{}' didn't work, using {} instead".format(
                    nwg_panel.common.defaults["master"], element.name))
                nwg_panel.common.defaults["master"] = element.name

                max_vol = element.get_volume_range()[1]
                vol = int(round(element.get_volume() * 100 / max_vol, 0))
                switch = element.get_switch()
            except:
                return 0, False

        del mixer

    elif nwg_panel.common.dependencies["amixer"]:
        # Same issue as above
        result = cmd2string("amixer sget {}".format(
            nwg_panel.common.defaults["master"]))
        if not result:
            try:
                nwg_panel.common.defaults["master"] = get_scontrol()
                result = cmd2string("amixer sget {}".format(
                    nwg_panel.common.defaults["master"]))
            except:
                result = None

        if result:
            lines = result.splitlines()
            for line in lines:
                if line.strip().startswith("Mono:"):
                    try:
                        vol = int(line.split()[3][1:-2])
                        try:
                            switch = "on" in line.split()[5]
                        except:
                            switch = "on" in line.split()[4]
                        break
                    except:
                        pass

                if line.strip().startswith("Front Left:"):
                    try:
                        vol = int(line.split()[4][1:-2])
                        switch = "on" in line.split()[5]
                        break
                    except:
                        pass

    return vol, switch