Skip to content

Instantly share code, notes, and snippets.

@scottwegner
Created March 14, 2014 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottwegner/9553399 to your computer and use it in GitHub Desktop.
Save scottwegner/9553399 to your computer and use it in GitHub Desktop.
Python code use for Raspberry Pi Intermission / Announce Device
import thread # the usual suspects of imports
from time import sleep
import RPi.GPIO as GPIO
import pygame.mixer
import random
from sys import exit
pygame.mixer.init(44100, -16, 1, 1024) # set up the mixer
announceAudio = pygame.mixer.Sound("seats-1.wav") # initially give announceAudio a value
soundChannelA = pygame.mixer.Channel(1) # initialize a mixer channel
soundChannelB = pygame.mixer.Channel(2) # initialize another mixer channel - we'll need 2
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # configure GPIO 23 for pushbutton in
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # configure GPIO 24 for pushbutton in
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # configure GPIO 25 for pushbutton in
GPIO.setup(18, GPIO.OUT) # configure GPIO 18 for a simple LED output
def do_play_music(): # this will be the thread that will start music playback
pygame.mixer.music.play(1) # play music as background
pygame.mixer.music.set_volume(0.4) # set music sound to low volume
sleep(3)
def music_callback(channel): # this is the callback function for the music playback
if GPIO.input(23):
music = ["auditorium-break-1.wav","auditorium-break-2.wav","auditorium-break-3.wav"]
random.shuffle(music,random.random) # randomly shuffle up the three music files
pygame.mixer.music.load(music[0]) # uses the pygame music function which streams the music
thread.start_new_thread(do_play_music, ()) # calls the music start thread
GPIO.remove_event_detect(23) # resets the GPIO event detect
GPIO.add_event_detect(23, GPIO.RISING, callback=music_callback, bouncetime=300)
sleep(1) # the above line re-initializes the event detect
def do_announce(): # the announcement thread routine
soundChannelA.play(announceAudio) # play the announcement
def announce_callback(channel): # this is the announcement callback
if GPIO.input(24):
global announceAudio # had to make this variable global so that it could be passed
seatsAudio = ["seats-1.wav","seats-2.wav","seats-3.wav"] # to the thread routine
random.shuffle(seatsAudio,random.random) # randomize the announce files
announceAudio = pygame.mixer.Sound(seatsAudio[0]) # assign the variable to be passed to the thread
thread.start_new_thread(do_announce, ()) # start the playback thread
GPIO.remove_event_detect(24) # reset the GPIO detect and reinitialize on next line
GPIO.add_event_detect(24, GPIO.RISING, callback=announce_callback, bouncetime=200)
sleep(1)
def do_fadeout(): # the fadeout thread
pygame.mixer.fadeout(5000) # fade out any announcement going on
pygame.mixer.music.fadeout(5000) # fade out background music
def fade_callback(channel): # the fadeout callback routine
if GPIO.input(25):
thread.start_new_thread(do_fadeout, ()) # start the fadeout thread
GPIO.remove_event_detect(25) # reset the GPIO detect and reinitialize it on the next line
GPIO.add_event_detect(25, GPIO.RISING, callback=fade_callback, bouncetime=200)
sleep(1)
GPIO.add_event_detect(23, GPIO.RISING, callback=music_callback, bouncetime=300) # these three lines set up the
GPIO.add_event_detect(24, GPIO.RISING, callback=announce_callback, bouncetime=300) # event detects for the first time
GPIO.add_event_detect(25, GPIO.RISING, callback=fade_callback, bouncetime=200) # and assign which callback is
# associated with which button
while True: # this is the main program loop - which is just that
try: # an endless loop that keep things going.
GPIO.output(18, True) # turn on the LED to show that the program is running.
if pygame.mixer.get_busy(): # if at any time an announcement is going on the music channel is lowered
pygame.mixer.music.set_volume(0.15) # lower the music volume while the announcer is on
else: # when the announcement is no longer happening - the music vol goes back up
pygame.mixer.music.set_volume(0.4) # raise the music volume back up after announcement is done
except KeyboardInterrupt:
GPIO.cleanup()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment