【问题标题】:Timing module functions and callback stream in sounddevicesounddevice中的定时模块函数和回调流
【发布时间】:2021-02-19 23:38:32
【问题描述】:

我正在使用 macOS Mojave 并尝试输入特定的时刻。在我正在开发的应用程序中,我需要在回调函数和 python 模块中的其他函数中存储特定动作/函数触发后的时间。我的目标是在同一时间轴上创建三个不同的图。

我明白:时间(在回调参数列表中)值是单调递增的,并且具有未指定的来源,即来自回调参数的 time.currentTime。我也读过:

PortAudio 流回调以非常高或实时的优先级运行。它需要始终如一地满足其时间期限。不要分配内存、访问文件系统、调用库函数或从流回调中调用其他函数,这可能会阻塞或花费不可预测的时间来完成。除了 cpu_load 之外,不允许从流回调中调用 PortAudio API 函数。

一种方法是在回调函数和其他函数中使用与以下相同的时间:

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))

但是,这会产生以下错误:

输入溢出 从 cffi 回调 init..callback_ptr at 0x109a00dd0>: 回溯(最近一次通话最后): 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py”,第 881 行,在 callback_ptr 回调、idata、odata、帧、时间、状态) _wrap_callback 中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py”,第 2678 行 回调(*args) 回调中的文件“timed_callbackex.py”,第 11 行 时间.追加(时间.时间()) AttributeError: cdata 'struct PaStreamCallbackTimeInfo *' 没有字段 'time'

那么,出于开发目的,你们建议在处理音频流输入时(例如从麦克风)对流的时间安排和其他操作有什么建议?

任何帮助将不胜感激。

【问题讨论】:

    标签: timing python-sounddevice


    【解决方案1】:

    您的回调函数存在问题。由于 sounddevice 回调函数有一个参数“time”,它与模块“time”冲突。我建议您将导入时间名称更改为其他名称作为快速修复:

    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)) 
    

    【讨论】:

    • 请在标签中指定您在这段代码中使用的语言和库
    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2011-06-23
    相关资源
    最近更新 更多