コード例 #1
0
        public static trigger_adapter trigger_create(string modelDir, string dictionaryFile, bool verboseLogging)
        {
            //printf("            creating recognizer\n");

            Pointer <ps_decoder_t> ps     = PointerHelpers.NULL <ps_decoder_t>();
            Pointer <cmd_ln_t>     config = PointerHelpers.NULL <cmd_ln_t>();

            config = cmd_ln.cmd_ln_init(config, pocketsphinx.ps_args(), 1,
                                        "-hmm", modelDir,
                                        "-dict", dictionaryFile,
                                        "-verbose", "y");

            ps = pocketsphinx.ps_init(config);

            cmd_ln.cmd_ln_free_r(config);

            trigger_adapter adapter = new trigger_adapter();

            adapter.ps = ps;
            adapter.user_is_speaking = false;
            adapter.last_hyp         = PointerHelpers.Malloc <byte>(512);
            adapter.last_hyp[0]      = 0;

            return(adapter);
        }
コード例 #2
0
        public static int trigger_start_processing(trigger_adapter adapter)
        {
            //printf("            process start\n");

            Pointer <ps_decoder_t> ps = adapter.ps;

            adapter.utt_started = true;
            return(pocketsphinx.ps_start_utt(ps)); // todo use ps_start_stream?
        }
コード例 #3
0
        public static int trigger_stop_processing(trigger_adapter adapter)
        {
            //printf("            process stop\n");

            Pointer <ps_decoder_t> ps = adapter.ps;

            if (adapter.utt_started)
            {
                pocketsphinx.ps_end_utt(ps);
                adapter.utt_started = false;
                if (adapter.last_hyp.IsNonNull)
                {
                    adapter.last_hyp[0] = (byte)'\0';
                }
            }

            return(0);
        }
コード例 #4
0
        public static int trigger_reconfigure(trigger_adapter adapter, Pointer <byte> keyfile)
        {
            Pointer <ps_decoder_t> ps = adapter.ps;

            //printf("            reconfiguring %s\n", keyfile);

            if (pocketsphinx.ps_set_kws(ps, cstring.ToCString("keyword_search"), keyfile) != 0)
            {
                return(-1);
            }

            if (pocketsphinx.ps_set_search(ps, cstring.ToCString("keyword_search")) != 0)
            {
                return(-1);
            }

            return(0);
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            string          rootDir  = "C:\\Code\\Durandal";
            string          modelDir = rootDir + "\\Data\\sphinx\\en-us-semi";
            string          dictFile = rootDir + "\\Data\\sphinx\\cmudict_SPHINX_40.txt";
            trigger_adapter trigger  = psphinx_trigger.trigger_create(modelDir, dictFile, true);

            Pointer <byte> configuration1 = cstring.ToCString("ACTIVATE/3.16227766016838e-13/\nEXECUTE COURSE/3.16227766016838e-13/\n");

            psphinx_trigger.trigger_reconfigure(trigger, configuration1);

            // Read input file 1
            byte[] file_bytes = System.IO.File.ReadAllBytes(rootDir + "\\Extensions\\Pocketsphinx\\Test1.raw");
            int    samples    = file_bytes.Length / 2;

            short[]         file           = new short[samples];
            Pointer <short> input_file_ptr = new Pointer <short>(new UpcastingMemoryBlockAccess <short>(new MemoryBlock <byte>(file_bytes)), 0);

            input_file_ptr.MemCopyTo(file, 0, samples);
            input_file_ptr = new Pointer <short>(file);

            // Send it to the trigger in chunks
            Pointer <byte> hyp     = PointerHelpers.Malloc <byte>(512);
            Pointer <byte> lasthyp = PointerHelpers.Malloc <byte>(512);

            psphinx_trigger.trigger_start_processing(trigger);

            for (int cursor = 0; cursor < (samples - 159); cursor += 160)
            {
                psphinx_trigger.trigger_process_samples(trigger, input_file_ptr + cursor, 160);
                psphinx_trigger.trigger_get_last_hyp(trigger, hyp);
                if (cstring.strlen(hyp) != 0 && cstring.strcmp(hyp, lasthyp) != 0)
                {
                    Console.Write("Got trigger {0} at sample number {1}\n", cstring.FromCString(hyp), cursor);
                    cstring.strncpy(lasthyp, hyp, 512);
                }
            }

            psphinx_trigger.trigger_stop_processing(trigger);

            Console.Write("\n\nON TO TEST #2\n\n\n");

            Pointer <byte> configuration2 = cstring.ToCString("COMPUTER/3.16227766016838e-13/\n");

            psphinx_trigger.trigger_reconfigure(trigger, configuration2);

            // Read input file 2
            file_bytes     = System.IO.File.ReadAllBytes(rootDir + "\\Extensions\\Pocketsphinx\\Test2.raw");
            samples        = file_bytes.Length / 2;
            file           = new short[samples];
            input_file_ptr = new Pointer <short>(new UpcastingMemoryBlockAccess <short>(new MemoryBlock <byte>(file_bytes)), 0);
            input_file_ptr.MemCopyTo(file, 0, samples);
            input_file_ptr = new Pointer <short>(file);

            // Send it to the trigger in chunks
            hyp     = PointerHelpers.Malloc <byte>(512);
            lasthyp = PointerHelpers.Malloc <byte>(512);
            psphinx_trigger.trigger_start_processing(trigger);

            for (int cursor = 0; cursor < (samples - 159); cursor += 160)
            {
                psphinx_trigger.trigger_process_samples(trigger, input_file_ptr + cursor, 160);
                psphinx_trigger.trigger_get_last_hyp(trigger, hyp);
                if (cstring.strlen(hyp) != 0 && cstring.strcmp(hyp, lasthyp) != 0)
                {
                    Console.Write("Got trigger {0} at sample number {1}\n", cstring.FromCString(hyp), cursor);
                    cstring.strncpy(lasthyp, hyp, 512);
                }
            }

            psphinx_trigger.trigger_stop_processing(trigger);

            psphinx_trigger.trigger_free(trigger);
        }
コード例 #6
0
        public static bool trigger_process_samples(trigger_adapter adapter, Pointer <short> samples, int numSamples)
        {
            Pointer <ps_decoder_t> ps = adapter.ps;

            pocketsphinx.ps_process_raw(ps, samples, (uint)numSamples, 0, 0);
            byte in_speech = pocketsphinx.ps_get_in_speech(ps);

            if (in_speech != 0 && !adapter.user_is_speaking)
            {
                adapter.user_is_speaking = true;
            }

            bool returnVal = false;

            BoxedValueInt  score = new BoxedValueInt();
            Pointer <byte> hyp   = pocketsphinx.ps_get_hyp(ps, score);

            if (hyp.IsNonNull)
            {
                //printf("            tenative hyp %s\n", hyp);
                if (!adapter.triggered)
                {
                    returnVal         = true;
                    adapter.triggered = true;
                    uint hypsize = cstring.strlen(hyp);
                    cstring.strncpy(adapter.last_hyp, hyp, hypsize);
                    adapter.last_hyp[hypsize] = 0;
                    //printf("            adapter last hyp is %s\n", hyp);
                }
            }

            if (in_speech == 0 && adapter.user_is_speaking)
            {
                /* speech .Deref. silence transition, time to start new utterance  */
                pocketsphinx.ps_end_utt(ps);
                adapter.utt_started = false;

                hyp = pocketsphinx.ps_get_hyp(ps, score);

                if (hyp.IsNonNull)
                {
                    //printf("            final hyp %s\n", hyp);
                    if (!adapter.triggered)
                    {
                        returnVal         = true;
                        adapter.triggered = true;
                        uint hypsize = cstring.strlen(hyp);
                        cstring.strncpy(adapter.last_hyp, hyp, hypsize);
                        adapter.last_hyp[hypsize] = 0;
                        //printf("            adapter last hyp is %s\n", hyp);
                    }
                }

                if (pocketsphinx.ps_start_utt(ps) < 0)
                {
                    //printf("            failed to restart utterance\n");
                }
                adapter.utt_started = true;

                adapter.user_is_speaking = false;
                adapter.triggered        = false;
                //printf("Ready....\n");
            }

            return(returnVal);
        }
コード例 #7
0
 public static int trigger_free(trigger_adapter adapter)
 {
     pocketsphinx.ps_free(adapter.ps);
     return(0);
 }
コード例 #8
0
 public static void trigger_get_last_hyp(trigger_adapter adapter, Pointer <byte> buffer)
 {
     cstring.strncpy(buffer, adapter.last_hyp, 512);
 }