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