int decode()
{
    int ret;
    int16 buf[BUFFER_SIZE];
    
    printf("Listening for input...\n");
    
    if (ad_start_rec(ad) < 0) {
        printf("Error starting recording.\n");
        return 0;
    }
    
	//check if not silent
	while ((ret = cont_ad_read(c_ad, buf, BUFFER_SIZE)) == 0)
        usleep(1000);
	
    if (ps_start_utt(ps, NULL) < 0) {
        printf("Failed to start utterance.\n");
        return 0;
    }
	
	ret = ps_process_raw(ps, buf, BUFFER_SIZE, 0, 0);
    if (ret < 0) {
        printf("Error decoding.\n");
        return 0;
    }
    
    do
    {
        ret = cont_ad_read(c_ad, buf, BUFFER_SIZE);

        if (ret < 0) {
            printf("Failed to record audio.\n");
            return 0;
        } else if(ret > 0) {
            // Valid speech data read.
            ret = ps_process_raw(ps, buf, 4096, 0, 0);
            if (ret < 0) {
                printf("Error decoding.\n");
                return 0;
            }
        } else {
            //no data
            usleep(1000);
        }
    } while(getRun());
    
    ad_stop_rec(ad);
    while (ad_read(ad, buf, BUFFER_SIZE) >= 0);
    cont_ad_reset(c_ad);

    ps_end_utt(ps);
    return 1;
}
Exemple #2
0
void filter_buffer_process_data(context_t *ctx)
{
    decoder_set_t *decset;
    decoder_t *dec;
    input_buf_t *inpbuf;
    filter_buf_t *filtbuf;
    cont_ad_t *cont;
    int32_t l, max, len;

    if (!ctx || !(decset = ctx->decset) || !(dec = decset->curdec) ||
        !(inpbuf = ctx->inpbuf) || !(cont = inpbuf->cont) ||
        !(filtbuf = ctx->filtbuf))
        return;

    len = 0;
    max = filtbuf->hwm - filtbuf->len;

    for (;;) {
        if (max <= 0)
            break;

        l = cont_ad_read(cont, filtbuf->buf + filtbuf->len + len, max);

        if (l <= 0)
            break;

        len += l;
        max -= l;
    }

    if (len > 0) {
        filtbuf->len += len;
        filtbuf->ts = cont->read_ts;

        if (ctx->verbose) {
            mrp_debug("got %u samples to filter buffer "
                      "(total size %u samples)", len, filtbuf->len);
        }

        utterance_start(ctx);

        if (filtbuf->len >= filtbuf->hwm)
            filter_buffer_utter(ctx, false);
    }
    else {
        if (dec->utter && (cont->read_ts - filtbuf->ts) > filtbuf->silen) {
            filter_buffer_utter(ctx, true);
            cont_ad_reset(cont);
            utterance_end(ctx);
        }
    }
}
int processCommands()
{
    int32 samples;
    int16 audioBuf[BUFFER_SIZE];
    char const *uttid;
    char const *hyp;
    
    while(run)
    {
        printf("Waiting for utterance...\n");
        samples = waitForNextUtterance();
        if(samples < 0)
            return -1;
        
        if(ps_start_utt(psDecoder, NULL) < 0) {
            fprintf(stderr, "Failed to start next utterance\n");
            return -1;
        }
        ps_process_raw(psDecoder, audioBuf, samples, FALSE, FALSE);
        
        printf("Recording...\n");
        fflush(stdout);
        record();
        
        ad_stop_rec(audioDevice);
        while(ad_read(audioDevice, audioBuf, BUFFER_SIZE) >= 0);
        cont_ad_reset(continousAudoDevice);
        ps_end_utt(psDecoder);
        
        hyp = ps_get_hyp(psDecoder, NULL, &uttid);
        printf("Heard: %s\n", hyp);
        
        if (ad_start_rec(audioDevice) < 0) {
            fprintf(stderr, "Failed to start audio device.\n");
            return -1;
        }
    }
    
    return 0;
}
Exemple #4
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);
}
Exemple #5
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);
}
Exemple #6
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");
}
static int record(inputThread_t *p_thread)
{
    int ret = 0;
	int32 ts;
	//get audioBuffer for audioQueue
	audioBuffer_t *resultBuf = reserveAudioBuffer();
	
	ret = initAudioBuffer(resultBuf);
	if(ret != 0) {
		PRINT_ERR("Failed to init resultBuf %d).\n", ret);
		return ret;
	}
	
	//start recording from audiodevice
	p_thread->listenState = INPUT_LISTENING;
	ret = ad_start_rec(p_thread->audioDevice);
    if (ret < 0) {
		PRINT_ERR("Could not start recording audio (%d).\n", ret);
        return ret;
	}
	
	//EXEC HOLD_TIME_TAKING(inputExecutionTime);
	//check if not silent
	while (((ret = cont_ad_read(p_thread->contAudioDevice, p_thread->inputBuffer, INPUT_BUFFER_SIZE)) == 0) && p_thread->keepRunning ) 
        usleep(10000);
	//EXEC RESUME_TIME_TAKING(inputExecutionTime);
	RESTART_TIME_TAKING(totalReactionTime);
	RESTART_TIME_TAKING(inputReactionTime);
	ts = p_thread->contAudioDevice->read_ts;
	p_thread->listenState = INPUT_PROCESSING;	
	//add read audio data to audioBuffer
	addAudioBuffer(resultBuf, p_thread->inputBuffer, ret);
	
	
    while(p_thread->keepRunning) {
        ret = cont_ad_read(p_thread->contAudioDevice, p_thread->inputBuffer, INPUT_BUFFER_SIZE);

        if (ret < 0) {
			//something went wrong
            PRINT_ERR("Failed to record audio (%d).\n", ret);
            break;
        } else if(ret > 0) {
            // valid speech data read
			// get new timestamp
			ts = p_thread->contAudioDevice->read_ts;
            addAudioBuffer(resultBuf, p_thread->inputBuffer, ret);
			if(isFullAudioBuffer(resultBuf)) {
				PRINT_INFO("AudioBuffer is full!\n");
				break;
			}
        } else {
            //no data
			if(sphinxTimestampDiff(ts, p_thread->contAudioDevice->read_ts) >= MS_TO_SAMPLES(SILENCE_MS))
				break;
			else
				usleep(10000);
        }
    }
    
    ad_stop_rec(p_thread->audioDevice);
	p_thread->listenState = INPUT_WAITING;
    while (ad_read(p_thread->audioDevice, p_thread->inputBuffer, INPUT_BUFFER_SIZE) >= 0);
    cont_ad_reset(p_thread->contAudioDevice);
	
	//EXEC HOLD_TIME_TAKING(inputExecutionTime);
	RESTART_TIME_TAKING(interpreterReactionTime);
	// enqueuing can also block
	enqueueBlockingQueue(p_thread->audioQueue, (void*) resultBuf);
	//EXEC RESUME_TIME_TAKING(inputExecutionTime);
	STOP_TIME_TAKING(inputReactionTime);
	
    return ret;
}
Exemple #8
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);
}
Exemple #9
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);
}