you want; see query_devices()). The default value(s) can be changed with
default.channels.
• dtype (str or numpy.dtype or pair thereof, optional) – The sample format of the
numpy.ndarray
19
provided to the stream callback, read() or write(). It may be
any of float32, int32, int16, int8, uint8. See numpy.dtype
20
. The float64 data type is
not supported, this is only supported for convenience in play()/rec()/playrec().
The packed 24 bit format ’int24’ is only supported in the “raw” stream classes, see
RawStream. The default value(s) can be changed with default.dtype.
• latency (float or {‘low’, ‘high’} or pair thereof, optional) – The desired latency in sec-
onds. The special values ’low’ and ’high’ (latter being the default) select the default
low and high latency, respectively (see query_devices()). The default value(s)
can be changed with default.latency. Where practical, implementations should
configure their latency based on this parameter, otherwise they may choose the closest
viable latency instead. Unless the suggested latency is greater than the absolute upper
limit for the device, implementations should round the latency up to the next practical
value – i.e. to provide an equal or higher latency wherever possible. Actual latency
values for an open stream may be retrieved using the latency attribute.
• callback (callable, optional) – User-supplied function to consume, process or generate
audio data in response to requests from an active stream. When a stream is running,
PortAudio calls the stream callback periodically. The callback function is responsible
for processing and filling input and output buffers, respectively.
If no callback is given, the stream will be opened in “blocking read/write” mode. In
blocking mode, the client can receive sample data using read() and write sample data
using write(), the number of frames that may be read or written without blocking is
returned by read_available and write_available, respectively.
The callback must have this signature:
callback(indata: ndarray, outdata: ndarray, frames: int,
time: CData, status: CallbackFlags) -> None
The first and second argument are the input and output buffer, respectively, as two-
dimensional numpy.ndarray
21
with one column per channel (i.e. with a shape of
(frames, channels)) and with a data type specified by dtype. The output buffer contains
uninitialized data and the callback is supposed to fill it with proper audio data. If no data
is available, the buffer should be filled with zeros (e.g. by using outdata.fill(0)).
Note: In Python, assigning to an identifier merely re-binds the identifier to another
object, so this will not work as expected:
outdata = my_data # Don't do this!
To actually assign data to the buffer itself, you can use indexing, e.g.:
outdata[:] = my_data
... which fills the whole buffer, or:
outdata[:, 1] = my_channel_data
... which only fills one channel.
19
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html#numpy.ndarray
20
http://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html#numpy.dtype
21
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html#numpy.ndarray