Imports System.Speech
Imports System.Speech.Recognition
Imports System.Speech.Synthesis
Public Class Form1
Private Engine As New SpeechRecognitionEngine
Private Synt As New SpeechSynthesizer
Private GrammarBuilder As New GrammarBuilder
Private Grammar As Grammar
Private Commands
As New Dictionary(Of
String,
String)
Private DefaultCommands() As String = New String() {"document", "image", "program", "music"}
Private Sub AddCommand(ByVal CommandLine As String, ByVal VocalCommand As String)
Me.Commands.Add(VocalCommand, CommandLine)
Me.ReloadAll()
End Sub
Private Sub ReloadAll()
Me.Engine.UnloadAllGrammars()
GrammarBuilder = New GrammarBuilder
GrammarBuilder.Append(New Choices(Commands.Keys.ToArray))
GrammarBuilder.Culture = Globalization.CultureInfo.GetCultureInfo("en-US")
Grammar = New Grammar(GrammarBuilder)
Me.Engine.LoadGrammar(Grammar)
End Sub
Private Sub Serialize
(Of T
)(ByVal Graph
As T,
ByVal File As String)
Dim Serializer As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim Stream
As New IO.
FileStream(File, IO.
FileMode.
Create)
Serializer.Serialize(Stream, Graph)
Stream.Close()
End Sub
Private Function Deserialize
(Of T
)(ByVal File As String) As T
Dim Serializer As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim Stream
As New IO.
FileStream(File, IO.
FileMode.
Open)
Dim Result As T
Result = Serializer.Deserialize(Stream)
Stream.Close()
Return Result
End Function
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Synt.SelectVoice("Microsoft Sam")
Dim T As New Threading.Thread(AddressOf Engine.SetInputToDefaultAudioDevice)
T.Start()
T.Join()
T = New Threading.Thread(AddressOf Synt.SetOutputToDefaultAudioDevice)
T.Start()
T.Join()
Try
Me.
Commands = Deserialize
(Of
Dictionary(Of
String,
String))(Application.
StartupPath & "\Settings.dat")
For Each Key As String In Me.Commands.Keys
If Array.IndexOf(DefaultCommands, Key) = -1 Then
lstLinks.Items.Add(New ListViewItem(New String() {Me.Commands(Key), Key}))
End If
Next
Catch Ex As Exception
With Me.Commands
.Add("document", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.MyDocuments & Chr(34))
.Add("image", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.MyPictures & Chr(34))
.Add("music", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.MyMusic & Chr(34))
.Add("program", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.ProgramFiles & Chr(34))
End With
End Try
GrammarBuilder.Append(New Choices(Me.Commands.Keys.ToArray))
GrammarBuilder.Culture = Globalization.CultureInfo.GetCultureInfo("en-US")
Grammar = New Grammar(GrammarBuilder)
Engine.UnloadAllGrammars()
Engine.LoadGrammar(Grammar)
AddHandler Engine.SpeechRecognized, AddressOf Speech_Recognized
AddHandler My.Application.UnhandledException, AddressOf Catch_Exception
End Sub
Private Sub Catch_Exception(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
MessageBox.Show("Si è verificato un errore nell'applicazione. Le cause potrebbero essere dovute alla mancanza di un programma adatto all'apertura del file richiesto.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
e.ExitApplication = False
End Sub
Private Sub Speech_Recognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
Dim Text As String = e.Result.Text
If Me.Commands.ContainsKey(Text) Then
Dim Cmd As String = Me.Commands(Text)
Shell(Cmd, AppWinStyle.NormalFocus)
End If
End Sub
Private Sub strStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strStart.Click
If strStart.Text = "Start" Then
Me.Engine.RecognizeAsync(RecognizeMode.Multiple)
strStart.Text = "Stop"
strStart.Image = imgList.Images(1)
Else
Me.Engine.RecognizeAsyncStop()
strStart.Text = "Start"
strStart.Image = imgList.Images(0)
End If
End Sub
Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Serialize(Commands, Application.StartupPath & "\Settings.dat")
Me.Engine.RecognizeAsyncStop()
End Sub
Private Sub strAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAdd.Click
Dim Link As New NewLinkDialog
If Link.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim S As String = Chr(34) & Link.FileName & Chr(34) & " " & Link.Arguments
Dim L As New ListViewItem(New String() {S, Link.VocalCommand})
lstLinks.Items.Add(L)
AddCommand(S, Link.VocalCommand)
End If
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSpellCheck.Click
If lstLinks.SelectedIndices.Count = 0 Then
Exit Sub
End If
Dim Selected As ListViewItem = lstLinks.SelectedItems(0)
Synt.Speak("Spell check")
Synt.Speak(Selected.SubItems(1).Text)
End Sub
Private Sub strToIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strToIcon.Click
Me.Visible = False
Me.ShowInTaskbar = False
End Sub
Private Sub ntfIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ntfIcon.MouseDoubleClick
Me.ShowInTaskbar = True
Me.Visible = True
End Sub
Private Sub strRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemove.Click
If lstLinks.SelectedIndices.Count = 0 Then
Exit Sub
End If
Dim Selected As ListViewItem = lstLinks.SelectedItems(0)
Dim Key As String = Selected.SubItems(1).Text
lstLinks.Items.Remove(Selected)
Me.Commands.Remove(Key)
Me.ReloadAll()
End Sub
End Class