Saturday, April 5, 2008

Talk, Computer will write for you



Aim of this project: Talk, Computer will write for you
Main Title: Voice Recognition
Project: Talk, Computer will write for you
Developer: Md. Redwanur Rahman
Location: Dhaka, Bangladesh.
Date: 11 February, 2008

Read Me:
This sample demonstrates how to do simple dictation in VB with SAPI 5.1. It uses shared recognized context object, uses the default audio input, loads in dictation grammar, sets up event handlers, and shows the recognized text in the dialog text box.
Note:
Since the text box is using system locale, it may not correctly show characters in other languages. For example, if you use Chinese Speech Recognition engine as the default engine on your English OS, the text box may show garbage even though the engine recognizes Chinese.
Helps:
You need to install SAPI, go to http://www.microsoft.com/speech/download/sdk51/ and download sapi5.1
Setup:
To use this get and install the sapi 5.1, free from microsoft
Then go to control panel, speech. Configure the microphone
(You may have to go to the volume control at the bottom of your screen and unmute the Line in)
Run the training session (you may have to add a new profile)
Make sure it is very quiet around you
Be sure to turn off the microphone, Line in after using this or it will
Go asunder (ie get all screwed up)
Have fun it's a blast

P.S it takes a while for the computer to register the voice the first time around so give it a few seconds and see if it comes up.

Program details:
Option Explicit
Dim WithEvents RecoContext As SpSharedRecoContext
Dim Grammar As ISpeechRecoGrammar
Dim m_bRecoRunning As Boolean
Dim m_cChars As Integer
Dim vol As Double ' this helps open the volume control
Dim bye As Integer 'this closes the volume control

Private Sub btnend_Click()
'to end you must turn off the volume control
'If bye = 1 Then End
'vol = Shell("sndvol32", 1)
' you need to do this so the volume control doesn't just
'stop and minimize
'bye = bye + 1
'disable the buttons
'btnStop.Enabled = False
'btnStart.Enabled = False
'change the end buton caption to exit
'btnend.Caption = "&Exit"
End
End Sub

Private Sub Form_Load()
SetState False
m_cChars = 0
'open the volume control to turn it on (not mute)**NOTE this is not microphone but LINE in
Dim vol As Double
vol = Shell("sndvol32", 1)
End Sub

Private Sub btnStart_Click()
Debug.Assert Not m_bRecoRunning
' Initialize recognition context object and grammar object, then
' start dictation
If (RecoContext Is Nothing) Then
Debug.Print "Initializing SAPI reco context object..."
Set RecoContext = New SpSharedRecoContext
Set Grammar = RecoContext.CreateGrammar(1)
Grammar.DictationLoad
End If
Grammar.DictationSetState SGDSActive
SetState True
End Sub

Private Sub btnStop_Click()
Debug.Assert m_bRecoRunning
Grammar.DictationSetState SGDSInactive
SetState False
End Sub

Private Sub Form_Unload(Cancel As Integer)
'open the volume control mute the line in box
vol = Shell("sndvol32", 1)
End Sub
' This function handles Recognition event from the recognized context object.
' Recognition event is fired when the speech recognition engines recognizes
' a sequences of words.
Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechRecognitionType, _
ByVal Result As ISpeechRecoResult _
)
Dim strText As String
'this is to open the notepad
Dim RetVal As Double
strText = Result.PhraseInfo.GetText
'do what you want for various words here
If strText = "green" Then Label1.BackColor = vbGreen
If strText = "red" Then Label1.BackColor = vbRed
If strText = "yellow" Then Label1.BackColor = vbYellow
If strText = "black" Then Label1.BackColor = vbBlack
'this is to end the program by saying by
If strText = "by" Then
MsgBox ("remember to turn off the volume line in")
End
End If
'1 is normal, 3 is maximized 6 is minimized
If strText = "not" Then RetVal = Shell("c:\windows\notepad.exe", 3)
'I'm not sure why microsoft put this in, I took it out and all is still okay
'Debug.Print "Recognition: " & strText & ", " & _
StreamNumber & ", " & StreamPosition
' Append the new text to the text box, and add a space at the end of the
' text so that it looks better
txtSpeech.SelStart = m_cChars
txtSpeech.SelText = strText & " "
m_cChars = m_cChars + 1 + Len(strText)
End Sub

' This function handles the state of Start and Stop buttons according to
' whether dictation is running.
Private Sub SetState(ByVal bNewState As Boolean)
m_bRecoRunning = bNewState
btnStart.Enabled = Not m_bRecoRunning
btnStop.Enabled = m_bRecoRunning
End Sub

For help email:  redu0007@yahoo.com
Web: www.geocities.com/redu0007
Conclusion:
This program recognized your voice and write in a text field. You can use this program to write in MS-word, MS-Excel

No comments: