Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1.  
  2. #!/usr/bin/env python
  3. import RPi.GPIO as GPIO
  4. import time
  5. import sys,tty,termios
  6.  
  7. buzzer_pin = 12
  8.  
  9. notes = {
  10. 'B0' : 31,
  11. 'C1' : 33, 'CS1' : 35,
  12. 'D1' : 37, 'DS1' : 39,
  13. 'EB1' : 39,
  14. 'E1' : 41,
  15. 'F1' : 44, 'FS1' : 46,
  16. 'G1' : 49, 'GS1' : 52,
  17. 'A1' : 55, 'AS1' : 58,
  18. 'BB1' : 58,
  19. 'B1' : 62,
  20. 'C2' : 65, 'CS2' : 69,
  21. 'D2' : 73, 'DS2' : 78,
  22. 'EB2' : 78,
  23. 'E2' : 82,
  24. 'F2' : 87, 'FS2' : 93,
  25. 'G2' : 98, 'GS2' : 104,
  26. 'A2' : 110, 'AS2' : 117,
  27. 'BB2' : 123,
  28. 'B2' : 123,
  29. 'C3' : 131, 'CS3' : 139,
  30. 'D3' : 147, 'DS3' : 156,
  31. 'EB3' : 156,
  32. 'E3' : 165,
  33. 'F3' : 175, 'FS3' : 185,
  34. 'G3' : 196, 'GS3' : 208,
  35. 'A3' : 220, 'AS3' : 233,
  36. 'BB3' : 233,
  37. 'B3' : 247,
  38. 'C4' : 262, 'CS4' : 277,
  39. 'D4' : 294, 'DS4' : 311,
  40. 'EB4' : 311,
  41. 'E4' : 330,
  42. 'F4' : 349, 'FS4' : 370,
  43. 'G4' : 392, 'GS4' : 415,
  44. 'A4' : 440, 'AS4' : 466,
  45. 'BB4' : 466,
  46. 'B4' : 494,
  47. 'C5' : 523, 'CS5' : 554,
  48. 'D5' : 587, 'DS5' : 622,
  49. 'EB5' : 622,
  50. 'E5' : 659,
  51. 'F5' : 698, 'FS5' : 740,
  52. 'G5' : 784, 'GS5' : 831,
  53. 'A5' : 880, 'AS5' : 932,
  54. 'BB5' : 932,
  55. 'B5' : 988,
  56. 'C6' : 1047, 'CS6' : 1109,
  57. 'D6' : 1175, 'DS6' : 1245,
  58. 'EB6' : 1245,
  59. 'E6' : 1319,
  60. 'F6' : 1397, 'FS6' : 1480,
  61. 'G6' : 1568, 'GS6' : 1661,
  62. 'A6' : 1760, 'AS6' : 1865,
  63. 'BB6' : 1865,
  64. 'B6' : 1976,
  65. 'C7' : 2093, 'CS7' : 2217,
  66. 'D7' : 2349, 'DS7' : 2489,
  67. 'EB7' : 2489,
  68. 'E7' : 2637,
  69. 'F7' : 2794, 'FS7' : 2960,
  70. 'G7' : 3136, 'GS7' : 3322,
  71. 'A7' : 3520, 'AS7' : 3729,
  72. 'BB7' : 3729,
  73. 'B7' : 3951,
  74. 'C8' : 4186, 'CS8' : 4435,
  75. 'D8' : 4699, 'DS8' : 4978
  76. }
  77. melody = [
  78. notes['A3'],notes['E5'],notes['D5'],notes['E5'],notes['A4'],
  79. notes['F3'],notes['F5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'],
  80. notes['D3'],notes['F5'],notes['E5'],notes['F5'],notes['A4'],
  81. notes['G3'],0,notes['D5'],notes['C5'],notes['D5'],notes['C5'],notes['B4'],notes['D5'],
  82. notes['C5'],notes['A3'],notes['E5'],notes['D5'],notes['E5'],notes['A4'],
  83. notes['F3'],notes['F5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'],
  84. notes['D3'],notes['F5'],notes['E5'],notes['F5'],notes['A4'],
  85. notes['G3'],0,notes['D5'],notes['C5'],notes['D5'],notes['C5'],notes['B4'],notes['D5'],
  86. notes['C5'],notes['B4'],notes['C5'],notes['D5'],notes['C5'],notes['D5'],
  87. notes['E5'],notes['D5'],notes['C5'],notes['B4'],notes['A4'],notes['F5'],
  88. notes['E5'],notes['E5'],notes['F5'],notes['E5'],notes['D5'],
  89. notes['E5'],
  90. ]
  91. tempo = [
  92. 1,16,16,4,4,
  93. 1,16,16,8,8,4,
  94. 1,16,16,4,4,
  95. 2,4,16,16,8,8,8,8,
  96. 4,4,16,16,4,4,
  97. 1,16,16,8,8,4,
  98. 1,16,16,4,4,
  99. 2,4,16,16,8,8,8,8,
  100. 4,16,16,4,16,16,
  101. 8,8,8,8,4,4,
  102. 2,8,4,16,16,
  103. 1,
  104. ]
  105.  
  106. def buzz(frequency, length): #create the function "buzz" and feed it the pitch and duration)
  107.  
  108. if(frequency==0):
  109. time.sleep(length)
  110. return
  111. period = 1.0 / frequency #in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec)
  112. delayValue = period / 2 #calcuate the time for half of the wave
  113. numCycles = int(length * frequency) #the number of waves to produce is the duration times the frequency
  114.  
  115. for i in range(numCycles): #start a loop from 0 to the variable "cycles" calculated above
  116. GPIO.output(buzzer_pin, True) #set pin 27 to high
  117. time.sleep(delayValue) #wait with pin 27 high
  118. GPIO.output(buzzer_pin, False) #set pin 27 to low
  119. time.sleep(delayValue) #wait with pin 27 low
  120.  
  121.  
  122.  
  123. def setup():
  124. GPIO.setmode(GPIO.BCM)
  125. GPIO.setwarnings(False)
  126. GPIO.setup(buzzer_pin, GPIO.IN)
  127. GPIO.setup(buzzer_pin, GPIO.OUT)
  128. GPIO.output(buzzer_pin, False)
  129.  
  130. def destroy():
  131. GPIO.cleanup() # Release resource
  132.  
  133.  
  134. def play(melody,tempo=[6],pause=0.3,pace=1.2):
  135.  
  136. for i in range(0, len(melody)): # Play song
  137.  
  138. noteDuration = pace/tempo[i]
  139. buzz(melody[i],noteDuration) # Change the frequency along the song note
  140.  
  141. pauseBetweenNotes = noteDuration * pause
  142. time.sleep(pauseBetweenNotes)
  143.  
  144. class _Getch:
  145. def __call__(self):
  146. fd = sys.stdin.fileno()
  147. old_settings = termios.tcgetattr(fd)
  148. try:
  149. tty.setraw(sys.stdin.fileno())
  150. ch = sys.stdin.read(1)
  151. finally:
  152. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  153. return ch
  154.  
  155. def get():
  156. inkey = _Getch()
  157. while(1):
  158. k=inkey()
  159. if k!='':break
  160. #print (ord(k))
  161. if ord(k)==100:play([notes['C4']],[6],0,0.8)
  162. if ord(k)==102:play([notes['D4']],[6],0,0.8)
  163. if ord(k)==103:play([notes['E4']],[6],0,0.8)
  164. if ord(k)==104:play([notes['F4']],[6],0,0.8)
  165. if ord(k)==106:play([notes['G4']],[6],0,0.8)
  166. if ord(k)==107:play([notes['A4']],[6],0,0.8)
  167. if ord(k)==108:play([notes['B4']],[6],0,0.8)
  168. if ord(k)==114:play([notes['CS4']],[6],0,0.8)
  169. if ord(k)==116:play([notes['DS4']],[6],0,0.8)
  170. if ord(k)==117:play([notes['FS4']],[6],0,0.8)
  171. if ord(k)==105:play([notes['GS4']],[6],0,0.8)
  172. if ord(k)==111:play([notes['AS4']],[6],0,0.8)
  173. return ord(k)
  174.  
  175.  
  176. if __name__ == '__main__': # Program start from here
  177. try:
  178. setup()
  179. #play(melody, tempo)
  180. print("Welcome in keyboard!\nPress [d-l] to do noise.\nExit: Ctrl+C")
  181. while True:
  182. if get()==3:
  183. break
  184. destroy()
  185. except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
  186. destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement