Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Speech Link - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Speech
  2. Imports System.Speech.Recognition
  3. Imports System.Speech.Synthesis
  4.  
  5. Public Class Form1
  6.     Private Engine As New SpeechRecognitionEngine
  7.     Private Synt As New SpeechSynthesizer
  8.     Private GrammarBuilder As New GrammarBuilder
  9.     Private Grammar As Grammar
  10.  
  11.     Private Commands As New Dictionary(Of String, String)
  12.     Private DefaultCommands() As String = New String() {"document", "image", "program", "music"}
  13.  
  14.     Private Sub AddCommand(ByVal CommandLine As String, ByVal VocalCommand As String)
  15.         Me.Commands.Add(VocalCommand, CommandLine)
  16.         Me.ReloadAll()
  17.     End Sub
  18.  
  19.     Private Sub ReloadAll()
  20.         Me.Engine.UnloadAllGrammars()
  21.  
  22.         GrammarBuilder = New GrammarBuilder
  23.         GrammarBuilder.Append(New Choices(Commands.Keys.ToArray))
  24.         GrammarBuilder.Culture = Globalization.CultureInfo.GetCultureInfo("en-US")
  25.         Grammar = New Grammar(GrammarBuilder)
  26.  
  27.         Me.Engine.LoadGrammar(Grammar)
  28.     End Sub
  29.  
  30.     Private Sub Serialize(Of T)(ByVal Graph As T, ByVal File As String)
  31.         Dim Serializer As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  32.         Dim Stream As New IO.FileStream(File, IO.FileMode.Create)
  33.         Serializer.Serialize(Stream, Graph)
  34.         Stream.Close()
  35.     End Sub
  36.  
  37.     Private Function Deserialize(Of T)(ByVal File As String) As T
  38.         Dim Serializer As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  39.         Dim Stream As New IO.FileStream(File, IO.FileMode.Open)
  40.         Dim Result As T
  41.         Result = Serializer.Deserialize(Stream)
  42.         Stream.Close()
  43.         Return Result
  44.     End Function
  45.  
  46.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  47.         Synt.SelectVoice("Microsoft Sam")
  48.         Dim T As New Threading.Thread(AddressOf Engine.SetInputToDefaultAudioDevice)
  49.         T.Start()
  50.         T.Join()
  51.  
  52.         T = New Threading.Thread(AddressOf Synt.SetOutputToDefaultAudioDevice)
  53.         T.Start()
  54.         T.Join()
  55.  
  56.         Try
  57.             Me.Commands = Deserialize(Of Dictionary(Of String, String))(Application.StartupPath & "\Settings.dat")
  58.             For Each Key As String In Me.Commands.Keys
  59.                 If Array.IndexOf(DefaultCommands, Key) = -1 Then
  60.                     lstLinks.Items.Add(New ListViewItem(New String() {Me.Commands(Key), Key}))
  61.                 End If
  62.             Next
  63.         Catch Ex As Exception
  64.             With Me.Commands
  65.                 .Add("document", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.MyDocuments & Chr(34))
  66.                 .Add("image", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.MyPictures & Chr(34))
  67.                 .Add("music", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.MyMusic & Chr(34))
  68.                 .Add("program", "explorer.exe " & Chr(34) & My.Computer.FileSystem.SpecialDirectories.ProgramFiles & Chr(34))
  69.             End With
  70.         End Try
  71.  
  72.         GrammarBuilder.Append(New Choices(Me.Commands.Keys.ToArray))
  73.         GrammarBuilder.Culture = Globalization.CultureInfo.GetCultureInfo("en-US")
  74.         Grammar = New Grammar(GrammarBuilder)
  75.  
  76.         Engine.UnloadAllGrammars()
  77.         Engine.LoadGrammar(Grammar)
  78.         AddHandler Engine.SpeechRecognized, AddressOf Speech_Recognized
  79.         AddHandler My.Application.UnhandledException, AddressOf Catch_Exception
  80.     End Sub
  81.  
  82.     Private Sub Catch_Exception(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
  83.         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)
  84.         e.ExitApplication = False
  85.     End Sub
  86.  
  87.     Private Sub Speech_Recognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
  88.         Dim Text As String = e.Result.Text
  89.  
  90.         If Me.Commands.ContainsKey(Text) Then
  91.             Dim Cmd As String = Me.Commands(Text)
  92.             Shell(Cmd, AppWinStyle.NormalFocus)
  93.         End If
  94.     End Sub
  95.  
  96.     Private Sub strStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strStart.Click
  97.         If strStart.Text = "Start" Then
  98.             Me.Engine.RecognizeAsync(RecognizeMode.Multiple)
  99.             strStart.Text = "Stop"
  100.             strStart.Image = imgList.Images(1)
  101.         Else
  102.             Me.Engine.RecognizeAsyncStop()
  103.             strStart.Text = "Start"
  104.             strStart.Image = imgList.Images(0)
  105.         End If
  106.     End Sub
  107.  
  108.     Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  109.         Serialize(Commands, Application.StartupPath & "\Settings.dat")
  110.         Me.Engine.RecognizeAsyncStop()
  111.     End Sub
  112.  
  113.     Private Sub strAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAdd.Click
  114.         Dim Link As New NewLinkDialog
  115.         If Link.ShowDialog = Windows.Forms.DialogResult.OK Then
  116.             Dim S As String = Chr(34) & Link.FileName & Chr(34) & " " & Link.Arguments
  117.             Dim L As New ListViewItem(New String() {S, Link.VocalCommand})
  118.             lstLinks.Items.Add(L)
  119.             AddCommand(S, Link.VocalCommand)
  120.         End If
  121.     End Sub
  122.  
  123.     Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSpellCheck.Click
  124.         If lstLinks.SelectedIndices.Count = 0 Then
  125.             Exit Sub
  126.         End If
  127.  
  128.         Dim Selected As ListViewItem = lstLinks.SelectedItems(0)
  129.         Synt.Speak("Spell check")
  130.         Synt.Speak(Selected.SubItems(1).Text)
  131.     End Sub
  132.  
  133.     Private Sub strToIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strToIcon.Click
  134.         Me.Visible = False
  135.         Me.ShowInTaskbar = False
  136.     End Sub
  137.  
  138.     Private Sub ntfIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ntfIcon.MouseDoubleClick
  139.         Me.ShowInTaskbar = True
  140.         Me.Visible = True
  141.     End Sub
  142.  
  143.     Private Sub strRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemove.Click
  144.         If lstLinks.SelectedIndices.Count = 0 Then
  145.             Exit Sub
  146.         End If
  147.  
  148.         Dim Selected As ListViewItem = lstLinks.SelectedItems(0)
  149.         Dim Key As String = Selected.SubItems(1).Text
  150.  
  151.         lstLinks.Items.Remove(Selected)
  152.         Me.Commands.Remove(Key)
  153.         Me.ReloadAll()
  154.     End Sub
  155. End Class