为持续时间y生成频率为x的音调,并在音调期间保持其他代码执行

问题描述 投票:0回答:1

在高层次上,我想要实现的是FFT分析的数据记录自动化。现在,我正在使用音调发生器生成恒定音调,然后记录我的系统输出。录音结束后,音调停止。对整个频域重复此操作。然后,使用各种分析步骤(例如FFT)处理所有数据。所有这些都是在Python3中完成的

我一直试图在这个过程中自动化无聊的东西,我想取出手动频率扫描。

我需要的是为持续时间y生成频率x的音调。启用音后,我需要执行代码才能继续录音。

在我的搜索到目前为止,我找到了winsound。但是winsound.beep不能用winsound的异步选项,我想避免使用记录信号。现在我没有想法。有没有人可以帮助我?

示例代码:

import winsound
winsound.Beep(500,1000)
print('this is not simultaneously printed with the sound playing')
python multithreading automation fft
1个回答
0
投票

@furas感谢您的反馈,您对使用线程的建议我已经成功地使用了概念验证。我在这里提供反馈和我的基本代码,以防其他任何人遇到这个问题。导入线程导入时间导入日志导入winsound

logging.basicConfig(level=logging.DEBUG,
                    format='(%(threadName)-10s) %(message)s',
                    )

def makeTone(freq,dur):
    logging.debug('Starting Sound with f=%sHz for t=%ss',freq,dur)
    winsound.Beep(freq,dur)

    logging.debug('Sound stopped')

def recorder():
    logging.debug('Startrecording')
    time.sleep(3)
    logging.debug('Stoprecording')

frequency = 500 #Hz
duration = 5000 #ms
makeTone = threading.Thread(name='makeTone', target=makeTone,args=(frequency, duration))
recorder = threading.Thread(name='recorder', target=recorder)

makeTone.start()
recorder.start()
© www.soinside.com 2019 - 2024. All rights reserved.