00001
00117
00118
00119
00120
00121
00122
00123
00124 #include <julius/julius.h>
00125
00126 #undef RDEBUG
00127
00128
00158 static void
00159 init_param(MFCCCalc *mfcc)
00160 {
00161 Value *para;
00162
00163 para = mfcc->para;
00164
00165
00166
00167 mfcc->param->header.samptype = F_MFCC;
00168 if (para->delta) mfcc->param->header.samptype |= F_DELTA;
00169 if (para->acc) mfcc->param->header.samptype |= F_ACCL;
00170 if (para->energy) mfcc->param->header.samptype |= F_ENERGY;
00171 if (para->c0) mfcc->param->header.samptype |= F_ZEROTH;
00172 if (para->absesup) mfcc->param->header.samptype |= F_ENERGY_SUP;
00173 if (para->cmn) mfcc->param->header.samptype |= F_CEPNORM;
00174
00175 mfcc->param->header.wshift = para->smp_period * para->frameshift;
00176 mfcc->param->header.sampsize = para->veclen * sizeof(VECT);
00177 mfcc->param->veclen = para->veclen;
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 if (mfcc->para->cmn || mfcc->para->cvn) CMN_realtime_prepare(mfcc->cmn.wrk);
00190 }
00191
00219 boolean
00220 RealTimeInit(Recog *recog)
00221 {
00222 Value *para;
00223 Jconf *jconf;
00224 RealBeam *r;
00225 MFCCCalc *mfcc;
00226
00227
00228 jconf = recog->jconf;
00229 r = &(recog->real);
00230
00231
00232
00233 r->maxframelen = MAXSPEECHLEN / recog->jconf->input.frameshift;
00234
00235
00236
00237 for(mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00238 if (mfcc->frontend.ssload_filename && mfcc->frontend.ssbuf == NULL) {
00239 if ((mfcc->frontend.ssbuf = new_SS_load_from_file(mfcc->frontend.ssload_filename, &(mfcc->frontend.sslen))) == NULL) {
00240 jlog("ERROR: failed to read \"%s\"\n", mfcc->frontend.ssload_filename);
00241 return FALSE;
00242 }
00243
00244 if (mfcc->frontend.sslen != mfcc->wrk->bflen) {
00245 jlog("ERROR: noise spectrum length not match\n");
00246 return FALSE;
00247 }
00248 mfcc->wrk->ssbuf = mfcc->frontend.ssbuf;
00249 mfcc->wrk->ssbuflen = mfcc->frontend.sslen;
00250 mfcc->wrk->ss_alpha = mfcc->frontend.ss_alpha;
00251 mfcc->wrk->ss_floor = mfcc->frontend.ss_floor;
00252 }
00253 }
00254
00255 for(mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00256
00257 para = mfcc->para;
00258
00259
00260
00261 if (para->energy && para->enormal) energy_max_init(&(mfcc->ewrk));
00262
00263
00264 if (para->delta) mfcc->db = WMP_deltabuf_new(para->baselen, para->delWin);
00265 if (para->acc) mfcc->ab = WMP_deltabuf_new(para->baselen * 2, para->accWin);
00266
00267
00268 mfcc->tmpmfcc = (VECT *)mymalloc(sizeof(VECT) * para->vecbuflen);
00269
00270
00271 if (para->cmn || para->cvn) mfcc->cmn.wrk = CMN_realtime_new(para, mfcc->cmn.map_weight);
00272
00273
00274 if (mfcc->cmn.load_filename) {
00275 if (para->cmn) {
00276 if ((mfcc->cmn.loaded = CMN_load_from_file(mfcc->cmn.wrk, mfcc->cmn.load_filename))== FALSE) {
00277 jlog("WARNING: failed to read initial cepstral mean from \"%s\", do flat start\n", mfcc->cmn.load_filename);
00278 }
00279 } else {
00280 jlog("WARNING: CMN not required on AM, file \"%s\" ignored\n", mfcc->cmn.load_filename);
00281 }
00282 }
00283
00284 }
00285
00286
00287 r->windowlen = recog->jconf->input.framesize + 1;
00288
00289
00290 r->window = mymalloc(sizeof(SP16) * r->windowlen);
00291
00292 return TRUE;
00293 }
00294
00319 void
00320 reset_mfcc(Recog *recog)
00321 {
00322 Value *para;
00323 MFCCCalc *mfcc;
00324 RealBeam *r;
00325
00326 r = &(recog->real);
00327
00328
00329
00330 for(mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00331
00332 para = mfcc->para;
00333
00334
00335
00336 if (para->energy && para->enormal) energy_max_prepare(&(mfcc->ewrk), para);
00337
00338
00339 if (para->delta) WMP_deltabuf_prepare(mfcc->db);
00340 if (para->acc) WMP_deltabuf_prepare(mfcc->ab);
00341 }
00342
00343 }
00344
00371 boolean
00372 RealTimePipeLinePrepare(Recog *recog)
00373 {
00374 RealBeam *r;
00375 PROCESS_AM *am;
00376 MFCCCalc *mfcc;
00377 #ifdef SPSEGMENT_NAIST
00378 RecogProcess *p;
00379 #endif
00380
00381 r = &(recog->real);
00382
00383
00384
00385 r->windownum = 0;
00386
00387 for(mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00388
00389
00390 if (recog->jconf->input.speech_input == SP_MFCMODULE) {
00391 if (mfc_module_set_header(mfcc, recog) == FALSE) return FALSE;
00392 } else {
00393 init_param(mfcc);
00394 }
00395
00396
00397 if (param_alloc(mfcc->param, 1, mfcc->param->veclen) == FALSE) {
00398 j_internal_error("ERROR: segmented: failed to allocate memory for rest param\n");
00399 }
00400
00401
00402 mfcc->f = 0;
00403 }
00404
00405
00406 if (recog->jconf->input.paramtype_check_flag) {
00407 for(am=recog->amlist;am;am=am->next) {
00408 if (!check_param_coherence(am->hmminfo, am->mfcc->param)) {
00409 jlog("ERROR: input parameter type does not match AM\n");
00410 return FALSE;
00411 }
00412 }
00413 }
00414
00415
00416
00417 if (recog->jconf->input.type == INPUT_WAVEFORM) {
00418 reset_mfcc(recog);
00419 }
00420
00421
00422 for(am=recog->amlist;am;am=am->next) {
00423 outprob_prepare(&(am->hmmwrk), r->maxframelen);
00424 }
00425
00426 #ifdef BACKEND_VAD
00427 if (recog->jconf->decodeopt.segment) {
00428
00429 spsegment_init(recog);
00430 }
00431 #else
00432 recog->triggered = FALSE;
00433 #endif
00434
00435 #ifdef DEBUG_VTLN_ALPHA_TEST
00436
00437 recog->speechlen = 0;
00438 #endif
00439
00440 return TRUE;
00441 }
00442
00475 boolean
00476 RealTimeMFCC(MFCCCalc *mfcc, SP16 *window, int windowlen)
00477 {
00478 int i;
00479 boolean ret;
00480 VECT *tmpmfcc;
00481 Value *para;
00482
00483 tmpmfcc = mfcc->tmpmfcc;
00484 para = mfcc->para;
00485
00486
00487
00488 for (i=0; i < windowlen; i++) {
00489 mfcc->wrk->bf[i+1] = (float) window[i];
00490 }
00491 WMP_calc(mfcc->wrk, tmpmfcc, para);
00492
00493 if (para->energy && para->enormal) {
00494
00495
00496
00497
00498
00499
00500
00501
00502 tmpmfcc[para->baselen-1] = energy_max_normalize(&(mfcc->ewrk), tmpmfcc[para->baselen-1], para);
00503 }
00504
00505 if (para->delta) {
00506
00507
00508 ret = WMP_deltabuf_proceed(mfcc->db, tmpmfcc);
00509 #ifdef RDEBUG
00510 printf("DeltaBuf: ret=%d, status=", ret);
00511 for(i=0;i<mfcc->db->len;i++) {
00512 printf("%d", mfcc->db->is_on[i]);
00513 }
00514 printf(", nextstore=%d\n", mfcc->db->store);
00515 #endif
00516
00517
00518
00519 if (! ret) {
00520 return FALSE;
00521 }
00522
00523
00524
00525 memcpy(tmpmfcc, mfcc->db->vec, sizeof(VECT) * para->baselen * 2);
00526 }
00527
00528 if (para->acc) {
00529
00530
00531
00532
00533 ret = WMP_deltabuf_proceed(mfcc->ab, tmpmfcc);
00534 #ifdef RDEBUG
00535 printf("AccelBuf: ret=%d, status=", ret);
00536 for(i=0;i<mfcc->ab->len;i++) {
00537 printf("%d", mfcc->ab->is_on[i]);
00538 }
00539 printf(", nextstore=%d\n", mfcc->ab->store);
00540 #endif
00541
00542
00543
00544 if (! ret) {
00545 return FALSE;
00546 }
00547
00548
00549
00550
00551
00552
00553 memcpy(tmpmfcc, mfcc->ab->vec, sizeof(VECT) * para->baselen * 2);
00554 memcpy(&(tmpmfcc[para->baselen*2]), &(mfcc->ab->vec[para->baselen*3]), sizeof(VECT) * para->baselen);
00555 }
00556
00557 #ifdef POWER_REJECT
00558 if (para->energy || para->c0) {
00559 mfcc->avg_power += tmpmfcc[para->baselen-1];
00560 }
00561 #endif
00562
00563 if (para->delta && (para->energy || para->c0) && para->absesup) {
00564
00565
00566 memmove(&(tmpmfcc[para->baselen-1]), &(tmpmfcc[para->baselen]), sizeof(VECT) * (para->vecbuflen - para->baselen));
00567 }
00568
00569
00570
00571
00572
00573
00574 if (para->cmn || para->cvn) CMN_realtime(mfcc->cmn.wrk, tmpmfcc);
00575
00576 return TRUE;
00577 }
00578
00579 static int
00580 proceed_one_frame(Recog *recog)
00581 {
00582 MFCCCalc *mfcc;
00583 RealBeam *r;
00584 int maxf;
00585 PROCESS_AM *am;
00586 int rewind_frame;
00587 boolean reprocess;
00588 boolean ok_p;
00589
00590 r = &(recog->real);
00591
00592
00593 ok_p = FALSE;
00594 maxf = 0;
00595 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00596 if (!mfcc->valid) continue;
00597 if (maxf < mfcc->f) maxf = mfcc->f;
00598 if (mfcc->f == 0) {
00599 ok_p = TRUE;
00600 }
00601 }
00602 if (ok_p && maxf == 0) {
00603
00604 if (recog->jconf->decodeopt.segment) {
00605 #ifdef BACKEND_VAD
00606
00607 #else
00608 if (!recog->process_segment) {
00609 callback_exec(CALLBACK_EVENT_RECOGNITION_BEGIN, recog);
00610 }
00611 callback_exec(CALLBACK_EVENT_SEGMENT_BEGIN, recog);
00612 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
00613 recog->triggered = TRUE;
00614 #endif
00615 } else {
00616 callback_exec(CALLBACK_EVENT_RECOGNITION_BEGIN, recog);
00617 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
00618 recog->triggered = TRUE;
00619 }
00620 }
00621
00622 switch (decode_proceed(recog)) {
00623 case -1:
00624 return -1;
00625 break;
00626 case 0:
00627 break;
00628 case 1:
00629
00630
00631 r->last_is_segmented = TRUE;
00632
00633
00634 return 1;
00635 }
00636 #ifdef BACKEND_VAD
00637
00638 if (recog->jconf->decodeopt.segment) {
00639 if (recog->triggered == FALSE) {
00640 if (spsegment_trigger_sync(recog)) {
00641 if (!recog->process_segment) {
00642 callback_exec(CALLBACK_EVENT_RECOGNITION_BEGIN, recog);
00643 }
00644 callback_exec(CALLBACK_EVENT_SEGMENT_BEGIN, recog);
00645 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
00646 recog->triggered = TRUE;
00647 }
00648 }
00649 }
00650 #endif
00651
00652 if (spsegment_need_restart(recog, &rewind_frame, &reprocess) == TRUE) {
00653
00654 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00655 if (!mfcc->valid) continue;
00656 mfcc->param->header.samplenum = mfcc->f + 1;
00657 mfcc->param->samplenum = mfcc->f + 1;
00658 }
00659
00660 spsegment_restart_mfccs(recog, rewind_frame, reprocess);
00661
00662 recog->adin->rehash = TRUE;
00663
00664 for(am=recog->amlist;am;am=am->next) {
00665 outprob_prepare(&(am->hmmwrk), am->mfcc->param->samplenum);
00666 }
00667 if (reprocess) {
00668
00669 while(1) {
00670 ok_p = TRUE;
00671 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00672 if (! mfcc->valid) continue;
00673 mfcc->f++;
00674 if (mfcc->f < mfcc->param->samplenum) {
00675 mfcc->valid = TRUE;
00676 ok_p = FALSE;
00677 } else {
00678 mfcc->valid = FALSE;
00679 }
00680 }
00681 if (ok_p) {
00682
00683
00684 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00685 if (! mfcc->valid) continue;
00686 mfcc->f--;
00687 }
00688 break;
00689 }
00690
00691 switch (decode_proceed(recog)) {
00692 case -1:
00693 return -1;
00694 break;
00695 case 0:
00696 break;
00697 case 1:
00698
00699 break;
00700 }
00701
00702 callback_exec(CALLBACK_EVENT_PASS1_FRAME, recog);
00703 }
00704 }
00705 }
00706
00707 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00708 if (mfcc->valid) {
00709 callback_exec(CALLBACK_EVENT_PASS1_FRAME, recog);
00710 break;
00711 }
00712 }
00713
00714 return 0;
00715 }
00716
00717
00786 int
00787 RealTimePipeLine(SP16 *Speech, int nowlen, Recog *recog)
00788 {
00789 int i, now, ret;
00790 MFCCCalc *mfcc;
00791 RealBeam *r;
00792
00793 r = &(recog->real);
00794
00795 #ifdef DEBUG_VTLN_ALPHA_TEST
00796
00797 adin_cut_callback_store_buffer(Speech, nowlen, recog);
00798 #endif
00799
00800
00801
00802
00803
00804
00805 now = 0;
00806
00807
00808
00809 r->last_is_segmented = FALSE;
00810
00811 #ifdef RDEBUG
00812 printf("got %d samples\n", nowlen);
00813 #endif
00814
00815 while (now < nowlen) {
00816
00817
00818 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00819 if (mfcc->f >= r->maxframelen) return(1);
00820 }
00821
00822
00823 for(i = min(r->windowlen - r->windownum, nowlen - now); i > 0 ; i--)
00824 r->window[r->windownum++] = (float) Speech[now++];
00825
00826
00827
00828
00829 if (r->windownum < r->windowlen) break;
00830 #ifdef RDEBUG
00831
00832
00833
00834 #endif
00835
00836 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00837 mfcc->valid = FALSE;
00838
00839
00840
00841 if ((*(recog->calc_vector))(mfcc, r->window, r->windowlen)) {
00842 #ifdef ENABLE_PLUGIN
00843
00844 plugin_exec_vector_postprocess(mfcc->tmpmfcc, mfcc->param->veclen, mfcc->f);
00845 #endif
00846
00847 mfcc->valid = TRUE;
00848
00849 if (param_alloc(mfcc->param, mfcc->f + 1, mfcc->param->veclen) == FALSE) {
00850 jlog("ERROR: failed to allocate memory for incoming MFCC vectors\n");
00851 return -1;
00852 }
00853 memcpy(mfcc->param->parvec[mfcc->f], mfcc->tmpmfcc, sizeof(VECT) * mfcc->param->veclen);
00854 #ifdef RDEBUG
00855 printf("DeltaBuf: %02d: got frame %d\n", mfcc->id, mfcc->f);
00856 #endif
00857 }
00858 }
00859
00860
00861
00862 ret = proceed_one_frame(recog);
00863
00864 if (ret == 1 && recog->jconf->decodeopt.segment) {
00865
00866
00867
00868
00869 r->rest_len = nowlen - now;
00870 if (r->rest_len > 0) {
00871
00872 if (r->rest_Speech == NULL) {
00873 r->rest_alloc_len = r->rest_len;
00874 r->rest_Speech = (SP16 *)mymalloc(sizeof(SP16)*r->rest_alloc_len);
00875 } else if (r->rest_alloc_len < r->rest_len) {
00876 r->rest_alloc_len = r->rest_len;
00877 r->rest_Speech = (SP16 *)myrealloc(r->rest_Speech, sizeof(SP16)*r->rest_alloc_len);
00878 }
00879 memcpy(r->rest_Speech, &(Speech[now]), sizeof(SP16) * r->rest_len);
00880 }
00881 }
00882 if (ret != 0) return ret;
00883
00884
00885
00886 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00887 if (!mfcc->valid) continue;
00888 mfcc->f++;
00889 }
00890
00891
00892
00893 memmove(r->window, &(r->window[recog->jconf->input.frameshift]), sizeof(SP16) * (r->windowlen - recog->jconf->input.frameshift));
00894 r->windownum -= recog->jconf->input.frameshift;
00895 }
00896
00897
00898
00899
00900
00901 return(0);
00902 }
00903
00937 int
00938 RealTimeResume(Recog *recog)
00939 {
00940 MFCCCalc *mfcc;
00941 RealBeam *r;
00942 boolean ok_p;
00943 #ifdef SPSEGMENT_NAIST
00944 RecogProcess *p;
00945 #endif
00946 PROCESS_AM *am;
00947
00948 r = &(recog->real);
00949
00950
00951
00952 if (recog->jconf->input.type == INPUT_WAVEFORM) {
00953 reset_mfcc(recog);
00954 }
00955
00956
00957 for(am=recog->amlist;am;am=am->next) {
00958 outprob_prepare(&(am->hmmwrk), r->maxframelen);
00959 }
00960
00961
00962
00963 for(mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00964 if (mfcc->param->samplenum == 0) mfcc->valid = FALSE;
00965 else mfcc->valid = TRUE;
00966 #ifdef RDEBUG
00967 printf("Resume: %02d: f=%d\n", mfcc->id, mfcc->mfcc->param->samplenum-1);
00968 #endif
00969
00970
00971 mfcc->f = 0;
00972
00973
00974 if (mfcc->para->cmn || mfcc->para->cvn) CMN_realtime_prepare(mfcc->cmn.wrk);
00975 }
00976
00977 #ifdef BACKEND_VAD
00978 if (recog->jconf->decodeopt.segment) {
00979 spsegment_init(recog);
00980 }
00981
00982 #else
00983 recog->triggered = FALSE;
00984 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00985 if (!mfcc->valid) continue;
00986 callback_exec(CALLBACK_EVENT_SEGMENT_BEGIN, recog);
00987 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
00988 recog->triggered = TRUE;
00989 break;
00990 }
00991 #endif
00992
00993
00994
00995
00996 while(1) {
00997 ok_p = TRUE;
00998 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
00999 if (! mfcc->valid) continue;
01000 if (mfcc->f < mfcc->param->samplenum) {
01001 mfcc->valid = TRUE;
01002 ok_p = FALSE;
01003 } else {
01004 mfcc->valid = FALSE;
01005 }
01006 }
01007 if (ok_p) {
01008
01009
01010 break;
01011 }
01012
01013
01014 switch (decode_proceed(recog)) {
01015 case -1:
01016 return -1;
01017 break;
01018 case 0:
01019 break;
01020 case 1:
01021
01022 r->last_is_segmented = TRUE;
01023 return 1;
01024 }
01025
01026 #ifdef BACKEND_VAD
01027
01028 if (recog->jconf->decodeopt.segment) {
01029 if (recog->triggered == FALSE) {
01030 if (spsegment_trigger_sync(recog)) {
01031 callback_exec(CALLBACK_EVENT_SEGMENT_BEGIN, recog);
01032 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
01033 recog->triggered = TRUE;
01034 }
01035 }
01036 }
01037 #endif
01038
01039
01040 callback_exec(CALLBACK_EVENT_PASS1_FRAME, recog);
01041
01042
01043
01044 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01045 if (!mfcc->valid) continue;
01046 mfcc->f++;
01047 }
01048
01049 }
01050
01051
01052 if (recog->jconf->input.type == INPUT_WAVEFORM) {
01053 memmove(r->window, &(r->window[recog->jconf->input.frameshift]), sizeof(SP16) * (r->windowlen - recog->jconf->input.frameshift));
01054 r->windownum -= recog->jconf->input.frameshift;
01055
01056
01057
01058
01059 if (r->rest_len > 0) {
01060 return(RealTimePipeLine(r->rest_Speech, r->rest_len, recog));
01061 }
01062 }
01063
01064
01065
01066 return 0;
01067
01068 }
01069
01070
01104 boolean
01105 RealTimeParam(Recog *recog)
01106 {
01107 boolean ret1, ret2;
01108 RealBeam *r;
01109 int ret;
01110 int maxf;
01111 boolean ok_p;
01112 MFCCCalc *mfcc;
01113 Value *para;
01114 #ifdef RDEBUG
01115 int i;
01116 #endif
01117
01118 r = &(recog->real);
01119
01120 if (r->last_is_segmented) {
01121
01122
01123
01124
01125
01126
01127
01128 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01129 mfcc->param->header.samplenum = mfcc->f + 1;
01130 mfcc->param->samplenum = mfcc->f + 1;
01131 }
01132 decode_end_segmented(recog);
01133
01134
01135
01136 return(TRUE);
01137 }
01138
01139 if (recog->jconf->input.type == INPUT_VECTOR) {
01140
01141 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01142 mfcc->param->header.samplenum = mfcc->f;
01143 mfcc->param->samplenum = mfcc->f;
01144 }
01145
01146 decode_end(recog);
01147 return TRUE;
01148 }
01149
01150
01151
01152 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01153 if (mfcc->para->delta || mfcc->para->acc) {
01154 mfcc->valid = TRUE;
01155 } else {
01156 mfcc->valid = FALSE;
01157 }
01158 }
01159
01160
01161 while (1) {
01162
01163
01164 ok_p = FALSE;
01165 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01166 if (mfcc->valid) {
01167 ok_p = TRUE;
01168 break;
01169 }
01170 }
01171 if (!ok_p) break;
01172
01173
01174 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01175
01176 para = mfcc->para;
01177
01178 if (! mfcc->valid) continue;
01179
01180
01181 ret1 = WMP_deltabuf_flush(mfcc->db);
01182 #ifdef RDEBUG
01183 printf("DeltaBufLast: ret=%d, status=", ret1);
01184 for(i=0;i<mfcc->db->len;i++) {
01185 printf("%d", mfcc->db->is_on[i]);
01186 }
01187 printf(", nextstore=%d\n", mfcc->db->store);
01188 #endif
01189 if (ret1) {
01190
01191 if (para->energy && para->absesup) {
01192 memcpy(mfcc->tmpmfcc, mfcc->db->vec, sizeof(VECT) * (para->baselen - 1));
01193 memcpy(&(mfcc->tmpmfcc[para->baselen-1]), &(mfcc->db->vec[para->baselen]), sizeof(VECT) * para->baselen);
01194 } else {
01195 memcpy(mfcc->tmpmfcc, mfcc->db->vec, sizeof(VECT) * para->baselen * 2);
01196 }
01197 if (para->acc) {
01198
01199 ret2 = WMP_deltabuf_proceed(mfcc->ab, mfcc->tmpmfcc);
01200 #ifdef RDEBUG
01201 printf("AccelBuf: ret=%d, status=", ret2);
01202 for(i=0;i<mfcc->ab->len;i++) {
01203 printf("%d", mfcc->ab->is_on[i]);
01204 }
01205 printf(", nextstore=%d\n", mfcc->ab->store);
01206 #endif
01207 if (ret2) {
01208
01209 memcpy(mfcc->tmpmfcc, mfcc->ab->vec, sizeof(VECT) * (para->veclen - para->baselen));
01210 memcpy(&(mfcc->tmpmfcc[para->veclen - para->baselen]), &(mfcc->ab->vec[para->veclen - para->baselen]), sizeof(VECT) * para->baselen);
01211 } else {
01212
01213
01214 continue;
01215 }
01216 }
01217
01218 } else {
01219
01220
01221 if (para->acc) {
01222
01223 ret2 = WMP_deltabuf_flush(mfcc->ab);
01224 #ifdef RDEBUG
01225 printf("AccelBuf: ret=%d, status=", ret2);
01226 for(i=0;i<mfcc->ab->len;i++) {
01227 printf("%d", mfcc->ab->is_on[i]);
01228 }
01229 printf(", nextstore=%d\n", mfcc->ab->store);
01230 #endif
01231 if (ret2) {
01232
01233 memcpy(mfcc->tmpmfcc, mfcc->ab->vec, sizeof(VECT) * (para->veclen - para->baselen));
01234 memcpy(&(mfcc->tmpmfcc[para->veclen - para->baselen]), &(mfcc->ab->vec[para->veclen - para->baselen]), sizeof(VECT) * para->baselen);
01235 } else {
01236
01237 mfcc->valid = FALSE;
01238 continue;
01239 }
01240 } else {
01241
01242 mfcc->valid = FALSE;
01243 continue;
01244 }
01245 }
01246
01247 if(para->cmn || para->cvn) CMN_realtime(mfcc->cmn.wrk, mfcc->tmpmfcc);
01248 if (param_alloc(mfcc->param, mfcc->f + 1, mfcc->param->veclen) == FALSE) {
01249 jlog("ERROR: failed to allocate memory for incoming MFCC vectors\n");
01250 return FALSE;
01251 }
01252
01253 memcpy(mfcc->param->parvec[mfcc->f], mfcc->tmpmfcc, sizeof(VECT) * mfcc->param->veclen);
01254 #ifdef ENABLE_PLUGIN
01255
01256 plugin_exec_vector_postprocess(mfcc->param->parvec[mfcc->f], mfcc->param->veclen, mfcc->f);
01257 #endif
01258 }
01259
01260
01261 ok_p = FALSE;
01262 maxf = 0;
01263 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01264 if (!mfcc->valid) continue;
01265 if (maxf < mfcc->f) maxf = mfcc->f;
01266 if (mfcc->f == 0) {
01267 ok_p = TRUE;
01268 }
01269 }
01270
01271 if (ok_p && maxf == 0) {
01272
01273 if (recog->jconf->decodeopt.segment) {
01274 #ifdef BACKEND_VAD
01275
01276 #else
01277 if (!recog->process_segment) {
01278 callback_exec(CALLBACK_EVENT_RECOGNITION_BEGIN, recog);
01279 }
01280 callback_exec(CALLBACK_EVENT_SEGMENT_BEGIN, recog);
01281 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
01282 recog->triggered = TRUE;
01283 #endif
01284 } else {
01285 callback_exec(CALLBACK_EVENT_RECOGNITION_BEGIN, recog);
01286 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
01287 recog->triggered = TRUE;
01288 }
01289 }
01290
01291
01292 ret = decode_proceed(recog);
01293 if (ret == -1) {
01294 return -1;
01295 } else if (ret == 1) {
01296
01297 break;
01298 }
01299
01300 #ifdef BACKEND_VAD
01301
01302 if (recog->jconf->decodeopt.segment) {
01303 if (recog->triggered == FALSE) {
01304 if (spsegment_trigger_sync(recog)) {
01305 if (!recog->process_segment) {
01306 callback_exec(CALLBACK_EVENT_RECOGNITION_BEGIN, recog);
01307 }
01308 callback_exec(CALLBACK_EVENT_SEGMENT_BEGIN, recog);
01309 callback_exec(CALLBACK_EVENT_PASS1_BEGIN, recog);
01310 recog->triggered = TRUE;
01311 }
01312 }
01313 }
01314 #endif
01315
01316
01317 callback_exec(CALLBACK_EVENT_PASS1_FRAME, recog);
01318
01319
01320 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01321 if (! mfcc->valid) continue;
01322 mfcc->f++;
01323 if (mfcc->f > r->maxframelen) mfcc->valid = FALSE;
01324 }
01325 }
01326
01327
01328 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01329 mfcc->param->header.samplenum = mfcc->f;
01330 mfcc->param->samplenum = mfcc->f;
01331 }
01332
01333 decode_end(recog);
01334
01335 return(TRUE);
01336 }
01337
01356 void
01357 RealTimeCMNUpdate(MFCCCalc *mfcc, Recog *recog)
01358 {
01359 boolean cmn_update_p;
01360 Value *para;
01361 Jconf *jconf;
01362 RecogProcess *r;
01363
01364 jconf = recog->jconf;
01365 para = mfcc->para;
01366
01367
01368 if(para->cmn) {
01369 if (mfcc->cmn.update) {
01370 cmn_update_p = TRUE;
01371 for(r=recog->process_list;r;r=r->next) {
01372 if (!r->live) continue;
01373 if (r->am->mfcc != mfcc) continue;
01374 if (r->result.status < 0) {
01375 cmn_update_p = FALSE;
01376 break;
01377 }
01378 }
01379 if (cmn_update_p) {
01380
01381 CMN_realtime_update(mfcc->cmn.wrk, mfcc->param);
01382 } else {
01383
01384 if (verbose_flag) {
01385 #ifdef BACKEND_VAD
01386 if (!recog->jconf->decodeopt.segment || recog->triggered) {
01387 jlog("STAT: skip CMN parameter update since last input was invalid\n");
01388 }
01389 #else
01390 jlog("STAT: skip CMN parameter update since last input was invalid\n");
01391 #endif
01392 }
01393 }
01394 }
01395
01396 if (mfcc->cmn.save_filename) {
01397 if (CMN_save_to_file(mfcc->cmn.wrk, mfcc->cmn.save_filename) == FALSE) {
01398 jlog("WARNING: failed to save CMN parameter to \"%s\"\n", mfcc->cmn.save_filename);
01399 }
01400 }
01401 }
01402 }
01403
01416 void
01417 RealTimeTerminate(Recog *recog)
01418 {
01419 MFCCCalc *mfcc;
01420
01421 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01422 mfcc->param->header.samplenum = mfcc->f;
01423 mfcc->param->samplenum = mfcc->f;
01424 }
01425
01426
01427 decode_end(recog);
01428 }
01429
01441 void
01442 realbeam_free(Recog *recog)
01443 {
01444 RealBeam *r;
01445
01446 r = &(recog->real);
01447
01448 if (recog->real.window) {
01449 free(recog->real.window);
01450 recog->real.window = NULL;
01451 }
01452 if (recog->real.rest_Speech) {
01453 free(recog->real.rest_Speech);
01454 recog->real.rest_Speech = NULL;
01455 }
01456 }
01457
01458
01459
01460
01461
01462
01463
01464
01465
01483 int
01484 mfcc_go(Recog *recog, int (*ad_check)(Recog *))
01485 {
01486 RealBeam *r;
01487 MFCCCalc *mfcc;
01488 int new_f;
01489 int ret, ret3;
01490
01491 r = &(recog->real);
01492
01493 r->last_is_segmented = FALSE;
01494
01495 while(1) {
01496
01497 ret = mfc_module_read(recog->mfcclist, &new_f);
01498
01499 if (debug2_flag) {
01500 if (recog->mfcclist->f < new_f) {
01501 jlog("%d: %d (%d)\n", recog->mfcclist->f, new_f, ret);
01502 }
01503 }
01504
01505
01506 if (ad_check != NULL) {
01507 if ((ret3 = (*(ad_check))(recog)) < 0) {
01508 if ((ret3 == -1 && mfcc->f == 0) || ret3 == -2) {
01509 return(-2);
01510 }
01511 }
01512 }
01513
01514 while(recog->mfcclist->f < new_f) {
01515
01516 recog->mfcclist->valid = TRUE;
01517
01518 #ifdef ENABLE_PLUGIN
01519
01520 plugin_exec_vector_postprocess(recog->mfcclist->param->parvec[recog->mfcclist->f], recog->mfcclist->param->veclen, mfcc->f);
01521 #endif
01522
01523
01524
01525
01526 switch(proceed_one_frame(recog)) {
01527 case -1:
01528 return -1;
01529 case 0:
01530 break;
01531 case 1:
01532 return 2;
01533 }
01534
01535
01536
01537 for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
01538 if (!mfcc->valid) continue;
01539 mfcc->f++;
01540 }
01541 }
01542
01543
01544 switch(ret) {
01545 case -1:
01546 return 0;
01547 case -2:
01548 return -1;
01549 case -3:
01550 return 1;
01551 }
01552 }
01553
01554
01555
01556
01557 return(1);
01558 }
01559
01560
01561
01562