Menu

Pipe and fork on Pocketsphinx

Help
Pablo
2011-05-30
2012-09-22
  • Pablo

    Pablo - 2011-05-30

    Hello!

    I try to put my hyp on a pipe (father) and manage with other program (son)
    with 2 sentences:

     switch (pid= fork())
        {
        case (pid_t) -1:
            perror("fork");
            exit(EXIT_FAILURE);
        case (pid_t) 0: // son
            close(tubo[0]);
           execve("./Data.cpp","Data",fdstr,Null);
       default: //padre
            close(tubo[1]);
            .
            .
            .
             "All code of pocketsphinx" and them this function:"
    
                if(write(tubo[1],hyp,sizeof(hyp)) == -1)
                perror("write");
    
        }
    

    In my DATA.cpp program:

    int main(int argc, char *argv[])
    {
        int fd, nread;
        char s[100];
    
        fd=atoi(argv[1]);
        printf("Lectura de descriptor: %d \n",fd);
    
        switch(nread = read(fd,s,sizeof(s)))
        {
        case -1:
            perror("read");
            break;
        case 0:
            perror("EOF");
            break;
        default:
            printf("Leídos : %d bytes : %s \n",nread,s);
        }
        exit(EXIT_SUCCESS);
    }
    

    Well, when i try to compile, qtcreator crash and i must force quit :(
    Qtcreator tell me that i have more than 600 warnings and 20-30 error before
    crash.

    When i try to compile without pipes and fork, the program works without
    warnings or errors.

    Can i use pipes with pocketsphinx? if do you want, i put my 2 programs

    int main(int argc, char *argv[]) {
    
        static ps_decoder_t *ps;
        static cmd_ln_t *config;
    
        config = cmd_ln_init(NULL, ps_args(), TRUE,
                             "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
                             "-lm", MODELDIR "/lm/en/turtle.DMP",
                             "-jsgf","/home/pablo/pocket_sphinx/grammars/newgrammar.jsgf",
             //              "-dict", MODELDIR "/lm/en/turtle.dic",
                             NULL);
    
        ps = ps_init(config);
        if (ps == NULL)
            return 1;
        ad_rec_t *ad;
        int16 adbuf[4096];
        int32 k, ts, rem;
        char const *hyp;
        char const *uttid;
        cont_ad_t *cont;
        char word[256];
        int pid, tubo[2];
        char fdstr[10]; //char que utilizamos para pasarle la id de la tubería al
                        //programa lecturaDatos.cpp
    
    
        pipe(tubo);
        switch (fork())
        {
        case (pid_t) -1: //error
            perror("fork");
            exit(EXIT_FAILURE);
    
        case (pid_t) 0: //hijo que ejecuta el programa.
            close (tubo[1]); //el hijo no va a escribir, solo va a leer.
            sprintf(fdstr,"%d",pfd[0]); //convertimos la id de la tuberia (lectura de datos)
                                        //a tipo char para poder pasarlo como argumento
                                        //luego en lecturaDatos.cpp lo pasamos a entero.
    
            execve("./DATA.cpp","DATA",fdstr,Null);
    
    
        default: //el padre se dedica a escribir cada hyp reconocida en la tuberia de escritura.
            close (tubo[0]); //tengo que cerrar el tubo de lectura porque el padre sólo escribe.
            if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),(int)cmd_ln_float32_r(config, "-samprate"))) == NULL)
                E_FATAL("Failed top open audio device\n");
    
            /* Initialize continuous listening module */
            if ((cont = cont_ad_init(ad, ad_read)) == NULL)
                E_FATAL("Failed to initialize voice activity detection\n");
            if (ad_start_rec(ad) < 0)
                E_FATAL("Failed to start recording\n");
            if (cont_ad_calib(cont) < 0)
                E_FATAL("Failed to calibrate voice activity detection\n");
    
            for (;;) {
                /* Indicate listening for next utterance */
                printf("READY....\n");
                fflush(stdout);
                fflush(stderr);
    
                /* Wait data for next utterance */
                while ((k = cont_ad_read(cont, adbuf, 4096)) == 0)
                    sleep_msec(100);
    
                if (k < 0)
                    E_FATAL("Failed to read audio\n");
    
                /*
                 * Non-zero amount of data received; start recognition of new utterance.
                 * NULL argument to uttproc_begin_utt => automatic generation of utterance-id.
                 */
                if (ps_start_utt(ps, NULL) < 0)
                    E_FATAL("Failed to start utterance\n");
                ps_process_raw(ps, adbuf, k, FALSE, FALSE); //adbuf es el buffer dnd esta el archivo raw que contiene lo que queremos decodificar.
                printf("Listening...\n");
                fflush(stdout);
    
                /* Note timestamp for this first block of data */
                ts = cont->read_ts;
    
                /* Decode utterance until end (marked by a "long" silence, >1sec) */
                for (;;) {
                    /* Read non-silence audio data, if any, from continuous listening module */
                    if ((k = cont_ad_read(cont, adbuf, 4096)) < 0)
                        E_FATAL("Failed to read audio\n");
                    if (k == 0) {
                        /*
                         * No speech data available; check current timestamp with most recent
                         * speech to see if more than 1 sec elapsed.  If so, end of utterance.
                         */
                        if ((cont->read_ts - ts) > DEFAULT_SAMPLES_PER_SEC) // samples per sec = 16000
                            break;
                    }
                    else {
                        /* New speech data received; note current timestamp */
                        ts = cont->read_ts;
                    }
    
                    /*
                     * Decode whatever data was read above.
                     */
                    rem = ps_process_raw(ps, adbuf, k, FALSE, FALSE);
    
                    /* If no work to be done, sleep a bit */
                    if ((rem == 0) && (k == 0))
                        sleep_msec(20);
                }
    
                /*
                 * Utterance ended; flush any accumulated, unprocessed A/D data and stop
                 * listening until current utterance completely decoded
                 */
                ad_stop_rec(ad);
                while (ad_read(ad, adbuf, 4096) >= 0);
                cont_ad_reset(cont);
    
                printf("Stopped listening, please wait...\n");
                fflush(stdout);
                /* Finish decoding, obtain and print result */
                ps_end_utt(ps);
                hyp = ps_get_hyp(ps, NULL, &uttid);
                printf("Decodificacion nº %s: %s\n", uttid, hyp);
                fflush(stdout);
    
                /* Escribimos en la tuberia lo que contenga hyp */
                if(write(tubo[1],hyp,sizeof(hyp)) == -1)
                perror("write");
    
                /* Exit if the first word spoken was GOODBYE */
                if (hyp) {
                    sscanf(hyp, "%s", word);
                    if (strcmp(word, "what") == 0)
                        break;
                }
    
                /* Resume A/D recording for next utterance */
                if (ad_start_rec(ad) < 0)
                    E_FATAL("Failed to start recording\n");
            }
    
            cont_ad_close(cont);
            ad_close(ad);
            ps_free(ps);
            close(tubo[1]); //cierro la tuberia de escritura
            return 0;
    
    }
    

    And DATA.cpp is upper

     
  • Nickolay V. Shmyrev

    Hello.

    What you wrote here is more or less good exept you are trying to execute CPP
    file:

    execve("./DATA.cpp","DATA",fdstr,Null);

    Which will not work for sure. Also note that Null must be NULL. There is no
    issue to use pocketsphinx with fork/execve, you just need to write your
    program properly.

    As for warnings, you might forgot some braces around code blocks and compiler
    told you about that.

     
  • Pablo

    Pablo - 2011-05-31

    hi!

    I try to run the program without pocketsphinx (only fork and pipes) and run
    propperly in both programs (father and son) and they can read and write good
    in the pipe. but when I replace the source code of the father with all code
    that you can see upper, the program crash :(

    I keep looking any mistake of the braces or semicolons.

    Thank you so much

     

Log in to post a comment.