MS Speech API 5.1 FreeBASIC

:2007
  Twitter   F******k   VKontakte   Telegram   Mastodon

Microsoft Speech API 4.0 ( . ), Microsoft Speech API 5.1 (MS SAPI 5.1) DispHelper.

dhCallMethod, dhGetValue dhPutValue, , SAPI 5.1, dhPutRef FOR_EACH. disphelper.bi. FOR_EACH , : FOR_EACH0, FOR_EACH1, FOR_EACH2,.. FOR_EACH9. , , (, , ).

0.15b FreeBASIC disphelper.bi FOR_EACH , - . , , FOR_EACH)

#include "disphelper/disphelper.bi"
:
#ifdef FOR_EACH0
#undef FOR_EACH0
#define FOR_EACH0(objName, pDisp, szMember) _
	scope 																						:_
		dim as IEnumVARIANT ptr xx_pEnum_xx = NULL    											:_
		DISPATCH_OBJ(objName)                													:_
		if (SUCCEEDED(dhEnumBegin(@xx_pEnum_xx, pDisp, szMember))) then 						:_
			do while(dhEnumNextObject(xx_pEnum_xx, @objName) = NOERROR)
#endif

, Scope "escope", . FOR_EACH.

0.16b FreeBASIC . , 0.15b 0.16b, , - NOT. , , MyFunc() TRUE FALSE. 0.15b :

IF NOT MyFunc() THEN
	MessageBox (NULL, "  FALSE.", "", 0)
ELSE
	MessageBox (NULL, "  TRUE.", "", 0)
END IF

, 0.16b, . MyFunc() TRUE FALSE. :

IF FALSE = MyFunc() THEN
	MessageBox (NULL, "  FALSE.", "", 0)
ELSE
	MessageBox (NULL, "  TRUE.", "", 0)
END IF

SpVoice

OLE , MS SAPI 5.1, , MS SAPI SDK 5.1 ( Automation). Visual Basic. MS Speech SDK, Microsoft (. ).

, , SpVoice. , DispHelper , IDispatch. , MS SAPI 5.1. DispHelper (methods) (properties) . SpVoice, :

  • .Speak (Text As String, [Flags As SpeechVoiceSpeakFlags) AS LONG -- , , . , . , , , XML . .
  • .GetVoices -- , , MS SAPI 5.1. .
  • .GetAudioOutputs -- , , . .
  • .Voice -- , SpObjectToken, , , .
  • .AudioOutput -- , SpObjectToken, .
  • .Rate AS LONG -- , ( -10 10). , , , , .
  • .Volume AS LONG -- , ( 0 100). , . .

, .GetVoices SpVoice ("" ( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens"). . , "" , SAPI5- . "" , ( -> ).

, . MS SAPI 5.1 FreeBASIC. . , DHCreateObject SpVoice, "Sapi.SpVoice":

DIM object IDispatch ptr
IF FAILED(dhCreateObject ("Sapi.SpVoice", NULL, @Object)) THEN
	'...  
END IF
	' ...  

.GetVoices , FOR_EACH0, , , . .

DIM szDescription AS WSTRING PTR
FOR_EACH0(spVoice, Object, ".GetVoices")
	'     
		dhGetValue("%S", @szDescription, spVoice, ".GetDescription")
	'        
		SendMessage (hCtrl, CB_ADDSTRING, 0, szDescription)
NEXT_(spvoice)

dhFreeString(szDescription)

, ( .Voice SpVoice). Index :

	DHGetValue ("%o", @spVoices, object,  ".GetVoices")
	DHGetValue ("%o", @spVoice, spVoices, ".Item(%u)", Index)
	dhPutRef(Object, ".Voice = %o", spVoice)

.GetVoices SpVoice , . , , , "Language=419". , FOR_EACH1:

DIM szDescription AS WSTRING PTR
FOR_EACH1(spVoice, Object, ".GetVoices(%s)", "Language=419")
	dhGetValue("%S", @szDescription, spVoice, ".GetDescription")
	SendMessage (hCtrl, CB_ADDSTRING, 0, szDescription)
NEXT_(spvoice)
dhFreeString(szDescription)

, :

	DHGetValue ("%o", @spVoices, object,  ".GetVoices(%s)", "Language=419")
	DHGetValue ("%o", @spVoice, spVoices, ".Item(%u)", Index)
	dhPutRef(Object, ".Voice = %o", spVoice)

, . , , , . .

-- . .Speak, , .

dhCallMethod(Object, ".Speak(%S)", Phrase)

Windows, MS SAPI 5.1 . , , . Enter "Speak" .

:

  1. speech.bas -- , .
  2. speech.rc -- .
  3. resource.bi -- .
  4. sapi5.bas -- -"" MS SAPI 5.1.
  5. sapi5.bi -- sapi5.bas.

speech.bas

' speech.bas -   ,   
' MS SAPI 5.1    FreeBASIC.
' :  fbc.exe" -s gui speech.rc speech.bas sapi5.bas

option explicit 
#define UNICODE
#include once "windows.bi" 
#include "disphelper/disphelper.bi"
#include "sapi5.bi"
#include "resource.bi"

DECLARE FUNCTION DlgProc (byval hwnd as HWND, byval umsg as UINT, byval wparam as WPARAM, byval lparam as LPARAM) as BOOL
                          
DIM SHARED as IDispatch ptr tts = NULL

dhInitialize( TRUE )
dhToggleExceptions( TRUE )
DialogBoxParam( GetModuleHandle( NULL), cptr(LPCSTR, IDD_DLG1), NULL, @DlgProc, NULL ) 
SAFE_RELEASE( tts)
dhUninitialize( TRUE )
END
' Program end 

                                 
FUNCTION DlgProc (byval hwnd as HWND, byval umsg as UINT, byval wparam as WPARAM, byval lparam as LPARAM) as BOOL
    DIM as long id, event
    
    SELECT CASE uMsg
    CASE WM_INITDIALOG
    IF TRUE <> SAPI5_Initialize(tts) THEN
    	EndDialog( hwnd, 0 ) 
	Return TRUE
    END IF

    '   
    SAPI5_GetVoices(tts, GetDlgItem(hWnd, IDC_VOICES_LIST))
    SAPI5_SelectVoice (tts, 0)
    SAPI5_SetSpeed (tts, 5)
    SAPI5_SetVolume(tts, 100)
    
    '   
    SAPI5_GetAudioOutputs(tts, GetDlgItem(hWnd, IDC_AUDIOOUTPUTS_LIST))
    SAPI5_SelectAudioOutput(tts, 0)

    CASE WM_CLOSE
    EndDialog( hwnd, 0 ) 

    CASE WM_COMMAND
    id    = loword( wParam )
    event = hiword( wParam )
    
    SELECT CASE id
	CASE IDOK 
	scope
	DIM szBuf as WSTRING * 1024
	GetDlgItemText(hWnd, IDC_TEXT_TO_SPEAK, szBuf, 1024)

	SAPI5_Speak (tts, @szBuf)
	END Scope

	CASE IDCANCEL
	EndDialog( hwnd, 0 )

	CASE IDC_VOICES_LIST:
	IF CBN_SELCHANGE = event  THEN
	    SAPI5_SelectVoice (tts, SendDlgItemMessage (hWnd, Id, CB_GETCURSEL, 0, 0))
	END IF

	CASE IDC_AUDIOOUTPUTS_LIST:
	IF CBN_SELCHANGE = event THEN
	    SAPI5_SelectAudioOutput(tts, SendDlgItemMessage (hWnd, Id, CB_GETCURSEL, 0, 0))
	End If

	END SELECT
        
    CASE ELSE
    return FALSE
    
    END SELECT
return TRUE
END FUNCTION

speech.rc

#include "resource.bi"

IDD_DLG1 DIALOGEX 0,0,200,90
CAPTION "SAPI 5: Text-to-speech Example" 
FONT 8,"MS Sans Serif" 
STYLE 0x10CC0000 
EXSTYLE 0x00000080 
BEGIN
LTEXT "Voice:", IDC_STATIC, 5, 5, 40, 9
COMBOBOX IDC_VOICES_LIST, 50, 5, 145, 10, WS_TABSTOP| CBS_DROPDOWNLIST | WS_VSCROLL  
LTEXT "Output:", IDC_STATIC, 5, 5+11, 40, 9
COMBOBOX IDC_AUDIOOUTPUTS_LIST	, 50, 5+11, 145, 10, WS_TABSTOP| CBS_DROPDOWNLIST | WS_VSCROLL  
LTEXT "Text to speak:", IDC_STATIC, 5, 5+22, 100,  10
EDITTEXT IDC_TEXT_TO_SPEAK, 5, 5+33, 190, 10

 DEFPUSHBUTTON  "Speak", IDOK, 120, 5+55, 35, 10 
PUSHBUTTON   "Close", IDCANCEL, 120+37, 5+55, 35, 10 
END

resource.bi


#define IDD_DLG1 1000 
#define IDC_BTN1 1002
#define IDC_STATIC -1
#define IDC_BTNSPEAK 1001

#define IDC_VOICES_LIST	2001
#define IDC_AUDIOOUTPUTS_LIST	2002
#define IDC_TEXT_TO_SPEAK	2003
#endif

sapi5.bas


option explicit 

#define UNICODE
#include once "windows.bi" 
#include "disphelper/disphelper.bi"

FUNCTION SAPI5_Initialize (byRef Object as IDispatch PTR) as BOOL
DIM as String ProgId = "Sapi.SpVoice"
DIM hr as HRESULT
    IF FAILED(dhCreateObject (ProgId, NULL, @Object)) THEN return FALSE
    return TRUE
END FUNCTION

SUB SAPI5_GetVoices (byRef object as IDispatch ptr,byval hCtrl as HWND)
DIM szDescription as WString ptr
    SendMessage (hCtrl, CB_RESETCONTENT, 0, 0)
    FOR_EACH0(spVoice, Object, ".GetVoices")
	dhGetValue("%S", @szDescription, spVoice, ".GetDescription")
	SendMessage (hCtrl, CB_ADDSTRING, 0, szDescription)
    NEXT_(spvoice)
    SendMessage (hCtrl, CB_SETCURSEL,  0, 0)
    dhFreeString(szDescription)
END SUB

SUB SAPI5_SelectVoice (byRef Object as IDispatch ptr , byVal Index as Integer)
DIM spVoices, spVoice as IDispatch ptr

    DHGetValue ("%o", @spVoices, object,  ".GetVoices")
    DHGetValue ("%o", @spVoice, spVoices, ".Item(%u)", Index)
    dhPutRef(Object, ".Voice = %o", spVoice)
END SUB

SUB SAPI5_GetAudioOutputs(byRef object as IDispatch ptr,byval hCtrl as HWND)
DIM szDescription as WString ptr

    SendMessage (hCtrl, CB_RESETCONTENT, 0, 0)
    FOR_EACH0(spAudioOutput, Object, ".GetAudioOutputs")
	dhGetValue("%S", @szDescription, spAudioOutput, ".GetDescription")
	SendMessage (hCtrl, CB_ADDSTRING, 0, szDescription)
    NEXT_(spAudioOutput)
    SendMessage (hCtrl, CB_SETCURSEL,  0, 0)
    dhFreeString(szDescription)
END SUB

SUB SAPI5_SelectAudioOutput(byRef Object as IDispatch ptr , byVal Index as Integer)
DIM spAudioOutputs, spAudioOutput as IDispatch ptr
    DHGetValue ("%o", @spAudioOutputs, object,  ".GetAudioOutputs")
    DHGetValue ("%o", @spAudioOutput, spAudioOutputs, ".Item(%u)", Index)
    dhPutRef(Object, ".AudioOutput= %o", spAudioOutput)
END SUB

SUB SAPI5_SetSpeed (byRef Object as IDispatch ptr , byVal Speed as Integer)
    dhPutValue(Object, ".Rate=%d", Speed) 
END SUB

SUB SAPI5_SetVolume(byRef Object as IDispatch ptr , byVal Volume as Integer)
    IF Volume > 100 THEN VOLUME = 100
    IF Volume < 0 THEN Volume=0 
    dhPutValue(Object, ".Volume=%u", Volume) 
END SUB

SUB SAPI5_Speak (byRef Object as IDispatch ptr , byVal Phrase as WString PTR)
    dhCallMethod(Object, ".Speak(%S)", Phrase)
END SUB

sapi5.bi

#ifndef _SAPI5_BI
#define _SAPI5_BI
DECLARE FUNCTION SAPI5_Initialize (byRef Object as IDispatch PTR) as BOOL
DECLARE SUB SAPI5_GetVoices (byRef Object as IDispatch ptr, byval hCtrl as HWND)
DECLARE SUB  SAPI5_SelectVoice (byRef Object as IDispatch ptr, byVal Index as Integer)
DECLARE SUB SAPI5_GetAudioOutputs(byRef Object as IDispatch ptr, byval hCtrl as HWND)
DECLARE SUB  SAPI5_SelectAudioOutput(byRef Object as IDispatch ptr, byVal Index as Integer)
DECLARE SUB SAPI5_SetSpeed(byRef Object as IDispatch ptr , byVal Speed as Integer)
DECLARE SUB SAPI5_SetVolume(byRef Object as IDispatch ptr , byVal Volume as Integer)
DECLARE SUB SAPI5_Speak (byRef Object as IDispatch ptr , byVal Phraseas  as WString PTR)
#endif


, .
: ©  ,  2004-2024