Sub Speech_IoR
    Type SpeechType
        Speaker As Integer
        Speed As Integer
        Volume As Integer
        File As String
    End Type
    Shared Speech As SpeechType
    Speech.Speaker = 0
    Speech.Speed = 0
    Speech.Volume = 100
    Speech.File = ""
End Sub

Sub Speech_Speaker (who$)
    Shared Speech As SpeechType
    Select Case UCase$(who$)
        Case "DAVID", "MALE", "M": Speech.Speaker = 0
        Case "ZIVA", "FEMALE", "F": Speech.Speaker = 1
    End Select
End Sub

Sub Speech_Speed (Speed)
    Shared Speech As SpeechType
    Speech.Speed = Speed
End Sub

Sub Speech_Volume (Volume)
    Shared Speech As SpeechType
    Speech.Volume = Volume
End Sub

Sub Speech_OutTo (where$)
    Shared Speech As SpeechType
    Select Case UCase$(where$)
        Case "CONSOLE", "CONS", "", "SPEAKER": Speech.File$ = ""
        Case Else: Speech.File$ = where$
    End Select
End Sub

Sub Speech_Say (text$)
    Shared Speech As SpeechType
    speak text$, Speech.Speaker, Speech.Speed, "", Speech.Volume
End Sub


Sub Speech_SaP (text$)
    Shared Speech As SpeechType
    Print text$
    speak text$, Speech.Speaker, Speech.Speed, Speech.File, Speech.Volume
End Sub

Sub Speech_ToWav (text$, file$)
    Shared Speech As SpeechType
    If UCase$(Right$(file$, 4)) = ".WAV" Then speak text$, Speech.Speaker, Speech.Speed, file$, Speech.Volume
End Sub

Sub speak (text As String, Speaker As Integer, Speed, savefile$, volume)
    Dim message As String: message = text 'an in function variable so we don't change the passed data
    If UCase$(Right$(savefile$, 4)) <> ".WAV" Then file$ = "" Else file$ = savefile$ 'again, don't change passed data
    'some symbols and such can't be used with Powershell like this, as they're command symbols
    'we need to strip them out of our text.  (Like apostrophes!)
    remove$ = "'" + Chr$(34) 'add to remove$ here, if more symbols need to be removed as future testing showcases problems
    For j = 1 To Len(remove$)
        Do
            i = InStr(message, Mid$(remove$, j, 1))
            If i Then message = Left$(message, i - 1) + Mid$(message, i + 1)
        Loop Until i = 0
    Next
    out$ = "Powershell -Command " + Chr$(34)
    out$ = out$ + "Add-Type -AssemblyName System.Speech; "
    out$ = out$ + "$Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer; "
    If file$ <> "" Then out$ = out$ + "$Speech.SetOutputToWaveFile({" + Chr$(34) + file$ + Chr$(34) + "}); " 'The command to send the output to a file and not the speakers.
    If Speaker = 0 Then out$ = out$ + "$Speech.SelectVoice('Microsoft David Desktop'); "
    If Speaker = 1 Then out$ = out$ + "$Speech.SelectVoice('Microsoft Zira Desktop'); "
    If volume >= 0 And volume <= 100 Then out$ = out$ + "$Speech.Volume =" + Str$(volume) + "; "
    If Speed Then out$ = out$ + "$Speech.Rate =" + Str$(Speed) + "; "
    out$ = out$ + "$Speech.Speak('" + message + "');" + Chr$(34)
    Shell _Hide out$
End Sub


