Speech-Recognizer-SPX

 view release on metacpan or  search on metacpan

server-demo  view on Meta::CPAN

#!/usr/bin/perl

# server-demo: demo of streaming audio to PocketSphinx over a socket.

# Copyright (c) 2000 Cepstral LLC.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# Written by David Huggins-Daines <dhuggins@cs.cmu.edu>

use strict;
use Speech::Recognizer::SPX::Server;
use IO::Socket;
use Fcntl;
use Errno;
use Time::HiRes qw(usleep gettimeofday);
use Getopt::Std;

use vars qw($SPS);
$SPS = 16000;

$|=1;

my %opt;
getopts('d:h', \%opt);

if ($opt{h}) {
    print <<EOF;
Usage: $0 [-d DEVICE]
EOF
    exit 0;
}
my $dspdev = $opt{d} || '/dev/dsp';

my $parent_sock = new IO::Socket;
my $child_sock = new IO::Socket;
socketpair $parent_sock, $child_sock, AF_UNIX, SOCK_STREAM, PF_UNIX
    or die "Can't create socket pair: $!";
my $parent = $$;

if (my $kid = fork) {
    # Audio::SPX really sucks, so don't use it.  The following is
    # totally Linux/FreeBSD specific (and probably won't work on
    # big-endian platforms either) but this is just a hacky demo
    # script anyway.  As long as you have a reasonable audio source
    # you should do fine.
    use constant SNDCTL_DSP_SPEED => 0xc0045002;
    use constant SNDCTL_DSP_SETFMT => 0xc0045005;
    use constant SNDCTL_DSP_SYNC => 0x5001;
    use constant SNDCTL_DSP_RESET => 0x5000;
    use constant AFMT_S16_LE => 0x10;
    my $spd = pack "L", $SPS;
    my $fmt = pack "L", AFMT_S16_LE;
    open DSP, "<$dspdev" or die "can't open $dspdev: $!\n";
    ioctl DSP, SNDCTL_DSP_SYNC, 0 or die $!;
    ioctl DSP, SNDCTL_DSP_RESET, 0 or die $!;
    ioctl DSP, SNDCTL_DSP_SPEED, $spd or die $!;
    ioctl DSP, SNDCTL_DSP_SETFMT, $fmt or die $!;

    my ($rout, $rin, $wout, $win, $eout, $ein) = ("") x 6; # Shut up -w
    my $srvfd = fileno $parent_sock;
    vec($rin, $srvfd, 1) = 1;



( run in 0.494 second using v1.01-cache-2.11-cpan-98e64b0badf )