Exemplo n.º 1
0
    def set_format(self, sample_rate, channels, channel_mask, bits_per_sample):
        """sets the output stream to the given format

        if the stream hasn't been initialized, this method initializes it

        if the stream has been initialized to a different format,
        this method closes and reopens the stream to the new format

        if the stream has been initialized to the same format,
        this method does nothing"""

        if (self.__coreaudio__ is None):
            # output hasn't been initialized

            from audiotools.output import CoreAudio

            AudioOutput.set_format(self, sample_rate, channels, channel_mask,
                                   bits_per_sample)

            self.__coreaudio__ = CoreAudio(sample_rate, channels, channel_mask,
                                           bits_per_sample)
        elif (not self.compatible(sample_rate=sample_rate,
                                  channels=channels,
                                  channel_mask=channel_mask,
                                  bits_per_sample=bits_per_sample)):
            # output has been initialized in a different format

            self.close()
            self.set_format(sample_rate=sample_rate,
                            channels=channels,
                            channel_mask=channel_mask,
                            bits_per_sample=bits_per_sample)
Exemplo n.º 2
0
    def set_format(self, sample_rate, channels, channel_mask, bits_per_sample):
        """sets the output stream to the given format

        if the stream hasn't been initialized, this method initializes it

        if the stream has been initialized to a different format,
        this method closes and reopens the stream to the new format

        if the stream has been initialized to the same format,
        this method does nothing"""

        if self.__coreaudio__ is None:
            # output hasn't been initialized

            from audiotools.output import CoreAudio

            AudioOutput.set_format(self, sample_rate, channels, channel_mask, bits_per_sample)

            self.__coreaudio__ = CoreAudio(sample_rate, channels, channel_mask, bits_per_sample)
        elif not self.compatible(
            sample_rate=sample_rate, channels=channels, channel_mask=channel_mask, bits_per_sample=bits_per_sample
        ):
            # output has been initialized in a different format

            self.close()
            self.set_format(
                sample_rate=sample_rate, channels=channels, channel_mask=channel_mask, bits_per_sample=bits_per_sample
            )
Exemplo n.º 3
0
class CoreAudioOutput(AudioOutput):
    """an AudioOutput subclass for CoreAudio output"""

    NAME = "CoreAudio"

    def __init__(self):
        self.__coreaudio__ = None
        AudioOutput.__init__(self)

    def __getstate__(self):
        """gets internal state for use by Pickle module"""

        return "CoreAudio"

    def __setstate__(self, name):
        """sets internal state for use by Pickle module"""

        AudioOutput.__setstate__(self, name)
        self.__coreaudio__ = None

    def description(self):
        """returns user-facing name of output device as unicode"""

        # FIXME - pull this from device description
        return u"Core Audio"

    def set_format(self, sample_rate, channels, channel_mask, bits_per_sample):
        """sets the output stream to the given format

        if the stream hasn't been initialized, this method initializes it

        if the stream has been initialized to a different format,
        this method closes and reopens the stream to the new format

        if the stream has been initialized to the same format,
        this method does nothing"""

        if self.__coreaudio__ is None:
            # output hasn't been initialized

            from audiotools.output import CoreAudio

            AudioOutput.set_format(self, sample_rate, channels, channel_mask, bits_per_sample)

            self.__coreaudio__ = CoreAudio(sample_rate, channels, channel_mask, bits_per_sample)
        elif not self.compatible(
            sample_rate=sample_rate, channels=channels, channel_mask=channel_mask, bits_per_sample=bits_per_sample
        ):
            # output has been initialized in a different format

            self.close()
            self.set_format(
                sample_rate=sample_rate, channels=channels, channel_mask=channel_mask, bits_per_sample=bits_per_sample
            )

    def play(self, framelist):
        """plays a FrameList"""

        self.__coreaudio__.play(framelist.to_bytes(False, True))

    def pause(self):
        """pauses audio output, with the expectation it will be resumed"""

        if self.__coreaudio__ is not None:
            self.__coreaudio__.pause()

    def resume(self):
        """resumes playing paused audio output"""

        if self.__coreaudio__ is not None:
            self.__coreaudio__.resume()

    def get_volume(self):
        """returns a floating-point volume value between 0.0 and 1.0"""

        if self.__coreaudio__ is None:
            self.set_format(*DEFAULT_FORMAT)
        try:
            return self.__coreaudio__.get_volume()
        except ValueError:
            # get_volume_scalar() call was unsuccessful
            return 1.0

    def set_volume(self, volume):
        """sets the output volume to a floating point value
        between 0.0 and 1.0"""

        if (volume >= 0) and (volume <= 1.0):
            if self.__coreaudio__ is None:
                self.set_format(*DEFAULT_FORMAT)
            try:
                self.__coreaudio__.set_volume(volume)
            except ValueError:
                # set_volume_scalar() call was unsuccessful
                pass
        else:
            raise ValueError("volume must be between 0.0 and 1.0")

    def close(self):
        """closes the output stream"""

        AudioOutput.close(self)

        if self.__coreaudio__ is not None:
            self.__coreaudio__.flush()
            self.__coreaudio__.close()
            self.__coreaudio__ = None

    @classmethod
    def available(cls):
        """returns True if the AudioOutput is available on the system"""

        try:
            from audiotools.output import CoreAudio

            return True
        except ImportError:
            return False
Exemplo n.º 4
0
class CoreAudioOutput(AudioOutput):
    """an AudioOutput subclass for CoreAudio output"""

    NAME = "CoreAudio"

    def __init__(self):
        self.__coreaudio__ = None
        AudioOutput.__init__(self)

    def __getstate__(self):
        """gets internal state for use by Pickle module"""

        return "CoreAudio"

    def __setstate__(self, name):
        """sets internal state for use by Pickle module"""

        AudioOutput.__setstate__(self, name)
        self.__coreaudio__ = None

    def description(self):
        """returns user-facing name of output device as unicode"""

        # FIXME - pull this from device description
        return u"Core Audio"

    def set_format(self, sample_rate, channels, channel_mask, bits_per_sample):
        """sets the output stream to the given format

        if the stream hasn't been initialized, this method initializes it

        if the stream has been initialized to a different format,
        this method closes and reopens the stream to the new format

        if the stream has been initialized to the same format,
        this method does nothing"""

        if (self.__coreaudio__ is None):
            # output hasn't been initialized

            from audiotools.output import CoreAudio

            AudioOutput.set_format(self, sample_rate, channels, channel_mask,
                                   bits_per_sample)

            self.__coreaudio__ = CoreAudio(sample_rate, channels, channel_mask,
                                           bits_per_sample)
        elif (not self.compatible(sample_rate=sample_rate,
                                  channels=channels,
                                  channel_mask=channel_mask,
                                  bits_per_sample=bits_per_sample)):
            # output has been initialized in a different format

            self.close()
            self.set_format(sample_rate=sample_rate,
                            channels=channels,
                            channel_mask=channel_mask,
                            bits_per_sample=bits_per_sample)

    def play(self, framelist):
        """plays a FrameList"""

        self.__coreaudio__.play(framelist.to_bytes(False, True))

    def pause(self):
        """pauses audio output, with the expectation it will be resumed"""

        if (self.__coreaudio__ is not None):
            self.__coreaudio__.pause()

    def resume(self):
        """resumes playing paused audio output"""

        if (self.__coreaudio__ is not None):
            self.__coreaudio__.resume()

    def get_volume(self):
        """returns a floating-point volume value between 0.0 and 1.0"""

        if (self.__coreaudio__ is None):
            self.set_format(*DEFAULT_FORMAT)
        try:
            return self.__coreaudio__.get_volume()
        except ValueError:
            # get_volume_scalar() call was unsuccessful
            return 1.0

    def set_volume(self, volume):
        """sets the output volume to a floating point value
        between 0.0 and 1.0"""

        if ((volume >= 0) and (volume <= 1.0)):
            if (self.__coreaudio__ is None):
                self.set_format(*DEFAULT_FORMAT)
            try:
                self.__coreaudio__.set_volume(volume)
            except ValueError:
                # set_volume_scalar() call was unsuccessful
                pass
        else:
            raise ValueError("volume must be between 0.0 and 1.0")

    def close(self):
        """closes the output stream"""

        AudioOutput.close(self)

        if (self.__coreaudio__ is not None):
            self.__coreaudio__.flush()
            self.__coreaudio__.close()
            self.__coreaudio__ = None

    @classmethod
    def available(cls):
        """returns True if the AudioOutput is available on the system"""

        try:
            from audiotools.output import CoreAudio

            return True
        except ImportError:
            return False