Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to CMU Sphinx using PHP

I have been looking into Speech Recognition and ways in which this can be implemented into a website. I have found many examples on using it with Python and even one with Node.js but I want to be able to use PHP with this.

Is there any way I can access CMUSphinx on a Linux server using PHP to process my inputs?

Thanks

like image 389
JustSteveKing Avatar asked Dec 04 '25 19:12

JustSteveKing


1 Answers

Can be done but to use asterisks as the audio capture and processing engine. See http://www.voip-info.org/wiki/view/Sphinx

Example code below after your server has been configured

    function sphinx($filename='', $timeout=3000, $service_port = 1069, $address = '127.0.0.1'){

        /* if a recording has not been passed in we create one */
        if ($filename=="") {
            $filename = "/var/lib/asterisk/sounds/sphinx_".$this->request['agi_uniqueid'];
            $extension = "wav";
            $this->stream_file('beep', 3000, 5);
            $this->record_file($filename, $extension, '0',$timeout);
            $filename=$filename.'.'.$extension;
        }   

        /* Create a TCP/IP socket. */
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket < 0) {
            return false;
        }

        $result = socket_connect($socket, $address, $service_port);
        if ($result < 0) {
           return false;
        }

        //open the file and read in data
        $handle = fopen($filename, "rb");
        $data = fread($handle, filesize($filename));

        socket_write($socket, filesize($filename)."\n");
        socket_write($socket, $data);

        $response = socket_read($socket, 2048);

        socket_close($socket);

        unlink($filename);
        return $response;
   }

Another thought after looking at the website is that sphinx 4 allows web service access to the recognition processing daemon ie: run sphinx as a daemon (its java!) then you can do socket opens as above to feed a .wav into it directly basically using a modification of the code above so instead of calling the asterisks server to retrieve then record the audio you would use something else perhaps html5 etc to record the audio.

Another thing to consider is that chrome and html5 has built in speech recognition

like image 114
Dave Avatar answered Dec 07 '25 10:12

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!