int recordInit()
{
    ad = ad_open();
    if (ad == NULL) {
        printf("Error opening recording device.\n");
        return 0;
    }

    c_ad = cont_ad_init(ad, ad_read);
    if (c_ad == NULL) {
        printf("Error initializing continues ad.\n");
        return 0;
    }

    if (ad_start_rec(ad) < 0) {
        printf("Error starting recording.\n");
        return 0;
    }

    if (cont_ad_calib(c_ad) < 0) {
        printf("Error calibrating continues ad.\n");
        return 0;
    }

    ad_stop_rec(ad);

    return 1;
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
    cont_ad_t *cont;
    ad_rec_t ad;
    int16 buf[512];
    int listening;

    ad.sps = 16000;
    ad.bps = 2;

    TEST_ASSERT(infp = fopen(TESTDATADIR "/chan3.raw", "rb"));
    TEST_ASSERT(cont = cont_ad_init(&ad, file_ad_read));

    printf("Calibrating ...");
    fflush(stdout);
    if (cont_ad_calib(cont) < 0)
	    printf(" failed; file too short?\n");
    else
	    printf(" done after %ld samples\n", ftell(infp) / 2);
    rewind(infp);

    listening = FALSE;
    while (1) {
	int k = cont_ad_read(cont, buf, 512);
	/* End of file. */
	if (k < 0) {
	    if (listening) {
		printf("End of file at %.3f seconds\n",
		       (double)(cont->read_ts - k) / 16000);
	    }
	    break;
	}

	if (cont->state == CONT_AD_STATE_SIL) {
	    /* Has there been enough silence to cut the utterance? */
	    if (listening && cont->seglen > 8000) {
		printf("End of utterance at %.3f seconds\n",
		       (double)(cont->read_ts - k - cont->seglen) / 16000);
		listening = FALSE;
	    }
	}
	else {
	    if (!listening) {
		printf("Start of utterance at %.3f seconds\n",
		       (double)(cont->read_ts - k) / 16000);
		listening = TRUE;
	    }
	}
    }

    cont_ad_close(cont);
    fclose(infp);
    return 0;
}
Exemplo n.º 3
0
static gboolean
gst_sphinx_sink_start (GstBaseSink * asink)
{
  GstSphinxSink *sphinxsink = GST_SINK (asink);

  sphinxsink->ad.self = sphinxsink;
  sphinxsink->ad.sps = 16000;
  sphinxsink->ad.self = sphinxsink;
  sphinxsink->ad.bps = sizeof(int16);
  sphinxsink->ad.calibrated = FALSE;
  sphinxsink->ad.calibrate_started = FALSE;
  
  sphinxsink->cont = cont_ad_init ((ad_rec_t*)&sphinxsink->ad, gst_sphinx_sink_ad_read);
  
  return TRUE;
}
Exemplo n.º 4
0
static gboolean
gst_pocketsphinx_start (GstBaseSink * asink)
{
    GstPocketSphinx *sphinxsink = GST_POCKETSPHINX (asink);
    GstSphinxSinkAd *ad = &sphinxsink->ad;

    ad->self = sphinxsink;
    ad->sps = 16000;
    ad->self = sphinxsink;
    ad->bps = sizeof(int16);
    ad->calibrated = FALSE;
    ad->calibrate_started = FALSE;
  
    sphinxsink->cont = cont_ad_init ((ad_rec_t *)ad, gst_pocketsphinx_ad_read);
  
    return TRUE;
}
int init()
{
    cmd_ln_t *config;
    
    printf("Initializing audio device...\n");
    
    config = cmd_ln_init(NULL, ps_args(), TRUE, 
                             "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
                             "-lm", MODELDIR "/lm/en/turtle.DMP",
                             "-dict", MODELDIR "/lm/en/turtle.dic",
                             NULL);
    if(config == NULL) {
        fprintf(stderr, "Could not init sphinx config.\n");
        return 1;
    }
    
    psDecoder = ps_init(config);
    if(psDecoder == NULL) {
        fprintf(stderr, "Could not init psDecoder.\n");
        return 2;
    }
    
    audioDevice = ad_open();
    if(audioDevice == NULL) {
        fprintf(stderr, "Could not open Audio Device.\n");
        return 3;
    }
    continousAudoDevice = cont_ad_init(audioDevice, ad_read);
    if(continousAudoDevice == NULL) {
        fprintf(stderr, "Could not open Audio Device.\n");
        return 4;
    }
    
    if(ad_start_rec(audioDevice) < 0) {
        fprintf(stderr,"Failed to start recording.\n");
        return 5;
    }
    
    if(cont_ad_calib(continousAudoDevice) < 0) {
        fprintf(stderr,"Failed to calibrate voice activity detection.\n");
        return 6;
    }
    
    return 0;
}
static int openAudioDevice(inputThread_t *p_thread)
{
	int ret;
	
	// open audio device, used for recording audio data
	p_thread->audioDevice = ad_open();
    if (p_thread->audioDevice == NULL) {
		PRINT_ERR("Failed to open audio device.\n");
        return -1;
	}
	
	// init audio device as continous audio device
    p_thread->contAudioDevice = cont_ad_init(p_thread->audioDevice, ad_read);
    if (p_thread->contAudioDevice == NULL) {
		PRINT_ERR("Failed to init continuous audio device.\n");
        return -2;
	}
	
	// calibrate audio device
	ret = ad_start_rec(p_thread->audioDevice);
    if (ret < 0) {
		PRINT_ERR("Failed to start recording (%d).\n", ret);
        return ret;
	}
	
	ret = cont_ad_calib(p_thread->contAudioDevice);
    if (ret < 0) {
		PRINT_ERR("Failed to calibrate continuous audio device (%d).\n", ret);
        return ret;
	}

    ad_stop_rec(p_thread->audioDevice);
	
	pthread_barrier_wait(&p_thread->startBarrier);
	
	return 0;
}
Exemplo n.º 7
0
/*
 * Main utterance processing loop:
 *     for (;;) {
 * 	   wait for start of next utterance;
 * 	   decode utterance until silence of at least 1 sec observed;
 * 	   print utterance result;
 *     }
 */
static void
recognize_from_microphone()
{
    ad_rec_t *ad;
    int16 adbuf[4096];
    int32 k, ts, rem;
    char const *hyp;
    char const *uttid;
    cont_ad_t *cont;
    char word[256];
	char c1[256], c2[256];

	int tracking = 0;
	int halted = 0;
	int LEFT = 0;
	int RIGHT = 1;
	int MOVE_CENT = 100; //1 meter
	int numwords;

	setlinebuf(stdout);

    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");

    /* 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");


	printf("LEDON BLUE\n");
    for (;;) {
        /* Indicate listening for next utterance */
        fprintf(stderr, "READY....\n");
        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);
        fprintf(stderr, "Listening...\n");

        /* 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)
                    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);

        fprintf(stderr, "Stopped listening, please wait...\n");
        fflush(stdout);
        /* Finish decoding, obtain and print result */
        ps_end_utt(ps);
        hyp = ps_get_hyp(ps, NULL, &uttid);
        fprintf(stderr, "%s: %s\n", uttid, hyp);

        /* Exit if the first word spoken was GOODBYE */
        if (hyp) {
			numwords = sscanf(hyp, "%s %s %s", word, c1, c2);
			if(strcmp(word, "GUGGUG") == 0) {
				if(strcmp(c1, "HALT") == 0) {
					printf("LEDOFF BLUE\n");
					halted = 1;
				} else if(strcmp(c1, "RESUME") == 0) {
					printf("LEDON BLUE\n");
					halted = 0;
				}
				if(strcmp(c1, "BEGIN") == 0 || strcmp(c1, "START") == 0) {
					if(strcmp(c2, "TRACKING") == 0 && !tracking) {
						printf("START TRACKING\n");
						tracking = 1;
						halted = 0;
					}
				} else if(strcmp(c1, "STOP") == 0) {
					if(strcmp(c2, "TRACKING") == 0 && tracking) {
						printf("STOP TRACKING\n");
						tracking = 0;
					}
				}
				if(!tracking && !halted && numwords == 3) {
					if(strcmp(c1, "TURN") == 0) {
						if(strcmp(c2, "AROUND") == 0) {
							printf("TURN %d 180\n", LEFT);
						} else if(strcmp(c2, "LEFT") == 0) {
							printf("TURN %d 90\n", LEFT);
						} else if(strcmp(c2, "RIGHT") == 0) {
							printf("TURN %d 90\n", RIGHT);
						}
					} else if(strcmp(c1, "MOVE") == 0) {
						if(strcmp(c2, "FORWARD") == 0) {
							printf("MOVE 0 %d\n", MOVE_CENT);
						} else if(strcmp(c2, "BACKWARD") == 0) {
							printf("MOVE 1 %d\n", MOVE_CENT);
						}
					}
				}
			}
        }

        /* 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);
}
Exemplo n.º 8
0
/*
 * Continuous recognition from a file
 */
static void
recognize_from_file() {
    cont_ad_t *cont;
    ad_rec_t file_ad = {0};
    int16 adbuf[4096];
    const char* hyp;
    const char* uttid;
    int32 k, ts, start;

    char waveheader[44];
    if ((rawfd = fopen(cmd_ln_str_r(config, "-infile"), "rb")) == NULL) {
	E_FATAL_SYSTEM("Failed to open file '%s' for reading",
			cmd_ln_str_r(config, "-infile"));
    }

    fread(waveheader, 1, 44, rawfd);

    file_ad.sps = (int32)cmd_ln_float32_r(config, "-samprate");
    file_ad.bps = sizeof(int16);

    if ((cont = cont_ad_init(&file_ad, ad_file_read)) == NULL) {
        E_FATAL("Failed to initialize voice activity detection");
    }
    if (cont_ad_calib(cont) < 0)
        E_FATAL("Failed to calibrate voice activity detection\n");
    rewind (rawfd);

    for (;;) {

	while ((k = cont_ad_read(cont, adbuf, 4096)) == 0);

        if (k < 0) {
    	    break;
    	}

        if (ps_start_utt(ps, NULL) < 0)
            E_FATAL("ps_start_utt() failed\n");

        ps_process_raw(ps, adbuf, k, FALSE, FALSE);

        ts = cont->read_ts;
        start = ((ts - k) * 100.0) / file_ad.sps;

        for (;;) {
            if ((k = cont_ad_read(cont, adbuf, 4096)) < 0)
            	break;

            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)
                    break;
            }
            else {
                /* New speech data received; note current timestamp */
                ts = cont->read_ts;
            }


            ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        }

        ps_end_utt(ps);

        if (cmd_ln_boolean_r(config, "-time")) {
	    print_word_times(start);
	} else {
	    hyp = ps_get_hyp(ps, NULL, &uttid);
            fprintf(stderr, "%s: %s\n", uttid, hyp);
        }
        fflush(stdout);
    }

    cont_ad_close(cont);
    fclose(rawfd);
}
Exemplo n.º 9
0
void listen::recognize_from_microphone(){

    ad_rec_t *ad;
    int16 adbuf[4096];
    int32 k, ts, rem;
    char buffer[128];
    char const *hyp;
    char const *uttid;
    cont_ad_t *cont;
    state = SLEEPING;
    FILE* pipe = popen(c.getValue("[General]", "Hcidump").c_str(), "r");
    std::string bufferStr;
    std::size_t found;
    
    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");

    /* 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");

    while(!feof(pipe) || state == SLEEPING){
        fgets(buffer, 128, pipe);
        bufferStr = buffer;

        found = bufferStr.find(c.getValue("[General]", "KeyPress"));
        if(found!=std::string::npos){
            i.pauseIfPlaying();
            s.speakThis(c.getValue("[General]", "WakeUpPhrase"));
            state = ACTIVE;
            while(state != SLEEPING){
            
                /* 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);
                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(;;){
                //while(sleep(2)){
                    /* 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)
                            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);

                fflush(stdout);

                /* Resume A/D recording for next utterance */
                if (ad_start_rec(ad) < 0)
                    E_FATAL("Failed to start recording\n");

                if(hyp != NULL){
                    
                    if(hyp == c.getValue("[General]", "Sleep")){
                        state = SLEEPING;
                        s.speakThis(c.getValue("[General]", "SleepPhrase"));
                    }else{
                        if(state != SLEEPING){
                            i.parse(hyp);
                        }
                        
                        //Hack for play/pause/select
                        std::string hypStr = hyp;
                        if(hypStr == "PLAY ITEM" || hypStr == "PAUSE ITEM" || hypStr == "SELECT ITEM"){
                            state = SLEEPING;
                        }
                    }
                }
            }
        }
    }
    pclose(pipe);
    cont_ad_close(cont);
    ad_close(ad);
}
Exemplo n.º 10
0
/*
 * Main utterance processing loop:
 *     for (;;) {
 * 	   wait for start of next utterance;
 * 	   decode utterance until silence of at least 1 sec observed;
 * 	   print utterance result;
 *     }
 */
static void
recognize_from_microphone(int outfd)
{
    int16 adbuf[4096];
    int32 k, ts, rem;
    char const *hyp;
    char const *uttid;
    char word[4096];

    if ((ad = ad_open_dev(NULL,
                          (int)cmd_ln_float32_r(config, "-adcdev"))) == NULL)
        E_FATAL("Failed to 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 */
	write(outfd, "READY\n", 6);

        /* 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);
	write(outfd, "Listening\n", 10);

        /* 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)
                    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);

        /* Finish decoding, obtain and print result */
        ps_end_utt(ps);
        hyp = ps_get_hyp(ps, NULL, &uttid);
        snprintf(word, sizeof(word)-1, "%s: %s\n", uttid, hyp);
	write(outfd, word, strlen(word));

        /* 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);

    fprintf (stderr, "listen thread exiting\n");
}
Exemplo n.º 11
0
static void
utt_livepretend(void *data, utt_res_t * ur, int32 sf, int32 ef,
                char *uttid)
{
    char fullrawfn[FILENAME_LENGTH];
    char *hypstr;

    cont_ad_t *cont_ad;
    ad_rec_t bogus_ad;
    short samples[SAMPLE_BUFFER_LENGTH];
    float32 **frames;
    kb_t *kb;
    int nread, n_frames, seg_n_frames;
    int ts, listening;

    kb = (kb_t *) data;

    /*  report_utt_res(ur); */
    sprintf(fullrawfn, "%s/%s%s", rawdirfn, ur->uttfile, decoder.rawext);
    if ((rawfd = fopen(fullrawfn, "rb")) == NULL) {
        E_FATAL("Cannnot open raw file %s.\n", fullrawfn);
    }

    if (ur->lmname != NULL)
        srch_set_lm((srch_t *) kb->srch, ur->lmname);
    if (ur->regmatname != NULL)
        kb_setmllr(ur->regmatname, ur->cb2mllrname, kb);

    bogus_ad.sps = (int32) cmd_ln_float32_r(kb->kbcore->config, "-samprate");
    if ((cont_ad = cont_ad_init(&bogus_ad, ad_file_read)) == NULL) {
        E_FATAL("Failed to initialize energy-based endpointer");
    }
    listening = 0;
    ts = 0;
    seg_n_frames = 0;
    while ((nread = cont_ad_read(cont_ad, samples, SAMPLE_BUFFER_LENGTH))
	   >= 0) {
        if (nread) {
            ts = cont_ad->read_ts;
            if (!listening) {
                char uttid[FILENAME_LENGTH];
                sprintf(uttid, "%s_%.3f", ur->uttfile,
                        (double) ts / bogus_ad.sps);
                if (s3_decode_begin_utt(&decoder, uttid) != S3_DECODE_SUCCESS)
                    E_FATAL("Cannot begin utterance decoding.\n");
                listening = 1;
            }
            ptmr_start(&(st->tm));
	    fe_process_utt(fe, samples, nread, &frames, &n_frames);
	    seg_n_frames += n_frames;
	    if (frames != NULL) {
		s3_decode_process(&decoder, frames, n_frames);
		ckd_free_2d((void **)frames);
	    }
            ptmr_stop(&(st->tm));

            if (s3_decode_hypothesis(&decoder, NULL, &hypstr, NULL) ==
                S3_DECODE_SUCCESS) {
                if (decoder.phypdump) {
                    E_INFO("PARTIAL_HYP: %s\n", hypstr);
                }
            }

	    /* If the segment is too long, break it. */
	    if (seg_n_frames > 15000) {
                s3_decode_end_utt(&decoder);
                listening = 0;
	    }
        }
        else {
            if (listening && cont_ad->read_ts - ts > 8000) {    /* HACK */
                s3_decode_end_utt(&decoder);
                listening = 0;
            }
        }
    }
    fclose(rawfd);
    cont_ad_close(cont_ad);
    if (listening)
        s3_decode_end_utt(&decoder);
}
Exemplo n.º 12
0
/*
 * Main utterance processing loop:
 *     for (;;) {
 * 	   wait for start of next utterance;
 * 	   decode utterance until silence of at least 1 sec observed;
 * 	   print utterance result;
 *     }
 */
static void
utterance_loop()
{
    int16 adbuf[4096];
    int32 k, ts, rem, score;
    char const *hyp;
    char const *uttid;
    cont_ad_t *cont;
    char word[256];

    /* Initialize continuous listening module */
    if ((cont = cont_ad_init(ad, ad_read)) == NULL)
        E_FATAL("cont_ad_init failed\n");
    if (ad_start_rec(ad) < 0)
        E_FATAL("ad_start_rec failed\n");
    if (cont_ad_calib(cont) < 0)
        E_FATAL("cont_ad_calib failed\n");

    for (;;) {
        /* Indicate listening for next utterance */
        printf("READY....\n");
        fflush(stdout);
        fflush(stderr);

        /* Await data for next utterance */
        while ((k = cont_ad_read(cont, adbuf, 4096)) == 0)
            sleep_msec(100);

        if (k < 0)
            E_FATAL("cont_ad_read failed\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("ps_start_utt() failed\n");
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        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("cont_ad_read failed\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)
                    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, &score, &uttid);
        printf("%s: %s (%d)\n", uttid, hyp, score);
        fflush(stdout);

        /* Exit if the first word spoken was GOODBYE */
        if (hyp) {
            sscanf(hyp, "%s", word);
            if (strcmp(word, "goodbye") == 0)
                break;
        }

        /* Resume A/D recording for next utterance */
        if (ad_start_rec(ad) < 0)
            E_FATAL("ad_start_rec failed\n");
    }

    cont_ad_close(cont);
}
Exemplo n.º 13
0
int
main(int argc, char *argv[])
{
    cont_ad_t *cont;
    FILE *infp;
    int16 buf[512];
    int listening;
    int k, n_calib_samp;
    int16 *calib, *cptr;

    TEST_ASSERT(infp = fopen(TESTDATADIR "/chan3.raw", "rb"));
    TEST_ASSERT(cont = cont_ad_init(NULL, NULL));

    n_calib_samp = cont_ad_calib_size(cont);
    calib = ckd_malloc(n_calib_samp * 2);
    printf("Reading %d calibration samples\n", n_calib_samp);
    TEST_ASSERT(fread(calib, 2, n_calib_samp, infp) == n_calib_samp);
    printf("Calibrating...\n");
    TEST_EQUAL(0, cont_ad_calib_loop(cont, calib, n_calib_samp));
    printf("Calibrated!\n");

    listening = FALSE;
    cptr = calib;
    while (1) {
	/* Use up the calibration samples first. */
	if (n_calib_samp) {
	    k = n_calib_samp;
	    if (k > 512)
		k = 512;
	    memcpy(buf, cptr, 512 * 2);
	    cptr += k;
	    n_calib_samp -= k;
	    if (k < 512)
		k = fread(buf + k, 2, 512-k, infp);
	}
	else {
	    k = fread(buf, 2, 512, infp);
	}

	/* End of file. */
	if (k < 256) { /* FIXME: It should do something useful with fewer samples. */
	    if (listening) {
		printf("End of file at %.3f seconds\n",
		       (double)(cont->read_ts - k) / 16000);
	    }
	    break;
	}

	k = cont_ad_read(cont, buf, k);

	if (cont->state == CONT_AD_STATE_SIL) {
	    /* Has there been enough silence to cut the utterance? */
	    if (listening && cont->seglen > 8000) {
		printf("End of utterance at %.3f seconds\n",
		       (double)(cont->read_ts - k - cont->seglen) / 16000);
		listening = FALSE;
	    }
	}
	else {
	    if (!listening) {
		printf("Start of utterance at %.3f seconds\n",
		       (double)(cont->read_ts - k) / 16000);
		listening = TRUE;
	    }
	}
    }

    ckd_free(calib);
    cont_ad_close(cont);
    fclose(infp);
    return 0;
}
Exemplo n.º 14
0
static void utterance_loop(int argc,char *argv[])
{
  int16 adbuf[4096];
  int32 k, fr, ts, rem;
  char *hyp;
  cont_ad_t *cont;
  int count=0,i=1,flag;
  char word[256];
  /* Initialize continuous listening module */
  if ((cont = cont_ad_init (ad, ad_read)) == NULL)
    E_FATAL("cont_ad_init failed\n");
  if (ad_start_rec (ad) < 0)
    E_FATAL("ad_start_rec failed\n");
  if (cont_ad_calib (cont) < 0)
    E_FATAL("cont_ad_calib failed\n");
  
  for (;;)
    {
           /* Indicate listening for next utterance */
      printf ("READY....\n");
      fflush (stdout); fflush (stderr);
      
      /* Await data for next utterance */
      while ((k = cont_ad_read (cont, adbuf, 4096)) == 0)
	sleep_msec(200);
      
      if (k < 0)
	E_FATAL("cont_ad_read failed\n");
      
      /*
       * Non-zero amount of data received; start recognition of new utterance.
       * NULL argument to uttproc_begin_utt => automatic generation of utterance-id.
       */
      if (uttproc_begin_utt (NULL) < 0)
	E_FATAL("uttproc_begin_utt() failed\n");
      uttproc_rawdata (adbuf, k, 0);
      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("cont_ad_read failed\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)
	    break;
	} else {
	  /* New speech data received; note current timestamp */
	  ts = cont->read_ts;
	}
	
	/*
	 * Decode whatever data was read above.  NOTE: Non-blocking mode!!
	 * rem = #frames remaining to be decoded upon return from the function.
	 */
	rem = uttproc_rawdata (adbuf, k, 0);
	
	/* 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);
#if 0
      /* Power histogram dump (FYI) */
      cont_ad_powhist_dump (stdout, cont);
#endif
      /* Finish decoding, obtain and print result */
      uttproc_end_utt ();
      if (uttproc_result (&fr, &hyp, 1) < 0)
	E_FATAL("uttproc_result failed\n");
      
      
      /*obtaining the results*/
      sscanf (hyp, "%s", word);
      printf ("%d: %s\n", fr,word); fflush (stdout);
 	  win=wnck_screen_get_active_window(scr);
	  tmp=wnck_screen_get_windows(wnck_window_get_screen(win));
	
      i=g_list_index(tmp,win);/*the place value  of window in the list*/
      printf("<<<<<<<<<<<<<<>>>>>>>>>>>>>>i:%d,%s\n\n\n",i,wnck_window_get_name(win));
      count=0;
      while(tmp!=NULL)
	{
	  printf("%d:%s\n\n",count,wnck_window_get_name(tmp->data));
	  tmp=tmp->next;count++;
	}
            /*comparison and action for DAKKU */
      if(strcmp(word,"PADU")==0)
	g_spawn_command_line_async("totem --play",NULL);
      if(strcmp(word,"EMACS")==0)
	g_spawn_command_line_async("emacs",NULL);
      if(strcmp(word,"SAMAYAM")==0)
	g_spawn_command_line_async(DHVANISCRIPT,NULL);
      if(strcmp(word,"VALAPARATHU")==0)
	g_spawn_command_line_async("epiphany",NULL);
      if(strcmp(word,"EAZHUTHIDAM")==0)
      g_spawn_command_line_async("gedit",NULL);
      /*Minimizing current active window*/
           if (strcmp (word, "CHURUKKU") == 0)
	      wnck_window_minimize(win);
      /*Moving focus(active window)towards the left of the panel.The active window is changed to
 the next normal window on the left.effect of alt+tab key press*/
      if(strcmp(word,"ADUTHATHU")==0)
	{
	  win=wnck_screen_get_active_window(scr);
	  tmp=wnck_screen_get_windows(wnck_window_get_screen(win));
	
	  while(tmp!=NULL)/*while traces the current active window through the list*/
	    {
	      printf("tracing:current:%s\n\ntmp:%s\n\n",wnck_window_get_name(win),wnck_window_get_name(tmp->data));	   
	      if(tmp->data==win)
		{   
			  printf("BREAKED with tmp:%s\n",wnck_window_get_name(tmp->data));
		  break;
		}
	    
	      tmp=tmp->next;
	      
	    }
	  if(tmp==NULL){printf("BULL SHIT GIVE A WINDOW IN THE LIST\n\n");}//exit(1);}
	  if(tmp->next==NULL)/*shifting back to the first window by refreshing the list*/
	    tmp=wnck_screen_get_windows(wnck_window_get_screen(win));
	  else
	    tmp=tmp->next;
	  printf("cuow:%s\n\n",wnck_window_get_name(tmp->data));
	
		
	  
	  while(tmp!=NULL)
	    {
	      printf("tmp in while:%s\n\n",wnck_window_get_name(tmp->data));
	      if(wnck_window_get_window_type(tmp->data)==WNCK_WINDOW_NORMAL)
		{
		  wnck_window_activate(tmp->data,0);
		  flag=1;
		  break;
		}
	      else
		tmp=tmp->next;
	    }
	  
	  if(flag==0)
	    {
	      printf("FLAG==0 DETECTED:\n");
	      tmp=wnck_screen_get_windows(wnck_window_get_screen(win));
	      while(tmp!=NULL)
		{
		  printf("tmp in last while:%s\n",wnck_window_get_name(tmp->data));
		  if(wnck_window_get_window_type(tmp->data)==WNCK_WINDOW_NORMAL)
		    {
		      wnck_window_activate(tmp->data,0);
		      break;
		    }
		  else
		    tmp=tmp->next;
		}
	    }
	
	 
	}
      if(strcmp(word,"VALUTHAKKU")==0)
	{
	  if(wnck_window_get_window_type(win)!= WNCK_WINDOW_NORMAL)
	    {
	      if(wnck_window_get_window_type(wnck_screen_get_previously_active_window(scr))==WNCK_WINDOW_NORMAL)
		win=wnck_screen_get_previously_active_window(scr);
	    }
	      wnck_window_unminimize(win,0);
	    
	}

      while(gtk_events_pending())/*gtk probing and refreshing the win and tmp*/
	{
	  gtk_main_iteration();
	  win=wnck_screen_get_active_window(scr);
	  tmp=wnck_screen_get_windows(wnck_window_get_screen(win));
	  
	}
      
      /* Resume A/D recording for next utterance */
      if (ad_start_rec (ad) < 0)
	E_FATAL("ad_start_rec failed\n");
    }
  
  cont_ad_close (cont);
}