def record_ringbuffer(self, ringbuffer, channels=None, start=0, allow_belated=True): """Send a `RingBuffer` to the callback to be recorded into. By default, the number of channels is obtained from the ring buffer's :attr:`~RingBuffer.elementsize`. """ samplesize, _ = _sd._split(self.samplesize) if channels is None: channels = ringbuffer.elementsize // samplesize channels, mapping = self._check_channels(channels, 'input') if ringbuffer.elementsize != samplesize * channels: raise ValueError('Incompatible elementsize') action = _ffi.new( 'struct action*', dict( type=RECORD_RINGBUFFER, allow_belated=allow_belated, requested_time=start, ringbuffer=ringbuffer._ptr, total_frames=ULONG_MAX, channels=channels, mapping=mapping, )) self._enqueue(action) return action
def _check_channels(self, channels, kind): """Check if number of channels or mapping was given.""" assert kind in ('input', 'output') try: channels, mapping = len(channels), channels except TypeError: mapping = tuple(range(1, channels + 1)) max_channels = _sd._split(self.channels)[kind == 'output'] if max(mapping) > max_channels: raise ValueError('Channel number too large') if min(mapping) < 1: raise ValueError('Channel numbers start with 1') return channels, mapping
def record_buffer(self, buffer, channels, start=0, allow_belated=True): """Send a buffer to the callback to be recorded into. """ channels, mapping = self._check_channels(channels, 'input') buffer = _ffi.from_buffer(buffer) samplesize, _ = _sd._split(self.samplesize) action = _ffi.new('struct action*', dict( type=RECORD_BUFFER, allow_belated=allow_belated, requested_time=start, buffer=_ffi.cast('float*', buffer), total_frames=len(buffer) // channels // samplesize, channels=channels, mapping=mapping, )) self._enqueue(action) return action
def play_buffer(self, buffer, channels, start=0, allow_belated=True): """Send a buffer to the callback to be played back. After that, the *buffer* must not be written to anymore. """ channels, mapping = self._check_channels(channels, 'output') buffer = _ffi.from_buffer(buffer) _, samplesize = _sd._split(self.samplesize) action = _ffi.new('struct action*', dict( type=PLAY_BUFFER, allow_belated=allow_belated, requested_time=start, buffer=_ffi.cast('float*', buffer), total_frames=len(buffer) // channels // samplesize, channels=channels, mapping=mapping, )) self._enqueue(action) return action