spacepaste

  1.  
  2. from gpiozero import LED
  3. from time import sleep
  4. import time
  5. import threading
  6. from threading import Thread
  7. import RPi.GPIO as GPIO
  8. import pygame
  9. import sys
  10. import subprocess
  11. import os
  12. import signal
  13. from multiprocessing import Process
  14. # SET PYGAME
  15. pygame.mixer.pre_init(44100, -16, 1, 4096)
  16. pygame.init()
  17. pygame.display.set_mode(( 100, 100))
  18. ########################################################################
  19. ########################## DEFINITIONS #################################
  20. ########################################################################
  21. proc1 = 0
  22. proc3 = 0
  23. unlockKeyboard = 2
  24. processes = []
  25. # NOTES SOUNDS
  26. pause = ''
  27. noteF_ = pygame.mixer.Sound('/home/pi/Desktop/notes/F3.wav')
  28. noteG_ = pygame.mixer.Sound('/home/pi/Desktop/notes/G3.wav')
  29. noteA_ = pygame.mixer.Sound('/home/pi/Desktop/notes/A3.wav')
  30. noteB_ = pygame.mixer.Sound('/home/pi/Desktop/notes/B3.wav')
  31. noteC = pygame.mixer.Sound('/home/pi/Desktop/notes/C4.wav')
  32. noteD = pygame.mixer.Sound('/home/pi/Desktop/notes/D4.wav')
  33. noteE = pygame.mixer.Sound('/home/pi/Desktop/notes/E4.wav')
  34. noteF = pygame.mixer.Sound('/home/pi/Desktop/notes/F4.wav')
  35. noteG = pygame.mixer.Sound('/home/pi/Desktop/notes/G4.wav')
  36. noteA = pygame.mixer.Sound('/home/pi/Desktop/notes/A4.wav')
  37. noteB = pygame.mixer.Sound('/home/pi/Desktop/notes/B4.wav')
  38. # SONGS NOTES
  39. bonjovi = [pause, noteA_, noteC, noteC, noteC, noteC, noteB_, noteA_, noteG_, noteG_, noteE, noteE, noteE, noteE, noteG, noteC, noteC, noteE, noteC, noteC, noteC, noteC, noteC, noteB_, noteA_, noteG_, noteG_, noteC, noteA_]
  40. guns = [pause, noteB_, noteB_, noteA_, noteA_, noteG_, noteA_, noteG_, noteA_, noteG_, noteF_, noteG_, noteB_, noteB_, noteB_, noteG_, noteB_, noteB_, noteB_, noteA_, noteA_, noteA_, noteG_, noteC, noteB_, noteG_, noteB_, noteB_, noteG_]
  41. u2 = [pause, noteC, noteC, noteC, noteD, noteE, noteD, noteD, noteE, noteD, noteC, noteC, noteD, noteE, noteD, noteD, noteE, noteD, noteC]
  42. # SONGS TIMES
  43. bonjoviTime = [1,0.5,0.5,1.75,0.25,0.5,0.5,0.5,0.25,1.25,0.25,0.25,0.25,0.25,0.5,0.5,0.5,1.5,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.25,2.25,1,1]
  44. gunsTime = [2.5,0.5,0.5,0.5,0.25,0.25,0.25,0.5,0.25,0.25,0.75,1,1,0.5,0.5,1,0.5,0.5,0.25,0.25,0.5,0.25,0.5,1.25,1,1,0.5,0.5, 2]
  45. u2Time = [3.25,0.25,0.25,0.25,0.5,2.75,0.25,0.5,0.25,1,2,0.25,0.5,3.25,0.25,0.5,0.25,1, 2.625]
  46. ## RPI 2 INPUT PINS
  47. GPIO.setup([14,15,18,23], GPIO.IN)
  48. ## RPI 3 INPUT PINS
  49. GPIO.setup([24,25,8,7], GPIO.IN)
  50. ## RPI 4 INPUT PINS
  51. GPIO.setup([12,16,20], GPIO.IN)
  52. ## MY VARS
  53. pinsAll = [4,17,27,22,10,9,11,5,6,13,19] # All keyboard notes
  54. ########################################################################
  55. ########################### FUNCTIONS ##################################
  56. ########################################################################
  57. # START ANIMATION (blink all keyboard items 3 times)
  58. def startAnimation():
  59. i = 0
  60. while (i < 3):
  61. turnPinOn(pinsAll,0)
  62. sleep(0.5)
  63. turnPinOff(pinsAll)
  64. sleep(0.5)
  65. i += 1
  66. # SEND IMPUT SIGNAL TO ANOTHER RASPBERRY (Used to start 'cascades')
  67. def sendInputToOtherRpi(pin):
  68. print('ACTIVATED RELAY ', pin)
  69. GPIO.setup(pin, GPIO.OUT)
  70. GPIO.output(pin, GPIO.HIGH)
  71. sleep(0.05)
  72. GPIO.setup(pin, GPIO.IN)
  73. # TURN PIN ON (optionally OFF)
  74. def turnPinOn(pin,turnOff):
  75. print('turn pin on',pin)
  76. GPIO.setup(pin, GPIO.OUT)
  77. GPIO.output(pin, GPIO.HIGH)
  78. if turnOff:
  79. sleep(0.1)
  80. turnPinOff(pin)
  81. sleep(0.05)
  82. # TURN PIN OFF
  83. def turnPinOff(pin):
  84. print('turn pin off',pin)
  85. GPIO.setup(pin, GPIO.IN)
  86. # RETURN PIN FOR NOTE CASCADE
  87. def returnRpiPinFromNote(note):
  88. if note == noteF_:
  89. return 14
  90. if note == noteG_:
  91. return 15
  92. if note == noteA_:
  93. return 18
  94. if note == noteB_:
  95. return 23
  96. if note == noteC:
  97. return 24
  98. if note == noteD:
  99. return 25
  100. if note == noteE:
  101. return 8
  102. if note == noteF:
  103. return 7
  104. if note == noteG:
  105. return 12
  106. if note == noteA:
  107. return 16
  108. if note == noteB:
  109. return 20
  110. # RETURN PIN FOR NOTE LED
  111. def returnNotePinFromNote(note):
  112. if note == noteF_:
  113. return 4
  114. if note == noteG_:
  115. return 17
  116. if note == noteA_:
  117. return 27
  118. if note == noteB_:
  119. return 22
  120. if note == noteC:
  121. return 10
  122. if note == noteD:
  123. return 9
  124. if note == noteE:
  125. return 11
  126. if note == noteF:
  127. return 5
  128. if note == noteG:
  129. return 6
  130. if note == noteA:
  131. return 13
  132. if note == noteB:
  133. return 19
  134. # PLAY NOTE SOUND
  135. def playNote(note):
  136. print('play note', note)
  137. note.play()
  138. def listeningToKeyboard():
  139. print('listening to keyboard')
  140. while True:
  141. for event in pygame.event.get():
  142. if event.type == pygame.KEYDOWN:
  143. key = event.key
  144. print('keydown')
  145. print(key)
  146. if key == pygame.K_m:
  147. print('bye bye!')
  148. pygame.quit()
  149. sys.exit()
  150. if key == pygame.K_l:
  151. playNote(noteF_)
  152. if key == pygame.K_k:
  153. playNote(noteG_)
  154. if key == pygame.K_j:
  155. playNote(noteA_)
  156. if key == pygame.K_f:
  157. playNote(noteB_)
  158. if key == pygame.K_d:
  159. playNote(noteC)
  160. if key == pygame.K_s:
  161. playNote(noteD)
  162. if key == pygame.K_a:
  163. playNote(noteE)
  164. if key == pygame.K_o:
  165. playNote(noteF)
  166. if key == pygame.K_i:
  167. playNote(noteG)
  168. if key == pygame.K_u:
  169. playNote(noteA)
  170. if key == pygame.K_r:
  171. playNote(noteB)
  172. if key == pygame.K_e:
  173. playCompleteSong('bonjovi')
  174. if key == pygame.K_w:
  175. playCompleteSong('guns')
  176. if key == pygame.K_q:
  177. #playCompleteSong('u2')
  178. gameOver()
  179. #############################################
  180. def playCompleteSong(song):
  181. if song == 'bonjovi':
  182. proc2 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/bon-jovi/bon_ jovi_drums+piano_v2.wav'])
  183. if song == 'guns':
  184. proc2 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/guns/guns_piano+drums_v2.wav'])
  185. if song == 'u2':
  186. proc2 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/u2/u2_drums+piano_v2.wav'])
  187. #cascadeTrigger(song)
  188. playDemoSong(song)
  189. def playDemoSong(song):
  190. choice = song
  191. if song == 'bonjovi':
  192. song = bonjovi
  193. time = bonjoviTime
  194. if song == 'guns':
  195. song = guns
  196. time = gunsTime
  197. if song == 'u2':
  198. song = u2
  199. time = u2Time
  200. i = 0
  201. for note in song:
  202. if note == pause:
  203. sleep(time[i])
  204. i += 1
  205. else:
  206. turnPinOn(returnNotePinFromNote(note),1)
  207. p = Process(target=turnPinOn, args=(returnNotePinFromNote(note), 1))
  208. p.start()
  209. processes.append(p)
  210. for p in processes:
  211. p.join()
  212. sleep(time[i] - 0.1)
  213. i += 1
  214. sleep(2)
  215. startAnimation()
  216. threading.Thread(playDrums(choice))
  217. sleep(1)
  218. def playDrums(song):
  219. print('PLAY ', song)
  220. global proc1
  221. if song == 'bonjovi':
  222. proc1 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/bon-jovi/bon-jovi-drums_v2.wav'])
  223. if song == 'guns':
  224. proc1 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/guns/guns_drums_v2.wav'])
  225. if song == 'u2':
  226. proc1 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/u2/u2_drums_v2.wav'])
  227. print(proc1)
  228. #listeningToKeyboard()
  229. global proc3
  230. proc3 = subprocess.call(['python', 'cascade-script-subprocess.py'])
  231. print(proc3)
  232. #threading.Thread(cascadeTrigger(song))
  233. def cascadeTrigger(song):
  234. if song == 'bonjovi':
  235. song = bonjovi
  236. timeS = bonjoviTime
  237. if song == 'guns':
  238. song = guns
  239. timeS = gunsTime
  240. if song == 'u2':
  241. song = u2
  242. timeS = u2Time
  243. print('cascade trigger')
  244. i = 0
  245. for note in song:
  246. if note == pause:
  247. sleep(timeS[i])
  248. i += 1
  249. else:
  250. print(note)
  251. sendInputToOtherRpi(returnRpiPinFromNote(note))
  252. sleep(timeS[i] - 0.1)
  253. i += 1
  254. def gameOver():
  255. proc1.terminate()
  256. proc3.terminate()
  257. turnPinOn(pinsAll,0)
  258. sleep(3)
  259. turnPinOff(pinsAll)
  260. kbT = Thread(target=listeningToKeyboard)
  261. kbT.start()
  262.