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.__alsaaudio__ is None): # output hasn't been initialized from audiotools.output import ALSAAudio AudioOutput.set_format(self, sample_rate, channels, channel_mask, bits_per_sample) self.__alsaaudio__ = ALSAAudio("default", sample_rate, channels, 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 to different format self.close() self.set_format(sample_rate=sample_rate, channels=channels, channel_mask=channel_mask, bits_per_sample=bits_per_sample)
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.__alsaaudio__ is None: # output hasn't been initialized from audiotools.output import ALSAAudio AudioOutput.set_format(self, sample_rate, channels, channel_mask, bits_per_sample) self.__alsaaudio__ = ALSAAudio("default", sample_rate, channels, 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 to different format self.close() self.set_format( sample_rate=sample_rate, channels=channels, channel_mask=channel_mask, bits_per_sample=bits_per_sample )
class ALSAAudioOutput(AudioOutput): """an AudioOutput subclass for ALSA output""" NAME = "ALSA" def __init__(self): self.__alsaaudio__ = None AudioOutput.__init__(self) def __getstate__(self): """gets internal state for use by Pickle module""" return "ALSA" def __setstate__(self, name): """sets internal state for use by Pickle module""" AudioOutput.__setstate__(self, name) self.__alsaaudio__ = None def description(self): """returns user-facing name of output device as unicode""" # FIXME - pull this from device description return u"Advanced Linux Sound Architecture" 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.__alsaaudio__ is None: # output hasn't been initialized from audiotools.output import ALSAAudio AudioOutput.set_format(self, sample_rate, channels, channel_mask, bits_per_sample) self.__alsaaudio__ = ALSAAudio("default", sample_rate, channels, 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 to 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.__alsaaudio__.play(framelist) def pause(self): """pauses audio output, with the expectation it will be resumed""" if self.__alsaaudio__ is not None: self.__alsaaudio__.pause() def resume(self): """resumes playing paused audio output""" if self.__alsaaudio__ is not None: self.__alsaaudio__.resume() def get_volume(self): """returns a floating-point volume value between 0.0 and 1.0""" if self.__alsaaudio__ is None: self.set_format(*DEFAULT_FORMAT) return self.__alsaaudio__.get_volume() 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.__alsaaudio__ is None: self.set_format(*DEFAULT_FORMAT) self.__alsaaudio__.set_volume(volume) else: raise ValueError("volume must be between 0.0 and 1.0") def close(self): """closes the output stream""" AudioOutput.close(self) if self.__alsaaudio__ is not None: self.__alsaaudio__.flush() self.__alsaaudio__.close() self.__alsaaudio__ = None @classmethod def available(cls): """returns True if ALSA is available and running on the system""" try: from audiotools.output import ALSAAudio return True except ImportError: return False
class ALSAAudioOutput(AudioOutput): """an AudioOutput subclass for ALSA output""" NAME = "ALSA" def __init__(self): self.__alsaaudio__ = None AudioOutput.__init__(self) def __getstate__(self): """gets internal state for use by Pickle module""" return "ALSA" def __setstate__(self, name): """sets internal state for use by Pickle module""" AudioOutput.__setstate__(self, name) self.__alsaaudio__ = None def description(self): """returns user-facing name of output device as unicode""" # FIXME - pull this from device description return u"Advanced Linux Sound Architecture" 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.__alsaaudio__ is None): # output hasn't been initialized from audiotools.output import ALSAAudio AudioOutput.set_format(self, sample_rate, channels, channel_mask, bits_per_sample) self.__alsaaudio__ = ALSAAudio("default", sample_rate, channels, 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 to 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.__alsaaudio__.play(framelist) def pause(self): """pauses audio output, with the expectation it will be resumed""" if (self.__alsaaudio__ is not None): self.__alsaaudio__.pause() def resume(self): """resumes playing paused audio output""" if (self.__alsaaudio__ is not None): self.__alsaaudio__.resume() def get_volume(self): """returns a floating-point volume value between 0.0 and 1.0""" if (self.__alsaaudio__ is None): self.set_format(*DEFAULT_FORMAT) return self.__alsaaudio__.get_volume() 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.__alsaaudio__ is None): self.set_format(*DEFAULT_FORMAT) self.__alsaaudio__.set_volume(volume) else: raise ValueError("volume must be between 0.0 and 1.0") def close(self): """closes the output stream""" AudioOutput.close(self) if (self.__alsaaudio__ is not None): self.__alsaaudio__.flush() self.__alsaaudio__.close() self.__alsaaudio__ = None @classmethod def available(cls): """returns True if ALSA is available and running on the system""" try: from audiotools.output import ALSAAudio return True except ImportError: return False