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 sk_dsp_comm.pyaudio_helper.DSP_io_stream(stream_callback, in_idx=1, out_idx=4, frame_length=1024, fs=44100, Tcapture=0, sleep_time=0.1)[source]

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.
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([Tsec, numChan]) Stream audio with start and stop radio buttons
pack_LR(left_out, right_out) Packs separate left and right channel data into one array to output and returns the output.
stop() Call to stop streaming
stream([Tsec, numChan]) 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([Tsec, numChan]) Stream audio in a thread using callback.
interaction  
DSP_callback_tic()[source]

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

DSP_callback_toc()[source]

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

DSP_capture_add_samples(new_data)[source]

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)[source]

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')[source]

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)[source]

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_data : input data from the streaming object in the callback function.
Returns:
left_in : array of incoming left channel samples
right_in : array of incoming right channel samples
in_out_check()[source]

Checks the input and output to see if they are valid

interactive_stream(Tsec=2, numChan=1)[source]

Stream audio with start and stop radio buttons

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:
Tsec : stream 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.
numChan : number of channels. Use 1 for mono and 2 for stereo.
pack_LR(left_out, right_out)[source]

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

Parameters:
left_out : left channel array of samples going to output
right_out : right channel array of samples going to output
Returns:
out : packed left and right channel array of samples
stop()[source]

Call to stop streaming

stream(Tsec=2, numChan=1)[source]

Stream audio using callback

Parameters:
Tsec : stream 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.
numChan : number of channels. Use 1 for mono and 2 for stereo.
stream_stats()[source]

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

thread_stream(Tsec=2, numChan=1)[source]

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

Parameters:
Tsec : stream 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.
numChan : number of channels. Use 1 for mono and 2 for stereo.
sk_dsp_comm.pyaudio_helper.available_devices()[source]

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
class sk_dsp_comm.pyaudio_helper.loop_audio(x, start_offset=0)[source]

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)[source]