1 /*---------------------------------------------------------------------------* 2 * rec_basi.c * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 #include <stdlib.h> 21 #include <string.h> 22 #ifndef _RTT 23 #include <stdio.h> 24 #endif 25 26 #ifdef unix 27 #include <unistd.h> 28 #endif 29 #include <assert.h> 30 31 32 #include "simapi.h" 33 #include "portable.h" 34 35 static const char rec_basi[] = "$Id: rec_basi.c,v 1.13.6.7 2007/10/15 18:06:24 dahan Exp $"; 36 37 /*chopped - chopped a lot of stuff out of this file - all the references to the 38 stuff within the rec structure. It seems like this should not be at this level. If 39 we leave it below here, we can change the search (with a new rec type) without 40 having to change this level*/ 41 42 CA_Recog *CA_AllocateRecognition() 43 { 44 CA_Recog *hRecog = NULL; 45 46 #ifdef SREC_ENGINE_VERBOSE_LOGGING 47 PLogMessage("in CA_AllocateRecognition\n"); 48 #endif 49 50 TRY_CA_EXCEPT 51 52 53 /* CREC_SET_SIGNATURE must be 'tied' to the 54 * actual creation of the recog_info structure. 55 * Any methods which take 'recog_info' as an argument, 56 * even 'destroy_recognition()' will test the signature! 57 */ 58 hRecog = (CA_Recog *) CALLOC_CLR(1, sizeof(CA_Recog), "ca.hRecog"); 59 60 hRecog->setup_count = 0; 61 hRecog->is_running = False; 62 hRecog->is_configured = False; 63 hRecog->is_resultBlocked = False; 64 hRecog->ca_rtti = CA_RECOGNIZER_SIGNATURE; 65 66 hRecog->recm = (multi_srec*)CALLOC_CLR(1, sizeof(multi_srec), "ca.hRecog.srec"); 67 return (hRecog); 68 69 BEG_CATCH_CA_EXCEPT 70 END_CATCH_CA_EXCEPT(hRecog) 71 } 72 73 int CA_ConfigureRecognition(CA_Recog *hRecog, CA_RecInputParams *hRecInput) 74 { 75 int rc = 0; 76 TRY_CA_EXCEPT 77 78 if (hRecog->is_configured == True) 79 SERVICE_ERROR(RECOGNIZER_ALREADY_CONFIGURED); 80 81 rc = allocate_recognition(hRecog->recm, 82 hRecInput->viterbi_prune_thresh, 83 hRecInput->max_hmm_tokens, 84 hRecInput->max_fsmnode_tokens, 85 hRecInput->max_word_tokens, 86 hRecInput->max_altword_tokens, 87 hRecInput->num_wordends_per_frame, 88 hRecInput->max_fsm_nodes, 89 hRecInput->max_fsm_arcs, 90 hRecInput->max_frames, 91 hRecInput->max_model_states, 92 hRecInput->max_searches); 93 if (rc) return rc; 94 95 /*rc =*/ 96 srec_eosd_allocate(&hRecog->eosd_parms, 97 hRecInput->eou_threshold, 98 hRecInput->eou_threshold, 99 hRecInput->terminal_timeout, 100 hRecInput->optional_terminal_timeout, 101 hRecInput->non_terminal_timeout, 102 hRecInput->max_frames); 103 if (rc) return rc; 104 105 hRecog->is_configured = True; 106 107 BEG_CATCH_CA_EXCEPT 108 END_CATCH_CA_EXCEPT(hRecog) 109 return 0; 110 } 111 112 void CA_UnconfigureRecognition(CA_Recog *hRecog) 113 { 114 TRY_CA_EXCEPT 115 116 if (hRecog->is_configured == False) 117 SERVICE_ERROR(RECOGNIZER_NOT_CONFIGURED); 118 119 if (hRecog->is_running == True) 120 SERVICE_ERROR(RECOGNIZER_ALREADY_STARTED); 121 122 srec_eosd_destroy(hRecog->eosd_parms); 123 free_recognition(hRecog->recm); 124 hRecog->is_configured = False; 125 126 BEG_CATCH_CA_EXCEPT 127 END_CATCH_CA_EXCEPT(hRecog) 128 } 129 130 131 132 void CA_FreeRecognition(CA_Recog *hRecog) 133 { 134 TRY_CA_EXCEPT 135 ASSERT(hRecog); 136 137 if (hRecog->is_configured == True) 138 SERVICE_ERROR(RECOGNIZER_ALREADY_CONFIGURED); 139 140 /* CREC_CLEAR_SIGNATURE must be 'tied' to the 141 * actual destruction of the recog_info structure. 142 * Any methods which take 'recog_info' as an argument, 143 * even 'destroy_recognition()' will test the signature! 144 */ 145 FREE(hRecog->recm); 146 FREE(hRecog); 147 148 return; 149 150 BEG_CATCH_CA_EXCEPT 151 END_CATCH_CA_EXCEPT(hRecog) 152 } 153 154 155 void CA_BeginRecognition(CA_Recog *hRecog, CA_Syntax *hSyntax, 156 int first_syntax_node, CA_RecInputParams *hRecInput) 157 { 158 TRY_CA_EXCEPT 159 /*ASSERT (hSyntax);*/ 160 ASSERT(hRecog); 161 ASSERT(hRecInput); 162 if (hRecog->is_running == True) 163 SERVICE_ERROR(RECOGNIZER_ALREADY_STARTED); 164 165 if (hRecog->is_configured == False) 166 SERVICE_ERROR(RECOGNIZER_NOT_CONFIGURED); 167 168 begin_recognition(hRecog->recm, first_syntax_node); 169 hRecog->is_running = True; 170 171 return; 172 173 BEG_CATCH_CA_EXCEPT 174 END_CATCH_CA_EXCEPT(hRecog) 175 } 176 177 178 void CA_AdvanceRecognitionByFrame(CA_Recog *hRecog, CA_Pattern *hPattern, 179 CA_Utterance *hUtterance) 180 { 181 int rc; 182 TRY_CA_EXCEPT 183 ASSERT(hRecog); 184 ASSERT(hPattern); 185 ASSERT(hUtterance); 186 if (hRecog->is_running == False) 187 SERVICE_ERROR(RECOGNIZER_NOT_STARTED); 188 if (hPattern->is_loaded == False) 189 SERVICE_ERROR(PATTERN_NOT_LOADED); 190 //if (0 && hPattern->setup_sub == NULL && hPattern->setup_whole == NULL) 191 //SERVICE_ERROR(PATTERN_NOT_SETUP); 192 193 rc = multi_srec_viterbi(hRecog->recm, 194 hRecog->eosd_parms, 195 &hPattern->data, 196 &hUtterance->data); 197 return; 198 199 BEG_CATCH_CA_EXCEPT 200 END_CATCH_CA_EXCEPT(hRecog) 201 } 202 203 204 int CA_EndRecognition(CA_Recog *hRecog, CA_Pattern *hPattern, 205 CA_Utterance *hUtterance) 206 { 207 TRY_CA_EXCEPT 208 int terminated; 209 210 ASSERT(hRecog); 211 ASSERT(hPattern); 212 ASSERT(hUtterance); 213 if (hRecog->is_running == False) 214 SERVICE_ERROR(RECOGNIZER_NOT_STARTED); 215 if (hPattern->is_loaded == False) 216 SERVICE_ERROR(PATTERN_NOT_LOADED); 217 //if (0 && hPattern->setup_sub == NULL && hPattern->setup_whole == NULL) 218 //SERVICE_ERROR(PATTERN_NOT_SETUP); 219 220 terminated = 1; 221 end_recognition(hRecog->recm); 222 223 if (terminated && hUtterance->data.gen_utt.do_channorm) 224 { 225 if (!inherit_recognition_statistics(&hUtterance->data, hRecog->recm, 226 hUtterance->data.gen_utt.channorm->dim)) 227 SERVICE_ERROR(UNEXPECTED_DATA_ERROR); /* TODO: find a suitable error code */ 228 } 229 230 if (terminated) hPattern->recog_terminated = True; 231 hRecog->is_running = False; 232 return (terminated); 233 234 BEG_CATCH_CA_EXCEPT 235 END_CATCH_CA_EXCEPT(hRecog) 236 } 237