Menu

C# + pocketsphinx_continuous.exe (Code Snippet)

Help
Jorge Rosa
2015-08-04
2015-08-18
  • Jorge Rosa

    Jorge Rosa - 2015-08-04

    Hello all,

    I am using:
    1) Visual C# 2010 (Windows Forms)
    2) pocketsphinx_continuous.exe (Sending line commands trought cmd.exe console)

    I want to share with you the simplest way (I think) you can bring "pocket sphinx" to live using C#. However this is not a complete functional code yet... Since: "output = e.Data" gets all the correct data ("INFO:" included), except it never gets the "READY....", the "Listening...", neither the "words" outputs (still, we can see them all in the "cmd.exe" console).

    HELP: How can I get the FULL console output?
    (So I could parse the output)

    NOTE: Odd... But I´ve noticed that, if I close the "cmd.exe" (console), before exiting my C# application... Then the "READY...." and the "Listening..." outputs are now pasted to my application as they should be before... BUT now that is too late (since closing the console, it closes "pocketsphinx_continuos.exe" too)... :(

    /*
    ----------------------------------------------------------------------
    Pocket Sphinx on C# - Full Code Snippet
    @ Copyrights: Jorge Rosa
    Email: jorge.bigarte@gmail.com
    Website: https://sites.google.com/site/jorgerosaportfolio
    1) Using "MS Windows 7" and "Microsoft Visual C# 2010 Express" 
    2) I´ve created a "Windows Form" (windows application) with a "textBox" on it.
    3) In the same path where my "myapp.exe" is running (in my case is in "(...)myapp/bin/Debug" folder, I´ve a created a folder named "Sphinx" with "pocketsphinx_continuos.exe", "pocketsphinx.dll", "sphinxbase.dll", "en-us.lm.dmp", "cmudict-en-us.dict" and all related files, inside it.
    ----------------------------------------------------------------------
    */
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;
    
    namespace myApp{
    
    public partial class Form1 : Form{
    
    public Form1(){
    InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e){        
    /* Start Sphinx: */
    RunPocketSphinx();
    }
    
    private void RunPocketSphinx(){
    string cmd1 = "/c pushd " + Directory.GetCurrentDirectory() + "\\Sphinx"; /* Path to... */
    string cmd2 = "&pocketsphinx_continuous.exe -inmic yes -hmm Bunch -lm en-us.lm.dmp -dict cmudict-en-us.dict"; /* Sphinx Commands */
    string line = cmd1 + cmd2;
    
    /* Create Process: */
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";            
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = false;    
    p.StartInfo.Arguments = line;            
    /* Output Handlers: */
    p.EnableRaisingEvents = true;
    p.OutputDataReceived += PocketSphinxHandler;
    p.ErrorDataReceived  += PocketSphinxHandler;            
    /* Start Process: */
    p.Start();
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();
    /* Exit process must be disabled, of course: */
    /* p.WaitForExit(); */
    }        
    
    private void PocketSphinxHandler(object sender, DataReceivedEventArgs e) {
    /* Get the output from "cmd.exe" console: */
    string output = e.Data + Environment.NewLine;
    
    /* Parsing the incoming data: */
    if (output.StartsWith("-")) { output = ""; }; /* Works great! */
    if (output.StartsWith("INFO:")) { output = ""; }; /* Works great! */
    if (output.StartsWith("READY....")) { output = "Sphinx is ready!..."; }; /* FAIL: Cant get it... */
    if (output.StartsWith("Listening...")) { output = "Sphinx is listening!..."; }; /* FAIL: Cant get it... */
    
    /* Display "cmd.exe" output in a TextBox: (Accessing texbox from another thread´s) */
    textBox1.Invoke((MethodInvoker)delegate { try { textBox1.Text = output; } catch { } });
    /* Display "cmd.exe" output in Visual C# Console: */
    Console.WriteLine(output);
    }
    
    }
    
    }
    

    ATTACHEMENT: The folders and files structure in a ".jpg" image.
    (As it is in my case, but could be helpful to start)

     

    Last edit: Jorge Rosa 2015-08-18
  • Jorge Rosa

    Jorge Rosa - 2015-08-18

    Help, please.

     
    • Nickolay V. Shmyrev

      You need to use pocketsphinx library through interop framework instead of parsing stdout of pocketsphinx_continuous.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.