' _,
' ___ _ _ _ ___(_)
'/ __| / \ | \| | _ | |
'\__ \/ \ \| \\ | _ \ |
'|___/_/ \_|_|\_|___/_|
'
' Speaker
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare PtrSafe Function GetShortPathName Lib "KERNEL32" Alias "GetShortPathNameW" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
#Else
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameW" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
#End If
' _,
' ___ _ _ _ ___(_)
'/ __| / \ | \| | _ | |
'\__ \/ \ \| \\ | _ \ |
'|___/_/ \_|_|\_|___/_|
'
'==================================================================================================
Private Sub SpeakerXL_Busy_Test()
Call SpeakerXL_Busy("My NickName is Sanbi", 0, 1.5)
End Sub
Sub SpeakerXL_Busy(Optional Text As String, _
Optional Speaker As Integer = 0, _
Optional Speed As Double = 1, _
Optional Volume As Integer = 100, _
Optional IsSpeakerVietnamese As Boolean = True)
DoEvents
Dim oVoice As SpeechLib.SpVoice
Set oVoice = VBA.CreateObject("SAPI.SpVoice")
If VBA.IsNumeric(Speaker) Then
Set oVoice.Voice = oVoice.GetVoices.Item(Speaker)
Else
Set oVoice.Voice = oVoice.GetVoices("Name=" & Speaker).Item(0)
End If
If IsSpeakerVietnamese Then
Dim i As Long
For i = 0 To oVoice.GetVoices.Count - 1
Set oVoice.Voice = oVoice.GetVoices.Item(i)
If oVoice.Voice.GetDescription Like "* An*" Then
Exit For
End If
Next
End If
oVoice.Rate = Speed
oVoice.Volume = Volume
oVoice.Speak Text
Set oVoice = Nothing
End Sub
' _,
' ___ _ _ _ ___(_)
'/ __| / \ | \| | _ | |
'\__ \/ \ \| \\ | _ \ |
'|___/_/ \_|_|\_|___/_|
'
Private Sub SpeakerXL_test()
Call SpeakerXL("My NickName is Sanbi", 0, 1.5, 100, 0)
'Call SpeakerXL("NickName c" & VBA.ChrW$(7911) & "a t" & VBA.ChrW$(244) & "i l" & VBA.ChrW$(224) & " Sanbi")
End Sub
Sub SpeakerXL(Optional Text As String, _
Optional Speaker = 0, _
Optional Speed As Double = 1, _
Optional Volume As Integer = 100, _
Optional IsSpeakerVietnamese As Boolean = True)
On Error Resume Next
Call mciSendString("close all", "", 0, 0)
Const SAFT48kHz16BitStereo = 39
Const SSFMCreateForWrite = 3
Dim oFileStream As SpeechLib.SpFileStream
Dim oVoice As SpeechLib.SpVoice
Dim xpath As String
xpath = VBA.Environ$("temp") & "\Sample.wav"
GoSub Save
xpath = GetShortPath(xpath)
Set oFileStream = VBA.CreateObject("SAPI.SpFileStream")
oFileStream.Format.Type = SAFT48kHz16BitStereo
oFileStream.Open xpath, SSFMCreateForWrite, True
Set oVoice = VBA.CreateObject("SAPI.SpVoice")
If VBA.IsNumeric(Speaker) Then
Set oVoice.Voice = oVoice.GetVoices.Item(Speaker)
Else
Set oVoice.Voice = oVoice.GetVoices("Name=" & Speaker).Item(0)
End If
' If IsSpeakerVietnamese Then
' Dim I As Long
' For I = 0 To oVoice.GetVoices.Count - 1
' Set oVoice.Voice = oVoice.GetVoices.Item(I)
' Debug.Print oVoice.Voice.GetDescription
' If oVoice.Voice.GetDescription Like "* An*" Then
'
' Exit For
' End If
' Next
' End If
Set oVoice.AudioOutputStream = oFileStream
oVoice.Volume = 100
oVoice.Speak Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault
oVoice.Rate = Speed
oVoice.Volume = 100
oFileStream.Close
Set oFileStream = Nothing
Set oVoice = Nothing
Call mciSendString("open " & xpath & " alias fmusic", "", 0, 0)
Call mciSendString("play fmusic", "", 0, 0)
Exit Sub
Save:
With VBA.CreateObject("ADODB.Stream")
.Type = 2 'Stream type
.Charset = "utf-8" 'or utf-16 etc
.Open
.WriteText ""
.SaveToFile xpath, 2 'Save binary data To disk
End With
VBA.err.Clear
Return
End Sub
' _,
' ___ _ _ _ ___(_)
'/ __| / \ | \| | _ | |
'\__ \/ \ \| \\ | _ \ |
'|___/_/ \_|_|\_|___/_|
'
Private Function GetShortPath(ByVal Path As String) As String
Dim ret&, Buff As String * 512
ret = GetShortPathName(VBA.StrConv(Path, 64), Buff, 512)
GetShortPath = VBA.Left(StrConv(Buff, 128), ret)
End Function