Hack61.Build a Custom Ringtone for Your Grandstream Phone

Hack 61. Build a Custom Ringtone for Your Grandstream Phone

Sure, your cell phone has a custom ringtone, but does your IP phone? With a little help from Perl, you'll able to load any sound you like onto the Grandstream phone.

If you carry a cell phone, you've no doubt changed your ringtone once or twice. From a sample of a vintage mechanical ringer to a recording of a C-3PO line from Star Wars, ringtones have become central to pop-culture communication. So why can't you customize the ringtone on a Grandstream IP phone, one of the cheapest and most popular SIP hardphones available?

Well, since you asked, you can. It just takes a little hack job.

The Grandstream's firmware stores the ringtone in its own odd format, a uLaw sound file with a custom header at the beginning of it. It's simple enough to make a uLaw sound file; just use SoX [Hack #24]. But to add the header, a little Perl magic is needed.

5.4.1. The Code

This script was written by Tony Mountifield, and its purpose is to create a Grandstream-compatible ringtone file:

 #!/usr/bin/perl $filename = shift or die "need output filename\n"; undef $/; # slurp whole file at once… $audio = <>; # … like this $filesize = 512 + length $audio; if ($filesize & 1) { # length odd, add a zero byte (should never happen) $audio .= chr(0); } die "Audio file too large\n" if $filesize > 65536; # this is the format for the header $headerfmt = "n n n C4 n C C C C a22 n x216 n n x36 a216"; # get the current date and time ($min, $hour, $day, $month, $year) = (localtime)[1..5]; $year += 1900; $month += 1; # create the header, with zero for the checksum $header = pack $headerfmt, 0, # 0000 $filesize/2, 0, # put checksum in later 1,0,0,1, # version $year, $month, $day, $hour, $min, $filename, 0,   # 0000 or 00C8 - why? 256, # 0100 $filesize/2, "Grandstream standard music ring"; # sanity check $headerlen = length $header; die "header length wrong ($headerlen)\n" unless $headerlen == 512; # add the audio $header .= $audio; # compute the checksum $checksum = unpack "%16n*", $header; #printf "checksum before = %04x\n", $checksum; # insert it in the correct place substr($header,4,2) = pack "n",-$checksum; # ensure the new checksum is zero $checksum = unpack "%16n*", $header; #printf "checksum after = %04x\n", $checksum; die "checksum failed\n" unless $checksum == 0; # write the file open F, ">$filename" or die "can't open output file $filename: $!\n"; print F $header; close F; 

5.4.2. Running the Code

To use this program, save it as makering.pl, make it executable (chmod 755 makering.pl), and pipe a uLaw sound file into it in a shell, like so:

 $ sox my_sound -r 8000 -c 1 -t ul - rate | makering.pl ring1.bin 

In this example, the file my_sound will be resampled to 8000 Hz and will be piped in uLaw format to the standard input of makering.pl, which is the Perl script shown earlier. The enhanced output is then saved as ring1.bin. Upload this file to the /tftpboot directory of your Grandstream's TFTP server and then reboot your Grandstream. (For some tips on setting up a TFTP server, see "Make IP Phone Configuration a Trivial Matter" [Hack #80].) With a fun new ringtone, your IP phone is now as cool as your cell phone.

VoIP Hacks
VoIP Hacks: Tips & Tools for Internet Telephony
ISBN: 0596101333
EAN: 2147483647
Year: 2005
Pages: 156

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net