Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from time import sleep
- import RPi.GPIO as GPIO
- from sys import exit
- #import sys
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(40, GPIO.IN) #1
- GPIO.setup(37, GPIO.IN) #2
- GPIO.setup(35, GPIO.IN) #4
- GPIO.setup(33, GPIO.IN) #8
- GPIO.setup(31, GPIO.IN) #16
- GPIO.setup(29, GPIO.IN) #reset
- #GPIO.setup(7, GPIO.IN) #aux input 1
- #GPIO.setup(11, GPIO.IN) #aux input 2
- #GPIO.setup(13, GPIO.IN) #aux input 3
- #GPIO.setup(15, GPIO.IN) #aux input 4
- pygame.init()
- pygame.mixer.init(44110, -16, 2, 4096)
- TOTALSOUNDS = 31
- SOUNDFOLDER = "/home/pi/sounds/"
- SOUNDNAME = "sound"
- SOUNDEXT = ".ogg"
- pygame.mixer.set_num_channels(4)
- snd = [""]
- soundChannel = [pygame.mixer.Channel(0), pygame.mixer.Channel(1), pygame.mixer.Channel(2), pygame.mixer.Channel(3)] #[""]
- sndchanres = [pygame.mixer.Channel(0), pygame.mixer.Channel(1), pygame.mixer.Channel(2)] #Restricted Sound Channel List
- sndMatrix = ["0","0","0","0","0"]
- lastplayed = 1
- debounce = 0
- reset = 0
- snch = 1
- sloop = 0
- for x in range(1, TOTALSOUNDS+1):
- snd = snd + [pygame.mixer.Sound(SOUNDFOLDER+SOUNDNAME+str(x)+SOUNDEXT)]
- #print ("Soundboard Ready.")
- soundChannel[0].play(snd[18])
- #soundChannel[1].play(snd[18])
- #soundChannel[2].play(snd[18])
- while True:
- try:
- if snch == 3:
- snch = 1
- if (GPIO.input(40) == True):
- sndMatrix[0] = "0"
- else:
- sndMatrix[0] = "1"
- if (GPIO.input(37) == True):
- sndMatrix[1] = "0"
- else:
- sndMatrix[1] = "1"
- if (GPIO.input(35) == True):
- sndMatrix[2] = "0"
- else:
- sndMatrix[2] = "1"
- if (GPIO.input(33) == True):
- sndMatrix[3] = "0"
- else:
- sndMatrix[3] = "1"
- if (GPIO.input(31) == True):
- sndMatrix[4] = "0"
- else:
- sndMatrix[4] = "1"
- if (GPIO.input(29) == True):
- reset = 1
- else:
- reset = 0
- sndtoplay = sndMatrix[4]+sndMatrix[3]+sndMatrix[2]+sndMatrix[1]+sndMatrix[0]
- #print ("found "+str(sndtoplay))
- # #temp keyboard code
- # sndtoplay = sys.stdin.readline()
- # if "r" in sndtoplay:
- # reset = 1
- # sndval = 0
- # else:
- # sndval = int(sndtoplay,2)
- # #end temp keyboard code - uncomment next line
- sndval = int(sndtoplay,2)
- #print ("sound number "+str(sndval))
- snch = 1
- sloop = 0
- sleepval = 0.001
- if sndval == 16:
- continue
- if sndval < 9:
- snch = 0
- if (sndval < debounce) and (sndval > debounce):
- #if sndval <> debounce:
- debounce = sndval
- continue
- if sndval > 8 and sndval < 16:
- snch = 2
- sloop = -1
- sleepval = 0.005
- if sndval == 12: #end game music, don't loop
- sloop = 0
- if sndval > 15 and sndval < TOTALSOUNDS:
- if (sndval < debounce) and (sndval > debounce):
- #if sndval < & > debounce:
- debounce = sndval
- continue
- if reset == 0:
- soundChannel[lastplayed].stop()
- reset = 0
- #print ("stop "+str(lastplayed))
- if sndval > 0 and sndval < TOTALSOUNDS:
- if soundChannel[snch].get_sound() == snd[sndval]:
- #print ('.'),
- sleep(sleepval)
- continue
- if sndval > 0 and sndval != 3 and sndval < 8:
- soundChannel[0].play(snd[sndval],sloop)
- lastplayed = snch
- soundChannel[1].stop()
- #print ("play "+str(sndval))
- elif sndval == 3:
- soundChannel[3].play(snd[sndval],sloop)
- #print ("play "+str(sndval))
- elif sndval == 8:
- soundChannel[0].play(snd[sndval],sloop)
- #print ("play "+str(sndval))
- elif sndval == 25 or sndval == 22:
- soundChannel[3].play(snd[sndval],sloop)
- #print ("play "+str(sndval))
- elif sndval == 12:
- pygame.mixer.stop()
- soundChannel[3].play(snd[sndval],sloop)
- pygame.mixer.fadeout(25000)
- elif sndval > 8 and sndval < TOTALSOUNDS and sndval != 25 and sndval != 22:
- sndchanres[snch].play(snd[sndval],sloop)
- lastplayed = snch
- #print ("play "+str(sndval))
- elif sndval == TOTALSOUNDS:
- pygame.mixer.stop()
- #print ('!'),
- elif sndval > TOTALSOUNDS or sndval < 0:
- #print ("invalid code - "+str(sndval))
- #print sndtoplay
- sleep(sleepval)
- except KeyboardInterrupt:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment