DWORD WINAPI process_thread(LPVOID aParam) { ad_rec_t *in_ad = 0; int16 samples[BUFSIZE]; int32 num_samples; WaitForSingleObject(startEvent, INFINITE); in_ad = ad_open_sps(cmd_ln_int32 ("-samprate")); ad_start_rec(in_ad); while (WaitForSingleObject(finishEvent, 0) == WAIT_TIMEOUT) { num_samples = ad_read(in_ad, samples, BUFSIZE); if (num_samples > 0) { if (ld_utt_proc_raw(&decoder, samples, num_samples) < 0) { printf(">>>>> ld_utt_proc_raw() returned unexpectedly.\n"); return -1; } } } ad_stop_rec(in_ad); ad_close(in_ad); if (ld_utt_end(&decoder)) { printf(">>>>> ld_utt_end() failed.\n"); return -1; } return 0; }
THREAD_START process_thread(void *aParam) { ad_rec_t *in_ad = 0; int16 samples[BUFSIZE]; int32 num_samples; cond_wait(startEvent); if ((in_ad = ad_open_sps((int) cmd_ln_float32("-samprate"))) == NULL) { printf("Failed to open audio input device\n"); exit(1); } ad_start_rec(in_ad); while (cond_wait_timed(&finishEvent, TIMEOUT) == COND_TIMEDOUT) { num_samples = ad_read(in_ad, samples, BUFSIZE); if (num_samples > 0) { /** dump the recorded audio to disk */ if (fwrite(samples, sizeof(int16), num_samples, dump) < num_samples) { printf("Error writing audio to dump file.\n"); } ld_process_raw(&decoder, samples, num_samples); } } ad_stop_rec(in_ad); ad_close(in_ad); ld_end_utt(&decoder); return 0; }
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; }
/* * Wait for the user to type a <CR> then capture input audio for approx. 2 sec * and compute a measure of its power. */ int main (int32 argc, char *argv[]) { char line[1024]; ad_rec_t *ad; int16 buf[1000]; int32 sps; sps = DEFAULT_SAMPLES_PER_SEC; if ((ad = ad_open_sps (sps)) == NULL) E_FATAL("ad_open_sps failed\n"); for (;;) { printf ("Hit <CR> to measure A/D power; Q<CR> to quit: "); fgets (line, sizeof(line), stdin); if ((line[0] == 'q') || (line[0] == 'Q')) break; ad_start_rec (ad); adpow(ad); ad_stop_rec (ad); while (ad_read (ad, buf, 1000) >= 0); /* Flush any buffered, unread data */ } ad_close (ad); return 0; }
int32 ad_close (ad_rec_t *r) { if (r->recording) ad_stop_rec (r); audioClose (); free (r); return 0; }
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; }
int32 ad_close (ad_rec_t *handle) { if (handle->dspH == NULL) return AD_ERR_NOT_OPEN; if (handle->recording) { if (ad_stop_rec (handle) < 0) return AD_ERR_GEN; } snd_pcm_close(handle->dspH); free(handle); return(0); }
int32 ad_close (ad_rec_t *r) { if (! r->opened) return AD_ERR_NOT_OPEN; if (r->recording) if (ad_stop_rec(r) < 0) return AD_ERR_WAVE; if (wavein_close (r) < 0) return AD_ERR_WAVE; return 0; }
int32 ad_close(ad_rec_t * r) { if (r->pa == NULL) return AD_ERR_NOT_OPEN; if (r->recording) { if (ad_stop_rec(r) < 0) return AD_ERR_GEN; } pa_simple_free(r->pa); free(r); return 0; }
int32 ad_close (ad_rec_t *handle) { if (handle->dspFD < 0) return AD_ERR_NOT_OPEN; if (handle->recording) { if (ad_stop_rec (handle) < 0) return AD_ERR_GEN; } close (handle->dspFD); free(handle); return(0); }
/* * Record A/D data for approximately specified number of seconds into specified file. */ int main (int32 argc, char *argv[]) { char line[1024]; ad_rec_t *ad; int16 buf[1000]; int32 len, k, sps; FILE *fp; if ((argc != 4) || (sscanf (argv[1], "%d", &sps) != 1) || (sscanf (argv[2], "%d", &len) != 1)) { E_FATAL("Usage: %s <sampling-rate> <#sec-to-record> <output-file>\n", argv[0]); } if ((fp = fopen (argv[3], "wb")) == NULL) E_FATAL("fopen(%s,wb) failed\n", argv[3]); len *= sps; /* Convert to min. #samples to record */ if ((ad = ad_open_sps (sps)) == NULL) E_FATAL("ad_open_sps(%d) failed\n", sps); printf ("Hit <CR> to start recording\n"); fgets (line, sizeof(line), stdin); ad_start_rec (ad); /* Record desired no. of samples */ while (len > 0) { /* Read A/D */ if ((k = ad_read (ad, buf, 1000)) < 0) E_FATAL("ad_read returned %d\n", k); /* Write data read, if any, to file (ad_read may return 0 samples) */ if (k > 0) { fwrite (buf, sizeof(int16), k, fp); fflush (fp); len -= k; } } ad_stop_rec (ad); ad_close (ad); fclose (fp); return 0; }
int32 ad_close (ad_rec_t *handle) { if (handle->recording) if (ad_stop_rec(handle) < 0) return AD_ERR_GEN; if (CloseAudioStream( handle->stream ) < 0) return AD_ERR_GEN; free(handle); #ifdef AD_PA_DEBUG fprintf(stderr, "A/D Closed\n"); #endif return 0; }
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; }
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; }
int main(int argc, char **argv) { live_decoder_t decoder; FILE *infile; ad_rec_t *ad; char *hypstr; int16 samples[BUFSIZE]; int32 num_samples; int32 tot_samples = 0; if (argc < 2) { printf("Usage: livedecoder2 [ARGS] INPUT_FILE\n"); return -1; } if (ld_init(&decoder, argc - ((argc - 1) % 2), argv)) { printf("ld_init() failed.\n"); return -1; } if (ld_utt_begin(&decoder, 0)) { printf("ld_utt_begin() failed.\n"); return -1; } if (argc % 2) { /* record for 5 seconds */ tot_samples = cmd_ln_int32 ("-samprate") * 5; ad = ad_open_sps(cmd_ln_int32 ("-samprate")); ad_start_rec(ad); while (tot_samples > 0) { num_samples = ad_read(ad, samples, tot_samples > BUFSIZE ? BUFSIZE : tot_samples); if (num_samples > 0) { if (ld_utt_proc_raw(&decoder, samples, num_samples) < 0) { printf("ld_utt_proc_raw() returned unexpectedly.\n"); return -1; } tot_samples -= num_samples; } } ad_stop_rec(ad); ad_close(ad); } else { infile = fopen(argv[1], "rb"); while (!feof(infile)) { num_samples = fread(samples, sizeof(int16), BUFSIZE, infile); if (num_samples > 0) { if (ld_utt_proc_raw(&decoder, samples, num_samples) < 0) { printf("ld_utt_proc_raw() returned unexpectedly.\n"); return -1; } } } fclose(infile); } if (ld_utt_end(&decoder)) { printf("ld_utt_end() failed.\n"); return -1; } ld_utt_hyps(&decoder, &hypstr, 0); printf("decoder returned:\n%s\n", hypstr); return 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); }
/* * 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); }
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); }
/* * 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; }
/* * 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); }