0

I am on macOS Mojave and trying to type-specific moments. In my application that I am developing, I need to store the times after a specific action/function is triggered in the callback function and other functions in a python module. My goal is to create three different plots on the same time axis.

I understand that: The time (in callback arg list) values are monotonically increasing and have the unspecified origin which is time.currentTime from the callback argument. I also read:

The PortAudio stream callback runs at very high or real-time priority. It is required to consistently meet its time deadlines. Do not allocate memory, access the file system, call library functions or call other functions from the stream callback that may block or take an unpredictable amount of time to complete. With the exception of cpu_load it is not permissible to call PortAudio API functions from within the stream callback.

one way would be using the same timing in callback function and other functions as in:

import sounddevice as sd
import time
duration = 5.5  # seconds

times = list()
def callback(indata, outdata, frames, time, status):
    global times
    if status:
        print(status)
    times.append(time.time())
    outdata[:] = indata

with sd.Stream(channels=2, callback=callback):
    sd.sleep(int(duration * 1000))

However, this produce error below:

input overflow From cffi callback <function _StreamBase.init..callback_ptr at 0x109a00dd0>: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 881, in callback_ptr callback, idata, odata, frames, time, status) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 2678, in _wrap_callback callback(*args) File "timed_callbackex.py", line 11, in callback times.append(time.time()) AttributeError: cdata 'struct PaStreamCallbackTimeInfo *' has no field 'time'

So for the development purposes what do you guys recommend for the timing of the steam and other actions as the audio steams input is processed say from a microphone?

Any help would be much appreciated.

chikitin
  • 762
  • 6
  • 28

1 Answers1

1

There is an issue in your callback function. Since the sounddevice call back function has one parameter "time" it is conflicting with the module "time". I suggest you change the import time name to a different one as a quick fix:

import sounddevice as sd
import time as tm
duration = 5.5  # seconds

times = list()
def callback(indata, outdata, frames, time, status):
    global times
    if status:
        print(status)
    times.append(tm.time())
    outdata[:] = indata

with sd.Stream(channels=2, callback=callback):
    sd.sleep(int(duration * 1000)) 
Rasecninja
  • 11
  • 1