Perl EAGI

  1. !/usr/bin/perl
  2. Note that this example doesn't check the results of AGI calls, and doesn't use
  3. Asterisk::AGI in an attempt to keep it simple and dependency free.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the same terms as Perl itself.
  6. Author: Simon P. Ditner / http://uc.org/simon
  7. Usage:
  8. - Create an AGI in /var/lib/asterisk/agi-bin, i.e.: perl.eagi
  9. - Call using EAGI from your dialplan: exten => 100,1,EAGI(perl.eagi)
use warnings;
use strict;

use IO::Handle;

$| = 1; # Turn of I/O Buffering
my $buffer = undef;
my $result = undef;
my $AUDIO_FD = 3; # Audio is delivered on file descriptor 3
my $audio_fh = new IO::Handle;
$audio_fh->fdopen( $AUDIO_FD, "r" ); # Open the audio file descriptor for reading

  1. Skip over the preamble that Asterisk sends this AGI
while( <STDIN> ) {
chomp;
last if length == 0;
}

  1. Playback beep
print "STREAM FILE beep \"#\"\n"; $result = <STDIN>;

  1. Record 5 seconds of audio at 8,000 samples/second (uses 16 bit integers)
  2. 5 seconds x 8000 samples/second x ( 16 bits / 8bits/byte ) = 80000 bytes
my $bytes_read = $audio_fh->read( $buffer, 80000 );
$audio_fh->close();

  1. Playback beep
print "STREAM FILE beep \"#\"\n"; $result = <STDIN>;

  1. Write the raw audio to a file for later analysis
my $fh;
open( $fh, ">/tmp/recording.raw" );
print $fh $buffer;
close( $fh );

  1. Also convert the raw audio on-the-fly to the GSM format using 'sox', so that
  2. we can play it back to the user right now.
open( $fh, "|/usr/bin/sox -t raw -r 8000 -s -b 16 -c 1 - /tmp/recording.gsm" );
  1. | | | | | | |
  2. | | | | | | '-- Write to this file
  3. | | | | | '-- Read from STDIN
  4. | | | | '-- Mono Audio
  5. | | | '---- Samples are words (a word is 2 bytes = 16 bit audio)
  6. | | '---- The audio is signed (32766..-32766)
  7. | '---- The sample rate is 8,000 samples/second
  8. '---- The input format is SLIN, which is 'raw' audio
print $fh $buffer;
close( $fh );

  1. Playback /tmp/recording.gsm
print "STREAM FILE /tmp/recording \"#\"\n"; $result = <STDIN>;

exit;
Created by: spditner, Last modification: Thu 15 of Jul, 2010 (05:50 UTC) by newacct


Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+

Page Changes | Comments

 

Featured -

Search: