Running Windows Speech SDK program in PHP


  • I wrote a simple code to generate a .WAV file from a hard-coded text ("Hello world") using Speech SDK. Here's my code:

    #include "stdafx.h"
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
        HRESULT     hr = S_OK;
        CComPtr <ISpVoice>          cpVoice;
        CComPtr <ISpStream>         cpWavStream;
        CSpStreamFormat             cAudioFmt;
        hr = ::CoInitialize(NULL);
        if (SUCCEEDED(hr))
            hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&cpVoice);
        if (SUCCEEDED(hr))
            hr = cAudioFmt.AssignFormat(SPSF_22kHz16BitMono);
        if (SUCCEEDED(hr))
            hr = SPBindToFile(L"hello.wav", SPFM_CREATE_ALWAYS, &cpWavStream, &cAudioFmt.FormatId(), cAudioFmt.WaveFormatExPtr(), SPEI_PHONEME);
        if (SUCCEEDED(hr))
            hr = cpVoice->SetOutput(cpWavStream, TRUE);
        if (SUCCEEDED(hr))
            hr = cpVoice->Speak(L"Hello World", SPF_DEFAULT, NULL);
        return 0;

    If I run the program directly on the console on a server, it produces a correct .WAV file (about 60 kB). However, if I call the program in a PHP script:

    $cmd = "helloTTS.exe";                // The name of the program

    The program runs and finishes, but the produced .WAV file is only 46 bytes. Why does this happen?

    Update: I tried to run the PHP script using PHP interactive mode in Windows (php -a), and the program works correctly; the size of the .WAV file is 60 kB.

    A question that might help me: which library (.DLL) is actually called by the functions such as ISpVoice::Speak()? Is it SAPI.DLL?

    • Edited by ThurN Tuesday, September 24, 2013 12:47 PM
    Monday, September 23, 2013 2:28 PM

All replies

  • so much for high definition audio

    probably the buffer in PHP is choking on the amount of data

    PHP is designed for database use and XML, multimedia needs a different approach

    how are you with JavaScript?

    Corsair Carbide 300R with window
    Corsair TX850V2 70A@12V
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 260 + BFG GTX 260 SLI 55nm 216 core (GT200 Tesla)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 x64 Professional
    LG WH14NS40 14X Blu-Ray Writer BDXL 128GB
    Microsoft Wireless Desktop 2000
    Wacom Bamboo CHT470M

    Monday, September 23, 2013 11:35 PM