Speech API example

Started by Vortex, February 09, 2025, 07:33:10 PM

Previous topic - Next topic

Vortex

Hello,

Here is a quick speech API example :

.386
.model flat,stdcall
option casemap:none

include         SAPIsample.inc

.data

CLSID_SpVoice   GUID {096749377h,03391h,011D2h,<09Eh,0E3h,000h,0C0h,04Fh,079h,073h,096h>}
IID_ISpVoice    GUID {06C44DF74h,072B9h,04992h,<0A1h,0ECh,0EFh,099h,06Eh,004h,022h,0D4h>}

MyText          dw 'Hello, this is a speech API sample.',0

.data?

pVoice          dd ?

.code

start:

    invoke  CoInitialize,0

    invoke  CoCreateInstance,ADDR CLSID_SpVoice,\
            NULL,CLSCTX_ALL,ADDR IID_ISpVoice,\
            ADDR pVoice

    coinvk  pVoice,ISpVoice,SetRate,<-2>
           
    coinvk  pVoice,ISpVoice,Speak,<OFFSET MyText>,\
            SPF_DEFAULT,NULL
   
    coinvk  pVoice,ISpVoice,Release

    invoke  CoUninitialize
    invoke  ExitProcess,0

END start
Code it... That's all...

Vortex

Code it... That's all...

Quin

Here I am afraid of COM and then you go and write a SAPI example in Assembly! Mind absolutely blown, thanks for sharing! That's blissfully simple too! I had no idea about the coinvk opcode.
You're a wizard, keep it up!  :o  8)

John Z

Hi Quin,

For more complex COM there are helper files.
SAPI is rather simple - here is working source I use in my vCardz_i program. It will speak any vcard field, as well as the whole address in two speeds ...
#include <objbase.h> // Necessary for COM ( + ole32.lib see Project Properties)
#include <sapi.h>     // Found in PellesC-Include-Folder

int Speak2(LPCTSTR szPhrase)
{
ISpVoice *pVoice = NULL;

if (FAILED(CoInitialize( 0 )) ) return FALSE;

HRESULT hr = CoCreateInstance(
&CLSID_SpVoice,
NULL,
CLSCTX_ALL,
&IID_ISpVoice,
(void **) &pVoice);

if( SUCCEEDED( hr ) ){
hr = pVoice->lpVtbl->Speak( pVoice, szPhrase, 0, 0);
pVoice->lpVtbl->Release( pVoice );
pVoice = NULL;
};

CoUninitialize ();
return 0;
}/* speak2 */

John Z

Quin

Hi John Z,
That's true, simple COM like this is doable in pure C. Sadly not all COM is easily doable though, for example I wanted to call the ITextServices COM interface using <textserv.h> as seen in this Stack Overflow post, but that header is C++ only and I couldn't manage to wrap it in C and have it still work  :'(

Vortex

Hi Quin,

Thanks, I am not an expert of COM. My example is a simple one. With time, I hope more sophisticated examples will follow this speech example. 
Code it... That's all...