Text-to-speech simple class c++ SAPI
프로그래밍 언어/C++2017. 4. 11. 20:56
TTS simple class c++ source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #pragma once #include <sapi.h> // SAPI #include <sphelper.h> // SAPI Helper #include "ComAutoInit.h" // COM auto initializer //------------------------------------------------------------------------ // Simple class to speak some text (using SAPI). //------------------------------------------------------------------------ class CTextSpeaker { public: //-------------------------------------------------------------------- // Initializes the text speaker. //-------------------------------------------------------------------- CTextSpeaker() { // // Create text to speech engine // HRESULT hr = m_tts.CoCreateInstance(CLSID_SpVoice); if (FAILED(hr)) { ATLTRACE(TEXT("Text-to-speech creation failed.\n")); AtlThrow(hr); } // // Get token corresponding to default voice // hr = SpGetDefaultTokenFromCategoryId(SPCAT_VOICES, &m_voiceToken, FALSE); if (FAILED(hr)) { ATLTRACE(TEXT("Can't get default voice token.\n")); AtlThrow(hr); } // // Set default voice // hr = m_tts->SetVoice(m_voiceToken); if (FAILED(hr)) { ATLTRACE(TEXT("Can't set default voice.\n")); AtlThrow(hr); } } //-------------------------------------------------------------------- // Speaks some text. // (The input text must not be empty.) //-------------------------------------------------------------------- void Speak(const CString & text) { // // Input text must not be empty // if (text.IsEmpty()) { ATLTRACE(TEXT("Empty text passed to CTextSpeaker::Speak().\n")); AtlThrow(E_INVALIDARG); } // // Speak input text // ULONG streamNumber; HRESULT hr = m_tts->Speak( text, SPF_IS_NOT_XML | SPF_ASYNC | SPF_PURGEBEFORESPEAK, &streamNumber); if (FAILED(hr)) { ATLTRACE(TEXT("Speak failed.\n")); AtlThrow(hr); } } //-------------------------------------------------------------------- // Cleanup //-------------------------------------------------------------------- ~CTextSpeaker() { // Nothing to do here. // Automatic cleanup thanks to C++ RAII is fine :-) } // // Ban copy // private: CTextSpeaker(const CTextSpeaker &); CTextSpeaker & operator=(const CTextSpeaker &); // // IMPLEMENTATION // private: // COM initialization and cleanup (must precede other COM related data members) CComAutoInit m_comInit; // Text to speech engine CComPtr<ISpVoice> m_tts; // Default voice token CComPtr<ISpObjectToken> m_voiceToken; }; |
'프로그래밍 언어 > C++' 카테고리의 다른 글
[C/C++] 각종 해시 라이브러리 모음 (0) | 2017.04.11 |
---|---|
[mfc] 작업표시줄 알림창 애니메이션 c++ (0) | 2017.04.11 |
ATL의 다이얼로그 리전 (0) | 2017.04.11 |
Visual Studio 2017 출시 및 ISO 파일 생성 방법 (1) | 2017.03.16 |
아스키(ASCII) 코드표 (1) | 2016.03.01 |