Asterisk AGI->stream_file is acting very strange

I’ve modified the festival-weather-script.pl (included with TrixBox/Asterisk?) so that it will work with Cepstral but I’m having a very strange problem… The problem is that my first call to $AGI->stream_file seems to be ignored.

Here is my AGI script:

#!/usr/bin/perl

#make a tts dir inside your sounds dir (as specified below)
#adjust the t2wp variable to point to your Cepstral bin directory

use Asterisk::AGI;
use File::Basename;
use Digest::MD5 qw(md5_hex);

$AGI = new Asterisk::AGI;

my %input = $AGI->ReadParse();
my ($text)=@ARGV;
my $hash = md5_hex($text);
my $sounddir = "/var/lib/asterisk/sounds/tts";
my $wavefile = "$sounddir/"."tts-$hash.wav";
my $t2wp= "/opt/swift/bin/";

$AGI->verbose("Text is: " . $text . "\n", 1);

unless (-f $wavefile) {
open(fileOUT, ">$sounddir"."/say-text-$hash.txt");
print fileOUT "$text";
close(fileOUT);
# The next line creates a new wave file...
my $execf=$t2wp."swift -n William -p audio/sampling-rate=8000 -f $sounddir/say-text-$hash.txt -o $wavefile";

$AGI->verbose("File: " . $wavefile . "\n", 1);

system($execf);
unlink($sounddir."/say-text-$hash.txt");
}
# The following line plays the wav file on the audio channel...
my $result2 = $AGI->stream_file('connecting');
my $result = $AGI->stream_file('tts/'.basename($wavefile,".wav"), 1);

$AGI->verbose("Trying to stream: " . 'tts/'.basename($wavefile,".wav") . "\n", 1);

$AGI->verbose("Stream_file returned " . $result2 . "\n", 1);
$AGI->verbose("Stream_file returned " . $result . "\n", 1);

In the Asterisk console (asterisk -r) here is what I see:

-- Executing AGI("SIP/1000-90df", "cepstral2.pl|Testing here") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/cepstral2.pl
  cepstral2.pl|Testing here: Text is: Testing here
  cepstral2.pl|Testing here: Trying to stream: tts/tts-360c161e24a8fb92b0f8c7dead20694e
  cepstral2.pl|Testing here: Stream_file returned
  cepstral2.pl|Testing here: Stream_file returned 0
-- AGI Script cepstral2.pl completed, returning 0

When this AGI script is executed, the first $AGI->stream_file is ignored (and returns blank [??]) but the second $AGI->stream_file works perfect (and correctly returns a 0).

What is going on here? Can anybody help me?

Hi sofakng,
please enable AGI debugging by typing ‘agi debug’ on the CLI.
Then see the debugging messages.

Here is the agi debug…

    -- Executing AGI("SIP/1000-3f41", "cepstral2.pl|Testing here") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/cepstral2.pl
AGI Tx >> agi_request: cepstral2.pl
AGI Tx >> agi_channel: SIP/1000-3f41
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1153324159.186
AGI Tx >> agi_callerid: 1000
AGI Tx >> agi_calleridname: unknown
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 1000
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: mainmenu
AGI Tx >> agi_extension: 2
AGI Tx >> agi_priority: 2
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
AGI Rx << VERBOSE "Text is: Testing here
  cepstral2.pl|Testing here: Text is: Testing here
AGI Tx >> 200 result=1
AGI Rx << " 1
AGI Tx >> 510 Invalid or unknown command
AGI Rx << VERBOSE "Stream_file returned
  cepstral2.pl|Testing here: Stream_file returned
AGI Tx >> 200 result=1
AGI Rx << " 1
AGI Tx >> 510 Invalid or unknown command
AGI Rx << VERBOSE "Stream_file returned 0
  cepstral2.pl|Testing here: Stream_file returned 0
AGI Tx >> 200 result=1
AGI Rx << " 1>
AGI Tx >> 510 Invalid or unknown command
    -- AGI Script cepstral2.pl completed, returning 0

I don’t understand AGI very well, so I don’t see the problem. Do you see what’s wrong?

AGI Tx >> 510 Invalid or unknown command

You code(relating to file handling)after
$AGI->verbose("Text is: " . $text . “\n”, 1);
is having some problem because a successfully run agi command returns
200 and not 510.

The file is not streaming bcause of the errors occuring in the code before it.