def write(self, frames, num_frames=None, exception_on_underflow=False): """ Write samples to the stream. Do not call when using *non-blocking* mode. :param frames: The frames of data. :param num_frames: The number of frames to write. Defaults to None, in which this value will be automatically computed. :param exception_on_underflow: Specifies whether an IOError exception should be thrown (or silently ignored) on buffer underflow. Defaults to False for improved performance, especially on slower platforms. :raises IOError: if the stream is not an output stream or if the write operation was unsuccessful. :rtype: `None` """ if not self._is_output: raise IOError("Not output stream", paCanNotWriteToAnInputOnlyStream) if num_frames == None: # determine how many frames to read width = get_sample_size(self._format) num_frames = int(len(frames) / (self._channels * width)) #print len(frames), self._channels, self._width, num_frames pa.write_stream(self._stream, frames, num_frames, exception_on_underflow)
def run(self): """ Plays the audio. This method plays the audio, and shouldn't be called explicitly, let the constructor do so. """ # From now on, it's multi-thread. Let the force be with them. st = self.stream._stream for chunk in chunks(self.audio, size=self.chunk_size*self.nchannels, dfmt=self.dfmt): #Below is a faster way to call: # self.stream.write(chunk, self.chunk_size) _portaudio.write_stream(st, chunk, self.chunk_size, False) if not self.go.is_set(): self.stream.stop_stream() if self.halting: break self.go.wait() self.stream.start_stream() # Finished playing! Destructor-like step: let's close the thread with self.lock: if self in self.device_manager._threads: # If not already closed self.stream.close() self.device_manager.thread_finished(self)
def write(self, frames, num_frames = None, exception_on_underflow = False): """ Write samples to the stream. :param `frames`: The frames of data. :param `num_frames`: The number of frames to write. Defaults to None, in which this value will be automatically computed. :param `exception_on_underflow`: Specifies whether an exception should be thrown (or silently ignored) on buffer underflow. Defaults to False for improved performance, especially on slower platforms. :raises IOError: if the stream is not an output stream or if the write operation was unsuccessful. :rtype: `None` """ if not self._is_output: raise IOError("Not output stream", paCanNotWriteToAnInputOnlyStream) if num_frames == None: # determine how many frames to read width = get_sample_size(self._format) num_frames = int(len(frames) / (self._channels * width)) #print len(frames), self._channels, self._width, num_frames pa.write_stream(self._stream, frames, num_frames, exception_on_underflow)
stream_output = _portaudio.open(format = FORMAT, channels = CHANNELS, rate = RATE, output = True, frames_per_buffer = chunk) print "* starting stream" _portaudio.start_stream(stream_input) _portaudio.start_stream(stream_output) print "* recording" for i in range(0, 44100 / chunk * RECORD_SECONDS): data = _portaudio.read_stream(stream_input, chunk) _portaudio.write_stream(stream_output, data, chunk) print "* stopping stream" _portaudio.stop_stream(stream_input) _portaudio.stop_stream(stream_output) print "* closing stream" _portaudio.close(stream_input) _portaudio.close(stream_output) # match initialize() with terminate() calls _portaudio.terminate()
print "* initializing" _portaudio.initialize() print "* opening" stream = _portaudio.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, output = True, frames_per_buffer = chunk) print "* starting stream" _portaudio.start_stream(stream) print "* recording" for i in range(0, 44100 / chunk * RECORD_SECONDS): data = _portaudio.read_stream(stream, chunk) _portaudio.write_stream(stream, data, chunk) print "* stopping stream" _portaudio.stop_stream(stream) print "* closing stream" _portaudio.close(stream) # match all initialize() with terminate() calls _portaudio.terminate()