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.com/libmad.dll and put it in the sox-14-4-1 folder at C:Program Files (x86).
  4. Start a command prompt from the Start menu by clicking on the shortcut or by typing in cmd in the Run/Search field.

The following examples will be executed in the command prompt environment. Note that the C:Program Files (x86) directory on later versions of Windows might be called C:Program Files. Just erase the (x86) part from the paths if the commands fail.

To start a recording on the Pi and send the output to our Windows machine, use the command that follows, but replace [IP address] with the IP address of your Pi and [password] with your login password:

C:> "C:Program Files (x86)PuTTYplink" pi@[IP address] -pw [password] sox -t alsa plughw:1 -t sox - | "C:Program Files (x86)sox-14-4-1sox" -q -t sox - -d

SoX will behave just as if it was running locally on the Pi with the volume meters moving on sound input.

Let's break down the command:

  • "C:Program Files (x86)PuTTYplink" is the full path to the plink application. The quotes are necessary because of the space in the Program Files (x86) directory name. plink is like a command line version of PuTTY but more suitable for interfacing with other applications such as SoX in our example.
  • We specify that we want to log in as the user pi@[IP address] and to use the password –pw [password] because the command won't work if it has to pause and prompt us for that information.
  • sox -t alsa plughw:1 -t sox - starts a sox command on the Pi itself but sends the output to our Windows machine through the SSH link.
  • | "C:Program Files (x86)sox-14-4-1sox" -q -t sox - -d then pipes that output to our local sox application which we've given a -q or quite mode argument for cosmetic reasons, otherwise SoX would show two competing progress displays.
  • The two -t sox arguments instruct SoX to use its own native, uncompressed file format, which is especially useful for transporting audio between SoX pipes such as this one.

Another useful trick is to be able to store the recording on your Windows machine instead of the SD card on the Pi. The following command will record from the Pi to the myrec.wav file on your local desktop:

C:> "C:Program Files (x86)PuTTYplink" pi@[IP address] -pw [password] sox -t alsa plughw:1 -t wav - > %UserProfile%Desktopmyrec.wav

Note the > character instead of the pipe, which is used to redirect the output to a file.

Of course, you should also know how to simply copy files from your Pi using the pscp command. The following command copies myrec.wav from the pi user's home directory to your local desktop:

C:> "C:Program Files (x86)PuTTYpscp" pi@[IP address]:myrec.wav %UserProfile%Desktopmyrec.wav

Just reverse the argument order of the previous command to copy myrec.wav from your local desktop to the pi user's home directory:

C:> "C:Program Files (x86)PuTTYpscp" %UserProfile%Desktopmyrec.wav pi@[IP address]:myrec.wav

Finally, let's make sure you never have to type one of those long commands again by creating a simple shortcut on the desktop. Type in the following command from the command prompt:

C:> notepad %UserProfile%DesktopPiRec.cmd

Answer Yes when prompted to create a new file, paste one of the long commands, then exit and save. You should now be able to double-click on the shortcut on your desktop to start a new listening or recording session.

Listening on Mac OS X or Linux

Since Mac OS X and most Linux distributions include an SSH client, all we need is SoX.

  1. First you need to add the SoX application to your OS:
    1. To download SoX for Mac OS X, visit http://sourceforge.net/projects/sox/files/sox/ and click on the download link for the latest version (sox-14.4.1-macosx.zip at the time of writing) and save it to your desktop.
    2. To install SoX on Linux, use the package manager of your distribution to add the sox package.
  2. On Mac, double-click on the SoX ZIP file to extract it.
  3. Open up a Terminal (located in /Applications/Utilities on the Mac).
  4. On Mac, type cd ~/Desktop/sox-14.4.1 to change into the extracted SoX directory. Then type sudo cp sox /usr/bin to copy the sox binary to a location in our default path.
  5. (Optional) On Mac, to be able to play MP3 files with SoX, download the decoder library file http://www.intestinate.com/libmad.dylib and save it to the extracted SoX directory. Then type sudo cp libmad.dylib /usr/lib to copy the decoder library to a location in our default path.

To start a recording on the Pi and send the output to our machine, use the following command, but replace [IP address] with the IP address of your Pi:

$ ssh pi@[IP address] sox -t alsa plughw:1 -t sox - | sox -q -t sox - -d

SoX will behave just as if it was running locally on the Pi with the volume meters moving on sound input.

Let's break down the command:

  • ssh pi@[IP address] sox -t alsa plughw:1 -t sox - starts a sox command on the Pi itself but sends the output to our machine through the SSH link.
  • | sox -q -t sox - -d then pipes that output to our local sox application which we've given a -q or quite mode argument for cosmetic reasons, otherwise SoX would show two competing progress displays.
  • The two -t sox arguments instruct SoX to use its own native, uncompressed file format, which is especially useful for transporting audio between SoX pipes like this one.

Another useful trick is to be able to store the recording on your machine instead of the SD card on the Pi. The following command will record from the Pi to myrec.wav on your local desktop:

$ ssh pi@[IP address] sox -t alsa plughw:1 -t wav - > ~/Desktop/myrec.wav

Note the > character instead of the pipe, which is used to redirect the output to a file.

Of course, you should also know how to simply copy files from your Pi using the scp command. The following command copies myrec.wav from the pi user's home directory to your local desktop:

$ scp pi@[IP address]:myrec.wav ~/Desktop/myrec.wav

Just reverse the argument order of the previous command to copy myrec.wav from your local desktop to the pi user's home directory:

$ scp ~/Desktop/myrec.wav pi@[IP address]:myrec.wav

To avoid having to remember those long commands, you could easily create aliases for them, using the same techniques we covered previously in this chapter. Only on Mac OS X you need to put your lines in ~/.bash_profile instead of ~/.bash_aliases:

$ echo "alias pilisten='ssh pi@[IP address] sox -t alsa plughw:1 -t sox - | sox -q -t sox - -d'" >> ~/.bash_profile
..................Content has been hidden....................

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