lazy_io Module

Audio recording input and playing output module

Summary of module contents:

Name Description
chunks This is a StrategyDict instance object called chunks. Strategies stored: 2.
RecStream Recording Stream
AudioIO Multi-thread stream manager wrapper for PyAudio.
AudioThread Audio output thread.
class RecStream(device_manager, file_obj, chunk_size, dfmt)[source]

Bases: audiolazy.lazy_stream.Stream

Recording Stream

A common Stream class with a stop method for input data recording and a recording read-only property for status.

__add__(other)
__and__(other)
__eq__(other)
__floordiv__(other)
__ge__(other)
__gt__(other)
__init__(device_manager, file_obj, chunk_size, dfmt)[source]
__invert__()
__le__(other)
__lshift__(other)
__lt__(other)
__matmul__(other)
__mod__(other)
__mul__(other)
__ne__(other)
__neg__()
__or__(other)
__pos__()
__pow__(other)
__radd__(other)
__rand__(other)
__rfloordiv__(other)
__rlshift__(other)
__rmatmul__(other)
__rmod__(other)
__rmul__(other)
__ror__(other)
__rpow__(other)
__rrshift__(other)
__rshift__(other)
__rsub__(other)
__rtruediv__(other)
__rxor__(other)
__sub__(other)
__truediv__(other)
__xor__(other)
recording
stop()[source]

Finishes the recording stream, so it can raise StopIteration

class AudioIO(wait=False, api=None)[source]

Bases: object

Multi-thread stream manager wrapper for PyAudio.

__del__()[source]

Default destructor. Use close method instead, or use the class instance as the expression of a with block.

__enter__()[source]

To be used only internally, in the with-expression protocol.

__exit__(etype, evalue, etraceback)[source]

Closing destructor for use internally in a with-expression.

__init__(wait=False, api=None)[source]

Constructor to PyAudio Multi-thread manager audio IO interface. The “wait” input is a boolean about the behaviour on closing the instance, if it should or not wait for the streaming audio to finish. Defaults to False. Only works if the close method is explicitly called.

close()[source]

Destructor for this audio interface. Waits the threads to finish their streams, if desired.

play(audio, **kwargs)[source]

Start another thread playing the given audio sample iterable (e.g. a list, a generator, a NumPy np.ndarray with samples), and play it. The arguments are used to customize behaviour of the new thread, as parameters directly sent to PyAudio’s new stream opening method, see AudioThread.__init__ for more.

record(chunk_size=None, dfmt='f', channels=1, rate=44100, **kwargs)[source]

Records audio from device into a Stream.

Parameters:
  • chunk_size – Number of samples per chunk (block sent to device).
  • dfmt – Format, as in chunks(). Default is “f” (Float32).
  • channels – Channels in audio stream (serialized).
  • rate – Sample rate (same input used in sHz).
Returns:

Endless Stream instance that gather data from the audio input device.

recording_finished(recst)[source]

Updates internal status about open recording streams. Should be called only by the internal closing mechanism of children RecStream instances.

terminate()[source]

Same as “close”.

thread_finished(thread)[source]

Updates internal status about open threads. Should be called only by the internal closing mechanism of AudioThread instances.

class AudioThread(device_manager, audio, chunk_size=None, dfmt='f', channels=1, rate=44100, daemon=True, **kwargs)[source]

Bases: threading.Thread

Audio output thread.

This class is a wrapper to ease the use of PyAudio using iterables of numbers (Stream instances, lists, tuples, NumPy 1D arrays, generators) as audio data streams.

__init__(device_manager, audio, chunk_size=None, dfmt='f', channels=1, rate=44100, daemon=True, **kwargs)[source]

Sets a new thread to play the given audio.

Parameters:
  • chunk_size – Number of samples per chunk (block sent to device).
  • dfmt – Format, as in chunks(). Default is “f” (Float32).
  • channels – Channels in audio stream (serialized).
  • rate – Sample rate (same input used in sHz).
  • daemon – Boolean telling if thread should be daemon. Default is True.
nchannels

Deprecated.

pause()[source]

Pauses the audio.

play()[source]

Resume playing the audio.

run()[source]

Plays the audio. This method plays the audio, and shouldn’t be called explicitly, let the constructor do so.

stop()[source]

Stops the playing thread and close

lazy_io.chunks StrategyDict

This is a StrategyDict instance object called chunks. Strategies stored: 2.

Strategy chunks.array. Docstring starts with:

Chunk generator based on the array module (Python standard library).

Strategy chunks.struct (Default). Docstring starts with:

Chunk generator based on the struct module (Python standard library).

Note

This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.

struct(seq, size=None, dfmt='f', byte_order=None, padval=0.0)

Chunk generator based on the struct module (Python standard library).

Low-level data blockenizer for homogeneous data as a generator, to help writing an iterable into a file. The dfmt should be one char, chosen from the ones in link:

Useful examples (integer are signed, use upper case for unsigned ones):

  • “b” for 8 bits (1 byte) integer
  • “h” for 16 bits (2 bytes) integer
  • “i” for 32 bits (4 bytes) integer
  • “f” for 32 bits (4 bytes) float (default)
  • “d” for 64 bits (8 bytes) float (double)

Byte order follows native system defaults. Other options are in the site:

They are:

  • “<” means little-endian
  • “>” means big-endian

Note

Default chunk size can be accessed (and changed) via chunks.size.

array(seq, size=None, dfmt='f', byte_order=None, padval=0.0)

Chunk generator based on the array module (Python standard library).

See chunk.struct for more help. This strategy uses array.array (random access by indexing management) instead of struct.Struct and blocks/deque (circular queue appending) from the chunks.struct strategy.

Hint

Try each one to find the faster one for your machine, and chooses the default one by assigning chunks.default = chunks.strategy_name. It’ll be the one used by the AudioIO/AudioThread playing mechanism.

Note

The dfmt symbols for arrays might differ from structs’ defaults.