Julius 4.2
|
00001 // SampleApp.cpp 00002 // 00003 00004 #include "stdafx.h" 00005 #include "SampleApp.h" 00006 #include "Julius.h" 00007 00008 // Use a Julius class 00009 cJulius julius; 00010 00011 #define MAX_LOADSTRING 100 00012 00013 // Global variables 00014 HINSTANCE hInst; // Current interface 00015 TCHAR szTitle[MAX_LOADSTRING]; // Text on the title bar 00016 TCHAR szWindowClass[MAX_LOADSTRING]; // Main windows class name 00017 00018 // Function prototype definitions 00019 ATOM MyRegisterClass(HINSTANCE hInstance); 00020 BOOL InitInstance(HINSTANCE, int); 00021 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 00022 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 00023 00024 int APIENTRY _tWinMain(HINSTANCE hInstance, 00025 HINSTANCE hPrevInstance, 00026 LPTSTR lpCmdLine, 00027 int nCmdShow) 00028 { 00029 UNREFERENCED_PARAMETER(hPrevInstance); 00030 UNREFERENCED_PARAMETER(lpCmdLine); 00031 00032 MSG msg; 00033 HACCEL hAccelTable; 00034 00035 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 00036 LoadString(hInstance, IDC_SAMPLEAPP, szWindowClass, MAX_LOADSTRING); 00037 MyRegisterClass(hInstance); 00038 00039 // Initialize application 00040 if (!InitInstance (hInstance, nCmdShow)) 00041 { 00042 return FALSE; 00043 } 00044 00045 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SAMPLEAPP)); 00046 00047 // Main message loop 00048 while (GetMessage(&msg, NULL, 0, 0)) 00049 { 00050 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 00051 { 00052 TranslateMessage(&msg); 00053 DispatchMessage(&msg); 00054 } 00055 } 00056 00057 return (int) msg.wParam; 00058 } 00059 00060 00061 00062 // 00063 // Function: MyRegisterClass() 00064 // 00065 // Register a window class. 00066 // 00067 ATOM MyRegisterClass(HINSTANCE hInstance) 00068 { 00069 WNDCLASSEX wcex; 00070 00071 wcex.cbSize = sizeof(WNDCLASSEX); 00072 00073 wcex.style = CS_HREDRAW | CS_VREDRAW; 00074 wcex.lpfnWndProc = WndProc; 00075 wcex.cbClsExtra = 0; 00076 wcex.cbWndExtra = 0; 00077 wcex.hInstance = hInstance; 00078 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SAMPLEAPP)); 00079 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 00080 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 00081 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SAMPLEAPP); 00082 wcex.lpszClassName = szWindowClass; 00083 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 00084 00085 return RegisterClassEx(&wcex); 00086 } 00087 00088 // 00089 // Function: InitInstance(HINSTANCE, int) 00090 // 00091 // Save instance handle and create a main window 00092 // 00093 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 00094 { 00095 HWND hWnd; 00096 00097 // CHANGE THIS TO YOUR ENVIRONMENT AND YOUR MODEL ENCODING! 00098 setlocale(LC_CTYPE, "Japanese_Japan.20932"); // Japanese EUC-JP 00099 00100 hInst = hInstance; // store instance to global 00101 00102 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 00103 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 00104 00105 if (!hWnd) 00106 { 00107 return FALSE; 00108 } 00109 00110 ShowWindow(hWnd, nCmdShow); 00111 UpdateWindow(hWnd); 00112 00113 return TRUE; 00114 } 00115 00116 // 00117 // Function: GetJconfFileName( char *, int ) 00118 // 00119 // Open file dialog to get jconf file name 00120 // 00121 #include "CommDlg.h" 00122 bool GetJconfFileName( char *buf, int buflen ) 00123 { 00124 wchar_t wszFileName[MAX_PATH]; 00125 bool ret; 00126 static OPENFILENAME ofn; 00127 00128 ZeroMemory( &wszFileName, sizeof(wchar_t) * MAX_PATH ); 00129 ZeroMemory( &ofn, sizeof(OPENFILENAME) ); 00130 ofn.lStructSize = sizeof(OPENFILENAME); 00131 ofn.hwndOwner = NULL; 00132 ofn.lpstrFilter = L"Jconf File(*.jconf)\0*.jconf\0\0"; 00133 ofn.lpstrFile = wszFileName; 00134 ofn.nMaxFile = MAX_PATH; 00135 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; 00136 ofn.lpstrDefExt = L"jconf"; 00137 ofn.lpstrTitle = L"Open Jconf"; 00138 00139 if( GetOpenFileName( &ofn ) ) { 00140 // convert wide-char to multi-byte char (limitation of JuliusLib...) 00141 size_t converted = 0; 00142 wcstombs_s(&converted, buf, buflen, wszFileName, _TRUNCATE); 00143 ret = true; 00144 } else { 00145 ret = false; 00146 } 00147 return ret; 00148 } 00149 00150 // 00151 // Function: WndProc(HWND, UINT, WPARAM, LPARAM) 00152 // 00153 // Process messages received at the main window 00154 // 00155 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 00156 { 00157 int wmId, wmEvent; 00158 PAINTSTRUCT ps; 00159 HDC hdc; 00160 int jEventId; 00161 int jResultId; 00162 static char conffile[1024]; 00163 00164 switch (message) 00165 { 00166 case WM_CREATE: 00167 break; 00168 case WM_JULIUS: 00169 jEventId = LOWORD(wParam); 00170 switch (jEventId) { 00171 case JEVENT_ENGINE_ACTIVE: DebugOut(hWnd, L"Engine Active"); break; 00172 case JEVENT_ENGINE_INACTIVE:DebugOut(hWnd, L"Engine Inactive"); break; 00173 case JEVENT_ENGINE_PAUSE: DebugOut(hWnd, L"Engine Pause"); break; 00174 case JEVENT_ENGINE_RESUME: DebugOut(hWnd, L"Engine Resume"); break; 00175 case JEVENT_AUDIO_READY: DebugOut(hWnd, L"Audio Input Ready"); break; 00176 case JEVENT_AUDIO_BEGIN: DebugOut(hWnd, L"Audio Input Begin"); break; 00177 case JEVENT_AUDIO_END: DebugOut(hWnd, L"Audio Input End"); break; 00178 case JEVENT_RECOG_BEGIN: DebugOut(hWnd, L"Recognition Begin"); break; 00179 case JEVENT_RECOG_END: DebugOut(hWnd, L"Recognition End"); break; 00180 case JEVENT_RECOG_FRAME: /*DebugOut(hWnd, L"Recognition Frame")*/; break; 00181 case JEVENT_RESULT_FRAME: /* DebugOut(hWnd, L"Result Frame"); */ break; 00182 case JEVENT_RESULT_PASS1: DebugOut(hWnd, L"Result Pass1"); break; 00183 case JEVENT_RESULT_FINAL: DebugOut(hWnd, L"Result Final"); 00184 jResultId = HIWORD(wParam); 00185 if (jResultId != 0) { 00186 DebugOut(hWnd, L"No result"); 00187 } else { 00188 DebugOut(hWnd, (wchar_t *)lParam); 00189 } 00190 break; 00191 case JEVENT_GRAM_UPDATE: DebugOut(hWnd, L"Grammar changed"); break; 00192 default: DebugOut(hWnd, L"! unknown event"); break; 00193 } 00194 //InvalidateRect(hWnd, NULL, TRUE); 00195 break; 00196 case WM_COMMAND: 00197 wmId = LOWORD(wParam); 00198 wmEvent = HIWORD(wParam); 00199 switch (wmId) 00200 { 00201 case IDM_OPENJCONF: 00202 // (re-)open jconf file and prepare for recognition 00203 if (GetJconfFileName( conffile, 1024 )) { 00204 // initialize Julius engine to prepare for recognition 00205 DebugOut(hWnd, L"Loading Julius Engine..."); 00206 if (! julius.initialize( conffile ) ) { 00207 MessageBox(hWnd, L"Error while loading Julius engine.\n", L"Error", MB_OK); 00208 break; 00209 } 00210 DebugOut(hWnd, L"Done."); 00211 DebugOut(hWnd, L"Do [Command]-[Start] to start recognition."); 00212 } 00213 break; 00214 case IDM_STARTPROCESS: 00215 // open audio stream and start recognition thread 00216 if (! julius.startProcess(hWnd)) { 00217 MessageBox(hWnd, L"failed to start process", L"Error", MB_OK); 00218 } 00219 break; 00220 case IDM_STOPPROCESS: 00221 // close audio stream and finish recognition thread 00222 julius.stopProcess(); 00223 break; 00224 case IDM_PAUSE: 00225 // pause engine 00226 julius.pause(); 00227 break; 00228 case IDM_RESUME: 00229 // resume paused engine 00230 julius.resume(); 00231 break; 00232 case IDM_ABOUT: 00233 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 00234 break; 00235 case IDM_EXIT: 00236 DestroyWindow(hWnd); 00237 break; 00238 default: 00239 return DefWindowProc(hWnd, message, wParam, lParam); 00240 } 00241 break; 00242 case WM_PAINT: 00243 hdc = BeginPaint(hWnd, &ps); 00244 EndPaint(hWnd, &ps); 00245 break; 00246 case WM_DESTROY: 00247 PostQuitMessage(0); 00248 break; 00249 default: 00250 return DefWindowProc(hWnd, message, wParam, lParam); 00251 } 00252 return 0; 00253 } 00254 00255 // Message handler for the version box 00256 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 00257 { 00258 UNREFERENCED_PARAMETER(lParam); 00259 switch (message) 00260 { 00261 case WM_INITDIALOG: 00262 return (INT_PTR)TRUE; 00263 00264 case WM_COMMAND: 00265 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 00266 { 00267 EndDialog(hDlg, LOWORD(wParam)); 00268 return (INT_PTR)TRUE; 00269 } 00270 break; 00271 } 00272 return (INT_PTR)FALSE; 00273 } 00274 00275 // Output a debug string to the main window 00276 void DebugOut(HWND hWnd, wchar_t *str) 00277 { 00278 static int line = 0; 00279 HDC hdc = GetDC(hWnd); 00280 TextOut(hdc , 10 , 10 + line * 20, str, lstrlen(str)); 00281 ReleaseDC(hWnd , hdc); 00282 if (++line > 22) line = 0; 00283 }