在固定一段时间后检测到某个音调后,如何使代码停止运行?

发布于 2025-02-07 06:25:12 字数 1392 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2025-02-14 06:25:12

我不知道这是否有效,但是您可以尝试这样的事情: -

import time

start_timer = True
flag = True
while True:
    
    # read pitch
    pitch = ...
    
    current_time = time.time()
    
    if pitch == 12000 and flag:
        start_timer = True
        flag = False
    
    if pitch!= 12000:
        flag = True
    
    if start_timer:
        start_time = time.time()
        start_timer = False
        
    if start_timer - current_time>5:
        break

I don't know if this works, but you can try something like this:-

import time

start_timer = True
flag = True
while True:
    
    # read pitch
    pitch = ...
    
    current_time = time.time()
    
    if pitch == 12000 and flag:
        start_timer = True
        flag = False
    
    if pitch!= 12000:
        flag = True
    
    if start_timer:
        start_time = time.time()
        start_timer = False
        
    if start_timer - current_time>5:
        break

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文