Initialize Pygame’s mixer.
Load the sounds.
Setup three channels (one for each sound) so that we can play
different sounds concurrently.
Let the user know the soundboard is ready.
Poll the button. If the button is pressed, execute the following line.
Play the sound.
Don’t “peg” the processor by checking the buttons faster
than we need to.
This will let us exit the script cleanly when the user hits Ctrl-C,
without showing the trace back message.
4. Go to the command line and navigate to the folder where
you’ve saved
soundboard.py
and execute the script:
pi@raspberrypi ~/soundboard $ python3 soundboard.py
5. After you see “Soundboard Ready,” start pushing the buttons
to play the sound samples.
Depending on how your Raspberry Pi is setup, your sound might
be sent via HDMI to your display, or it may be sent to the 3.5mm
analog audio output jack on the board. To change that, exit out of
the script by pressing Ctrl-C and executing the following command
to use the analog audio output:
pi@raspberrypi ~/soundboard $ sudo amixer cset numid=31
To send the audio through HDMI to the monitor, use:
pi@raspberrypi ~/soundboard $ sudo amixer cset numid=32
Of course, the stock sounds aren’t very interesting, but you can
replace them with any of your own sounds: applause, laughter,
buzzers, and dings. Add them to the
soundboard
directory and
update the code to point to those files. If you want to use more
sounds on your soundboard, add additional buttons and update
the code as necessary.
120 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 120GSW_RASPI_4ED_FIN.indd 120 10/28/21 10:54 AM10/28/21 10:54 AM