简体   繁体   English

如何在python中的按键事件之前发出连续的哔哔声或其他简单的声音

[英]How to have a continuous beep or other simple sound until key press event in python

I've written the following simple program in python in order for the user to test/adjust their sound volume.我已经在 python 中编写了以下简单程序,以便用户测试/调整他们的音量。 It uses winsound.Beep() to make a repeating beeping noise until it's interrupted by a msvcrt.kbhit() event, and uses threading to allow input while the winsound.Beep() is going on:它使用 winsound.Beep() 发出重复的哔哔声,直到它被 msvcrt.kbhit() 事件中断,并在 winsound.Beep() 进行时使用线程来允许输入:


import winsound, threading, msvcrt

class soundThread(threading.Thread):
    def __init__(self, threadID, pitch, duration):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.pitch = pitch
        self.duration = duration
    def run(self):
        soundTone(self.pitch,self.duration)        

def soundTone(pitch,duration):
    winsound.Beep(pitch,int(duration*1000))

def main():
    """main code module"""
    print("The computer will play a beeping tone at a frequency of 440 MHz.")
    print("It will continue to do so until you press a key which will cause the")
    print("sound to stop and the program to exit.  While the tone is sounding")
    print("adjust your volume so that you can hear the tone clearly and")
    print("comfortably.")
    print("")
    print("Tone starting now...")

    loopBool = True
    soundBool = True
    i = 0;
    while loopBool:
        i+=1
        # make beeping sound
        if soundBool:
            beep = soundThread(i,440,2.0)
            beep.start()

        # stop loop if a key is pressed
        if msvcrt.kbhit():
            loopBool = False
            print("Key press detected.  Waiting for sound to stop...")

        # don't make new beep thread if current one is still active
        if beep.isAlive():
            soundBool = False
        else:
            soundBool = True

    print("Sound stopped.  Exiting program.")

if __name__ == '__main__':
    main()

So the code itself works fine.所以代码本身工作正常。 However aesthetically I don't really like having the winsound.Beep() having to start and stop over and over again, and when a key is pressed the program has to wait for the current Beep() thread to end before the entire program can end.但是从美学上讲,我真的不喜欢让 winsound.Beep() 不得不一遍又一遍地开始和停止,并且当按下某个键时,程序必须等待当前 Beep() 线程结束,然后整个程序才能运行结尾。 However, I can't think of any way to do it using winsound, and I can't find any other built in modules that allow simple generation of sounds like this.但是,我想不出任何使用 winsound 的方法,也找不到任何其他内置模块可以简单地生成这样的声音。

I saw some very similar questions on this site, but all the solutions required non-standard modules like audiolab or PyMedia, but I'd like to do this using standard modules if possible.我在这个网站上看到了一些非常相似的问题,但所有的解决方案都需要非标准模块,比如 audiolab 或 PyMedia,但如果可能的话,我想使用标准模块来做到这一点。 Is there some way to have a continuous beep until a keyboard interrupt, without having to install any non-standard modules?有没有办法在键盘中断之前连续发出哔哔声,而不必安装任何非标准模块? If there isn't any way with the basic modules, I'm using the Anaconda python distribution, so I at least have access to numpy and scipy, etc., though I kind of doubt those packages would be helpful here.如果基本模块没有任何方法,我正在使用 Anaconda python 发行版,所以我至少可以访问 numpy 和 scipy 等,尽管我有点怀疑这些包在这里会有帮助。

There is minor correction to your code.对您的代码进行了小幅修正。 The method name should be in snake_case:方法名应该在snake_case中:

# don't make new beep thread if current one is still active
if beep.is_alive():

not CamelCase:不是 CamelCase:

# don't make new beep thread if current one is still active
if beep.isAlive():

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM