Python alsaaudio.ALSAAudioError() Examples

The following are 9 code examples of alsaaudio.ALSAAudioError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module alsaaudio , or try the search function .
Example #1
Source File: console_callbacks.py    From spotify-connect-web with MIT License 6 votes vote down vote up
def acquire(self):
        if self._session.is_active():
            try:
                pcm_args = {
                    'type': alsa.PCM_PLAYBACK,
                    'mode': alsa.PCM_NORMAL,
                }
                if self._args.playback_device != 'default':
                    pcm_args['device'] = self._args.playback_device
                else:
                    pcm_args['card'] = self._args.device
                pcm = alsa.PCM(**pcm_args)

                pcm.setchannels(CHANNELS)
                pcm.setrate(RATE)
                pcm.setperiodsize(PERIODSIZE)
                pcm.setformat(alsa.PCM_FORMAT_S16_LE)

                self._device = pcm
                print "AlsaSink: device acquired"
            except alsa.ALSAAudioError as error:
                print "Unable to acquire device: ", error
                self.release() 
Example #2
Source File: sound_manager.py    From piclodio3 with MIT License 5 votes vote down vote up
def _get_mixer():
        try:
            mixer = alsaaudio.Mixer()
        except alsaaudio.ALSAAudioError:
            # no master, we are on a Rpi
            mixer = alsaaudio.Mixer("PCM")
        return mixer 
Example #3
Source File: console_callbacks.py    From spotify-connect-web with MIT License 5 votes vote down vote up
def write(self, data):
        if self._session.is_active() and self._device is not None:
            # write is asynchronous, so, we are in race with releasing the device
            self._lock.acquire()
            try:
                if self._device is not None:
                    self._device.write(data)
            except alsa.ALSAAudioError as error:
                print "Ups! Some badness happened: ", error
            finally:
                self._lock.release() 
Example #4
Source File: mixer.py    From mopidy-alsamixer with Apache License 2.0 5 votes vote down vote up
def __init__(self, config):
        super().__init__()
        self.config = config
        self.cardindex = self.config["alsamixer"]["card"]
        self.control = self.config["alsamixer"]["control"]
        self.min_volume = self.config["alsamixer"]["min_volume"]
        self.max_volume = self.config["alsamixer"]["max_volume"]
        self.volume_scale = self.config["alsamixer"]["volume_scale"]

        known_cards = alsaaudio.cards()
        try:
            known_controls = alsaaudio.mixers(self.cardindex)
        except alsaaudio.ALSAAudioError:
            raise exceptions.MixerError(
                f"Could not find ALSA soundcard with index {self.cardindex:d}. "
                "Known soundcards include: "
                ", ".join(known_cards)
            )

        if self.control not in known_controls:
            raise exceptions.MixerError(
                f"Could not find ALSA mixer control {self.control} on "
                f"card {self.cardindex:d}. "
                f"Known mixers on card {self.cardindex:d} include: "
                ", ".join(known_controls)
            )

        self._last_volume = None
        self._last_mute = None

        logger.info(
            f"Mixing using ALSA, card {self.cardindex:d}, "
            f"mixer control {self.control!r}."
        ) 
Example #5
Source File: mixer.py    From mopidy-alsamixer with Apache License 2.0 5 votes vote down vote up
def get_mute(self):
        try:
            channels_muted = self._mixer.getmute()
        except alsaaudio.ALSAAudioError as exc:
            logger.debug(f"Getting mute state failed: {exc}")
            return None
        if all(channels_muted):
            return True
        elif not any(channels_muted):
            return False
        else:
            # Not all channels have the same mute state
            return None 
Example #6
Source File: mixer.py    From mopidy-alsamixer with Apache License 2.0 5 votes vote down vote up
def set_mute(self, mute):
        try:
            self._mixer.setmute(int(mute))
            return True
        except alsaaudio.ALSAAudioError as exc:
            logger.debug(f"Setting mute state failed: {exc}")
            return False 
Example #7
Source File: test_mixer.py    From mopidy-alsamixer with Apache License 2.0 5 votes vote down vote up
def test_fails_if_card_is_unknown(self, alsa_mock):
        alsa_mock.cards.return_value = ["PCH", "SB"]
        alsa_mock.mixers.side_effect = alsa_mock.ALSAAudioError(
            "No such file or directory [hw:2]"
        )
        config = {"alsamixer": {"card": 2, "control": "Master"}}

        with self.assertRaises(exceptions.MixerError):
            self.get_mixer(config=config)

        alsa_mock.mixers.assert_called_once_with(2) 
Example #8
Source File: test_mixer.py    From mopidy-alsamixer with Apache License 2.0 5 votes vote down vote up
def test_get_mute_when_unsupported(self, alsa_mock):
        alsa_mock.ALSAAudioError = alsaaudio.ALSAAudioError
        mixer = self.get_mixer(alsa_mock)
        mixer_mock = alsa_mock.Mixer.return_value
        mixer_mock.getmute.side_effect = alsa_mock.ALSAAudioError

        self.assertIsNone(mixer.get_mute())

        mixer_mock.getmute.assert_called_once_with() 
Example #9
Source File: sound_alsa.py    From pynab with GNU General Public License v3.0 4 votes vote down vote up
def __test_device(device, record):
        """
            Test selected ALSA device, making sure it handles both stereo and
            mono and both 44.1KHz and 22.05KHz on output, mono and 16 kHz on
            input.

            On a typical RPI configuration, default with hifiberry card is not
            configured to do software-mono, so we'll use
            plughw:CARD=sndrpihifiberry instead.
            Likewise, on 2019 cards, hw:CARD=seeed2micvoicec is not able to run
            mono sound.

            @param device: name of the sound device
            @type device: six.text_type
            @param record: C{True} if this method is looking for recording
            device. C{False} if the device should only playback.
            @type record: bool
        """
        try:
            dev = None

            if record:
                dev = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, device=device)
            else:
                dev = alsaaudio.PCM(device=device)

            if (
                dev.setformat(alsaaudio.PCM_FORMAT_S16_LE)
                != alsaaudio.PCM_FORMAT_S16_LE
            ):
                return False
            if record:
                if dev.setchannels(1) != 1:
                    return False
                if dev.setrate(16000) != 16000:
                    return False
            else:
                if dev.setchannels(2) != 2:
                    return False
                if dev.setchannels(1) != 1:
                    return False
                if dev.setrate(44100) != 44100:
                    return False
                if dev.setrate(22050) != 22050:
                    return False
        except alsaaudio.ALSAAudioError:
            return False
        finally:
            if dev:
                dev.close()
        return True