pyaudio_helper

Support functions and classes for using PyAudio for real-time DSP

Copyright (c) September 2017, Mark Wickert, Andrew Smit All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.

class pyaudio_helper.pyaudio_helper.DSPIOStream(stream_callback, in_idx=1, out_idx=4, frame_length=1024, fs=44100, t_capture=0, sleep_time=0.1, t_sec: int = 2, num_chan: int = 1)

Real-time DSP one channel input/output audio streaming

Use PyAudio to explore real-time audio DSP using Python

Mark Wickert, Andrew Smit September 2017

Methods

DSP_callback_tic()

Add new tic time to the DSP_tic list.

DSP_callback_toc()

Add new toc time to the DSP_toc list.

DSP_capture_add_samples(new_data)

Append new samples to the data_capture array and increment the sample counter If length reaches Tcapture, then the newest samples will be kept.

DSP_capture_add_samples_stereo(...)

Append new samples to the data_capture_left array and the data_capture_right array and increment the sample counter.

cb_active_plot(start_ms, stop_ms[, line_color])

Plot timing information of time spent in the callback.

create_interactive_widgets(start_handler, ...)

This method will create the necessary ipywidget objects and return the final Box to display.

get_lr(in_data)

Splits incoming packed stereo data into separate left and right channels and returns an array of left samples and an array of right samples

in_out_check()

Checks the input and output to see if they are valid

interactive_stream([t_sec, num_chan])

Stream audio with start and stop buttons from InteractiveWidgets.

pack_lr(left_out, right_out)

Packs separate left and right channel data into one array to output and returns the output.

start_stream_interactive_callback()

This method will handle the start button callback.

start_streaming_button_handler(change)

Use this handler to bind the start button.

stop()

Call to stop streaming

stop_stream_interactive_callback()

This method will handle the stop button callback.

stop_streaming_button_handler(change)

Use this handler to bind the stop button.

stream([t_sec, num_chan])

Stream audio using callback

stream_stats()

Display basic statistics of callback execution: ideal period between callbacks, average measured period between callbacks, and average time spent in the callback.

thread_stream([t_sec, num_chan])

Stream audio in a thread using callback.

set_stopped_state

DSP_callback_tic()

Add new tic time to the DSP_tic list. Will not be called if Tcapture = 0.

DSP_callback_toc()

Add new toc time to the DSP_toc list. Will not be called if Tcapture = 0.

DSP_capture_add_samples(new_data)

Append new samples to the data_capture array and increment the sample counter If length reaches Tcapture, then the newest samples will be kept. If Tcapture = 0 then new values are not appended to the data_capture array.

DSP_capture_add_samples_stereo(new_data_left, new_data_right)

Append new samples to the data_capture_left array and the data_capture_right array and increment the sample counter. If length reaches Tcapture, then the newest samples will be kept. If Tcapture = 0 then new values are not appended to the data_capture array.

cb_active_plot(start_ms, stop_ms, line_color='b')

Plot timing information of time spent in the callback. This is similar to what a logic analyzer provides when probing an interrupt.

cb_active_plot( start_ms,stop_ms,line_color=’b’)

get_lr(in_data)

Splits incoming packed stereo data into separate left and right channels and returns an array of left samples and an array of right samples

Parameters
in_datainput data from the streaming object in the callback function.
Returns
left_inarray of incoming left channel samples
right_inarray of incoming right channel samples
in_out_check()

Checks the input and output to see if they are valid

interactive_stream(t_sec=None, num_chan=None)

Stream audio with start and stop buttons from InteractiveWidgets.

Interactive stream is designed for streaming audio through this object using a callback function. This stream is threaded, so it can be used with ipywidgets. Click on the “Start Streaming” button to start streaming and click on “Stop Streaming” button to stop streaming.

Parameters
t_secstream time in seconds if Tsec > 0. If Tsec = 0, then stream goes to infinite
mode. When in infinite mode, the “Stop Streaming” radio button or Tsec.stop() can be
used to stop the stream.
num_channumber of channels. Use 1 for mono and 2 for stereo.
pack_lr(left_out, right_out)

Packs separate left and right channel data into one array to output and returns the output.

Parameters
left_outleft channel array of samples going to output
right_outright channel array of samples going to output
Returns
outpacked left and right channel array of samples
start_stream_interactive_callback()

This method will handle the start button callback. :return:

stop()

Call to stop streaming

stop_stream_interactive_callback()

This method will handle the stop button callback. :return:

stream(t_sec=2, num_chan=1)

Stream audio using callback

Parameters
t_secstream time in seconds if Tsec > 0. If Tsec = 0, then stream goes to infinite
mode. When in infinite mode, Tsec.stop() can be used to stop the stream.
num_channumber of channels. Use 1 for mono and 2 for stereo.
stream_stats()

Display basic statistics of callback execution: ideal period between callbacks, average measured period between callbacks, and average time spent in the callback.

thread_stream(t_sec=2, num_chan=1)

Stream audio in a thread using callback. The stream is threaded, so widgets can be used simultaneously during stream.

Parameters
t_secstream time in seconds if Tsec > 0. If Tsec = 0, then stream goes to infinite
mode. When in infinite mode, Tsec.stop() can be used to stop the stream.
num_channumber of channels. Use 1 for mono and 2 for stereo.
class pyaudio_helper.pyaudio_helper.LoopAudio(x, start_offset=0)

Loop signal ndarray during playback. Optionally start_offset samples into the array. Array may be 1D (one channel) or 2D (two channel, Nsamps by 2)

Mark Wickert July 2017

Methods

get_samples

get_samples(frame_count)
pyaudio_helper.pyaudio_helper.available_devices()

Display available input and output audio devices along with their port indices.

Returns

Dictionary whose keys are the device index, the number of inputs and outputs, and their names.

Return type

dict