Professional Documents
Culture Documents
JARVIS Speech Recognition Program: Thursday, May 30, 2013
JARVIS Speech Recognition Program: Thursday, May 30, 2013
Imports System.Speech.Recognition
Imports System.Speech.Recognition.SrgsGrammar
Imports System.Runtime.InteropServices 'For Monitor Command
Public Class Form1
'This object represents the Speech recognition engine
Private recognizer As SpeechRecognizer
Dim QuestionEvent As String
Public Sub New()
InitializeComponent() ' This call is required by the Windows Form Designer.
recognizer = New SpeechRecognizer() ' Add any initialization after the InitializeComponent() call.
AddHandler recognizer.SpeechDetected, AddressOf recognizer_SpeechDetected 'this event is
raised when the user begins to speak
AddHandler recognizer.SpeechRecognitionRejected, AddressOf
recognizer_SpeechRecognitionRejected 'this is raised when spoken words are not recognized as
compliant to our grammar rules
AddHandler recognizer.SpeechRecognized, AddressOf recognizer_SpeechRecognized 'this is raised
when the application correctly recognizes spoken words
Dim grammar As New GrammarBuilder()
grammar.Append(New Choices(System.IO.File.ReadAllLines("Commands.txt")))
recognizer.LoadGrammar(New Grammar(grammar))
End Sub
Private Sub recognizer_SpeechRecognized(ByVal sender As Object, ByVal e As
SpeechRecognizedEventArgs)
Dim Random As New Random
Dim Number As Integer = Random.Next(1, 10)
Dim Jarvis = CreateObject("sapi.spvoice")
Select Case e.Result.Text.ToUpper
'GREETINGS
Case Is = 1, 2
Jarvis.speak("Good as always. Thanks for asking")
Case Is = 3, 4
Jarvis.speak("I'm doing particularly well actually. How are you?")
QuestionEvent = "Particularly well"
Case Is = 5, 6
Jarvis.speak("I'm good. How about you sir?")
QuestionEvent = "I'm good"
Case Else : Jarvis.speak("Couldn't be better. And you?")
QuestionEvent = "And you"
End Select
End Select
End Sub
'LABEL DETECT / REJECT
Private Sub recognizer_SpeechDetected(ByVal sender As Object, ByVal e As
SpeechDetectedEventArgs)
Label1.ForeColor = Color.Green : Label1.BackColor = Color.Transparent : Label1.Text = "Speech
engine has detected that you spoke something"
End Sub
Private Sub recognizer_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As
SpeechRecognitionRejectedEventArgs)
Label1.ForeColor = Color.Red : Label1.BackColor = Color.Transparent : Label1.Text = ("Sorry, but
the phrase could not be recognized")
End Sub
'LOADS THE COMMANDS INTO AN ARRAY AND LISTBOX
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim Commands() As String = System.IO.File.ReadAllLines("Commands.txt")
For i As Integer = 0 To Commands.Count - 1
lstboxCommands.Items.Add(Commands(i))
Next
End Sub
'HIDES COMMANDS LIST BOX
Private Sub lstboxCommands_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lstboxCommands.SelectedIndexChanged
lstboxCommands.Visible = False
End Sub
'FOR MONITOR COMMAND
Public WM_SYSCOMMAND As Integer = &H112
Public SC_MONITORPOWER As Integer = &HF170
<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As Integer, ByVal hMsg As Integer, ByVal wParam
As Integer, ByVal lParam As Integer) As Integer
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Timer1.Tick
lblTimer.Text = FormatNumber(Val(lblTimer.Text) - Val(0.01), 2)
If lblTimer.Text = 0 Then
ShutdownProcedure()
Timer1.Enabled = False
lblTimer.Visible = False
End If
End Sub
Sub ShutdownProcedure()
If QuestionEvent = "Shutdown" Then
System.Diagnostics.Process.Start("shutdown", "-s")
ElseIf QuestionEvent = "Restart" Then
System.Diagnostics.Process.Start("shutdown", "-r")
ElseIf QuestionEvent = "Log Off" Then
System.Diagnostics.Process.Start("shutdown", "-l")
End If
End Sub
End Class