Thursday, May 30, 2013

JARVIS Speech Recognition Program


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 = "HELLO JARVIS"
                Jarvis.Speak("Hello sir")
            Case Is = "GOODBYE JARVIS"
                Jarvis.Speak("Until next time")
                Me.Close()

                'DATE / TIME / WEATHER
            Case Is = "WHAT TIME IS IT"
                Jarvis.Speak(Format(Now, "Short Time"))
            Case Is = "WHAT DAY IS IT"

                Jarvis.Speak(Format(Now, "Long Date"))
            Case Is = "HOWS THE WEATHER"
                System.Diagnostics.Process.Start("https://www.google.com/webhp?sourceid=chrome-instant&ion=1&ie=UTF-8#output=search&sclient=psy-ab&q=weather&oq=&gs_l=&pbx=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.47008514,d.eWU&fp=6c7f8a5fed4db490&biw=1366&bih=643&ion=1&pf=p&pdl=300")
                Jarvis.Speak("Searching for local weather")

                'SHELL COMMANDS
            Case Is = "RUN GOOGLE CHROME"
                Jarvis.Speak("Right Away")
                Shell()
            Case Is = "RUN ITUNES"
                Jarvis.speak("I'm in the mood for a bit of music actually")
                Shell()

                'WEBSITES
            Case Is = "RUN FACEBOOK"
                System.Diagnostics.Process.Start("http://www.facebook.com")
            Case Is = "RUN GOOGLE"
                System.Diagnostics.Process.Start("http://www.google.com")
            Case Is = "RUN YAHOO"
                System.Diagnostics.Process.Start("http://www.yahoo.com")
            Case Is = "RUN PANDORA"
                System.Diagnostics.Process.Start("http://www.pandora.com")
            Case Is = "RUN THE PIRATE BAY"
                System.Diagnostics.Process.Start("http://www.thepiratebay.se")
            Case Is = "RUN YOUTUBE"
                System.Diagnostics.Process.Start("http://www.youtube.com")

                'MISCELLANEOUS
            Case Is = "SHOW COMMANDS"
                lstboxCommands.Visible = True
                Jarvis.speak("Here we are")
            Case Is = "HIDE COMMANDS"
                lstboxCommands.Visible = False
                Jarvis.speak("Very well")
            Case Is = "OUT OF THE WAY JARVIS", "OUT OF THE WAY"
                Select Case Number
                    Case Is < 6
                        Jarvis.speak("My apologies sir")
                    Case Is > 5
                        Jarvis.speak("Right away")
                End Select
                Me.WindowState = FormWindowState.Minimized
            Case Is = "COME BACK JARVIS", "COME BACK"
                Me.WindowState = FormWindowState.Normal

                Jarvis.speak("Here I am")
            Case Is = "OPEN DISK DRIVE"
                Dim oWMP = CreateObject("WMPlayer.OCX.7")
                Dim CDROM = oWMP.cdromCollection
                If CDROM.Count = 2 Then
                    CDROM.Item(1).Eject()
                    Jarvis.speak("Its now open")
                End If
            Case Is = "MONITOR OFF"
                SendMessage(Me.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2)
                Jarvis.Speak("I'll just close my eyes for a minute")
            Case Is = "SHUTDOWN"
                QuestionEvent = "Shutdown"
                Timer1.Enabled = True
                lblTimer.Visible = True
            Case Is = "RESTART"
                QuestionEvent = "Restart"
                Timer1.Enabled = True
                lblTimer.Visible = True
            Case Is = "LOG OFF"
                QuestionEvent = "Log Off"
                Timer1.Enabled = True
                lblTimer.Visible = True
            Case Is = "ABORT"
                Timer1.Enabled = False
                lblTimer.Visible = False

                QuestionEvent = String.Empty
            Case Is = "SPEED UP"
                If Timer1.Interval = 1 Then
                    Jarvis.Speak("Performing at maximum capacity")
                Else : Timer1.Interval = Timer1.Interval / 10
                End If
            Case Is = "SLOW DOWN"
                If Timer1.Interval = 1000 Then
                    Jarvis.Speak("If it were any slower it would be standing still")
                Else : Timer1.Interval = Timer1.Interval * 10

                End If
              'SOCIAL
            Case Is = "JARVIS"
                Jarvis.speak("Yes?")
            Case Is = "GOOD", "IM GOOD"
                Select Case QuestionEvent
                    Case Is = "Particularly well"
                        Jarvis.speak("Glad to hear it")
                    Case Is = "I'm good"
                        Jarvis.speak("Of course")
                    Case Is = "And you"
                        Jarvis.speak("Good")
                End Select
            Case Is = "HOW ARE YOU", "HOW ARE YOU DOING", "HOW ARE YOU TODAY", "HOWS LIFE"
                Select Case Number
                    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

30 comments:

  1. How do you put this in microsoft visual like as a c++ and copy and paste?

    ReplyDelete
    Replies
    1. http://www.youtube.com/watch?v=A9MdymHGxkE

      its all there buddy! ;)

      Delete
  2. This is awesome Michae! You're the man!

    ReplyDelete
  3. ahhh,there it is...myyy prreeessshhhaaassss!! Thank you Michael. Now my work can truly begin

    ReplyDelete
  4. Firs I must say, you've done a great job!
    Two thumbs up!

    I have a question. Hope you will give an answer.
    How to make the AI learn new words, commands, phrases by itself?

    ReplyDelete
  5. I have checked out your YouTube videos and . Think they are great! Is there a way I can ask it to Google search things?

    ReplyDelete
  6. This is such a cool program! I seem to be having issues though, I want to make it so that when I ask Jarvis how he is doing, then after he says what he will say and asks me how I am, I want to be able to set it up so that if I reply "not so good, or "bad" that he will make a reply to those Statements and try to cheer me up with a joke or open a site that has jokes on it. I tried copying the way you have with the good reply, but the only time I can get him to say something like "I am sorry to hear that" is if he says he is not well and he puts question event into a notsogood state. Please help me out. I really would appreciate it. Thank you ahead of time.

    ReplyDelete
  7. hello...sir u done a gr8 job sir... now im ur fan i love tis..
    sir actually i creating antivirus program in c# sir can u help me sir...if u want reply me...here my mail id:girithecreator@gmail.com

    ReplyDelete
  8. It would not be hard to make a version of this that would let users add their own command on the fly. I'd be happy to do it, and make it available, with your permission.

    ReplyDelete
  9. hi! i have a question to you.
    About:

    - Dim Jarvis = CreateObject("sapi.spvoice")

    - when i saying "Hello Jarvis" there is no voice that talk back to me. how did you get jarvis voice?

    ReplyDelete
  10. i need your facebook account please

    ReplyDelete
  11. Simply the Best Dude..

    Your hard work is really appreaciatable ... many thanks...

    ReplyDelete
  12. HaA Michael ..... Thank you very much ...... with the help of your source code i make my own Speech Recognition Assistance ..... Thank you very very much .........

    ReplyDelete
  13. Do you have a download link for this

    ReplyDelete
  14. how did u changed SAPI default voise

    ReplyDelete
  15. Why you don't use a database to store e.Result? it would be shorter than creating so many 'case' (case without if inside).

    ReplyDelete
  16. There is some solution to implement feature to add commands (later) without programming?

    ReplyDelete
  17. Hi. Great Job Michael. I'm facing a little problem in the program. In the errors list its giving an error "Event 'Tick' cannot be found."
    What do I do?

    ReplyDelete
  18. An error occurred creating the form. See Exception.InnerException for details. The error is: The language for the grammar does not match the language of the speech recognizer.
    I got this error and some times it open dis assemby and says no source available

    ReplyDelete
  19. No clarity in your video. Can you upload one with a better quality ?

    ReplyDelete
  20. hey great stuff bro, really appreciate it now i am trying to modify the code for playing media files, can we import media files and ask a command to play a specific file ?
    i am using VS'12..
    expecting a positive reply
    cheers :D

    ReplyDelete
  21. I'm having trouble on the command line: Me.MainForm = Global.JARVIS.Form1.

    It says it can't create a form. What do I do?

    ReplyDelete
  22. error while running jarvis speech recognition(SAPI does not implement phonetic alphabet selection.)

    ReplyDelete
  23. Hi Michael C,
    am getting the follwing error while i excecute .exe files through voice commands [error in this line:]
    case "open my app" System.Diagnostics.Process.Start(@"E:\CustomizableJarvis\CustomizableJarvis\CustomizeableJarvis\Resources\myapp.exe");
    break;

    System.ComponentModel.Win32Exception
    {"The system cannot find the file specified"}

    System.ComponentModel.Win32Exception was unhandled by user code
    HResult=-2147467259
    Message=The system cannot find the file specified
    Source=System
    ErrorCode=-2147467259
    NativeErrorCode=2
    StackTrace:
    at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
    at System.Diagnostics.Process.Start()
    at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
    at System.Diagnostics.Process.Start(String fileName)
    at CustomizeableJarvis.Form1.Default_SpeechRecognized(Object sender, SpeechRecognizedEventArgs e) in E:\new update\CustomizableJarvis_2\CustomizableJarvis\CustomizeableJarvis\DefaultCommands.cs:line 275
    at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
    at System.Speech.Recognition.SpeechRecognitionEngine.SpeechRecognizedProxy(Object sender, SpeechRecognizedEventArgs e)
    InnerException:

    ReplyDelete
  24. hello.....Michael C ,
    thanx for giving me code of jarvis,
    u can't stop working on jarvis.

    ReplyDelete
  25. im not sure what im doing wrong but my jarvis wont work :(

    ReplyDelete
  26. when I go to customize and change anything and try to save it I get a error saying ( Unhandled exception has occurred in your application. If you continue, the application will ignore this error and attempt to continue.Ifyou click quite,the application will close immediately Access to path C:\User\bla bla bla shell commands txt is denied. How do you fix it?

    ReplyDelete
  27. i follow your videos instructions but after that in jarvis program my default command are not showing

    ReplyDelete