Speech Recognition Program (Source Code Included)

Speech Recognition Program written in VB Amazon This is another nifty Visual Basic program I developed with Microsoft's speech libraries. It is more advanced in that you can command your computer to do things for you by speaking the corresponding command into the program.

I guess for what it does this application can be called a 'voice commander' or 'voice recognition program' or 'speech recognition program'.

Currently this speech recognition program supports three types of commands: KEYS, EXECUTE, and HTML.

KEYS is used when you want to issue key stroke events to the computer (e.g. press Alt + F4 to close the current window). EXECUTE is used when you'd like to execute some program on your computer, possibly with parameters. HTML is when you have a browse open and you'd like it to point to a specified URL.

Obviously these are all simple tasks that take very little time if you do it manually, but there are situations where this comes in handy.
For example, I love to go to howstuffworks.com on Firefox. Firefox takes some time to load, so it'd be nice if I can just speak 'Fox how stuff works' into my microphone and a Firefox browser is launched with howstuffworks.com already set in its URL field.
In addition for those who are not much computer literate this program can be useful.

Here are a few screen shots of this program:

Speech Recognition Program written in VB

Fancy Speech Recognition Program written in VB

I learned several important lessons in VB: how to iconize a program so that it appears in the application tray; how to load a customized icon for the executable of this program; several Windows API calls.

You can simply click on 'Hide' or the underscore box for the program to disappear and you can recover it by double clicking the icon in the application tray. This mechanism is ideal for some types of applications but not good for others.

In general if the application needs to be running constantly with minimal human intervention, it should have this capability (e.g. web server, anti virus program). If the application is used intensively for a short period of time, it can be spared that functionality (e.g. MS Word, an integrated development environment). Here's a snippet of the main source code file, fMain.frm.

...
Private Sub Form_Load()

If App.PrevInstance Then
MsgBox 'Sorry, but you have Voice Commander already started.', vbMsgBoxSetForeground + vbInformation, 'PC Alarm'
End
End If

'populate form fields
comboAction.AddItem 'KEYS'
comboAction.AddItem 'EXECUTE'
comboAction.AddItem 'HTML'

'load voice commands
'ReDim gVoiceCmd(1)

'Dim tvc As VC
'tvc.cmd = 'close'
'tvc.action = KEYS
'ReDim tvc.data(0)
'tvc.data(0) = 18 & ' ' & vbKeyF4
'gVoiceCmd(0) = tvc
'
'tvc.cmd = 'IE Amazon'
''tvc.action = EXECUTE
'ReDim tvc.data(1)
'tvc.data(0) = 'C:\Program Files\Internet Explorer\iexplore.exe'
'tvc.data(1) = 'amazon.com'
'gVoiceCmd(1) = tvc

gGrammarFile = '\g.xml'
loadVC
'MsgBox 'g: ' & UBound(gVoiceCmd)
'list voice commands
g.Rows = UBound(gVoiceCmd) + 2
g.Cols = 4
g.TextMatrix(g.Row - 1, g.Col) = 'Command'
g.TextMatrix(g.Row - 1, g.Col + 1) = 'Action'
g.TextMatrix(g.Row - 1, g.Col + 2) = 'Data'

gMaxWidth = 0
Dim i, j, ts As String
For i = 0 To UBound(gVoiceCmd)
    'ts = gVoiceCmd(i).cmd & vbTab & gVoiceCmd(i).action & vbTab
    g.TextMatrix(g.Row + i, g.Col - 1) = i + 1
    'listVC(0).AddItem gVoiceCmd(i).cmd
    g.TextMatrix(g.Row + i, g.Col) = gVoiceCmd(i).cmd
    'listVC(1).AddItem getActionStr(gVoiceCmd(i).action)
    g.TextMatrix(g.Row + i, g.Col + 1) = getActionStr(gVoiceCmd(i).action)
    
    If UBound(gVoiceCmd(i).data) = 0 Then
        ts = gVoiceCmd(i).data(0)
    Else
        ts = ''
        For j = 0 To UBound(gVoiceCmd(i).data)
            ts = ts & gVoiceCmd(i).data(j) & ' '
        Next j
        'get rid of trailing space
        ts = Left(ts, Len(ts) - 1)
    End If
    
    If gMaxWidth < p.TextWidth(ts) Then
        gMaxWidth = p.TextWidth(ts)
    End If
    'listVC(2).AddItem ts
    
    'g.AddItem (i + 1) & vbTab & gVoiceCmd(i).cmd & vbTab & _
    '    getActionStr(gVoiceCmd(i).action) & ts
    g.TextMatrix(g.Row + i, g.Col + 2) = ts
        
Next i

'load skin

gKeyMode = False
comboAction.Text = 'EXECUTE'
comboAction_Click

gCounter = UBound(gVoiceCmd) + 2
gFocusing = False
'writeGrammar

'round window
Dim hrgn As Long
hrgn = CreateRoundRectRgn(0, 0, ScaleX(Me.Width, vbTwips, vbPixels), ScaleY(Me.Height, vbTwips, vbPixels), 30, 30)
SetWindowRgn Me.hWnd, hrgn, True
DeleteObject hrgn

hrgn = CreateRoundRectRgn(0, 0, ScaleX(g.Width, vbTwips, vbPixels), ScaleY(g.Height, vbTwips, vbPixels), 15, 15)
SetWindowRgn g.hWnd, hrgn, True
DeleteObject hrgn

'make sure fields inside g are of good size
'Dim row_height, col_width
'col_width = g.Width / (g.Cols - 1)
'For i = 1 To g.Cols - 1
'    g.ColWidth(i) = col_width
'Next
'1st col is number column, so need only small width
g.ColWidth(0) = 360
g.ColWidth(2) = 900
'g.ColWidth(g.Cols - 1) = g.Width - g.ColWidth(0) - g.ColWidth(1) - g.ColWidth(2) - 100
g.ColWidth(g.Cols - 1) = gMaxWidth + 100

'load skin
'grad.MetalCylander RGB(25, 25, 25), RGB(215, 215, 215), RGB(50, 50, 50), RGB(230, 230, 230), Width \ 2, (Width \ 2) + (Width \ 3), Width, Me
grad.MetalCylander RGB(7, 145, 25), RGB(35, 53, 22), RGB(50, 177, 50), RGB(18, 31, 23), Width \ 2, (Width \ 2) + (Width \ 3), Width, Me

'create icon in the lower right corner =)
gHW = Me.hWnd
myNID.cbSize = Len(myNID)
myNID.hWnd = gHW
myNID.uID = uID
myNID.uFlags = NIF_MESSAGE Or NIF_TIP Or NIF_ICON
myNID.uCallbackMessage = cbNotify
myNID.hIcon = vcImg
myNID.szTip = 'Voice Commander' & Chr(0)
ShellNotifyIcon NIM_ADD, myNID
Hook

'load grammar and start engine
On Error GoTo Err_SAPILoad

    Set RC = New SpSharedRecoContext
    Set Grammar = RC.CreateGrammar(1)
    'Grammar.CmdLoadFromFile App.Path & '\g.xml', SLODynamic
    loadGrammar
    activateVC
    gComputerOn = False
    'Grammar.CmdSetRuleIdState 0, SGDSActive
        
    Exit Sub
    
Err_SAPILoad:
    MsgBox 'Error loading SAPI objects! Please make sure SAPI5.1 is correctly installed: ' & Error(Err), vbCritical
    'exit application
    End
End Sub
...
Caveats
Note you need msflxgrd.ocx and comdlg32.ocx. Also make sure SAPI5.1 is correctly installed on your computer.

You can simply use Notepad to open and read the source code in plain text including fMain.frm and clsGradient.cls.
Source Code
Here's the entire source code. Use at your own risks.
Source Code for Voice Recognition Program in Visual Basic
Post your comment below.
Anything is okay.
I am serious.