login | register
Fri 09 of Jan, 2009 [01:15 UTC]

voip-info.org

History

Perl EAGI

Created by: spditner,Last modification on Tue 24 of Jun, 2008 [16:25 UTC] by seraph
#!/usr/bin/perl 

# Note that this example doesn't check the results of AGI calls, and doesn't use 
# Asterisk::AGI in an attempt to keep it simple and dependency free. 

# This program is free software; you can redistribute it and/or modify 
# it under the same terms as Perl itself. 

# Author: Simon P. Ditner / http://uc.org/simon 

# Usage: 
#    - Create an AGI in /var/lib/asterisk/agi-bin, i.e.: perl.eagi 
#    - 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 

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

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

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

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

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

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

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

exit;

Comments

Comments Filter
222

333Information abour $buffer var

by seraph, Tuesday 24 of June, 2008 [16:27:16 UTC]
Hello there.

I just found this script which actually works for an aplication that I'm developing with EAGI, I just want to know what's in the $buffer var

my $bytes_read = $audio_fh->read( $buffer, 80000 );

Also if is it possible to record this info into a dat file with structure sample time sample rate and sample value, something like.

open( $fh, ">/tmp/recording.dat" );

I know it's possible to convert it using SOX but in the proyect there's a condition that can't be removed, the condition is that the script must be executed in less that 1 second. and I have to decode what is the DAT file so teh script to decode already takes 0,7 seconds and sox to convert it into a dat file takes 0.8 and sometimes more.

I hope it can be achieve.

Thanks a lot
222

333IO::Handle problem

by repa, Saturday 12 of May, 2007 [21:22:10 UTC]
The IO::Handle is a little bit buggy for me, sometimes it just won't open the file descriptor. Fortunately there is another way to open a file descriptor:
$FDNUM=3;
open(FH, "<&$FDNUM");