Welcome back to the Trick or Trivia Blog. In this installment, I am going to show you how I managed to get the audio portion of this project up and running. I hit a slight bump in the road shortly after setting down to figure all of this out. I had planned on having several different layers of sound playing at once, but have only managed to get a background and foreground set of layers working together, and I think that will be enough.
I had originally planned on running the background / ambient audio from within the same python script that the main program was in, but I slowly realized that this was not only a bad idea, but it simply might not work with the way I plan on randomizing questions. I will confess that I spent more than a few hours trying to get the ambient audio working in a subprocess, and several other parallel processing methods, but failed miserably, and decided to run the ambient audio another way. I decided to take the easy route, and just write a separate python script that would allow me to play the ambient audio loop when the booted up. By removing this process from my main TrickOrTrivia.py script, I was able to move on to getting the audio working with the buttons. Before we get into how I did that, let’s take a quick look at the hardware that is used in this installment of Trick Or Trivia.
Below you will see a list of the hardware used to build out the audio portion of this project. In addition to these components, you will need the following tools: a soldering iron, solder, flush cutters, wire strippers, 3-10 feet of 2-pair cable, and a 6-inch or longer 3.5mm to 3.5mm audio extension cable.
Newark Part No.
Manufacturer / Description
RASPBERRY PI 2, MODEL B,
RASPBERRY PI 8GB NOOBS MICRO SD CARD
POWER SUPPLY 5V, 1A
USB A PLUG TO MICRO USB B PLUG
ADAFRUIT USB WIFI MODULE
VISATON SPEAKER, 20 kHz, 8OHM, 4W
|7-Inch Touch Screen||1||Raspberry Pi 7" Touch Screen Display|
MCM Part No.
Manufacturer / Description
Audio Amplifier Kit 2 X 5W RMS
Building the Velman 2x5W Amplifier
One of the major things that I have learned from being in the Haunted Attraction industry is that lighting and sound are two of the biggest “make it or break it” features of a successful prop. When I was putting together the kit for this project, I knew I wanted audio to be a big part of the project. The Raspberry Pi makes it quite easy to add audio to a project, but unfortunately, unless your project makes use of earbuds, you will need to add an amplifier to the project to drive more powerful speakers.
For this project, I chose the Velleman 2x5W Amplifier kit from MCM Electronics. This kit is designed for even the most novice maker to be able to assemble, and it’s quite powerful for its small size. I also chose to use a single small three-inch, . This speakers is a little undersized for this project and this amp, but it works just fine as long as you do not max out the amp’s volume control.
The kit is very straight forward, does not include any confusing, hard-to-identify parts, nor does it utilize any SMD parts that would make it hard to solder. The toughest part to solder in the whole kit is the power indicator LED, as you need to bend it at a very specific point if you want to follow the build instructions 100% word-for-word. I built this entire board in less than 10 minutes.
I sort of went off script and soldered up several of the amp’s components at once. If you follow the directions, you will solder each type of component step by step. This was way to slow for me, and I have hand soldered so many SMD boards in the past few months, that I can solder a through-hole board like this with my eyes closed.
I finished up the board with a second round of soldering. This time I soldered the IC, and other large / heavy components. When soldering terminal blocks, ICs, and other components that are hard to keep in place, or that have several leads, I like to solder one of the leads on an end of the component first. This lets me lock the component in place, then I can use my fingers to re-align the part while re-heating that single solder joint.
It’s hard to see in this photo, but I set the potentiometer all the way to the left, then placed the knob on it with the indicator dot down in the bottom left corner. This will place the dot almost perfectly opposite this position when the volume is turned to max.
The one thing I always say about soldering is that flux is your friend. Velleman must know this as well because they coated the entire bottom of the PCB in a very sticky resin-based flux. I still used my flux pen on a per-joint basis as I like flux on the component leads I am soldering as well.
Wiring up the speaker is pretty straight forward as Visaton was kind enough to mark the leads with + and - symbols to identify its leads. For those wondering, the + lead is almost always the larger of the two leads. Rumor has it, that this was adopted as common practice first in the automotive industry back in the 1970s. You will note that I used some spare two-conductor, shielded microphone wire. You can use any two-conductor wire you have, just pay attention to the polarity. The speaker will work even if it’s reversed, but the best sound quality comes from a properly wired speaker.
Connect the other end of the speaker wire to the amp while paying attention to the polarity. You can also connect the power cable to the screw terminals to the left at this point. The amp requires a 6-14v 1A DC power source. You can power this with an old 9v or 12v wall-adapter, or even a 9-volt battery, but the battery will struggle to output enough current to keep the amp at full capacty.
The Ambient Audio Code
To start off let's quickly cover the background / ambient audio working and how I set it up to begin when the Raspberry Pi Boots. Below is the Python script that I wrote to play the mp3 file I selected as the ambient source. I have broken out each section, and commented on what it does.
To get started we need to import the pygame library. I know a lot of you would have liked to see me use OMXplayer, but there were some things I could not get to work as they should, and I just chose to use something I was familiar with instead.
Next we need to define the path to the ambient.mp3 file, and give it a name.
audio_path = '/home/pi/Desktop/audio/ambient.mp3'
Now we need to set a variable to True
var = True
Now we need to write a while-loop to play our mp3 file, and set it to only play if var is equal to True.
while var ==True:
Now we need to initialize PyGame.
Then we need to load the MP3 file we want to play.
Now we need to set the pygame player’s volume. The range is between 0.0 and 1.0 so a setting of 0.5 would be half way.
Finally we need to tell pygame to play the MP3 file, and set it to loop five times.
The full code is pasted below. Alternatively you can download this code used in this tutorial at the Github repository for this project. The audio files are available for download from here. If you do not want to modify the code, create a folder in the Desktop directory called “audio” and move all three of the mp3 files into it.
import pygame audio_path = '/home/pi/Desktop/audio/ambient.mp3' var = True while var ==True: pygame.mixer.init() pygame.mixer.music.load(audio_path) pygame.mixer.music.set_volume(1.0) pygame.mixer.music.play(5)
Navigate to the Open a new file called ambient.py using the Nano text editor by entering the following command
sudo nano ambient.py
Then copy and paste the code above into the file. Save and exit, and then use the following command to test the pi.
sudo python ambient.py
You should hear the ambient.mp3 file begin to play if you have the amplifier / speaker combo we just built hooked up via a 3.5mm to 3.5mm audio cable from the amp to the Raspberry Pi. To get this python script to run on boot, we need to add it to the Raspberry Pi’s crontab. Enter the following command in the terminal to create a new crontab entry.
sudo crontab -e
Now paste the following line at the bottom of the crontab.
@reboot sudo python /home/pi/Desktop/TriviaScrips/ambient.py
then save and exit out of the file. Reboot the Raspberry Pi using the command below. When the Pi reboots, you should hear the ambient.py file playing after you login.
If the audio is quite low despite the amplifier’s volume being maxed out, you will need to turn the Raspberry Pi’s volume up. This is as simple as entering the small command found below, into the terminal.
amixer cset numid=1 -- 400
The range of amixer’s volume is -10200 and +400 in centi-dB units. Since we are using an external amplifier, we can set the Raspberry Pi’s volume to its max setting at +400, and adjust the volume on the amp accordingly.
Correct and Incorrect Answer Audio
I plan on changing out the audio files used for the correct and incorrect answer triggers, but for now the two that are in the download will work just fine. I wish I had time to get the Correct and Incorrect audio recorded and mastered, but I have not had time to sit down and hook up my recording gear.
The first thing we need to do is open the TrickorTrivia.py script and make some modifications. This script is the same as the one we used in the last installment of this tutorial, but has a new name. With that said, I am not going to go over every single line of code. Just the few bits and pieces we need to add.
The first thing we need to do is import the pygame library.
Now we need to define two audio paths and give them names.
correct_audio_path = '/home/pi/Desktop/audio/correct.mp3' incorrect_audio_path = '/home/pi/Desktop/audio/incorrect.mp3'
Finally we need to edit the blink_led functions, to make the LED illuminate once when the correct and incorrect answers are selected. We also need to add in a few lines to play the correct and incorrect answer audio files. For a more precise breakdown of this audio code, see the ambient code written earlier in this post.
def blink_led(): # endless loop, on/off for 1 second while True: GPIO.output(26,True) pygame.mixer.init() pygame.mixer.music.load(correct_audio_path) pygame.mixer.music.set_volume(1.0) pygame.mixer.music.play(5) time.sleep(10) GPIO.output(26,False) GPIO.cleanup() pygame.quit() sys.exit()
def blink_led_2(): # endless loop, on/off for 1 second while True: GPIO.output(19, True) pygame.mixer.init() pygame.mixer.music.load(incorrect_audio_path) pygame.mixer.music.set_volume(1.0) pygame.mixer.music.play(5) time.sleep(10) GPIO.output(19,False) GPIO.cleanup() pygame.quit() sys.exit()
That’s all that we have to modify in this script to get the audio files playing when a button is pressed. When the correct answer is chosen, the green LED will illuminate, the correct.mp3 file will play, and when done, the LED will turn off and the script will exit. The same goes for the incorrect answer. The red LED will illuminate, the incorrect.mp3 file will play, and then when finished the LED will turn off and the script will exit. Below is the full code.
To test if the code works, enter the Desktop GUI by typing the following command:
Then open LXterminal, and navigate to the TrickorTrivia.py script.
Then run the TrickorTrivia.py scrip with the following command:
sudo python TrickorTrivia.py
This should open the trivia interface and when you select the answer, the corresponding LED should light up and audio file play.
I really wish I could have gotten the ambient audio working on a sub-process, but it will work just fine the way I set it up by running on boot via crontab. I will admit that the speaker is a little underpowered, and I am not utilizing the Amp to its full potential. This is an easy fix, if you have an old set of small bookshelf speakers laying around. That is going to wrap this installment of Project: Trick or Trivia. Check back in a few days for the next installment. Until then, remember to Hack The World, and Make Awesome!