Menu

Error: Failed to read audio(pocketsphinx + espeak)

Help
rezaee
2018-05-03
2018-05-14
  • rezaee

    rezaee - 2018-05-03

    I am trying to pass pocketsphinx's recognized hyp to espeak TTS engine by this simple code:

    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    #include <sphinxbase/err.h>
    #include <sphinxbase/ad.h>
    #include "pocketsphinx.h"
    #include <espeak/speak_lib.h>
    #include <string>
    #include <iostream>
    
    static ps_decoder_t *ps;
    static cmd_ln_t *config;
    static FILE *rawfd;
    ad_rec_t *ad;
    
    static void recognize_from_microphone()
    {
    
        int16 adbuf[2048];
        uint8 utt_started, in_speech;
        int32 k;
        char  *hyp;
    
        if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),(int) cmd_ln_float32_r(config,"-samprate"))) == NULL)
            E_FATAL("Failed to open audio device\n");
        if (ad_start_rec(ad) < 0)
            E_FATAL("Failed to start recording\n");
    
        if (ps_start_utt(ps) < 0)
            E_FATAL("Failed to start utterance\n");
    
        utt_started = FALSE;
        E_INFO("Ready....\n");
    
        for (;;) 
        {
    
            if ((k = ad_read(ad, adbuf, 2048)) < 0)
                E_FATAL("Failed to read audio\n");
            ps_process_raw(ps, adbuf, k, FALSE, FALSE);
            in_speech = ps_get_in_speech(ps);
            if (in_speech && !utt_started) 
            {
                utt_started = TRUE;
                E_INFO("Listening...\n");
            }
            if (!in_speech && utt_started) 
            {
                ps_end_utt(ps);
                hyp = (char*)ps_get_hyp(ps, NULL );
                if (hyp != NULL) 
                {
                std::string answerr = std::string("espeak '")+hyp+"'" ;
            system(answerr.c_str());
                    printf("%s\n",hyp);
                    fflush(stdout);
    
                }
    
                if (ps_start_utt(ps) < 0)
                    E_FATAL("Failed to start utterance\n");
                utt_started = FALSE;
                E_INFO("Ready....\n");
    
            }
        }//for
        ad_close(ad);
    }
    
    int main(int argc, char *argv[])
    {
    
        config = cmd_ln_init(NULL, ps_args(), TRUE,
                     "-hmm", MODELDIR"/en-us/en-us",
                         "-lm", MODELDIR "/en-us/en-us.lm.bin",
                         "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
                         NULL);
    
        ps = ps_init(config);
        recognize_from_microphone();
        ps_free(ps);
        cmd_ln_free_r(config);
    
        return 0;
    }
    

    You can compile and test this code by this command:~~~

    g++ -std=c++11 -o myapp source.cpp pkg-config --cflags gtk+-3.0 pkg-config --libs gtk+-3.0 -lespeak -DMODELDIR=\"pkg-config --variable=modeldir pocketsphinx\" pkg-config --cflags --libs pocketsphinx sphinxbase
    ~~~

    But you can see there is a problem! The program says and repeats only the first recognized word/sentence, and can not continue recognizing and saying new words/sentences and stick in a infinite loop!

    Can anyone solve this problem? I tried many ways in weeks but couldn't!

     

    Last edit: rezaee 2018-05-14
    • Nickolay V. Shmyrev

      Sorry, posting many question on the same subject is not really productive. It is hard to track your progress and it is very unlikely you'll get any meaningful response. Try to update a single thread.

       
      • rezaee

        rezaee - 2018-05-14

        Sorry for that! I edited my question and tried to write the code more simple.
        May you test this code and try to solve the problem with ad_stop_rec(ad); or any other solutions you may know?

        I tried a lot but couldn't al all!

         

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.