def __init__(self, source_name, rate, list_sources=False): self.source_name = source_name self.rate = rate self.sources = OrderedDict() self._clear_chunk() self._list_sources = list_sources # Exception raised in a callback, to be re-raised in the main thread self._exception = None # Wrap callback methods in appropriate ctypefunc instances so # that the Pulseaudio C API can call them self._context_notify_cb = pa.pa_context_notify_cb_t(self.context_notify_cb) self._source_info_cb = pa.pa_source_info_cb_t(self.source_info_cb) self._stream_read_cb = pa.pa_stream_request_cb_t(self.stream_read_cb) # stream_read_cb() puts finished chunks into this queue self._chunks = Queue.Queue() # Create the mainloop thread and set our context_notify_cb # method to be called when there's updates relating to the # connection to Pulseaudio _mainloop = pa.pa_threaded_mainloop_new() _mainloop_api = pa.pa_threaded_mainloop_get_api(_mainloop) context = pa.pa_context_new(_mainloop_api, sys.argv[0]) pa.pa_context_set_state_callback(context, self._context_notify_cb, None) pa.pa_context_connect(context, None, 0, None) pa.pa_threaded_mainloop_start(_mainloop)
def __init__(self, source_name, rate): self.source_name = source_name self.rate = rate self.bufs = [] self.buf_samples = 0 # Wrap callback methods in appropriate ctypefunc instances so # that the Pulseaudio C API can call them self._context_notify_cb = P.pa_context_notify_cb_t(self.context_notify_cb) self._source_info_cb = P.pa_source_info_cb_t(self.source_info_cb) self._stream_read_cb = P.pa_stream_request_cb_t(self.stream_read_cb) # stream_read_cb() puts peak samples into this Queue instance self._samples = Queue() # Create the mainloop thread and set our context_notify_cb # method to be called when there's updates relating to the # connection to Pulseaudio _mainloop = P.pa_threaded_mainloop_new() _mainloop_api = P.pa_threaded_mainloop_get_api(_mainloop) context = P.pa_context_new(_mainloop_api, 'peak_demo') P.pa_context_set_state_callback(context, self._context_notify_cb, None) P.pa_context_connect(context, None, 0, None) P.pa_threaded_mainloop_start(_mainloop)
def __init__(self, source_name, rate): self.source_name = source_name self.rate = rate self.bufs = [] self.buf_samples = 0 # Wrap callback methods in appropriate ctypefunc instances so # that the Pulseaudio C API can call them self._context_notify_cb = P.pa_context_notify_cb_t(self.context_notify_cb) self._source_info_cb = P.pa_source_info_cb_t(self.source_info_cb) self._stream_read_cb = P.pa_stream_request_cb_t(self.stream_read_cb) # stream_read_cb() puts peak samples into this Queue instance self._samples = Queue() # Create the mainloop thread and set our context_notify_cb # method to be called when there's updates relating to the # connection to Pulseaudio _mainloop = P.pa_threaded_mainloop_new() _mainloop_api = P.pa_threaded_mainloop_get_api(_mainloop) context = P.pa_context_new(_mainloop_api, 'peak_demo') P.pa_context_set_state_callback(context, self._context_notify_cb, None) P.pa_context_connect(context, None, 0, None) P.pa_threaded_mainloop_start(_mainloop)
def __call__(self, app_name, vol_delta=None, toggle_mute=False, mute=None): """ Adjust the volume of a given application stream in pulseaudio WARNING: Not thread safe! This is really intended to be instantiated, called once, then thrown away (treat it like a function - it's only a class because it needs some internal state passed to and from the various callbacks) """ # Is there a way to do these with decorators? # The problem is that transforming these into pa_... types can # only occur when the class is instantiated (and the functions # transformed into instance methods hiding the self parameter # from pa_...), but not when the functions are initially # defined (they are still ordinary functions at that point). # # Basically, I'd need a decorator that does not apply to the # function following it's definition, but instead automatically # applies to the instance method created when the class is # instantiated self._finished_callback = _lib.pa_context_success_cb_t( self._finished_callback) self._finished_ref_callback = _lib.pa_context_success_cb_t( self._finished_ref_callback) self._sink_input_info_callback = _lib.pa_sink_input_info_cb_t( self._sink_input_info_callback) self._sink_info_callback = _lib.pa_sink_info_cb_t( self._sink_info_callback) self._context_state_callback = _lib.pa_context_notify_cb_t( self._context_state_callback) # Only 1 command allowed simultaneously for now assert (int(vol_delta is not None) + int(toggle_mute) + int(mute is not None) == 1) self.app_name = app_name self.vol_delta = vol_delta self.toggle_mute = toggle_mute self.mute = mute self.ret_vol = None self.ret_mute = None self.eof = None self.count = 0 self._main() return (self.ret_vol, self.ret_mute)
def __call__(self, app_name, vol_delta=None, toggle_mute=False, mute=None): """ Adjust the volume of a given application stream in pulseaudio WARNING: Not thread safe! This is really intended to be instantiated, called once, then thrown away (treat it like a function - it's only a class because it needs some internal state passed to and from the various callbacks) """ # Is there a way to do these with decorators? # The problem is that transforming these into pa_... types can # only occur when the class is instantiated (and the functions # transformed into instance methods hiding the self parameter # from pa_...), but not when the functions are initially # defined (they are still ordinary functions at that point). # # Basically, I'd need a decorator that does not apply to the # function following it's definition, but instead automatically # applies to the instance method created when the class is # instantiated self._finished_callback = _lib.pa_context_success_cb_t(self._finished_callback) self._finished_ref_callback = _lib.pa_context_success_cb_t(self._finished_ref_callback) self._sink_input_info_callback = _lib.pa_sink_input_info_cb_t(self._sink_input_info_callback) self._sink_info_callback = _lib.pa_sink_info_cb_t(self._sink_info_callback) self._context_state_callback = _lib.pa_context_notify_cb_t(self._context_state_callback) # Only 1 command allowed simultaneously for now assert(int(vol_delta is not None) + int(toggle_mute) + int(mute is not None) == 1) self.app_name = app_name self.vol_delta = vol_delta self.toggle_mute = toggle_mute self.mute = mute self.ret_vol = None self.ret_mute = None self.eof = None self.count = 0 self._main() return (self.ret_vol, self.ret_mute)