<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>VoIP Tech Chat &#187; script</title>
	<atom:link href="http://www.voiptechchat.com/tag/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.voiptechchat.com</link>
	<description>Patrick and Fred Chat... sometimes about VoIP</description>
	<lastBuildDate>Fri, 30 Dec 2011 01:34:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Use Asterisk, Cepstral, and Perl to Get Parking and Weather Updates</title>
		<link>http://www.voiptechchat.com/voip/218/use-asterisk-cepstral-and-perl-to-get-parking-and-weather-updates/</link>
		<comments>http://www.voiptechchat.com/voip/218/use-asterisk-cepstral-and-perl-to-get-parking-and-weather-updates/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 23:39:13 +0000</pubDate>
		<dc:creator>Fred</dc:creator>
				<category><![CDATA[VoIP]]></category>
		<category><![CDATA[Adhersion]]></category>
		<category><![CDATA[asterisk]]></category>
		<category><![CDATA[Digium]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[telephone]]></category>

		<guid isPermaLink="false">http://www.voiptechchat.com/?p=218</guid>
		<description><![CDATA[Note: For information on Ann Arbor DDA blocking this application, click here. Patrick and Fred eat, breathe, and live telecommunication. So, when the chance to make fun little phone applications come around, we like to jump on it. Recently, while &#8230; <a href="http://www.voiptechchat.com/voip/218/use-asterisk-cepstral-and-perl-to-get-parking-and-weather-updates/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="note"><strong>Note:</strong> For information on Ann Arbor DDA blocking this application, <a href="http://www.voiptechchat.com/voip/255/a2dda-blocks-asterisk-parking-data/">click here</a>.</p>
<div id="attachment_24" class="wp-caption alignright" style="width: 248px"><img class="size-medium wp-image-24" title="VoIP-Information" src="http://www.voiptechchat.com/wp-content/uploads/2008/05/voip-retro-icon-238x300.gif" alt="VoIP is Fun" width="238" height="300" /><p class="wp-caption-text">VoIP is Fun</p></div>
<p>Patrick and Fred eat, breathe, and live telecommunication. So, when the chance to make fun little phone applications come around, we like to jump on it. Recently, while discussing parking issues with Ann Arbor’s <a href="http://vielmetti.typepad.com/" onclick="pageTracker._trackPageview('/outgoing/vielmetti.typepad.com/?referer=');">Edward Vielmetti</a>, and idea came to mind. Mr. Vielmetti is active within the Ann Arbor community and among the many hats he wears, one seeks to provide access to accurate information concerning downtown transportation and parking (wow that was a mouthful). While discussing the lack of accurate bus information in Ann Arbor, the conversation took a tangent (yes, how odd that conversations with Fred or Patrick take tangents) on the area of parking.</p>
<p>The parking garages in Ann Arbor provide nice signs in front displaying in brightly lit numbers, the amount of available spaces remaining. Ann Arbor also posts this information on the web at the <a href="http://www.a2dda.org/parking__transportation/available_parking_spots/" onclick="pageTracker._trackPageview('/outgoing/www.a2dda.org/parking_transportation/available_parking_spots/?referer=');">A2DDA</a> website. An idea came to mind that basically, you can tell the available spots either from the website or right when you reach the garage — but what if you wanted to know while driving to the garage?</p>
<p>So, with that long introduction, we present “using asterisk, cepstral, and perl to get parking and weather updates.” Ok, for the non-techs, don’t panic! We’ll talk about the techie stuff in a little bit. The bottom line is using Asterisk, Cepstral, and Perl, you can check the internet for the spaces available and let the caller know — all in real time. For a working example, call <span style="text-decoration: line-through;">+1 (212) 937-7844 and </span><strong><span style="text-decoration: line-through;"><span style="font-weight: normal;">press 6 </span></span>+1 (<span style="font-weight: normal;"><strong>734) 272-0909 <span style="font-weight: normal;">(this is not a toll-free number)</span><span style="font-weight: normal;"><span style="font-weight: normal;">.</span></span></strong></span></strong></p>
<p><span id="more-218"></span></p>
<p>First, let’s talk about the three major components here: Asterisk, Cepstral, and Perl.</p>
<h4>Asterisk</h4>
<p><a href="http://www.asterisk.org" onclick="pageTracker._trackPageview('/outgoing/www.asterisk.org?referer=');">Asterisk</a> (by <a href="http://www.digium.com" onclick="pageTracker._trackPageview('/outgoing/www.digium.com?referer=');">Digium</a>) is the world’s leading open source telephony engine and tool kit. Asterisk empowers communication with it’s flexibility. Whether working as a simple office telephone system, a robust Call Center platform, or anything in-between, Asterisk provides advanced features at a very low deployment cost.</p>
<p>Asterisk is free, open source software provided under the <a href="http://www.gnu.org/" onclick="pageTracker._trackPageview('/outgoing/www.gnu.org/?referer=');">GNU General Public License</a> (GPL). Asterisk is the most popular open source software available, with the Asterisk Community being the top influencer in VoIP.</p>
<p>Why free? “<a href="http://www.digium.com" onclick="pageTracker._trackPageview('/outgoing/www.digium.com?referer=');">It’s just how Digium rolls.</a>” They really take that GPL open source to heart.</p>
<h4>Cepstral</h4>
<p><a href="http://cepstral.com" onclick="pageTracker._trackPageview('/outgoing/cepstral.com?referer=');">Cepstral</a> provides speech technologies and services for the spoken delivery of information. They build high quality, natural sounding voices for hand-held, desktop, and server applications. Their technology is easy to incorporate and operates in a small memory footprint with low computing resources. Cepstral is not free, however a full user’s license can cost under $30.00 (as of January 2009 at least).</p>
<p>If you are looking for a free alternative to Cepstral, try <a href="http://festvox.org/festival/" onclick="pageTracker._trackPageview('/outgoing/festvox.org/festival/?referer=');">Festival</a>. Festival is free, but sounds very “machine” like. Cepstral is a little more human.</p>
<h4>Perl</h4>
<p><a href="http://www.perl.org/" onclick="pageTracker._trackPageview('/outgoing/www.perl.org/?referer=');">Perl</a> is a stable, cross platform programming language. Perl is open-source and is sometimes called the “duct tape of the internet.” If MacGyver programmed, he would have used Perl.</p>
<p>Ok, with our introductions in hand, let’s get on with the show. First step was to get the information from the A2DDA website using a perl script. the perl code for that was:</p>
<pre>my $ua = LWP::UserAgent-&gt;new( timeout =&gt; 45);
my $URL = 'http://www.a2dda.org/parking.php';
$ua-&gt;agent('AsteriskAGIQuery/1');
my $req = new HTTP::Request GET =&gt; $URL;
my $res = $ua-&gt;request($req);
if ($res-&gt;is_success()) {
 if ($res-&gt;content =~ /4th and Washington&lt;/td&gt;&lt;td&gt;(.*)&lt;/td&gt;&lt;/tr&gt;/) {
  my $spaces = $1;
  if($spaces =~ m/(d+)/) {
			$spaces = $1;
			return $spaces;
		}
 }
}</pre>
<p>Sure, it could be nicer, but it works.</p>
<p>Ok, next was to resign the script so that it would work with the <a href="http://www.voip-info.org/wiki-Asterisk+AGI" onclick="pageTracker._trackPageview('/outgoing/www.voip-info.org/wiki-Asterisk+AGI?referer=');">Asterisk AGI</a> (the gateway interface of Asterisk). We modified the script slightly to read the amount of parking spaces at 4th and Washington and if we’re unable to get a number, report an ERROR.</p>
<pre>#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
$|=1;
sub trim($);

my %AGI;

while(&lt;STDIN&gt;) {
	chomp;
	last unless length($_);
	if (/^agi_(w+):s+(.*)$/) {
		$AGI{$1} = $2;
	}
}

print qq(VERBOSE "STATUS: checking a2dda.org for spaces available" 2n);

my $spacesavail = "";

if ($spacesavail = &amp;parking_lookup) {
	print qq(VERBOSE "There are $spacesavail spaces at 4th and Washington" 2n);
	print qq(SET VARIABLE SPACESAVAIL "$spacesavail"n);
	exit(0);
	}
else {
	print qq(VERBOSE "STATUS: unable to determine spaces" 2n);
}

print qq(SET VARIABLE SPACESAVAIL "ERROR"n);
exit(0); 

sub parking_lookup {
  my $ua = LWP::UserAgent-&gt;new( timeout =&gt; 45);
  my $URL = 'http://www.a2dda.org/parking.php';
  $ua-&gt;agent('AsteriskAGIQuery/1');
  my $req = new HTTP::Request GET =&gt; $URL;
  my $res = $ua-&gt;request($req);
  if ($res-&gt;is_success()) {
  		if ($res-&gt;content =~ /4th and Washington&lt;/td&gt;&lt;td&gt;(.*)&lt;/td&gt;&lt;/tr&gt;/) {
      my $spaces = $1;
      if($spaces =~ m/(d+)/) {
							$spaces = $1;
							return $spaces;
						}
     }
  }
  return "";
}</pre>
<p>Now, we have a number of available spaces, and integrating it was as easy as:</p>
<pre>exten =&gt; s,1,NoOp(checking parking)
exten =&gt; s,n,playback(/tmp/spaces-greet)
exten =&gt; s,n,AGI(getspaces.pl)
exten =&gt; s,n,GotoIf($["${SPACESAVAIL}" = "ERROR" ]?error)
exten =&gt; s,n,SayNumber(${SPACESAVAIL})
exten =&gt; s,n,playback(/tmp/spaces-avail)</pre>
<p>So why do we need Cepstral?</p>
<p>Good question! During testing of the script, we noticed that the number of spaces was always 130. No matter what we did, we kept getting back 130. Fred was pulling out what little hair he has&#8230; checking the asterisk&#8230; checking the code&#8230; couldn’t figure it out. Then, after checking the website, realized that <strong>Ann Arbor’s garages weren’t updating the spaces available</strong>. Perhaps a glitch, perhaps they don’t care, perhaps the information is being held hostage. No one knows for sure, but as of this moment, it’s pinned at 130.</p>
<p>Anyway, so enter Cepstral. Since the parking spots wouldn’t change, maybe the weather would. So, using another <a href="http://www.voiptechchat.com/voip/208/perl-script-to-put-weather-on-your-polycom-microbrowser/">script to check the weather</a>, you can modify the results and create a sound file using Cepstral (and swift), such as:</p>
<pre>my $hash = "";
$hash .= ('0'..'9', "a".."f")[int(rand(16))] for 1 .. 8;

my $text = qq(Currently &lt;break strength="weak" /&gt; it is $currtempf degrees &lt;break strength="weak" /&gt; in $city &lt;break strength="medium" /&gt; Current conditions &lt;break strength="weak" /&gt; $currcondition &lt;break strength="medium" /&gt; Rest of day predicts &lt;break strength="weak" /&gt; $todaycond &lt;break strength="medium" /&gt;);

my $sounddir = "/tmp";
my $wavefile = "$sounddir/"."tts-$hash.wav";
my $wavefileAst = "$sounddir/"."tts-$hash";
my $t2wp= "/opt/swift/bin/"; 

unless (-f $wavefile) {
       open(fileOUT, "&gt;$sounddir"."/say-text-$hash.txt");
       print fileOUT "$text";
       close(fileOUT);
							my $execf=$t2wp."swift -f $sounddir/say-text-$hash.txt -p audio/channels=1,audio/volume=70,audio/sampling-rate=8000,audio/deadair=2 -o $wavefile";
       system($execf);
       unlink($sounddir."/say-text-$hash.txt");
} 

print qq(SET VARIABLE SWIFTFILE "$wavefileAst"n);</pre>
<p>Yes, again the code could be nicer.. but in this case we create a sound file for the weather conditions and then make a sound file variable. Yes, AGI supports streaming the audio and even playing it back directly from the script&#8230; so why did we chose to exit the script and return to the dialplan? Asterisk is very versatile. We like returning to the dialplan, and that’s how we code. Life is that simple. What works for you best, can sometimes be the best solution.</p>
<p>Ok, bringing that into the asterisk dial plan, you have:</p>
<pre>exten =&gt; s,1,NoOp(checking parking)
exten =&gt; s,n,playback(/tmp/spaces-greet)
exten =&gt; s,n,AGI(testspaces.pl)
exten =&gt; s,n,GotoIf($["${SPACESAVAIL}" = "ERROR" ]?error)
exten =&gt; s,n,SayNumber(${SPACESAVAIL})
exten =&gt; s,n,playback(/tmp/spaces-avail)
exten =&gt; s,n,goto(weather)
exten =&gt; s,n(error),playback(/tmp/spaces-error)
exten =&gt; s,n(weather),AGI(current-weather.pl)
exten =&gt; s,n,GotoIf($["${CITY}" = "ERROR" ]?end)
exten =&gt; s,n,playback(${SWIFTFILE})
exten =&gt; s,n(end),goto(main,s,1)</pre>
<p>Your comments are always welcome!</p>
<h3>UPDATE — 01/10/2009</h3>
<p>Well, the system has been upgraded a little bit&#8230; if you call <strong><span style="text-decoration: line-through;">+1 (212) 937-7844</span></strong><span style="text-decoration: line-through;"> and press option </span><strong><span style="text-decoration: line-through;">6</span> +1 (<span style="font-weight: normal;"><strong>734) 272-0909</strong> you can choose your garage, and then be automatically called back (on the number you called from) when there are fewer than 10 spaces available.</span></strong></p>
<p>Comments&#8230; we love to hear them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voiptechchat.com/voip/218/use-asterisk-cepstral-and-perl-to-get-parking-and-weather-updates/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.269 seconds -->

