Recording audio to wav file — taped 0.0.2 documentation

# Recording audio to wav file

This example requires PyAudio

    pip install PyAudio

class `stream2py.examples.usage.record_audio_to_files.` `BufferReaderConsumer`

  Bases: `threading.Thread`

  Call reader_handler function with reader at defined time intervals between calls

  `__init__`(buffer_reader: stream2py.buffer_reader.BufferReader, interval: Union[int, float], logging_enabled: bool = False)
    Parameters

      - buffer_reader – BufferReader created from a StreamBuffer

      - interval – seconds between reader_handler calls

      - logging_enabled – log debug messages using logging module

  abstract `reader_handler`(buffer_reader: stream2py.buffer_reader.BufferReader)

    Implements how to read from buffer and what to do with the data

  `run`()

    Calls self.reader_handler in a loop while sleeping for self.interval seconds after each call until stop event is set or Exception is raised by self.reader_handler

  `stop`()

    Sets stop event and waits for thread to finish

class `stream2py.examples.usage.record_audio_to_files.` `PyAudioSaver`

  Bases: `stream2py.examples.usage.record_audio_to_files.BufferReaderConsumer`

  Save audio to wav file. Start a new wav file on errors such as input overflow.

  `__init__`(buffer_reader, interval, rootdir, logging_enabled)
    Parameters

      - buffer_reader – BufferReader created from a StreamBuffer with PyAudioSourceReader

      - interval – seconds between reader_handler calls

      - rootdir – folder path where files will be saved under

      - logging_enabled – log debug messages using logging module

  `close_file`()

    Close wav file

  `file_is_open`() → bool

    Check if file is open :return: boolean

  `open_file`(source_reader_info: dict, timestamp: int) → wave.Wave_write

    open wav file for writing

  `reader_handler`(buffer_reader: stream2py.buffer_reader.BufferReader)

    Implements how to read from buffer and what to do with the data

  `save_error`(source_reader_info, timestamp, in_data, status_flags)

    Save to error wav file

  static `set_params`(file: wave.Wave_write, source_reader_info: dict)

    Set wav file params

  `stop`()

    Stop BufferReaderConsumer loop and close wav file

  `write_to_file`(in_data)

    Write to wav file

`stream2py.examples.usage.record_audio_to_files.` `audio_to_files`(rate, width, channels, input_device_index, frames_per_buffer, interval, rootdir, logging_enabled)

  Basically the main function to run the example. It will record audio with stream2py.sources.audio.PyAudioSourceReader and save to wav files with stream2py.examples.usage.record_audio_to_files.PyAudioSaver

  Check this source code to see how to put together the three components: SourceReader, StreamBuffer, BufferReader

  Parameters

    - rate – Specifies the desired sample rate (in Hz)

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

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

    - input_device_index – Index of Input Device to use. Unspecified (or None) uses default device.

    - frames_per_buffer – Specifies the number of frames per buffer.

    - interval – seconds between reader_handler calls

    - rootdir – folder path where files will be saved under

    - logging_enabled – log debug messages using logging module