Pradeep Singh | 24th Jun 2018
In this article, I am going to share a sample Python script with you that would help you to play an audio (.wav) file on your analog phone line.
1. Hardware Setup:
I would use the same setup that I used for my other modem related articles (i.e. Raspberry Pi 3 connected with USRobotics USR5637 USB Modem).
If you haven’t gone through my other articles, the following picture depicts my Raspberry Pi + Analog Dial-up Modem setup –
2. Compression Method and Sampling Rate Specifications:
While trying to play an audio file over the phone line using the analog modem, you need to specify voice compression method and the sampling specification using the VSM AT Command (+VSM=cml,vsr). The “cml” parameter identifies the compression method and the “vsr” parameter identifies the sampling rate. You may refer to the following table for the supported values for the USRobotics Modem –
Before proceeding further, you need to check the compression method and sampling rate of the file that you want to play over the phone line. If the compression method and/or the sampling rate are not supported by your modem, you will not be able to play the audio.
3. Record Audio File:
If you don’t have a .wav file with the compression method and sampling rate supported by your modem, you need to record it on your computer. Though you can use any software to record the audio file, for this article, I am going to use Audacity audio software.
3.1 Download Audacity:
You can download Audacity from the following link and install on your computer –
3.2 Record Audio File using Audacity:
Before you start recording the audio file, make sure you select “mono” recording channel and “8000Hz” sampling rate as shown in the following screenshot –
3.3 Export the Audio File:
To export the recorded file, go to “File” menu and select “Export –> Export as WAV“. This will show you a Save File dialog box. Make sure you select “Other uncompressed files” for “File type“, “WAV (Microsoft)” for “Header” and “Unsigned 8-bit PCM” for “Encoding” as shown in the following screenshot –
4. AT+VSM Command Values for Recorded Audio File:
Since the audio file recorded in previous steps is saved with the compression method of 8-Bit PCM and contains samples at the rate of 8000Hz, you should use the +VSM AT command with following values –
5. AT Command Sequence for Audio Playback using Analog Voice Modem:
To play the audio file you need to follow the following AT command sequence from the serial communication program or the code that you may write (I used Python 2.7 to execute these commands).
If you don’t know the meaning of DTE or DCE, you may google the details. However, following diagram will give you a quick idea about the DTE and DCE devices in my setup –
The following tables show the AT Commands and their respective responses from the modem (DCE).
In this example, we are assuming that a call will be received on the modem and the Raspberry Pi will pass the AT Commands to the modem to pick the call and play an audio message –
After going off-hook, we are now ready to play the wave file over the phone line using the following AT command sequence. You can use a WAVE driver to send the audio data to the modem.
Once the audio playback is completed, you should hang-up the call using the ATH command as shown in the following table –
6. Download Python Code Sample:
You can download the sample Python script with implementation on these AT Commands from my Github Repository , using the following command –
git clone https://github.com/pradeesi/play_audio_over_phone_line
Following terminal output shows the git clone output on raspberry pi –
pi@raspberrypi:~ $ git clone https://github.com/pradeesi/play_audio_over_phone_line Cloning into 'play_audio_over_phone_line'... remote: Counting objects: 10, done. remote: Compressing objects: 100% (9/9), done. remote: Total 10 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (10/10), done. pi@raspberrypi:~ $
On successful code download, you should see “play_audio_over_phone_line” directory under your present working directory. CD into the “play_audio_over_phone_line” directory and make sure you have all the files as shown in the following terminal output –
pi@raspberrypi:~ $ cd play_audio_over_phone_line/ pi@raspberrypi:~/play_audio_over_phone_line $ ls README.md play_audio.py sample.wav pi@raspberrypi:~/play_audio_over_phone_line $
7. Execute Python Script To Play Audio On The Phone Line:
To run the python script, execute the following command (make sure you are inside play_audio_over_phone_line directory)-
Now you can call your landline number and your modem should play the wave file over the phone line.
Following screenshot shows the terminal output of the python script, during the audio playback –
Record Audio from Phone Line with Raspberry Pi
Incoming Call Details Logger with Raspberry Pi
USB Analog Modem with Raspberry Pi
Python Script to Reset USB Modem COM Port on Raspberry Pi
Python Script to Autodetect Modem COM Port on Raspberry Pi
Wikipedia – Voice modem command set
USRobotics 56K USB Modem Voice Commands
USRobotics 56K USB Modem – Data and General Commands
Voice +V Commands – Developer’s Guide