Advertisement
here2share

# midi_is_possible_demo2.py

May 19th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. # midi_is_possible_demo2.py
  2. #
  3. # Internal Speaker Beeping Module for Windows
  4. #
  5.  
  6. import time
  7. import winsound
  8.  
  9. # Setup Notes
  10.  
  11. notes = {}
  12. notes['pause'] = 0
  13. notes['p'] = 0
  14. notes['r'] = 0
  15. notes['.'] = 0
  16. notes['C'] = 1
  17. notes['c'] = 2
  18. notes['D'] = 3
  19. notes['d'] = 4
  20. notes['E'] = 5
  21. notes['F'] = 6
  22. notes['f'] = 7
  23. notes['G'] = 8
  24. notes['g'] = 9
  25. notes['A'] = 10
  26. notes['a'] = 11
  27. notes['B'] = 12
  28. notes['c#'] = 1
  29. notes['d#'] = 3
  30. notes['e#'] = 5
  31. notes['f#'] = 6
  32. notes['g#'] = 8
  33. notes['a#'] = 10
  34. notes['b#'] = 12
  35.  
  36. # Note Types
  37. note_types = {}
  38. note_types[5] = 50
  39. note_types[1] = 100
  40. note_types[6] = 150
  41. note_types[2] = 200
  42. note_types[4] = 400
  43. note_types[8] = 800
  44. note_types[3] = 60
  45.  
  46. dur = {}
  47. dur["sixteenth"] = 50
  48. dur["eighth"] = 100
  49. dur["dotted_eighth"] = 150
  50. dur["quarter"] = 200
  51. dur["half"] = 400
  52. dur["whole"] = 800
  53. dur["triplet"] = 60
  54. dur["16"] = 50
  55. dur["8"] = 100
  56. dur[".2"] = 150
  57. dur["4"] = 200
  58. dur["2"] = 400
  59. dur["1"] = 800
  60. dur["3"] = 60
  61.  
  62. #!# End Notes Config #!#
  63.  
  64. aaa=''
  65. def rw_notes(octave, note, note_type):
  66.         global aaa
  67.         if '#' in note:
  68.                 freq=note[0]
  69.         else:
  70.                 freq=note.upper()
  71.         dur_ = dur[note_type]
  72.         aaa+=str(octave)+str(freq)+str(dur_)
  73.        
  74. def play_midi(array,tempo=0):
  75.         """Play a note at a certain octave by calculating the frequency of
  76. the sound it would represent on the motherboard's speaker."""
  77.         while 1:
  78.                 try:
  79.                         print array[:3]
  80.                         octave, note, note_type = array[0],array[1],array[2]
  81.                         array = array[3:]
  82.                         # Match the note and note type to the dictionaries
  83.                         note = notes[note]
  84.                         note_type = note_types[int(note_type)]
  85.  
  86.                         # Chill for a bit if it's a pause
  87.                         if not note:
  88.                                 time.sleep(note_type/1000)
  89.                                 return
  90.  
  91.                         # Calculate C for the provided octave
  92.                         frequency = 32.7032 * (2**int(octave))
  93.  
  94.                         # Calculate the frequency of the given note
  95.                         frequency *= 1.059463094**note
  96.  
  97.                         # Beep it up
  98.                         winsound.Beep(int(frequency), note_type)
  99.  
  100.                         # Delay after the beep so it doesn't all run together
  101.                         time.sleep(tempo/600.00)
  102.                 except: break
  103. #
  104.  
  105. ###             Mission Impossible
  106. play_midi('3G43G43a24C23G43G43F23f23G43G43a24C23G43G43F23f2',80)
  107.  
  108. #print aaa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement