• VC6.0 C++ 如何调用微软windows系统SDK 语音API


    下载3个语音API安装包 http://www.microsoft.com/en-us/download/details.aspx?id=10121

     需要安装微软语音API安装包:SpeechSDK51LangPack、SpeechSDK51以及 msttss22L、

     
    为了在VC中使用这SDK,必需在工程中添加SDK的include和lib目录,为免每个工程都添加目录,最好的办法是在VC的
        
        Option->Directoris立加上SDK的include和lib目录。也可以直接将sapi.h sapi.lib 分别放到VC6的 头文件合库文件中

    有关TTS用法:https://msdn.microsoft.com/en-us/library/ms720165(v=vs.85).aspx#InitializeCOM
        
        一个最简单的例子
     

     #include <sapi.h>
        
        #pragma comment(lib,"ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
        
        #pragma comment (lib,"sapi.lib") //sapi.lib在SDK的lib目录,必需正确配置
        
        int main (int argc, char* argv[])
        
        {
    
     ISpVoice * pVoice = NULL;
    
    //COM初始化:
    
    if (FAILED(::CoInitialize(NULL)))
    
    return FALSE;
    
    //获取ISpVoice接口:
    
    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    
    if( SUCCEEDED( hr ) )
    
    {hr = pVoice->Speak(L"欢迎使用本系统,管理员请登录或者注册", 0, NULL);
    
    pVoice->Release();
    
    pVoice = NULL;}
    
    ::CoUninitialize(); //释放
    
    return TRUE;
    
    }

    详细解释:

     
    void voice_succeed()
    {
    ISpVoice * pVoice; //pVoice指针
    
    
    ::CoInitialize(NULL);  //初始化COM库
    
    
    
    //获取核心的应用程序接口ISpVoice接口
     
    CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);//创建识别引擎
     
    pVoice->Speak(L"登录成功!欢迎使用!", 0,NULL);

    //发音 speak 参数详解:1.用来输入文本字符串 2.标志speak的方式 3.获取当前文本输入等候播放队列位置

    pVoice->Release();//释放资源

    
    pVoice->Release();//释放资源
    
    ::CoUninitialize(); //释放资源
    
    }

     用CoInitialize(), CoCreateInstance()获取IspVoice接口就够了, 程序结束前一定要用CoUninitialize()释放资源。
        
     获取IspVoice接口,然后用ISpVoice::Speak()把文本输出为语音,程序的核心就是IspVoice接口。

     把文本语音输出为WAV文件

     

       #include <sapi.h>
        
        #include <sphelper.h>
        
        #pragma comment(lib,"ole32.lib")
        
        #pragma comment(lib,"sapi.lib")
        
        int main(int argc, char* argv[])
        
        {
        
        ISpVoice * pVoice = NULL;
        
        if (FAILED(::CoInitialize(NULL)))
        
        return FALSE;
        
        HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL,
        
        IID_ISpVoice, (void **)&pVoice);
        
        if( SUCCEEDED( hr ) )
        
        {
        
        CComPtr<ISpStream> cpWavStream;
        
        CComPtr<ISpStreamFormat> cpOldStream;
        
        CSpStreamFormat OriginalFmt;
        
        pVoice->GetOutputStream( &cpOldStream );
        
        OriginalFmt.AssignFormat(cpOldStream);
        
        hr = SPBindToFile( L"D:\output.wav",SPFM_CREATE_ALWAYS,
        
        &cpWavStream,&OriginalFmt.FormatId(),
        
        OriginalFmt.WaveFormatExPtr() );
        
        if( SUCCEEDED( hr ) )
        
        {
        
        pVoice->SetOutput(cpWavStream,TRUE);
        
        WCHAR WTX[] = L"<VOICE REQUIRED=''NAME=Microsoft Mary''/>text to wave";
        
        pVoice->Speak(WTX, SPF_IS_XML, NULL);
        
        pVoice->Release();
        
        pVoice = NULL;
        
        }
        
        }
        
        ::CoUninitialize();
        
        return TRUE;
        
        }
        
  • 相关阅读:
    VS2010下配置CxImage
    Visual Studio 2010 开发配置
    主机屋使用感受
    Web开发者必备的20款超赞jQuery插件
    自动页面居中
    jQuery+CSS打造的网页背景颜色切换效果
    小按钮,大学问
    【网站开发必备】——12款响应式 Lightbox(灯箱)效果插件
    修正 IE 的双倍页边距 bug
    a>b?a:b
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/4776670.html
Copyright © 2020-2023  润新知