MS Speech API 4.0 FreeBASIC

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

, FreeBASIC , MS SAPI 4. , FreeBASIC, , MS Windows. COM OLE , ( , ). CreateObject() Visual Basic, Visual Basic for Application VBScript.

, , FreeBASIC, , , FreeBASIC Visual Basic. OLE (OLE Automation). MS Speech API 4 (MS SAPI 4) OLE , , , , FreeBASIC. , SAPI 4, , , FreeBASIC, "examples\disphelper\" (SAPI 4 SAPI 5).

Visual Basic, VBA ., VBScript CreateObject(), , , OLE-. Visual Basic OLE- "". , (methods) / (properties) . OLE- IDispatch, , Visual Basic. (OLE-) OLE "" - , , , , . Visual Basic , .

Visual Basic, FreeBASIC , , OLE . , Disphelper, FreeBASIC. C/C++ Disphelper ( ).

, FreeBASIC , -, Disphelper, -, SAPI 4 Disphelper.

Visual Basic FreeBASIC, .

' Visual Basic
SET tts = CreateObject("ActiveVoice.ActiveVoice.1")
tts.Speak ("Hello, world!")

' FreeBASIC dhCreateObject ("ActiveVoice.ActiveVoice.1", NULL, @tts) DHCallMethod (tts, ".Speak(%S)", "Hello, world!")

, . Disphelper SAPI 4.

Disphelper

Disphelper IDispatch OLE- . OLE- , printf() C. , printf(), . Disphelper , printf().

, Disphelper ( readme.htm, Disphelper):

  • %d - LONG (INTEGER)
  • %u - ULONG (UINTEGER, DWORD)
  • %e - DOUBLE
  • %b - BOOL
  • %s - LPSTR ( , STRING PTR, ZSTRING PTR)
  • %S - LPWSTR ( , WSTRING PTR)
  • %o - IDispatch PTR ( IDispatch OLE-)
  • %p - LPVOID ( )

Disphelper, .

  1. DISPATCH_OBJ() - , IDispatch PTR.
  2. DHCreateObject () - OLE- IDispatch. CreateObject () Visual Basic.
  3. DHCallMethod() - (method) OLE-.
  4. DHPutValue() - (property) OLE-.
  5. DHGetValue () - (property) OLE-.

, . DHCallMethod, DHPutValue DHGetValue. disphelper.bi, FreeBASIC "inc\disphelper\"..

DHCallMethod , , . D IDispatch PTR (, Visual Basic, ), , . , . , Method1 Object1, :

DHCallMethod (Object1, ".Method1(%d)", 100)

DHPutValue? . , Property1 :

DHPutValue (Object1, ".Property1=%d", 100)

DHGetValue. , , , , :

Dim Value As Integer
DHGetValue ("%d", @Value, Object1, ".Property1")

Disphelper HRESULT, OLE-. . , . (, ), .

Direct Speech Synthesis

SAPI 4

Direct Speech Synthesis - , MS SAPI 4. , . MS Windows 2000, SAPI 4 , , . MS Windows 9x XP SAPI MS SAPI 4 SDK4. SAPI 4 , . :


Member:  ActiveVoice.ActiveVoice.1 Function:  CreateObject
Error In:  CreateObjectEx
Error:      
Code:  800401f3
Source:  Application

, , ProgId ( "ActiveVoice.ActiveVoice.1") CLSID, .

. Windows XP "activevoice.reg" :

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\ActiveVoice.ActiveVoice.1]
@="Direct Speech Synthesis"

[HKEY_CLASSES_ROOT\ActiveVoice.ActiveVoice.1\Clsid]
@="{EEE78591-FE22-11D0-8BEF-0060081841DE}"

Windows ( Enter) . SAPI 4 .

Win32 API, , , , .

Direct Speech Synthesis

, . , MS SAPI 4 SDK.

, Disphelper.

:

  • .AudioPause() - .
  • .AudioReset() - .
  • .AudioResume() - .
  • .Speak(text As String) - , .
  • .Select(index As Long) - , .

, :

  • .CountEngines As Long - , SAPI 4 .
  • .CurrentMode As Long - .
  • .FileName As String - wav, . , .
  • .Initialized As Short - 1 .
  • .Pitch As Long - .
  • .ProductName(index As Long) As String - .
  • .Speaker(index As Long) As String - () .
  • .Speaking As Short - 1, ; 0.
  • .Speed As Long - .
  • .Tagged As Boolean - , , .

FreeBASIC

Disphelper FreeBASIC. -, Disphelper Unicode. :

#define UNICODE

-, "disphelper.bi", FreeBASIC. :

#include "disphelper/disphelper.bi"

-, Direct Speech Synthesis . .

Windows, . , , .

:

  1. speech.bas - , .
  2. sapi4.bas - , Direct Speech Synthesis Disphelper.
  3. sapi4.bi - sapi4.bas.
  4. resource.bi - .
  5. speech.rc - .

sapi4.bas. Disphelper . sapi4.bas. "", "" Disphelper. -, ( ) speech.bas. , MS SAPI 5 FreeBASIC, , sapi4.bas , sapi5.bas. , sapi4.bas , "" .

, DISPATCH_OBJ(), ( IDispatch OLE-) , FreeBASIC . :

 
dim SHARED 	  as IDispatch ptr tts = NULL

speech.bas

'  speech.bas
'   SAPI 4    
'    FreeBASIC
'' : fbc -s gui speech.rc speech.bas sapi4.bas 
'' 
'' 
option explicit 
#define UNICODE
#include once "windows.bi" 
#include "disphelper/disphelper.bi"

'       SAPI 4
#include "sapi4.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

'  Disphelper
	dhInitialize( TRUE )

'       
	dhToggleExceptions( TRUE )

'   
  DialogBoxParam( GetModuleHandle( NULL ), cptr( LPCSTR, IDD_DLG1 ), NULL, @DlgProc, NULL ) 

'  ,  
	SAFE_RELEASE( tts)

'    Disphelper
	dhUninitialize( TRUE )
  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
'   Direct Speech Synthesis 
If TRUE <> SAPI4_Initialize(tts) Then
  	EndDialog( hwnd, 0 ) 
Return TRUE
End If

'     
SAPI4_GetVoices(tts, GetDlgItem(hWnd, IDC_VOICES_LIST))

'    
SAPI4_SelectVoice (tts, 1)

'   
SAPI4_SetSpeed (tts, 80)
  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)
SAPI4_Speak (tts, @szBuf)
End Scope

case IDCANCEL
    	EndDialog( hwnd, 0 )

case IDC_VOICES_LIST:
'    
If (event = CBN_SELCHANGE) Then
SAPI4_SelectVoice (tts, SendDlgItemMessage (hWnd, Id, CB_GETCURSEL, 0, 0)+1)
End If
  	end select
    
  case else
  	return FALSE
  
  end select
  
  return TRUE
end function 

sapi4.bas


'  sapi4.bas
'       Direct Speech 
'    Disphelper

option explicit 

#define UNICODE
#include once "windows.bi" 
#include "disphelper/disphelper.bi"
' SAPI4_Initialize    ActiveVoice.ActiveVoice.1   .
' : TRUE,    ; FALSE -   .
FUNCTION SAPI4_Initialize (byRef Object As IDispatch PTR) As BOOL
Dim As String ProgId = "ActiveVoice.ActiveVoice.1"
IF FAILED(dhCreateObject (ProgId, NULL, @Object) THEN
RETURN FALSE
END IF
dhPutValue(Object, ".Initialized = %u", 1)
RETURN TRUE
END FUNCTION

'  SAPI4_GetVoices    , 
'       ,   .
SUB SAPI4_GetVoices (byRef object As IDispatch PTR,byval hCtrl as HWND)
Dim MaxVoice AS INTEGER
Dim szBuf AS WSTRING * 1024
Dim szSpeaker AS WSTRING PTR
Dim szProduct AS WSTRING PTR
Dim i AS INTEGER

SendMessage (hCtrl, CB_RESETCONTENT, 0, 0)
DHGetValue("%d", @MaxVoice, Object, ".CountEngines")
FOR i = 1 TO MaxVoice
DHGetValue("%S", @szSpeaker, Object, ".Speaker(%d)", i)
DHGetValue("%S", @szProduct, Object, ".ProductName (%d)", i)
szBuf = *szSpeaker + ": " +*szProduct
SendMessage (hCtrl, CB_ADDSTRING, 0, @szBuf)
NEXT
SendMessage (hCtrl, CB_SETCURSEL, 0, 0)

			dhFreeString(szSpeaker)
			dhFreeString(szProduct)
END SUB

'  SAPI4_SelectVoice      
'    .
SUB SAPI4_SelectVoice (byRef Object As IDispatch PTR, byVal Index As INTEGER)
Dim MaxVoice AS INTEGER
DHGetValue("%d", @MaxVoice, Object, ".CountEngines")
IF Index > 0 AND Index <= MaxVoice THEN 
dhCallMethod(Object, ".Select(%u)", Index)
END IF
END SUB

'  SAPI4_SetSpeed    .
'        .
SUB SAPI4_SetSpeed (byRef Object As IDispatch PTR, byVal Speed As INTEGER)
dhPutValue(Object, ".Speed=%u", Speed) 
END SUB

'  SAPI4_Speak   ,  .
SUB SAPI4_Speak (byRef Object As IDispatch PTR, byVal Phrase As WString PTR)
dhCallMethod(Object, ".Speak(%S)", Phrase)
END SUB

sapi4.bi

'  sapi4.bi
'   sapi4.bas
#ifndef __SAPI4_BI__
#define __SAPI4_BI__

DECLARE FUNCTION SAPI4_Initialize (byRef Object As IDispatch PTR) As BOOL
DECLARE SUB SAPI4_GetVoices (byRef Object As IDispatch PTR, byval hCtrl as HWND)
DECLARE SUB SAPI4_SelectVoice (byRef Object As IDispatch PTR, byVal Index As Integer)   
DECLARE SUB SAPI4_SetSpeed (byRef Object As IDispatch PTR, byVal Speed As Integer)
DECLARE SUB SAPI4_Speak (byRef Object As IDispatch PTR, byVal Phrase As WString PTR)
#endif

resource.bi


#ifndef IDC_STATIC 
#define IDC_STATIC -1
#endif
#define IDD_DLG1 1000 
#define IDC_VOICES_LIST	1001
#define IDC_TEXT_TO_SPEAK	1002

speech.rc

#include "resource.bi"

IDD_DLG1 DIALOGEX 0,0,200,100 
CAPTION "Text-to-speech" 
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 "Text to speak:", IDC_STATIC, 5, 5+11, 100, 10
	EDITTEXT IDC_TEXT_TO_SPEAK, 5, 5+22, 190, 10

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


, .
: ©  ,  2004-2024