skip to Main Content

I want to convert a given text to an audio file in a c++ console application. I am developing the application in visual studio on windows 10. I found a related code on Microsoft’s learn challenge simple tts guide sapi 5.4 webpage. Below is the code that i am using.

#include <iostream>
#include <sphelper.h>


using namespace std;


int main()
{
    HRESULT             hr = S_OK;
    CComPtr <ISpVoice>      cpVoice;
    CComPtr <ISpStream>     cpStream;
    CSpStreamFormat         cAudioFmt;

    //Create a SAPI Voice
    hr = cpVoice.CoCreateInstance(CLSID_SpVoice);

    //Set the audio format
    if (SUCCEEDED(hr))
    {
        hr = cAudioFmt.AssignFormat(SPSF_22kHz16BitMono);
    }

    //Call SPBindToFile, a SAPI helper method,  to bind the audio stream to the file
    if (SUCCEEDED(hr))
    {

        hr = SPBindToFile(L"c:\ttstemp.wav", SPFM_CREATE_ALWAYS,
            &cpStream; , &cAudioFmt.FormatId(), cAudioFmt.WaveFormatExPtr());
    }

    //set the output to cpStream so that the output audio data will be stored in cpStream
    if (SUCCEEDED(hr))
    {
        hr = cpVoice->SetOutput(cpStream, TRUE);
    }

    //Speak the text "hello world" synchronously
    if (SUCCEEDED(hr))
    {
        hr = cpVoice->Speak(L"Hello World", SPF_DEFAULT, NULL);
    }

    //close the stream
    if (SUCCEEDED(hr))
    {
        hr = cpStream->Close();
    }

    //Release the stream and voice object
    cpStream.Release();
    cpVoice.Release();
}

As far as I understand this code must produce a wav file under drive c: saying "hello world".

In addition to this code I have used a sphelper.h header file. I found the raw code for sphelper.h file on github. But my solution does not work.

When I compile this code in c++ in visual studio the compiler gives error. One error is ‘GetVersionExW’: was declared deprecated. And the other error is ‘expected an expression’ for the line

hr = SPBindToFile(L"c:\ttstemp.wav", SPFM_CREATE_ALWAYS,
            &cpStream; , &cAudioFmt.FormatId(), cAudioFmt.WaveFormatExPtr());

I guess the first error has to do with the sphelper.h header file. The problem is solved for the first error if I do something like this:

#define FKG_FORCED_USAGE 1
#include <sphelper.h>
#undef FKG_FORCED_USAGE

Or something like this:

#pragma warning(disable:4996) 
#include <sphelper.h>
#pragma warning(default: 4996)

And the problem for the second error is solved when i remove the semicolon (;) after &cpstream.

The code compiles when I remove these two errors. But even in that case the program does not produce a wav file under drive c:.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks your suggestion solved the problem. First I removed the semicolon (;) after &cpstream and I added the following code:

    #define FKG_FORCED_USAGE 1
    #include <sphelper.h>
    #undef FKG_FORCED_USAGE
    

    After this I added

    CoInitializeEx(nullptr, COINIT_MULTITHREADED);
    

    to the beginning of main. Now my solution compiles and produces a wav file.


  2. hr = SPBindToFile(L"c:\ttstemp.wav", SPFM_CREATE_ALWAYS,
                &cpStream; , &cAudioFmt.FormatId(), cAudioFmt.WaveFormatExPtr());
    

    There’s a semicolon after &cpStream

    'GetVersionExW': was declared deprecated:

    My compiler doesn’t complain this; also you should be able to just ignore it since it’s WinAPI using it not you.

    Another mistake I spotted:

    Before consuming COM APIs you must call CoInitializeEx first, so add CoInitializeEx(nullptr, COINIT_MULTITHREADED); to the beginning of main.

    It doesn’t generate a file under C:

    Writing to C requires admin privileges. If you correctly handle errors you should be able to see that SPBindToFile returns with Invalid Parameters. Change L"c:\ttstemp.wav" to something like L"c:\users\<your username>\desktop\ttstemp.wav"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search