Date: 02-16-2022
Return to Index
created by gbSnippets
'There are a thousand ways that a program can benefit from being able to
'tell the user information, instead of just presenting it as text on the screen.
'In this snippet, the function that speaks the text is in a thread of its own
'so that the main program can continue working while the text is spoken.
'Compiler Comments:
'This code is written to compile in PBWin10. To compile in PBWin9, replace this line:
Set oSp = New Dispatch In "SAPI.SpVoice"
'with this line:
Let oSp = NewCOM "SAPI.SpVoice"
'Primary Code:
'This code is the same as was shown in snippets http://gbl_00210, but written
'as a thread function. This version uses a Global variable to hold the text
'being spoken, but the primary code below includes an additional version that
'does not require a Global variable.
Thread Function TalkThread(ByVal x As Long) As Long
'uses Global text$ value
Local vRes, vTxt, vTime As Variant, oSp as Dispatch
Let oSp = NewCOM "SAPI.SpVoice"
If IsFalse IsObject(oSp) Then Exit Function
vTxt = text$
Object Call oSp.Speak(vTxt) To vRes
vTime = -1 As Long
Object Call oSp.WaitUntilDone(vTime) To vRes
End Function
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as DWord, text$
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"No Global Used", 50,10,100,20
Control Add Button, hDlg, 200,"Global Used", 50,40,100,20
Control Add TextBox, hDlg, 300,"", 50,70,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local hThread as DWord
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
'no Global used
Local StrAddress as DWord, sText$ 'local variable sText$
stext$ = "No Global variable used here!"
StrAddress = VarPTR(stext$)
Thread Create TalkThread(StrAddress) To hThread 'start the new thread, argument not used
Thread Close hThread To hThread 'suggested by PowerBASIC Inc. as good practice
Sleep 250
Control Set Text hDlg, 300, "Still talking?"
End If
If CB.Msg = %WM_Command AND CB.Ctl = 200 AND CB.Ctlmsg = %BN_Clicked Then
'Global used
text$ = "This version uses a Global variable."
Thread Create TalkThread2(0) To hThread 'start the new thread, argument not used
Thread Close hThread To hThread 'suggested by PowerBASIC Inc. as good practice
Control Set Text hDlg, 300, "Still talking?"
End If
End Function
Thread Function TalkThread(ByVal StrAddress As DWord) As DWord
'no Global required
Local vRes, vTxt, vTime As Variant, oSp as Dispatch, p AS String POINTER
p = StrAddress
Let oSp = NewCOM "SAPI.SpVoice"
If IsFalse IsObject(oSp) Then Exit Function
vTxt = @p
Object Call oSp.Speak(vTxt) To vRes
vTime = -1 As Long
Object Call oSp.WaitUntilDone(vTime) To vRes
End Function
Thread Function TalkThread2(ByVal x As Long) As Long
'uses Global text$ value
Local vRes, vTxt, vTime As Variant, oSp as Dispatch
Let oSp = NewCOM "SAPI.SpVoice"
If IsFalse IsObject(oSp) Then Exit Function
vTxt = text$
Object Call oSp.Speak(vTxt) To vRes
vTime = -1 As Long
Object Call oSp.WaitUntilDone(vTime) To vRes
End Function
'gbs_00483
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm