Guest User

Untitled

a guest
Jun 22nd, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. import pygame
  2. from time import sleep
  3. import RPi.GPIO as GPIO
  4. from sys import exit
  5. #import sys
  6.  
  7. GPIO.setmode(GPIO.BOARD)
  8. GPIO.setup(40, GPIO.IN) #1
  9. GPIO.setup(37, GPIO.IN) #2
  10. GPIO.setup(35, GPIO.IN) #4
  11. GPIO.setup(33, GPIO.IN) #8
  12. GPIO.setup(31, GPIO.IN) #16
  13. GPIO.setup(29, GPIO.IN) #reset
  14. #GPIO.setup(7, GPIO.IN) #aux input 1
  15. #GPIO.setup(11, GPIO.IN) #aux input 2
  16. #GPIO.setup(13, GPIO.IN) #aux input 3
  17. #GPIO.setup(15, GPIO.IN) #aux input 4
  18.  
  19. pygame.init()
  20. pygame.mixer.init(44000, -16, 2, 4096)
  21.  
  22. TOTALSOUNDS = 31
  23. SOUNDFOLDER = "/home/pi/sounds/"
  24. SOUNDNAME = "sound"
  25. SOUNDEXT = ".ogg"
  26.  
  27. pygame.mixer.set_num_channels(4)
  28.  
  29. snd = [""]
  30. soundChannel = [pygame.mixer.Channel(0), pygame.mixer.Channel(1), pygame.mixer.Channel(2), pygame.mixer.Channel(3)] #[""]
  31. sndchanres = [pygame.mixer.Channel(0), pygame.mixer.Channel(1), pygame.mixer.Channel(2)] #Restricted Sound Channel List
  32. sndMatrix = ["0","0","0","0","0"]
  33. lastplayed = 1
  34. debounce = 0
  35. reset = 0
  36. snch = 1
  37. sloop = 0
  38.  
  39. for x in range(1, TOTALSOUNDS+1):
  40.  snd = snd + [pygame.mixer.Sound(SOUNDFOLDER+SOUNDNAME+str(x)+SOUNDEXT)]
  41.  
  42. #print ("Soundboard Ready.")
  43. soundChannel[0].play(snd[18])
  44. #soundChannel[1].play(snd[18])
  45. #soundChannel[2].play(snd[18])
  46.  
  47. while True:
  48.  try:
  49.   if snch == 3:
  50.    snch = 1
  51.   if (GPIO.input(40) == True):
  52.    sndMatrix[0] = "0"
  53.   else:
  54.    sndMatrix[0] = "1"
  55.   if (GPIO.input(37) == True):
  56.    sndMatrix[1] = "0"
  57.   else:
  58.    sndMatrix[1] = "1"
  59.   if (GPIO.input(35) == True):
  60.    sndMatrix[2] = "0"
  61.   else:
  62.    sndMatrix[2] = "1"
  63.   if (GPIO.input(33) == True):
  64.    sndMatrix[3] = "0"
  65.   else:
  66.    sndMatrix[3] = "1"
  67.   if (GPIO.input(31) == True):
  68.    sndMatrix[4] = "0"
  69.   else:
  70.    sndMatrix[4] = "1"
  71.   if (GPIO.input(29) == True):
  72.    reset = 1
  73.   else:
  74.    reset = 0
  75.   sndtoplay = sndMatrix[4]+sndMatrix[3]+sndMatrix[2]+sndMatrix[1]+sndMatrix[0]
  76.   #print ("found "+str(sndtoplay))
  77.   # #temp keyboard code
  78.   # sndtoplay = sys.stdin.readline()
  79.   # if "r" in sndtoplay:
  80.   # reset = 1
  81.   # sndval = 0
  82.   # else:
  83.   # sndval = int(sndtoplay,2)
  84.   # #end temp keyboard code - uncomment next line
  85.   sndval = int(sndtoplay,2)
  86.   #print ("sound number "+str(sndval))
  87.   snch = 1
  88.   sloop = 0
  89.   sleepval = 0.001
  90.   if sndval == 16:
  91.    continue
  92.   if sndval < 9:
  93.    snch = 0
  94.   if (sndval < debounce) and (sndval > debounce):
  95.    #if sndval <> debounce:
  96.    debounce = sndval
  97.    continue
  98.   if sndval > 8 and sndval < 16 and sndval != 12:
  99.    snch = 2
  100.    sloop = -1
  101.    sleepval = 0.005
  102.   if sndval == 12: #end game music, don't loop
  103.    sloop = 0
  104.   if sndval > 15 and sndval < TOTALSOUNDS:
  105.    if (sndval < debounce) and (sndval > debounce):
  106.    #if sndval < & > debounce:
  107.     debounce = sndval
  108.     continue
  109.   if reset == 0:
  110.    soundChannel[lastplayed].stop()
  111.    reset = 0
  112.    #print ("stop "+str(lastplayed))
  113.   if sndval > 0 and sndval < TOTALSOUNDS:
  114.    if soundChannel[snch].get_sound() == snd[sndval]:
  115.     #print ('.'),
  116.     sleep(sleepval)
  117.     continue
  118.   if sndval > 0 and sndval != 3 and sndval < 8:
  119.    soundChannel[0].play(snd[sndval],sloop)
  120.    lastplayed = snch
  121.    soundChannel[1].stop()
  122.    #print ("play "+str(sndval))
  123.   elif sndval == 3:
  124.    soundChannel[3].play(snd[sndval],sloop)
  125.    #print ("play "+str(sndval))
  126.   elif sndval == 8:
  127.    soundChannel[0].play(snd[sndval],sloop)
  128.    #print ("play "+str(sndval))
  129.   elif sndval == 25 or sndval == 22:
  130.    soundChannel[3].play(snd[sndval],sloop)
  131.    #print ("play "+str(sndval))
  132.   elif sndval == 12:
  133.    pygame.mixer.stop()
  134.    soundChannel[3].play(snd[sndval],sloop)
  135.    pygame.mixer.fadeout(25000)
  136.   elif sndval > 8 and sndval < TOTALSOUNDS and sndval != 25 and sndval != 22:
  137.    sndchanres[snch].play(snd[sndval],sloop)
  138.    lastplayed = snch
  139.    #print ("play "+str(sndval))
  140.   elif sndval == TOTALSOUNDS:
  141.    pygame.mixer.stop()
  142.    #print ('!'),
  143.   elif sndval > TOTALSOUNDS or sndval < 0:
  144.    #print ("invalid code - "+str(sndval))
  145.    #print sndtoplay
  146.    sleep(sleepval)
  147.  except KeyboardInterrupt:
  148.   exit()
Advertisement
Add Comment
Please, Sign In to add comment