TwoToneDetect on a Rasperry Pi

Rapberry Pi Running TwoToneDetect

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.  This page describes what I  did to get it running on my Pi.  Your mileage may vary. 

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.  Then take a look at this tutorial to get the Pi configured during the initial boot up.  You'll want to configure the keyboard, locale, and timezone, as well as expand the root partition so that you can use your entire SD card.  Finally, make sure you enable SSH server on your Pi.  This will save you later if you find (like I did) that your keyboard and mouse don't work with USB 1.1 if you have to disable USB 2.0 to get your sound card to work properly.

After you've done your initial configuring, get yourself familiar with the Pi.  You can type:

startx

to start a graphical interface similar to Windows, or just work with the command line, whichever you prefer.  Most of  the TTD setup will be done at the command line, although configuring the audio input is done with a GUI for simplicity.

Installing Prerequisites for TwoToneDetect

Because Pyinstaller doesn't work with the ARM platform that the Pi uses, I can't build an executable for the Pi that will automatically incorporate all of the required prerequisite packages for running TwoToneDetect.  Instead, we'll have to install them manually.  If you follow the instructions here you should get everything you need.  Make sure your Pi is connected to the web before proceeding.

First, we need to install distribute so that we can install a few Python packages. We do this by entering the following at the command prompt:

mkdir python-distribute
cd python-distribute
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py

Then do this, which gets the python.h header file required to compile other modules using PIP:

sudo apt-get install python-dev

Then download PIP and install it by doing this:

cd ..
mkdir pip
cd pip
curl –O https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
tar zxf pip-1.3.1.tar.gz
cd pip-1.3.1
sudo python setup.py install

Once we have PIP installed we can use it to grab and install a couple more required Python modules:
sudo pip install Pycrypto
sudo pip install Iniparse

Installing Pyaudio requires a few more steps:

sudo apt-get install git
git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
sudo pip install pyaudio

We also need to install ffmpeg with and amr codec:
sudo apt-get install ffmpeg libavcodec-extra-53
And we'll install pulse audio and the pulse audio control GUI for setting our audio levels:
sudo apt-get install pulseaudio
sudo apt-get install pavucontrol

Finally, we'll need to change the resampling method for pulseaudio, since TwoToneDetect uses a lower sampling rate than the default of most sound cards.  Use this to start open the required file in the "nano" text editor:

sudo nano /etc/pulse/daemon.conf

then find the line that says:
;resample-method=speex-float-3

and change it to:
resample-method=trivial
note that you need to remove the semicolon at the beginning of the line!

Then reboot to cause changes to take effect.

Setting up pulseaudio

Unlike under Windows, we won't be able to specify our USB sound card 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 under XWindows.  From a terminal window in the XWindows GUI, type pavucontrol to bring up the GUI.  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.

Installing and configuring TwoToneDetect

To install TwoToneDetect, simply download the latest tarball to the directory of your choice on the Pi and extract it using:

tar zxf TwoToneDetect60Pi.tgz

Previous versions:

v60Pi.tgz

v59Pi_b.tgz

The Raspberry Pi version of TwoToneDetect does not include any GUI (to reduce CPU load), which makes configuration a little more difficult.  To edit the tones.cfg file, there are a few options:

  1. Edit manually from the command line using the nano text editor (see below)
  2. Edit manually using the text editor in the Leafpad text editor in the XWindows GUI (see below)
  3. Edit the tones.cfg file using the TonesEditor GUI on a Windows PC and transfer the file to the Pi using WinSCP
  4. Edit the tones.cfg file using the TonesEditor GUI in XWindows by navigating to the TwoToneDetect directory and typing python TonesEditor3.pyc

To edit the config.cfg file, you'll need to manually edit it using  either nano (from the command line) or Leafpad (in the XWindows GUI).  Before editing the config.cfg file, you'll need to determine the index of the pulseaudio input and output so that you can set the input_device_index and output_device_index parameters accordingly.  You'll also need to determine what level to set the audio_threshold parameter to.  To help with these tasks, I've include a LevelMeter utility.  To start it, navigate to your TwoToneDetect directory and type:

python LevelMeter.pyc

The utility will list all of your input and output sound devices.  You want to find the input and output named pulse.  Make note of the index (number) of these, as this is what you'll want to specify for the input_device_index and output_device_index in your config.cfg file. 

Enter in the index corresponding to pulse for the input and output, and the utility will start taking audio samples and printing the audio level to the console window.  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 now what the input_device_index, output_device_index, and audio_threshold parameters should be set to, you can edit the config.cfg file.  Note that the email password must be base64 encoded in the config.cfg file.  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:

python TwoToneDetect60Pi.pyc

USB Audio Input Issues

The USB audio support on the Raspberry Pi still has some issues.  See this forum thread for more information.  To solve this, I had to add this statement to the beginning of /boot/cmdline.txt file:

dwc_otg.speed=1

After modification, /boot/cmdline.txt should look something like this:

dwc_otg.speed=1 dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

After doing this my USB mouse and keyboard stopped working, so I had to access my Pi via PuTTY using SSH.  However, this fixed my audio issues and made TwoToneDetect work well.

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
Subpages (1): PagerPiPyr
Comments