在固定一段时间后检测到某个音调后,如何使代码停止运行?
我正在使用Pyaudio和Aubio库来读取来自音调生成器(摩托罗拉代码合成器II)的音调,该音调通过音频Jack USB端口插入我的计算机。(7.1 Channel Sound)。我有可以成功阅读音调生成器设置的音调的代码。我使用的频率为1200 Hz来测试我的代码。我的目标是让我的代码认识到,如果播放了1200 Hz的音调5秒钟,它应该停止聆听(停止流),但我似乎无法获得我的代码来识别音调已经播放的时间。
我是Python的新手,并感谢任何帮助。我的音调生成器并不精确,因此我试图获取我的代码,以确保听到的音调在小于1200 Hz的小范围内,持续5秒钟,然后才能关闭流。
我的代码:
# Pitch Detection
# Needed libraries
import aubio
import numpy as num
import pyaudio
import timeit
# PyAudio object
p = pyaudio.PyAudio()
# Open stream
stream = p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, input=True,
frames_per_buffer=1024)
# Aubio's pitch detection
pDetection = aubio.pitch("default", 2048,
2048//2, 44100)
# Set unit
pDetection.set_unit("Hz")
pDetection.set_silence(-40)
# Listen
while True:
data = stream.read(1024)
samples = num.fromstring(data,
dtype=aubio.float_type)
pitch = pDetection(samples)[0]
print(pitch)
if 1198.50 < pitch < 1202.50:
#print('1200 Hz Detected')
timeout = 5
timeout_start = timeit.timeit()
while timeit.timeit() < timeout_start + timeout:
print('Longtone')
print(pitch)
break
else:
print('1200 Hz not detected')
# if time()==stop:
print("*** Longtone complete")
stream.stop_stream()
stream.close()
p.terminate()
I am using the PyAudio and aubio libraries to read the tone coming in from a tone generator (Motorola Code Synthesizer II) which is plugged in to my computer via an Audio Jack usb port.(7.1 Channel Sound). I have code that can successfully read the pitch of the tone that the Tone Generator is set too. I am using a frequency of 1200 Hz to test my code. My goal is to have my code recognize that if a 1200 Hz tone is played for 5 seconds, it should stop listening (stop the stream) but I cannot seem to get my code to recognize how long the tone has been playing for.
I'm new to python and appreciate any help. My tone generator isn't exact so I am attempting to get my code to make sure the tone being heard is within a small range around 1200 Hz for 5 seconds before the stream should be closed.
My code:
# Pitch Detection
# Needed libraries
import aubio
import numpy as num
import pyaudio
import timeit
# PyAudio object
p = pyaudio.PyAudio()
# Open stream
stream = p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, input=True,
frames_per_buffer=1024)
# Aubio's pitch detection
pDetection = aubio.pitch("default", 2048,
2048//2, 44100)
# Set unit
pDetection.set_unit("Hz")
pDetection.set_silence(-40)
# Listen
while True:
data = stream.read(1024)
samples = num.fromstring(data,
dtype=aubio.float_type)
pitch = pDetection(samples)[0]
print(pitch)
if 1198.50 < pitch < 1202.50:
#print('1200 Hz Detected')
timeout = 5
timeout_start = timeit.timeit()
while timeit.timeit() < timeout_start + timeout:
print('Longtone')
print(pitch)
break
else:
print('1200 Hz not detected')
# if time()==stop:
print("*** Longtone complete")
stream.stop_stream()
stream.close()
p.terminate()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否有效,但是您可以尝试这样的事情: -
I don't know if this works, but you can try something like this:-