Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi for Secret Agents

You're reading from  Raspberry Pi for Secret Agents

Product type Book
Published in Apr 2013
Publisher Packt
ISBN-13 9781849695787
Pages 152 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Stefan Sjogelid Stefan Sjogelid
Profile icon Stefan Sjogelid

Table of Contents (12) Chapters

Chapter 2. Audio Antics

Greetings! Glad to see that you have powered through the initial setup and could join us for our first day of spy class. In this chapter, we'll be exploring the auditory domain and all the fun things humans (and machines) can do with sound waves.

Configuring your audio gadgets


Before you go jamming all your microphones and noisemakers into the Pi, let's take a minute to get to know the underlying sound system and the audio capabilities of the Raspberry Pi board itself.

Introducing the ALSA sound system

The Advanced Linux Sound Architecture (ALSA), is the underlying framework responsible for making all the sound stuff work on the Pi. ALSA provides kernel drivers for the Pi itself and for most USB gadgets that produce or record sound. The framework also includes code to help programmers make audio applications and a couple of command-line utilities that will prove very useful to us.

In ALSA lingo, each audio device on your system is a card, a word inherited from the days when most computers had a dedicated "sound card". This means that any USB device you connect, that makes or records sound, is a card as far as ALSA is concerned—be it a microphone, headset, or webcam.

Type in the following command to view a list of all connected audio...

Recording conversations for later retrieval


So we have our audio gear all configured and ready to record—let's get sneaky with it!

Picture the following scenario: you know that something fishy is about to go down and you'd like to record whatever sound that fishiness makes. Your first challenge will be to hide the Pi out of sight with as few cables running to it as possible. Unless you're working with a battery, the Pi will have to be hidden somewhere within a few meters of a power outlet.

Next, you'll want to connect your USB microphone and keep it hidden, yet uncovered if possible, to avoid a muffled recording. Unless you expect the action to take place right in front of the microphone, you should set the capture signal to the max with alsamixer for the microphone to be able to pick up as much of the room as possible.

Now, all we need to worry about is how to trigger the recording.

Writing to a WAV file

The Waveform Audio File (WAV) is the most common file format used for recording audio.

  • To...

Listening in on conversations from a distance


What if we want to listen in on some event, live as it goes down, but from a safe distance away from where the Pi's recording—exactly like a baby monitor?

We would need a way of broadcasting whatever is recorded across a network to another computer that we can listen to. Actually, we already have everything required to do this, SSH and SoX; one just have to know how to compose the command lines to wield these powerful tools.

Listening on Windows

You should have the full PuTTY suite installed from the Connecting to the Pi from Windows section from Chapter 1, Getting Up to No Good, as we will be using the plink command for this example.

  1. To download SoX for Windows, visit http://sourceforge.net/projects/sox/files/sox/ and click on the download link for the latest version (sox-14.4.1-win32.exe at the time of writing).

  2. Run the installer to install SoX.

  3. (Optional) To be able to play MP3 files with SoX, download the decoder library file from http://www.intestinate...

Talking to people from a distance


Instead of listening in on the action, maybe you'd like to be the one creating all the noise by making the Pi an extension of your own voice. You'll be on a computer with a microphone and the Pi can be somewhere else broadcasting your message to the world through a pair of speakers (or a megaphone). In other words, the roles of the Pi and your computer from the previous topic will be reversed.

Talking on Windows

First make sure SoX is added to Windows as per the instructions in the Listening on Windows section.

  1. Connect your microphone and check the input volume of your device. On Windows 7 you'll find the settings in Control Panel | Hardware and Sound | Manage audio devices under the Recording tab. Make your microphone the default device by selecting it and clicking on Set Default.

  2. Start a command prompt from the Start menu by clicking on the shortcut or by typing cmd in the Run/Search field.

  3. We can start a monitoring loop first to ensure our microphone works...

Distorting your voice in weird and wonderful ways


Tired of your own voice by now? Let's make it more interesting by applying some interesting SoX effects!

SoX comes with a number of sound effects that can be applied to your audio and optionally saved. Some effects are suitable to use on your live voice while others only make sense when applied to already recorded files.

To see a list of all the possible effects and their parameters, use the following command:

pi@raspberrypi ~ $ sox --help-effect=all

To apply an effect, specify the effect followed by any parameters after the output file or device.

In this example, we'll start a monitoring loop on the Pi and apply a reverb effect to our voice, live as it plays back through the speakers:

pi@raspberrypi ~ $ sox -t alsa plughw:1 -d reverb

How about that? Sounds like we're stuck in a cave. Let's see what parameters the reverb effect takes:

pi@raspberrypi ~ $ sox -t alsa plughw:1 -d reverb ?
usage: [-w|--wet-only] [reverberance (50%) [HF-damping ...

Scheduling your audio actions


In this section, we'll be looking at different techniques of triggering a recording or a playback and optionally how to make it stop after a certain period of time.

Start on power up

The first method we'll cover is also the most blunt—how to start a recording or playback directly when powering up the Raspberry Pi. There isn't really a standardized way of auto-starting regular user applications on boot, so we'll have to improvise a bit to come up with our own way of doing what we want.

The Raspbian boot process is basically a collection of shell scripts being run one after the other, each script performing some important task. One of the last scripts to run is /etc/rc.local, which is a good starting point for our custom autorun solution. Right now, the script doesn't do much, just prints out the IP address of the Pi.

You can try running the script any time using the following command:

pi@raspberrypi ~ $ /etc/rc.local

We could just jam our list of commands right in...

Summary


In this chapter, we learned a great deal about audio under Linux in general and about the ALSA sound system in particular. We know how to configure and test the audio output of the Raspberry Pi board itself and how to set up our USB audio gadgets for recording.

We learned how to use SoX to record sound and store it in multiple formats, how we can avoid typing the same thing over and over with aliases, and how to keep a recording session running with tmux even when network connectivity is spotty.

Armed with only SoX and SSH software, we turned our Pi into a very capable radio—we can put it in a room and listen in, like a baby monitor, or we can let it broadcast our voice and music to the world.

We also learned how to apply SoX effects to spice up our voice or let the Pi make the noise using eSpeak. Finally, we looked at a few different techniques for controlling the timing of our sound-related mischief.

In the upcoming chapter, we'll explore the world of video streaming and motion detection...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi for Secret Agents
Published in: Apr 2013 Publisher: Packt ISBN-13: 9781849695787
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}