Ich wollte ein Tool schreiben, welches mir es ermöglicht, einen MPD mit Hilfe eines Telefons zu steuern.
MPD ist der Music Player Daemon - Homepage
Ich habe also angefangen, mir ein AGI zu schreiben, welches mir eine solche Steuerung erlaubt.
Mittlerweile haben mir Pylon und hotshot geholfen und haben nun eine funktionierende Version erstellt. Danke euch beiden.
Asterisk-Server mit AGI- und perl-Unterstützung - Link
Music Player Daemon - Link
mpc auf dem Asterisk-Server mit Verbindung zum MPD - Link
Das perl Asterisk-Modul (Asterisk::AGI) - Link
perl-know-how da der code noch nicht fertig ist und nur ruckelig läuft.
#!/usr/bin/perl -w
# initialize asterisk
use Asterisk::AGI;
#use strict;
#use warnings;
$|=1;
$ENV{'MPD_HOST'} = "trillian";
$ENV{'PATH'} = "$ENV{'PATH'}:/usr/bin";
# Modes
my $AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();
my $key=0;
my $SHOWGREET=1;
my $MAINKEYS='1234567890*#';
my $DEBUG=1;
# location of the wave file cache and owrking directory
my $SOUNDDIR = "/var/lib/asterisk/festivalcache";
if (! -e $SOUNDDIR) { `mkdir -p -m0776 $SOUNDDIR`; }
# festival text2wave location
my $T2WDIR= "/usr/bin/";
my $ZERO = ord('0'); # ASCII codes?
my $ONE = ord('1'); # Give me a break!
my $TWO = ord('2');
my $THREE = ord('3');
my $FOUR = ord('4');
my $FIVE = ord('5');
my $SIX = ord('6');
my $SEVEN = ord('7');
my $EIGHT = ord('8');
my $NINE = ord('9');
my $STAR = ord('*');
my $POUND = ord('#');
#Play the greeting
if (! -e "$SOUNDDIR/greeting.ul") {
`echo "Welcome to the trillian M.P.D. client\n Press hash to get the menu\n" | text2wave -o $SOUNDDIR/greeting.ul -otype ulaw -`;
}
if ($SHOWGREET) {
$AGI->stream_file("$SOUNDDIR/greeting",0123);
}
# Create menu if not exists
if (! -e "$SOUNDDIR/menu.ul") {
open(FH, ">$SOUNDDIR/menu.txt");
print FH "Press \"1\" to Rewind 10 seconds\n\n";
print FH "Press \"2\" to Increase the Volume\n\n";
print FH "Press \"3\" to Forward 10 seconds\n\n";
print FH "Press \"4\" to Previous track\n\n";
print FH "Press \"5\" to Toggle Play/Pause\n\n";
print FH "Press \"6\" to Skip to Next track\n\n";
print FH "Press \"7\" to Toggle Repeat mode\n\n";
print FH "Press \"8\" to Decrease Volume\n\n";
print FH "Press \"9\" to Toggle Random mode\n\n";
print FH "Press \"0\" to Stop playing\n\n";
print FH "Press \"*\" to Exit";
close(FH);
qx(text2wave -o $SOUNDDIR/menu.ul -otype ulaw $SOUNDDIR/menu.txt);
}
main($key); # Never returns..
myExit("Mission Improbable!");
sub main
{
while (1)
{
$key = 0;
while ($key == 0)
{
$key= $AGI->wait_for_digit(4000) if ($key == 0);
}
if ($key == $STAR) { &myExit("User wanted to kill"); }
elsif ($key == $POUND) { &play_main_menu(); $key="ack";}
elsif ($key == $ZERO ) { system("mpc stop 1>/dev/null 2>/dev/null"); $key="ack";}
elsif ($key == $ONE ) { system("mpc seek -0:0:10 1>/dev/null 2>/dev/null"); $key="ack";}
elsif( $key == $TWO ) { system("mpc volume +1 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $THREE ) { system("mpc seek +0:0:10 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $FOUR ) { system("mpc prev 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $FIVE ) { system("mpc toggle 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $SIX ) { system("mpc next 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $SEVEN ) { system("mpc repeat 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $EIGHT ) { system("mpc volume -1 1>/dev/null 2>/dev/null"); $key="ack"; }
elsif( $key == $NINE ) { system("mpc random 1>/dev/null 2>/dev/null"); $key="ack"; }
else
{
$AGI->verbose('PANIC! key:' . $key . '!!!', 1) if ($DEBUG);
$key = 0;
}
}
}
sub play_main_menu {
$AGI->stream_file("$SOUNDDIR/menu", $MAINKEYS);
}
sub myExit {
my $msg = shift;
$AGI->verbose("Exited: $msg");
$AGI->hangup();
die $msg;
exit;
}
sub getTTSFilename {
my ( $text ) = @_;
my $hash = md5_hex($text);
my $wavefile = "$SOUNDDIR"."tts-diirectory-$hash.wav";
unless( -f $wavefile ) {
open( fileOUT, ">$SOUNDDIR"."say-text-$hash.txt" );
print fileOUT "$text";
close( fileOUT );
my $execf=$T2WDIR."text2wave $SOUNDDIR"."say-text-$hash.txt -F 8000 -o $wavefile";
system( $execf );
unlink( $SOUNDDIR."say-text-$hash.txt" );
}
return "$SOUNDDIR".basename($wavefile,".wav");
}
sub check_result {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
print STDERR "FAIL ($res)\n";
$fail++;
exit 1;
} else {
print STDERR "PASS ($1)\n";
return $1;
}
} else {
print STDERR "FAIL (unexpected result '$res')\n";
exit 1;
}
}