-
- from gpiozero import LED
- from time import sleep
- import time
- import threading
- from threading import Thread
- import RPi.GPIO as GPIO
- import pygame
- import sys
- import subprocess
- import os
- import signal
- from multiprocessing import Process
-
- # SET PYGAME
- pygame.mixer.pre_init(44100, -16, 1, 4096)
- pygame.init()
- pygame.display.set_mode(( 100, 100))
-
- ########################################################################
- ########################## DEFINITIONS #################################
- ########################################################################
-
- proc1 = 0
- proc3 = 0
- unlockKeyboard = 2
- processes = []
-
- # NOTES SOUNDS
- pause = ''
- noteF_ = pygame.mixer.Sound('/home/pi/Desktop/notes/F3.wav')
- noteG_ = pygame.mixer.Sound('/home/pi/Desktop/notes/G3.wav')
- noteA_ = pygame.mixer.Sound('/home/pi/Desktop/notes/A3.wav')
- noteB_ = pygame.mixer.Sound('/home/pi/Desktop/notes/B3.wav')
- noteC = pygame.mixer.Sound('/home/pi/Desktop/notes/C4.wav')
- noteD = pygame.mixer.Sound('/home/pi/Desktop/notes/D4.wav')
- noteE = pygame.mixer.Sound('/home/pi/Desktop/notes/E4.wav')
- noteF = pygame.mixer.Sound('/home/pi/Desktop/notes/F4.wav')
- noteG = pygame.mixer.Sound('/home/pi/Desktop/notes/G4.wav')
- noteA = pygame.mixer.Sound('/home/pi/Desktop/notes/A4.wav')
- noteB = pygame.mixer.Sound('/home/pi/Desktop/notes/B4.wav')
-
- # SONGS NOTES
- 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_]
- 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_]
- u2 = [pause, noteC, noteC, noteC, noteD, noteE, noteD, noteD, noteE, noteD, noteC, noteC, noteD, noteE, noteD, noteD, noteE, noteD, noteC]
-
- # SONGS TIMES
- 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]
- 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]
- 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]
-
- ## RPI 2 INPUT PINS
- GPIO.setup([14,15,18,23], GPIO.IN)
- ## RPI 3 INPUT PINS
- GPIO.setup([24,25,8,7], GPIO.IN)
- ## RPI 4 INPUT PINS
- GPIO.setup([12,16,20], GPIO.IN)
-
- ## MY VARS
- pinsAll = [4,17,27,22,10,9,11,5,6,13,19] # All keyboard notes
-
- ########################################################################
- ########################### FUNCTIONS ##################################
- ########################################################################
-
- # START ANIMATION (blink all keyboard items 3 times)
- def startAnimation():
- i = 0
- while (i < 3):
- turnPinOn(pinsAll,0)
- sleep(0.5)
- turnPinOff(pinsAll)
- sleep(0.5)
- i += 1
-
- # SEND IMPUT SIGNAL TO ANOTHER RASPBERRY (Used to start 'cascades')
- def sendInputToOtherRpi(pin):
- print('ACTIVATED RELAY ', pin)
- GPIO.setup(pin, GPIO.OUT)
- GPIO.output(pin, GPIO.HIGH)
- sleep(0.05)
- GPIO.setup(pin, GPIO.IN)
-
- # TURN PIN ON (optionally OFF)
- def turnPinOn(pin,turnOff):
- print('turn pin on',pin)
- GPIO.setup(pin, GPIO.OUT)
- GPIO.output(pin, GPIO.HIGH)
- if turnOff:
- sleep(0.1)
- turnPinOff(pin)
- sleep(0.05)
-
- # TURN PIN OFF
- def turnPinOff(pin):
- print('turn pin off',pin)
- GPIO.setup(pin, GPIO.IN)
-
- # RETURN PIN FOR NOTE CASCADE
- def returnRpiPinFromNote(note):
- if note == noteF_:
- return 14
- if note == noteG_:
- return 15
- if note == noteA_:
- return 18
- if note == noteB_:
- return 23
- if note == noteC:
- return 24
- if note == noteD:
- return 25
- if note == noteE:
- return 8
- if note == noteF:
- return 7
- if note == noteG:
- return 12
- if note == noteA:
- return 16
- if note == noteB:
- return 20
-
- # RETURN PIN FOR NOTE LED
- def returnNotePinFromNote(note):
- if note == noteF_:
- return 4
- if note == noteG_:
- return 17
- if note == noteA_:
- return 27
- if note == noteB_:
- return 22
- if note == noteC:
- return 10
- if note == noteD:
- return 9
- if note == noteE:
- return 11
- if note == noteF:
- return 5
- if note == noteG:
- return 6
- if note == noteA:
- return 13
- if note == noteB:
- return 19
-
- # PLAY NOTE SOUND
- def playNote(note):
- print('play note', note)
- note.play()
-
- def listeningToKeyboard():
- print('listening to keyboard')
- while True:
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- key = event.key
- print('keydown')
- print(key)
- if key == pygame.K_m:
- print('bye bye!')
- pygame.quit()
- sys.exit()
- if key == pygame.K_l:
- playNote(noteF_)
- if key == pygame.K_k:
- playNote(noteG_)
- if key == pygame.K_j:
- playNote(noteA_)
- if key == pygame.K_f:
- playNote(noteB_)
- if key == pygame.K_d:
- playNote(noteC)
- if key == pygame.K_s:
- playNote(noteD)
- if key == pygame.K_a:
- playNote(noteE)
- if key == pygame.K_o:
- playNote(noteF)
- if key == pygame.K_i:
- playNote(noteG)
- if key == pygame.K_u:
- playNote(noteA)
- if key == pygame.K_r:
- playNote(noteB)
- if key == pygame.K_e:
- playCompleteSong('bonjovi')
- if key == pygame.K_w:
- playCompleteSong('guns')
- if key == pygame.K_q:
- #playCompleteSong('u2')
- gameOver()
-
- #############################################
- def playCompleteSong(song):
- if song == 'bonjovi':
- proc2 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/bon-jovi/bon_ jovi_drums+piano_v2.wav'])
- if song == 'guns':
- proc2 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/guns/guns_piano+drums_v2.wav'])
- if song == 'u2':
- proc2 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/u2/u2_drums+piano_v2.wav'])
- #cascadeTrigger(song)
- playDemoSong(song)
-
- def playDemoSong(song):
- choice = song
- if song == 'bonjovi':
- song = bonjovi
- time = bonjoviTime
- if song == 'guns':
- song = guns
- time = gunsTime
- if song == 'u2':
- song = u2
- time = u2Time
-
- i = 0
- for note in song:
- if note == pause:
- sleep(time[i])
- i += 1
- else:
- turnPinOn(returnNotePinFromNote(note),1)
- p = Process(target=turnPinOn, args=(returnNotePinFromNote(note), 1))
- p.start()
- processes.append(p)
-
- for p in processes:
- p.join()
-
- sleep(time[i] - 0.1)
- i += 1
-
- sleep(2)
- startAnimation()
- threading.Thread(playDrums(choice))
- sleep(1)
-
- def playDrums(song):
- print('PLAY ', song)
- global proc1
- if song == 'bonjovi':
- proc1 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/bon-jovi/bon-jovi-drums_v2.wav'])
- if song == 'guns':
- proc1 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/guns/guns_drums_v2.wav'])
- if song == 'u2':
- proc1 = subprocess.Popen(['aplay','-q', '/home/pi/Desktop/piano-wav-final/material_v2/u2/u2_drums_v2.wav'])
- print(proc1)
- #listeningToKeyboard()
- global proc3
- proc3 = subprocess.call(['python', 'cascade-script-subprocess.py'])
- print(proc3)
- #threading.Thread(cascadeTrigger(song))
-
- def cascadeTrigger(song):
- if song == 'bonjovi':
- song = bonjovi
- timeS = bonjoviTime
- if song == 'guns':
- song = guns
- timeS = gunsTime
- if song == 'u2':
- song = u2
- timeS = u2Time
- print('cascade trigger')
- i = 0
- for note in song:
- if note == pause:
- sleep(timeS[i])
- i += 1
- else:
- print(note)
- sendInputToOtherRpi(returnRpiPinFromNote(note))
- sleep(timeS[i] - 0.1)
- i += 1
-
- def gameOver():
- proc1.terminate()
- proc3.terminate()
- turnPinOn(pinsAll,0)
- sleep(3)
- turnPinOff(pinsAll)
-
- kbT = Thread(target=listeningToKeyboard)
- kbT.start()
-