Advertisement
here2share

# midi_Mission_Impossible_demo.py

May 19th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. # midi_Mission_Impossible_demo.py
  2. #
  3. # Internal Speaker Beeping Module for Windows
  4. #
  5.  
  6. import time
  7. import winsound
  8.  
  9. # Set delay tempo
  10. tempo = 0.15
  11.  
  12. # Setup Notes
  13. notes = {}
  14. notes["pause"] = 0
  15. notes["c"] = 1
  16. notes["c#"] = 2
  17. notes["d"] = 3
  18. notes["d#"] = 4
  19. notes["e"] = 5
  20. notes["f"] = 6
  21. notes["f#"] = 7
  22. notes["g"] = 8
  23. notes["g#"] = 9
  24. notes["a"] = 10
  25. notes["a#"] = 11
  26. notes["b"] = 12
  27.  
  28. # Note Types
  29. note_types = {}
  30. note_types["sixteenth"] = 50
  31. note_types["eigth"] = 100
  32. note_types["dotted_eigth"] = 150
  33. note_types["quarter"] = 200
  34. note_types["half"] = 400
  35. note_types["whole"] = 800
  36. note_types["triplet"] = 60
  37.  
  38. #!# End Notes Config #!#
  39.  
  40. def play_note(octave, note, note_type):
  41.     """Play a note at a certain octave by calculating the frequency of
  42. the sound it would represent on the motherboard's speaker."""
  43.  
  44.     # Match the note and note type to the dictionaries
  45.     note = notes[note]
  46.     note_type = note_types[note_type]
  47.  
  48.     # Chill for a bit if it's a pause
  49.     if not note:
  50.         time.sleep(note_type/1000)
  51.         return
  52.  
  53.     # Calculate C for the provided octave
  54.     frequency = 32.7032 * (2**octave)
  55.  
  56.     # Calculate the frequency of the given note
  57.     frequency *= 1.059463094**note
  58.  
  59.     # Beep it up
  60.     winsound.Beep(int(frequency), note_type)
  61.  
  62.     # Delay after the beep so it doesn't all run together
  63.     time.sleep(tempo)
  64.  
  65. def mission_impossible():
  66.     oct = 3
  67.     play_note(oct, "g", "half")
  68.     play_note(oct, "g", "half")
  69.     play_note(oct+1, "a#", "quarter")
  70.     play_note(oct+1, "c", "quarter")
  71.     play_note(oct, "g", "half")
  72.     play_note(oct, "g", "half")
  73.     play_note(oct, "f", "quarter")
  74.     play_note(oct, "f#", "quarter")
  75.     play_note(oct, "g", "half")
  76.     play_note(oct, "g", "half")
  77.     play_note(oct+1, "a#", "quarter")
  78.     play_note(oct+1, "c", "quarter")
  79.     play_note(oct, "g", "half")
  80.     play_note(oct, "g", "half")
  81.     play_note(oct, "f", "quarter")
  82.     play_note(oct, "f#", "quarter")
  83.  
  84. def party():
  85.     print 'Mission Impossible'
  86.     mission_impossible()
  87.  
  88. if __name__ == '__main__':
  89.     party()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement