Text2Speech.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Text2Speech.h : CText2Speech 的声明
  2. /*************************************************************
  3. /* Copyright (C), 2010-2020, ***. Co., Ltd.
  4. /* 文件名: Text2Speech.h
  5. /* 作者: Jeff.W
  6. /* 创建日期: 2010-08-07
  7. /* 版本号: Ver 1.0.0.0
  8. /* 描述: 文本转语音;
  9. /* 其它:
  10. /* :
  11. /*
  12. /* 历史修改记录:
  13. /* 作者 时间 版本 描述
  14. /* Jeff.W 10/08/07 1.0.0.0 创建这个模块;
  15. /* Jeff.W 14/12/20 1.0.0.1 修改成可在UNICODE和多字节下使用;
  16. /* 同时,sapi类库属于5.1,有些过时,属于xp时代产品,需要升级该类库来适应win7以上更高系统版本;
  17. /* 2000\xp\2003没有自带的lily语音库,需要下载安装;win7版本自带;
  18. ***************************************************************/
  19. #ifndef __TEXT_2_SPEECK_HEADER__
  20. #define __TEXT_2_SPEECK_HEADER__
  21. #include <atlbase.h>
  22. extern CComModule _Module;
  23. #include <atlcom.h>
  24. #include "sapi.h"
  25. #include "sphelper.h"
  26. #include "spuihelp.h"
  27. // Speech SDK支持事件。
  28. // 为了与窗口交互,这里在类中定义了消息WM_TTSEVENT。
  29. // 当发生Speech事件时,向相应的窗口发送WM_TTSEVENT消息。在窗口中响应该消息就响应了相应的事件。
  30. // speech message
  31. #define WM_TTSEVENT WM_USER + 101
  32. #define USE_CSTRING 0
  33. #if 0
  34. typedef struct __STVOICES
  35. {
  36. ULONG uVoiceIndex; // 语音在系统中的索引位置;
  37. WCHAR szDescription; // 语音在系统中的名称描述;
  38. }STVoices,*pSTVoices;
  39. #endif
  40. #pragma once
  41. class CText2Speech
  42. {
  43. public:
  44. CText2Speech(void);
  45. virtual ~CText2Speech(void);
  46. // 初始化和释放函数;
  47. BOOL Initialize(IN HWND hWnd = NULL);
  48. void Destroy();
  49. // 语音操作函数;
  50. HRESULT Speak(IN const TCHAR *pText, IN DWORD dwFlags = SPF_DEFAULT);
  51. HRESULT Pause(){ return m_cpVoice->Pause(); }
  52. HRESULT Resume(){ return m_cpVoice->Resume(); }
  53. // 语速函数;
  54. HRESULT SetRate(IN long lRateAdjust){ return m_cpVoice->SetRate(lRateAdjust); }
  55. HRESULT GetRate(OUT long* plRateAdjust){ return m_cpVoice->GetRate(plRateAdjust); }
  56. // 音量函数;
  57. HRESULT SetVolume(IN USHORT usVolume){ return m_cpVoice->SetVolume(usVolume); }
  58. HRESULT GetVolume(OUT USHORT* pusVolume){ return m_cpVoice->GetVolume(pusVolume); }
  59. // 语言函数;
  60. ULONG GetVoiceCount();
  61. HRESULT GetVoice(OUT WCHAR **ppszDescription, IN ULONG lIndex = -1);
  62. HRESULT SetVoice(IN WCHAR **ppszDescription);
  63. HRESULT SetVoice(IN const ULONG &uIndex);
  64. // 获取/设置输出频率
  65. HRESULT GetOutAudio(OUT SPSTREAMFORMAT &eFmt);
  66. HRESULT SetOutAudio(IN const SPSTREAMFORMAT &eFmt = SPSF_8kHz8BitMono);
  67. // 播放WAV文件;
  68. HRESULT PlayWav(IN const TCHAR *szWavFileName);
  69. // 将文本保存在WAV文件中;
  70. HRESULT Save2Wav(IN const TCHAR *pText, IN const TCHAR *szWavFileName);
  71. #if USE_CSTRING
  72. // 获取错误信息函数;
  73. CString GetErrorString() {
  74. return m_sError;
  75. }
  76. #endif
  77. // interface
  78. CComPtr<ISpVoice> m_cpVoice; // 语音接口;
  79. CComPtr<ISpAudio> m_cpOutAudio; // 声音输出接口;
  80. private:
  81. #if USE_CSTRING
  82. CString m_sError;
  83. #endif
  84. };
  85. #endif