Now that you have the circuit breadboarded, it’s time to work
on the code:
1. Create a new directory in your home directory called
sound-
board
.
2. Open that folder and create a file there called
soundboard.py
.
3. Open
soundboard.py
and type in the following code:
import pygame.mixer
from time import sleep
from gpiozero import Button
from sys import exit
button1 = Button(4)
button2 = Button(14)
button3 = Button(25) Again, BCM 4,14 and 25 match up
with pins 4,14, and 25??
pygame.mixer.init(48000, -16, 1, 1024)
soundA = pygame.mixer.Sound(
"/usr/share/sounds/alsa/Front_Center.wav")
soundB = pygame.mixer.Sound(
"/usr/share/sounds/alsa/Front_Left.wav")
soundC = pygame.mixer.Sound(
"/usr/share/sounds/alsa/Front_Right.wav")
soundChannelA = pygame.mixer.Channel(1)
soundChannelB = pygame.mixer.Channel(2)
soundChannelC = pygame.mixer.Channel(3)
print "Soundboard Ready."
while True:
try:
button1.wait_for_press()
soundChannelA.play(soundA)
button2.wait_for_press()
soundChannelB.play(soundB)
button3.wait_for_press()
soundChannelC.play(soundC)
sleep(.01)
except KeyboardInterrupt:
exit()
Programming Inputs and Outputs with Python 119
GSW_RASPI_4ED_FIN.indd 119GSW_RASPI_4ED_FIN.indd 119 10/28/21 10:54 AM10/28/21 10:54 AM
Initialize Pygames 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
Going Further
gpiozero documentation
(gpiozero.readthedocs.io/en/stable/)
The gpiozero library is not only under active development, it’s
got a ton of functionality that we’ve barely touched on here.
It would be well worth your time to dig through the documenta-
tion, breadboard up some components, and see what you can
do with it.
Programming Inputs and Outputs with Python 121
GSW_RASPI_4ED_FIN.indd 121GSW_RASPI_4ED_FIN.indd 121 10/28/21 10:54 AM10/28/21 10:54 AM
GSW_RASPI_4ED_FIN.indd 122GSW_RASPI_4ED_FIN.indd 122 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset