PyAudio Source

This source requires PyAudio. Find PyAudio installation instructions here.

pip install PyAudio
class audiostream2py.audio.PyAudioSourceReader[source]

Bases: audiostream2py.audio.AudioSegmentMixin, audiostream2py.audio.FillErrorWithOnesMixin, audiostream2py.audio.BasePyAudioSourceReader

PyAudioSourceReader changed to handle errors and serve data in an easy-to-read dataclass.

__init__(*, rate=44100, width=2, unsigned=True, channels=1, input_device_index=None, frames_per_buffer=1024, input_device=None, verbose=True, ts_refresh_period=1000)
Parameters
  • rate – Specifies the desired sample rate (in Hz)

  • width – Sample width in bytes (1, 2, 3, or 4)

  • unsigned – For 1 byte width, specifies signed or unsigned format. Ignored if byte width is not 1.

  • channels – The desired number of input channels. Ignored if input_device is not specified (or None).

  • input_device – Specification of what input device to use.

  • input_device_index – Legacy specification of input Device to use. Has to be the integer index, unlike input_device which can be a name too. Unspecified (or None) uses default device.

  • frames_per_buffer – Specifies the number of frames per buffer.

  • verbose – Permission to print stuff when we feel like it?

  • ts_refresh_period – period, in seconds, with which a buffer will be timestamped with

the host-system time, instead of using the frame rate and frame count values. This limits the drift between buffer timestamps and host-system time due to frame rate innaccuracies. Crucial if we need to synchronize data from different sources.

class audiostream2py.audio.PaStatusFlags(value)[source]

Bases: enum.IntFlag

Enum to check status_flag for error codes.

>>> from audiostream2py.audio import PaStatusFlags
>>> PaStatusFlags(0)
<PaStatusFlags.paNoError: 0>
>>> PaStatusFlags(2)
<PaStatusFlags.paInputOverflow: 2>
>>> PaStatusFlags(3)
<PaStatusFlags.paInputOverflow|paInputUnderflow: 3>
>>> PaStatusFlags.paInputOverflow in PaStatusFlags(3)  # Check if contains certain error
True
>>> PaStatusFlags.paNoError == PaStatusFlags(3)  # Check for no error
False
paNoError = 0
paInputUnderflow = 1
paInputOverflow = 2
paOutputUnderflow = 4
paOutputOverflow = 8
paPrimingOutput = 16
hostTimeSync = 32

PyAudio Mixins

Example of how to use mixins to manipulate behavior to suit your needs.

class audiostream2py.audio.DictDataMixin[source]

Mixin to reduce data to a dict with bt, tt, wf, and status_flag. Removing typically discarded information.

data_to_append(start_date, end_data, waveform, frame_count, time_info, status_flags)[source]

Simplify data only

Parameters
  • timestamp – start time of waveform

  • waveform – recorded input data

  • frame_count – discarded

  • time_info – discarded

  • status_flags – PaStatusFlags error codes

Returns

{‘bt’: timestamp, ‘wf’: waveform, ‘status_flags’: status_flags}

key(data) → stream2py.utility.typing_hints.ComparableType[source]
Parameters

data – {‘bt’: timestamp, ‘wf’: waveform, ‘status_flags’: status_flags}

Returns

data[‘tt’]

Example PyAudio Mixin Usage

Mixins must be subclassed in order of precedence from left to right. Meaning the base class at the right and mixins with overloading methods to the left.