Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.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 # play music as background # set music sound to low volume
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[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 # 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)
def do_fadeout(): # the fadeout thread
pygame.mixer.fadeout(5000) # fade out any announcement going on # 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)
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 # lower the music volume while the announcer is on
else: # when the announcement is no longer happening - the music vol goes back up # raise the music volume back up after announcement is done
except KeyboardInterrupt:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.