944,033 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 7515
  • Python RSS
0

Musical Beeps (Python)

by on Aug 16th, 2005
If you have a Windows computer you can play musical beeps through the internal speaker. In this case it will sound like a very tiny Big Ben, brought to you by the module winsound and its method Beep().
Python Code Snippet (Toggle Plain Text)
  1. # play music on the PC internal speaker
  2. # tested with Python24 on a Windows XP computer vegaseat 15aug2005
  3.  
  4. import winsound
  5. import time
  6.  
  7. # the notes
  8. P = 0 # pause
  9. C = 1
  10. CS = 2 # C sharp
  11. D = 3
  12. DS = 4
  13. E = 5
  14. F = 6
  15. FS = 7
  16. G = 8
  17. GS = 9
  18. A = 10
  19. AS = 11
  20. B = 12
  21.  
  22. EN = 100 # eighth note
  23. QN = 200 # quarter note
  24. HN = 400 # half note
  25. FN = 800 # full note
  26.  
  27. def play(octave, note, duration):
  28. """play note (C=1 to B=12), in octave (1-8), and duration (msec)"""
  29. if note == 0: # a pause
  30. time.sleep(duration/1000)
  31. return
  32. frequency = 32.7032 # C1
  33. for k in range(0, octave): # compute C in given octave
  34. frequency *= 2
  35.  
  36. for k in range(0, note): # compute frequency of given note
  37. frequency *= 1.059463094 # 1.059463094 = 12th root of 2
  38. time.sleep(0.010) # delay between keys
  39.  
  40. winsound.Beep(int(frequency), duration)
  41.  
  42. def bigben():
  43. play(4,E,HN)
  44. play(4,C,HN)
  45. play(4,D,HN)
  46. play(3,G,HN+QN); play(3,P,QN)
  47. play(3,G,HN)
  48. play(4,D,HN)
  49. play(4,E,HN)
  50. play(4,C,HN+QN)
  51.  
  52. bigben()
Message:
Previous Thread in Python Forum Timeline: more information about python
Next Thread in Python Forum Timeline: simple python web server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC