TwoToneDetect on a Raspberry Pi

IMG_20130317_151303

This page describes how to get a special version of TwoToneDetect up  and running on a $35 Raspberry Pi single board computer.  Along with a $5 USB sound card and a cheap receiver ($50 Wouxon or similar), you can have a full TTD setup running 24/7 that consumes very little power for under $100.

For alternative instructions on running TTD on the Ubuntu MATE operating system on a Pi3 (rather than Raspbian), see this forum post.

What you’ll need

  • A Raspberry Pi
  • An SD card (4 GB or greater)
  • 5V Power supply with a micro-USB cord to power the Pi
  • A USB sound card (I use this one)
  • A radio receiver (radio, scanner, pager, etc.)
  • A network connection (I used wired ethernet.  You should be able to get this running on WiFi but I haven’t attempted that yet)

Getting Started

First, follow the instructions here to get the latest Raspbian operating system installed on your SD card.  Follow the ‘Download and image Raspbian directly’ section from that page (about halfway down) for getting your image.

TwoToneDetect will work using the GUI found in the Stretch operating system, or it can run without the GUI from the command line using Stretch-Lite  Then take a look at this tutorial to get the Pi configured during the initial boot up.  You’ll want to configure the Localization Options (Locale, TimeZone, Keyboard, and Wifi country) and also Interfacing Options (enable SSH and VNC if so desired for remote access).  If using the GUI image these are available under Preferences/RPi Configuration.

After you’ve done your initial configuring, get yourself familiar with the Pi.  TwoToneDetect can be run from the desktop GUI of the Stretch operating system, or directly from the command line if using Stretch Lite.  Most of  the TTD setup will be done at the command line, although configuring the audio input can be done with a GUI for simplicity.

Installing Prerequisites for TwoToneDetect

From a terminal, run the following to install pulseaudio:

 sudo apt-get install pulseaudio

 

If running Stretch (not Stretch Lite), run this to install a graphical audio control application:

sudo apt-get install pavucontrol

Then install ffmpeg, which is used to convert raw audio recordings to  compressed MP3 and AMR files:

sudo apt-get install ffmpeg

Finally install some required audio codecs:

sudo apt-get install libavcodec-extra57

Installing TwoToneDetect

Now we’re ready to get the TwoToneDetect files.  First let’s make a TTD directory to put our files in, and then go into that directory:

mkdir TTD
cd TTD
Now we’ll fetch the TwoToneDetect files:

curl -O http://www.twotonedetect.net/downloads/TwoToneDetect70.tgz

And then unpack the files:

tar zxf TwoToneDetect70.tgz
There should now be a number of files in your TTD directory.  The following files are executable:
TwoToneDetect70
LevelMeter
TonesEditor9a
They can be run as follows from a command prompt, for example:
/home/pi/TTD/TwoToneDetect70
There will also be a tones.cfg and config.cfg file in the directory.  These files contain the information that TwoToneDetect needs in order to run properly, and will be set up below.

Setting up pulseaudio

Unlike under Windows, we won’t be able to specify our USB sound card directly as the audio input to TwoToneDetect.  This is because the Pi’s audio driver system doesn’t support different sampling rates for the sound card.  It will only allow us to sample at the default rate.  Instead, we’ll use pulseaudio as the input to TwoToneDetect, since pulseaudio will perform resampling from the USB sound card to TwoToneDetect.  To set our audio input levels, we can use the pulse audio control GUI from the desktop.  Access PAVucontrol from the Pi/Sound and Video option from the task bar.  Click on the “Input” tab and set the volume of your USB sound card appropriately, using your radio with open squelch or tuned to a weather broadcast channel to get the levels correct.  Set any other inputs to zero volume. Note:  If you are running a GUI-less version of Raspbian (like Stretch Lite), you can use an environment variable to set the sound input device that you want to use with TTD.  See the section below on “Running Multiple TwoToneDetect Instances” for instructions on how to do this.

Configuring TwoToneDetect

To edit the config.cfg and tones.cfg files, there are a few options:

  1. Edit manually from the command line using the nano text editor (see below)
  2. Edit the tones.cfg file using the TonesEditor GUI on a Windows PC and transfer the file to the Pi using WinSCP
  3. Edit using the TwoToneDetect GUI and/or TonesEditor GUI in the desktop GUI (if not running Stretch Lite):

 

If you’re running TwoToneDetect from the command line without a GUI, you’ll need to determine what level to set the audio_threshold parameter in the config.cfg file to.  To help with this tasks, I’ve include a LevelMeter utility.  To start it, type:

/home/pi/TTD/LevelMeter

The utility will list all of your input and output sound devices.  You want to find the input and output named pulse, if it exists.  If not, try default  or sysdefault.

Enter in the index corresponding to the desired input and output, and the utility will start taking audio samples and printing the audio level to the console window.  You’ll know you have the correct audio device selected when the LED on your USB sound card starts flashing.  Make note of what the audio levels are with no audio input present, and also with audio present.  You’ll want to set your audio_threshold parameter to be somewhat above the levels you see with no audio present, but not so high as to be above the levels when audio is present.  You can adjust the audio of your scanner or radio using this utility as needed.  To stop the audio sampling and terminate the LevelMeter utility, hit CTRL-C.

Now that you know what the audio_threshold parameters should be set to, you can edit the config.cfg file.  Note that the email password and upload ftp password must be base64 encoded in the config.cfg file if you’re editing manually via a text editor.  There are a number of websites that can do this encoding for you, or you can copy this parameter over from a Windows computer that you have running TwoToneDetect.

Once you have the tones.cfg and config.cfg files set up correctly, you can start TwoToneDetect by typing:

./TwoToneDetect70

 

If you’re using the desktop GUI and want to eliminate some extra clicking (or want to have TTD start automatically), you may need to go to a file browser and highlight TwoToneDetect70, then go to EDIT, Preferences, in the GENERAL Tab check the “Don’t ask options on launch executable file” option.

Running Multiple TwoToneDetect Instances

Run this command to list the input devices available to pulseaudio:

LANG=C pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2

In my case this returned something like this:

alsa_output.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-stereo.monitor
alsa_input.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-mono
alsa_output.usb-0d8c_Generic_USB_Audio_Device-00-Device.analog-stereo.monitor
alsa_input.usb-0d8c_Generic_USB_Audio_Device-00-Device.analog-mono

Next, when you start TTD, set the PULSE_SOURCE environment variable to whichever input you want to use from the step above (don’t use the ones that end in “monitor”). Here is an example:

PULSE_SOURCE=alsa_input.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-mono python TwoToneDetect68Pi.py

So, to run two instances of TTD, you’d have two TTD folders set up, and use a different PULSE_SOURCE when starting each instance. You can use the same strategy with the PULSE_SOURCE environment variable to run different instances of darkice for streaming. Of course this can all be scripted for automation. I haven’t tried more than two instances/sound cards, but it does work with two, even with simultaneous detection of pages on each card. I’m not sure what the upper limit is.

Manually editing the tones.cfg file

To manually edit the tones.cfg file without using the GUI, open the file using a text editor.  Each tone set is specified by a [Tone] or [Longtone] delimiter.  For two-tone (A-B format) pages, use [Tone], for long tone pages (long A or long B format), use [Longtone].  See below for a simple example.

[Tone1]
Atone = 389
Btone = 539
Btonelength = 3
Description = Station 5
mp3_Emails = email@address.com

[Longtone1]
Longtone = 457.9
Description = Long Tone Page
Longtonelength = 7
mp3_Emails = email@address.com

Additional tone sets can be added by simply adding more [Tone] or [Longtone] sets to the text file.  However, be sure to use different numbers in the heading.  For example, use [Tone1] and [Tone2].  Using [Tone1] twice, even with a different Description field, will not work.  When you have the tones.cfg file set up the way you want it, be sure to save it before closing.  See above for information on each of the parameters in the tones.cfg file.

Example using all mandatory and optional parameters:

[Tone1]
Atone = 707.3
Btone = 746.8
Atonelength = .6
Btonelength = 1
Description = Fire Department 1
mp3_Emails = email@address.com,email2@address.com,email3@address.com
amr_Emails = email2@textmessaging.com,email1@textmessaging.com
ignore_after = 60                    ;
alert_command = c:\test.bat
post_email_command = c:\test2.bat
radio_frequency = 154.475
exclude_from = 13:25
exclude_to = 13:35
exclude_emails = email2@textmessaging.com,email2@address.com

playback_during_record = 1

record_delay = 2.5