00001
00002 #ifndef _CRT_SECURE_NO_WARNINGS
00003 #define _CRT_SECURE_NO_WARNINGS
00004 #endif
00005
00006 #include "..\stdafx.h"
00007
00008 #include "OpenNiWin.h"
00009 #include "NiJointsTool.h"
00010
00011 #include "Graph.h"
00012 #include "WinTools.h"
00013
00014
00015 using namespace jbxl;
00016
00017 #if defined(ENABLE_OPENNI) || defined(ENABLE_OPENNI2)
00018
00019 using namespace jbxwl;
00020
00021
00022 COpenNiWin::COpenNiWin(NiSDK_Lib lib)
00023 {
00024 m_err_mesg = _T("");
00025
00026 audio = NULL;
00027 speech = NULL;
00028
00029 m_library = lib;
00030 m_image_scale = 2;
00031 m_depth_scale = 2;
00032 m_skeleton_line = 2;
00033
00034 m_is_tracking = FALSE;
00035 m_is_detected = FALSE;
00036 m_is_mirroring = TRUE;
00037
00038 m_use_image = TRUE;
00039 m_use_led = FALSE;
00040 m_use_motor = FALSE;
00041 m_use_face = FALSE;
00042 m_use_speech = FALSE;
00043
00044
00045 m_enable_face = FALSE;
00046 m_enable_speech = FALSE;
00047 m_enable_motor = TRUE;
00048
00049 m_use_nite_smth = TRUE;
00050 m_nite_smooth = 0.0;
00051 m_force_pose = FALSE;
00052
00053 m_confidence = 0.75;
00054 m_profile = XN_SKEL_PROFILE_ALL;
00055 m_ground_level = NI_DEFAULT_GROUND_LEVEL;
00056
00057 pViewData = NULL;
00058 pDepthData = NULL;
00059
00060 hasDepthData = FALSE;
00061 hasUserData = FALSE;
00062 isDetectShadow = FALSE;
00063 isDetectFace = FALSE;
00064
00065 clearAvatarDetected();
00066 }
00067
00068
00069
00070 BOOL COpenNiWin::init(void)
00071 {
00072 BOOL ret = COpenNiTool::init(m_use_image);
00073
00074 return ret;
00075 }
00076
00077
00078
00079 void COpenNiWin::free(void)
00080 {
00081 freeRingBuffer();
00082
00083 return;
00084 }
00085
00086
00087
00088 CString COpenNiWin::get_err_message(void)
00089 {
00090 CString mesg = mbs2ts((char*)COpenNiTool::m_err_mesg.buf);
00091
00092 if (mesg.IsEmpty()) {
00093 mesg = m_err_mesg;
00094 m_err_mesg = _T("");
00095 }
00096
00097 return mesg;
00098 }
00099
00100
00101
00102 void COpenNiWin::clearJointsData(void)
00103 {
00104 for (int i=0; i<OPENNI_JOINT_NUM; i++) {
00105 rotQuat[i].init(-1.0);
00106 posVect[i].init(-1.0);
00107 crdVect[i].init(-1.0);
00108 jntAngl[i] = 0.0;
00109 }
00110
00111 clear_JointsData();
00112 }
00113
00114
00115
00116 BOOL COpenNiWin::initRingBuffer(void)
00117 {
00118 BOOL ret = TRUE;
00119
00120 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00121 posRing[j].init(NI_RING_BUFFER_SIZE, sizeof(Vector<float>));
00122 rotRing[j].init(NI_RING_BUFFER_SIZE, sizeof(Quaternion));
00123 if (posRing[j].state<0 || rotRing[j].state<0) {
00124 freeRingBuffer();
00125 m_err_mesg = _T("COpenNiWin::initRingBuffer(): WARNING: Out of Memory.");
00126 ret = FALSE;
00127 break;
00128 }
00129 }
00130
00131 return ret;
00132 }
00133
00134
00135
00136 void COpenNiWin::freeRingBuffer(void)
00137 {
00138 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00139 if (posRing[j].enable) posRing[j].free();
00140 if (rotRing[j].enable) rotRing[j].free();
00141 }
00142 }
00143
00144
00145
00146 void COpenNiWin::clearRingBuffer(void)
00147 {
00148 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00149 if (posRing[j].enable) posRing[j].clear();
00150 if (rotRing[j].enable) rotRing[j].clear();
00151 }
00152 }
00153
00154
00155
00156 void COpenNiWin::backup2RingBuffer(void)
00157 {
00158 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00159 if (posVect[j].c>=m_confidence) posRing[j].put(&posVect[j]);
00160 if (rotQuat[j].c>=m_confidence) rotRing[j].put(&rotQuat[j]);
00161
00162
00163 }
00164 }
00165
00166
00167
00168
00170
00171
00172 void COpenNiWin::clearAvatarDetected(void)
00173 {
00174 clearJointsData();
00175 clearRingBuffer();
00176
00177 startPos = Vector<float>(0.0, 0.0, 0.0);
00178 currentPos = Vector<float>(0.0, 0.0, 0.0);
00179 m_is_detected = FALSE;
00180 m_ground_level = NI_DEFAULT_GROUND_LEVEL;
00181
00182 return;
00183 }
00184
00185
00186
00187 BOOL COpenNiWin::checkAvatarDetected(void)
00188 {
00189 if (posVect[NI_SDK_PELVIS].c>=m_confidence && crdVect[NI_SDK_PELVIS].c>0.0) {
00190 startPos = posVect[NI_SDK_PELVIS];
00191 m_is_detected = TRUE;
00192
00193
00194 if (posVect[NI_SDK_R_ANKLE].c>=m_confidence && crdVect[NI_SDK_R_ANKLE].c>0.0) {
00195 m_ground_level = posVect[NI_SDK_R_ANKLE].z - startPos.z;
00196 if (posVect[NI_SDK_L_ANKLE].c>=m_confidence && crdVect[NI_SDK_L_ANKLE].c>0.0) {
00197 m_ground_level = Min(m_ground_level, posVect[NI_SDK_L_ANKLE].z - startPos.z);
00198 }
00199 }
00200 else if (posVect[NI_SDK_L_ANKLE].c>=m_confidence && crdVect[NI_SDK_L_ANKLE].c>0.0) {
00201 m_ground_level = posVect[NI_SDK_L_ANKLE].z - startPos.z;
00202 }
00203 }
00204
00205 return m_is_detected;
00206 }
00207
00208
00209
00210 void COpenNiWin::setMirroring(BOOL mirror)
00211 {
00212
00213 setGlobalMirror(mirror);
00214 m_is_mirroring = mirror;
00215 }
00216
00217
00218
00219 void COpenNiWin::setTiltMotor(int ang)
00220 {
00221 if (m_use_motor && m_enable_motor) {
00222 m_enable_motor = FALSE;
00223 set_TiltMotor(ang);
00224 m_enable_motor = TRUE;
00225 }
00226 }
00227
00228
00229
00230 BOOL COpenNiWin::startDetection(void)
00231 {
00232 clearJointsData();
00233
00234 float smooth = 0.0;
00235 if (m_use_nite_smth) smooth = m_nite_smooth;
00236
00237
00238 clear_JointsData();
00239
00240 BOOL ret = start_Detection(m_profile, smooth);
00241 if (ret) {
00242 setLEDColor(NI_LED_BLINK_ORANGE);
00243 }
00244 else {
00245 m_err_mesg = _T("COpenNiWin::startDetection(): ERROR: Maybe NITE is not installed.");
00246 }
00247
00248 DEBUG_INFO("COpenNiWin::startDetection(): started Detection.");
00249 return ret;
00250 }
00251
00252
00253
00254 BOOL COpenNiWin::stopDetection(void)
00255 {
00256 BOOL ret = stop_Detection();
00257 setLEDColor(NI_LED_GREEN);
00258
00259 return ret;
00260 }
00261
00262
00263
00264 BOOL COpenNiWin::restartDetection(void)
00265 {
00266 BOOL ret = stopDetection();
00267 if (ret) ret = startDetection();
00268
00269 return ret;
00270 }
00271
00272
00273
00274
00275
00276 void COpenNiWin::makeDisplayImage()
00277 {
00278 int index;
00279 uByte* src = NULL;
00280 uByte* ptr;
00281
00282 if (pViewData==NULL) return;
00283
00284 if (m_use_image) src = getImageData();
00285
00286 if (src!=NULL) {
00287 for (int j=0; j<pViewData->ysize; j++) {
00288 int ls = j*m_image_scale*getXSize();
00289
00290 for(int i=0; i<pViewData->xsize; i++) {
00291 int li = i*m_image_scale;
00292
00293 if (!m_is_mirroring) {
00294 if (getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
00295 li = getXSize() - li - 1;
00296 }
00297 }
00298
00299 ptr = &(pViewData->point(i, j));
00300 index = (ls + li)*3;
00301 ptr[0] = src[index+2];
00302 ptr[1] = src[index+1];
00303 ptr[2] = src[index];
00304 ptr[3] = 0;
00305 }
00306 }
00307 }
00308
00309
00310 else {
00311 for(int j=0; j<pViewData->ysize; j++) {
00312 int ls = j*m_image_scale*getXSize();
00313
00314 for(int i=0; i<pViewData->xsize; i++) {
00315 int li = i*m_image_scale;
00316 ptr = &(pViewData->point(i, j));
00317 ptr[0] = ptr[1] = ptr[2] = 224;
00318 ptr[3] = 0;
00319 }
00320 }
00321 }
00322 }
00323
00324
00325
00326
00327
00328
00329 void COpenNiWin::makeDisplayDepth(CExView* pview)
00330 {
00331 if (pview==NULL) return;
00332 pDepthData = &(pview->viewData);
00333
00334 uWord* src = NULL;
00335 if (hasDepthData) {
00336 src = (uWord*)getDepthData();
00337 }
00338 if (src==NULL) return;
00339
00340
00341 uWord max = pview->cMax;
00342 uWord min = pview->cMin;
00343
00344 uWord* wrk = (uWord*)malloc(sizeof(uWord)*pDepthData->ysize*pDepthData->xsize);
00345
00346
00347 for (int j=0; j<pDepthData->ysize; j++) {
00348 int jj = j*pDepthData->xsize;
00349 int ls = j*m_depth_scale*getXSize();
00350
00351 for (int i=0; i<pDepthData->xsize; i++) {
00352 int li = i*m_depth_scale;
00353
00354 if (!m_is_mirroring) {
00355 if (m_library==NiSDK_OpenNI || getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
00356 li = getXSize() - li - 1;
00357 }
00358 }
00359
00360 int k = jj + i;
00361 wrk[k] = src[ls+li];
00362 if (wrk[k]>max) wrk[k] = max;
00363 else if (wrk[k]<min) wrk[k] = min;
00364 }
00365 }
00366
00367
00368 float dif = (float)(max - min);
00369 uByte* ptr = (uByte*)pDepthData->grptr;
00370
00371 if (dif!=0.0) {
00372 for (int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
00373 ptr[i] = (uByte)((max - wrk[i])/dif*255);
00374 }
00375 }
00376 else {
00377 for (int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
00378 ptr[i] = (uByte)(255 - wrk[i]/256);
00379 }
00380 }
00381 ::free(wrk);
00382
00383 return;
00384 }
00385
00386
00387
00388
00390
00391
00392 BOOL COpenNiWin::trackingJoints(BOOL use_rot_data)
00393 {
00394 BOOL ret = FALSE;
00395
00396 if (hasUserData) {
00397
00398 if (m_library==NiSDK_OpenNI) isDetectShadow = detectShadow();
00399 else isDetectShadow = TRUE;
00400
00401
00402 if (tracking_user>0) {
00403 BOOL istracking = isTracking(tracking_user);
00404 if (!istracking || !isDetectShadow) {
00405
00406 clearAvatarDetected();
00407 lostTrackingUser((int)tracking_user);
00408 if (istracking) stopTracking(tracking_user);
00409 setDenyTrackingSearch(tracking_user);
00410 tracking_user = 0;
00411 }
00412 }
00413
00414 if ((int)tracking_user==0) {
00415 m_is_detected = FALSE;
00416 tracking_user = getTrackingUser();
00417 if ((int)tracking_user>0 && (int)tracking_user<=OPENNI_USERS_NUM) {
00418
00419 clearJointsData();
00420 detectTrackingUser((int)tracking_user);
00421 }
00422 }
00423
00425
00426
00427 if ((int)tracking_user>0 && (int)tracking_user<=OPENNI_USERS_NUM) {
00428
00429 getJointsPosData(tracking_user);
00430 if (use_rot_data) getJointsRotData(tracking_user);
00431
00432 if (m_is_detected) {
00433 convertJointsData();
00434
00435 backup2RingBuffer();
00436 saveJointsData();
00437 loggingJointsData();
00438 }
00439
00440 paintShadow();
00441 if (m_is_detected) {
00442 drawSkeleton(NiGetSkeletonColor((int)tracking_user), m_skeleton_line);
00443 drawAddition(NiGetSkeletonColor((int)tracking_user), m_skeleton_line);
00444 }
00445
00446 ret = TRUE;
00447 }
00448 }
00449
00450 else {
00451 tracking_user = 0;
00452 m_is_detected = FALSE;
00453 }
00454
00455
00456 if (ret!=m_is_tracking) {
00457 m_is_tracking = ret;
00458 if (ret) setLEDColor(NI_LED_RED);
00459 else setLEDColor(NI_LED_BLINK_ORANGE);
00460 }
00461
00462 return ret;
00463 }
00464
00465
00466
00467
00468
00469 void COpenNiWin::getJointsPosData(unsigned int uid)
00470 {
00471 get_JointsPositionData(uid);
00472
00473
00474 for (int j=1; j<OPENNI_JOINT_NUM; j++) {
00475 XnVector3D pos = jointPosData[j];
00476 posVect[j].set(-pos.Z/1000., pos.X/1000., pos.Y/1000., 0.0, jointPosConfidence[j]);
00477 }
00478
00479
00480 if (m_profile==XN_SKEL_PROFILE_ALL) {
00481 posVect[NI_SDK_PELVIS] = (posVect[NI_SDK_R_HIP] + posVect[NI_SDK_L_HIP])*0.5;
00482 posVect[NI_SDK_PELVIS].c = Min(jointPosConfidence[NI_SDK_R_HIP], jointPosConfidence[NI_SDK_L_HIP]);
00483 }
00484 else if (m_profile==XN_SKEL_PROFILE_UPPER) {
00485 posVect[NI_SDK_PELVIS] = 2.0*posVect[NI_SDK_TORSO] - posVect[NI_SDK_NECK];
00486 posVect[NI_SDK_PELVIS].c = Min(jointPosConfidence[NI_SDK_TORSO], jointPosConfidence[NI_SDK_NECK]);
00487 jointPosConfidence[NI_SDK_PELVIS] = (float)posVect[NI_SDK_PELVIS].c;
00488 }
00489
00490
00491 set2DCoordinate();
00492 checkBoneLength();
00493 if (!m_is_detected) checkAvatarDetected();
00494
00495
00496 currentPos = posVect[NI_SDK_PELVIS];
00497 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00498 posVect[j] = posVect[j] - startPos;
00499 if (posVect[j].c<m_confidence || crdVect[j].c<0.0) posVect[j].c = -1.0;
00500 }
00501
00502 checkGroundLevel();
00503 }
00504
00505
00506
00507
00508
00509 void COpenNiWin::getJointsRotData(unsigned int uid)
00510 {
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 }
00535
00536
00537
00538
00540
00541
00542 BOOL COpenNiWin::detectShadow(void)
00543 {
00544
00545 unsigned short label = 0;
00546 unsigned short* lbl = NULL;
00547
00548 if (pViewData==NULL) return FALSE;;
00549
00550 if (getDevState()==NI_STATE_DETECT_EXEC && hasUserData) {
00551 lbl = (unsigned short*)getSceneData();
00552 }
00553 if (lbl==NULL) return FALSE;
00554
00555 for(int j=0; j<pViewData->ysize; j++) {
00556 int ls = j*m_image_scale*getXSize();
00557
00558 for(int i=0; i<pViewData->xsize; i++) {
00559 int li = i*m_image_scale;
00560
00561 label = lbl[ls+li];
00562 if (label>0) {
00563 if ((int)label==(int)tracking_user) return TRUE;
00564 }
00565 }
00566 }
00567
00568 return FALSE;
00569 }
00570
00571
00572
00573 void COpenNiWin::paintShadow(void)
00574 {
00575 unsigned short label = 0;
00576 unsigned short* lbl = NULL;
00577 unsigned short* ldp = NULL;
00578
00579 uByte* ptr;
00580
00581
00582
00583 if (pViewData==NULL) return;
00584
00585 if (getDevState()==NI_STATE_DETECT_EXEC && hasUserData) {
00586 lbl = (unsigned short*)getSceneData();
00587 ldp = (unsigned short*)getDepthData();
00588 }
00589 if (lbl==NULL && ldp==NULL) return;
00590
00591
00592 for(int j=0; j<pViewData->ysize; j++) {
00593 int lj = j*m_image_scale;
00594 int ls = lj*getXSize();
00595
00596 for(int i=0; i<pViewData->xsize; i++) {
00597 int li = i*m_image_scale;
00598
00599 if (!m_is_mirroring) {
00600 if (getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
00601 li = getXSize() - li - 1;
00602 }
00603 }
00604
00605 label = lbl[ls+li];
00606 if (label>0) {
00607 ptr = &(pViewData->point(i, j));
00608 NiSetUserColor(label, ptr, m_use_image);
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 }
00619 }
00620 }
00621 }
00622
00623
00624
00625 void COpenNiWin::drawSkeleton(int col, int line)
00626 {
00627 drawJointConnection(NI_SDK_HEAD, NI_SDK_NECK, col, line);
00628 drawJointConnection(NI_SDK_NECK, NI_SDK_TORSO, col, line);
00629 drawJointConnection(NI_SDK_TORSO, NI_SDK_PELVIS, col, line);
00630
00631 drawJointConnection(NI_SDK_NECK, NI_SDK_L_SHLDR, col, line);
00632 drawJointConnection(NI_SDK_NECK, NI_SDK_R_SHLDR, col, line);
00633
00634 drawJointConnection(NI_SDK_L_SHLDR, NI_SDK_L_ELBOW, col, line);
00635 drawJointConnection(NI_SDK_R_SHLDR, NI_SDK_R_ELBOW, col, line);
00636 drawJointConnection(NI_SDK_L_ELBOW, NI_SDK_L_WRIST, col, line);
00637 drawJointConnection(NI_SDK_R_ELBOW, NI_SDK_R_WRIST, col, line);
00638
00639 drawJointConnection(NI_SDK_PELVIS, NI_SDK_L_HIP, col, line);
00640 drawJointConnection(NI_SDK_PELVIS, NI_SDK_R_HIP, col, line);
00641 drawJointConnection(NI_SDK_L_HIP, NI_SDK_L_KNEE, col, line);
00642 drawJointConnection(NI_SDK_R_HIP, NI_SDK_R_KNEE, col, line);
00643 drawJointConnection(NI_SDK_L_KNEE, NI_SDK_L_ANKLE, col, line);
00644 drawJointConnection(NI_SDK_R_KNEE, NI_SDK_R_ANKLE, col, line);
00645 }
00646
00647
00648
00649 void COpenNiWin::drawJointConnection(int j1, int j2, int col, int line)
00650 {
00651 if (pViewData==NULL) return;
00652
00653 MSGraph<unsigned int> vp;
00654 vp.xs = pViewData->xsize;
00655 vp.ys = pViewData->ysize;
00656 vp.zs = 1;
00657 vp.gp = (unsigned int*)pViewData->grptr;
00658
00659 if (crdVect[j1].c>0.0 && crdVect[j2].c>0.0) {
00660 int xs = crdVect[j1].x;
00661 int xe = crdVect[j2].x;
00662
00663 if (!m_is_mirroring) {
00664 if (getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
00665 xs = vp.xs - xs - 1;
00666 xe = vp.xs - xe - 1;
00667 }
00668 }
00669
00670 MSGraph_Line(vp, xs, crdVect[j1].y, xe, crdVect[j2].y, col);
00671 for (int i=1; i<line; i++) {
00672 MSGraph_Line(vp, xs+i, crdVect[j1].y, xe+i, crdVect[j2].y, col);
00673 MSGraph_Line(vp, xs-i, crdVect[j1].y, xe-i, crdVect[j2].y, col);
00674 MSGraph_Line(vp, xs, crdVect[j1].y+i, xe, crdVect[j2].y+i, col);
00675 MSGraph_Line(vp, xs, crdVect[j1].y-i, xe, crdVect[j2].y-i, col);
00676 }
00677 }
00678 }
00679
00680
00681
00682 void COpenNiWin::set2DCoordinate(void)
00683 {
00684 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00685 crdVect[j].init(-1.0);
00686 }
00687
00688
00689 if (hasDepthGen() && pViewData!=NULL) {
00690
00691 for (int j=0; j<OPENNI_JOINT_NUM; j++) {
00692 float cnfd;
00693
00694 if (j==NI_SDK_PELVIS) {
00695
00696 if (m_profile==XN_SKEL_PROFILE_UPPER) {
00697 cnfd = (float)Min(jointPosConfidence[NI_SDK_TORSO], jointPosConfidence[NI_SDK_NECK]);
00698 }
00699 else {
00700 cnfd = (float)Min(jointPosConfidence[NI_SDK_R_HIP], jointPosConfidence[NI_SDK_L_HIP]);
00701 }
00702 }
00703 else {
00704 cnfd = (float)jointPosConfidence[j];
00705 }
00706
00707
00708 if (cnfd>=m_confidence) {
00709 XnVector3D pos;
00710
00711 if (j==NI_SDK_PELVIS) {
00712 if (m_profile==XN_SKEL_PROFILE_UPPER) {
00713 pos.X = (float)(2.0*jointPosData[NI_SDK_TORSO].X - jointPosData[NI_SDK_NECK].X);
00714 pos.Y = (float)(2.0*jointPosData[NI_SDK_TORSO].Y - jointPosData[NI_SDK_NECK].Y);
00715 pos.Z = (float)(2.0*jointPosData[NI_SDK_TORSO].Z - jointPosData[NI_SDK_NECK].Z);
00716 }
00717 else {
00718 pos.X = (float)((jointPosData[NI_SDK_R_HIP].X + jointPosData[NI_SDK_L_HIP].X)*0.5);
00719 pos.Y = (float)((jointPosData[NI_SDK_R_HIP].Y + jointPosData[NI_SDK_L_HIP].Y)*0.5);
00720 pos.Z = (float)((jointPosData[NI_SDK_R_HIP].Z + jointPosData[NI_SDK_L_HIP].Z)*0.5);
00721 }
00722 }
00723 else {
00724 pos = jointPosData[j];
00725 }
00726
00727 XnPoint3D pt[1] = {pos};
00728 convert2Projective(1, pt, pt);
00729 int xs = (int)(pt[0].X/m_image_scale);
00730 int ys = (int)(pt[0].Y/m_image_scale);
00731
00732 crdVect[j].x = xs;
00733 crdVect[j].y = ys;
00734 if (xs>0 && xs<pViewData->xsize && ys>0 && ys<pViewData->ysize) {
00735 crdVect[j].c = 1.0;
00736 }
00737 }
00738 }
00739 }
00740
00741 return;
00742 }
00743
00744
00745
00746
00748
00749
00750
00751 BOOL COpenNiWin::initSpeech(void)
00752 {
00753 m_err_mesg = _T("");
00754
00755 if (audio==NULL) audio = new COpenNiAudio(NULL);
00756
00757 BOOL ret = audio->init(NULL);
00758 if (!ret) m_err_mesg = _T("COpenNiWin::initSpeech(): ERROR: Audio Initialization Error!!");
00759
00760
00761 if (ret) {
00762 speech = makeSpeech();
00763 WAVEFORMATEX format = audio->getAudioFormat();
00764 IStream* stream = audio->getIStream();
00765 ret = speech->init(stream, &format);
00766 if (!ret) m_err_mesg = _T("COpenNiWin::initSpeech(): ERROR: Speech Initialization Error!!");
00767 }
00768
00769
00770 if (!ret) {
00771 if (speech!=NULL) {
00772 speech->free();
00773 deleteNull(speech);
00774 }
00775 deleteNull(audio);
00776 return FALSE;
00777 }
00778
00779 return TRUE;
00780 }
00781
00782
00783
00784 BOOL COpenNiWin::createSpeech(LPCTSTR lang, LPCTSTR grfile)
00785 {
00786 m_err_mesg = _T("");
00787
00788 BOOL ret = TRUE;
00789 if (speech==NULL) ret = initSpeech();
00790
00791 if (ret) {
00792 ret = speech->create(lang);
00793 if (!ret) m_err_mesg = _T("COpenNiWin::createSpeech(): ERROR: Speech Context Creation Error!! Perhaps, Language Pack is not installed.");
00794 }
00795
00796 if (ret) {
00797 ret = speech->load(grfile);
00798 if (!ret) {
00799 m_err_mesg = _T("COpenNiWin::createSpeech(): ERROR: Grammar File Loading Error!! File = ");
00800 m_err_mesg += grfile;
00801 }
00802 }
00803
00804
00805 if (!ret) return FALSE;
00806 return TRUE;
00807 }
00808
00809
00810
00811 BOOL COpenNiWin::startSpeech(float confidence)
00812 {
00813 m_err_mesg = _T("");
00814
00815 if (audio==NULL || speech==NULL) {
00816 m_err_mesg = _T("COpenNiWin::startSpeech(): ERROR: Audio or Speech Instance is/are NULL!!");
00817 return FALSE;
00818 }
00819
00820 BOOL ret = audio->startCapture();
00821 if (!ret) m_err_mesg = _T("COpenNiWin::startSpeech(): ERROR: Audio Capture Starting Error!!");
00822
00823 if (ret) {
00824 ret = speech->start(confidence);
00825 if (!ret) {
00826 audio->stopCapture();
00827 m_err_mesg = _T("COpenNiWin::startSpeech(): ERROR: Speech Platform Starting Error!!");
00828 }
00829 }
00830
00831 if (!ret) {
00832 if (m_err_mesg==_T("")) m_err_mesg = _T("COpenNiWin::startSpeech(): ERROR: Unknown Error!!");
00833 return FALSE;
00834 }
00835
00836 return TRUE;
00837 }
00838
00839
00840
00841 void COpenNiWin::stopSpeech(void)
00842 {
00843 if (speech!=NULL) {
00844 speech->stop();
00845 }
00846
00847 if (audio!=NULL) {
00848 audio->stopCapture();
00849 }
00850
00851 return;
00852 }
00853
00854
00855
00856 void COpenNiWin::deleteSpeech(BOOL rls)
00857 {
00858 DEBUG_INFO("COpenNiWin::deleteSpeech(): START");
00859
00860 if (speech!=NULL) {
00861 stopSpeech();
00862
00864 if (rls) speech->free();
00865 delete speech;
00866 speech = NULL;
00867 }
00868
00869
00870 deleteNull(audio);
00871
00872 DEBUG_INFO("COpenNiWin::deleteSpeech(): END");
00873 return;
00874 }
00875
00876
00877 #endif // defined(ENABLE_OPENNI) || defined(ENABLE_OPENNI2)