title: How to have a continuous beep or other simple sound until key press event in python
body: <p>I've written the following simple program in python in order for the user to test/adjust their sound volume.  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:</p>
  
  <hr>
  
  <pre><code>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()
  </code></pre>
  
  <hr>
  
  <p>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.  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.</p>
  
  <p>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.  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.</p>