Helping Meghan Speak (Avalon & SAPI)


I saw this post http://www.longhornblogs.com/robert/archive/2005/11/11/15246.aspx and just had to help out. Here is something I quickly threw together.  It uses SAPI 5.1 & Avalon.  To use this, you need to add a reference to the SAPI com component (Install the SAPI SDK if you don’t already have it).  Download the project and code.  Good luck Meghan!!


 


<Window x:Class=SpeakForMe.Window1xmlns=http://schemas.microsoft.com/winfx/avalon/2005xmlns:x=http://schemas.microsoft.com/winfx/xaml/2005Title=SpeakForMeHeight=Auto>


   <Window.Resources>
      
<
XmlDataProvider x:Key=Phrases XPath=Phrases Source=Phrases.xml>
      </
XmlDataProvider>


      <DataTemplate x:Key=PhraseTemplate>
         
<
TextBlock Text={Binding .InnerText}“/>
      </DataTemplate>
   
</
Window.Resources>


   <DockPanel>


      <Grid DockPanel.Dock=Bottom>
         
<
Grid.ColumnDefinitions>
            
<
ColumnDefinition/>
            
<
ColumnDefinition Width=100/>
         
</
Grid.ColumnDefinitions>
         
<
Grid.RowDefinitions>
            
<
RowDefinition Height=100/>
         
</Grid.RowDefinitions>


         <TextBox x:Name=WhatToSayTextBox Background=AliceBlue FontSize=24 Grid.Column=0 Grid.Row=0 />
         
<
Button x:Name=SayItButton Grid.Column=1 Grid.Row=0 Click=SayItClicked>Say It!</Button>
      
</
Grid>


      <ListBox x:Name=PhraseList Background=LightPink ItemsSource={Binding Source={StaticResource Phrases}, XPath=Phrase}ItemTemplate={StaticResource PhraseTemplate}/>


   </DockPanel>


</Window>





using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Diagnostics;
using SpeechLib;
using System.Xml;

namespace
SpeakForMe
{


   /// <summary>
   /// Interaction logic for Window1.xaml
   /// </summary>


   public partial class Window1 : Window
   
{


      public Window1()
      
{
         
InitializeComponent();
         
WhatToSayTextBox.KeyDown +=
new System.Windows.Input.KeyEventHandler(WhatToSayTextBox_KeyDown);
         
PhraseList.SelectionChanged +=
new SelectionChangedEventHandler(PhraseList_SelectionChanged);
      
}


      void PhraseList_SelectionChanged(object sender, SelectionChangedEventArgs e)
      
{
         
if (e.AddedItems.Count > 0)
         
{
            
XmlElement x = e.AddedItems[0] as XmlElement;
            
WhatToSayTextBox.Text = x.InnerText;
         
}
      
}


      void WhatToSayTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
      
{
         
if (e.Key == System.Windows.Input.Key.Enter)
            
SayIt(WhatToSayTextBox.Text);
      
}


      private void SayItClicked(object sender, RoutedEventArgs e) 
      
{
         
SayIt(WhatToSayTextBox.Text);
      
}


      private void SayIt(string whatToSay)
      
{
         
uint num;

         
Debug.WriteLine(“Say: “ + whatToSay);
         
ISpVoice voice = new SpVoiceClass();
         
voice.Speak(whatToSay, 0 ,
out num);
      
}
   
}
}





Phrases.xml


<?xml version=1.0 encoding=utf-8 ?>
<
Phrases>
   <
Phrase>Hello World!</Phrase>
   
<
Phrase>Yes</Phrase>
   
<
Phrase>No</Phrase>
</
Phrases>

Comments (7)

  1. C++ guy says:

    The SAPI stuff looks cool.

    But this code sample actually serves to show how much typing is required to build a simple UI in Avalon and C#. Please tell me you used a graphical lay-out tool and didn’t hand-code all that XML? And those 10 "using …" lines must have been added by some wizard, right?

    I’m going to go buy stock in companies that make ergonomic keyboards and voice recognition software.

  2. interscape says:

    Matt,

    I wanted to take a second and thank you for putting this together for Meghan. She saw it this evening and flipped out at how cool it was. I’m currently setting the Speech and Avalon SDKs up on my VPC, so that I can add some UI for adding new phrases.

    It was really awesome of you to put this together for her, and she’s very grateful. I had no idea working with the Speech SDK was so easy!

    Thanks again, Matt!

    -Robert & Meghan

  3. PatriotB says:

    Ohhhhh.. that pink……….. :(

    Just wait until WPF is released, all of the beautiful application UIs we’ll see…

    (I’m getting flashbacks of 1997 home pages with blinking text, dizzying background patterns, marquees, background sounds…)

    Sorry, couldn’t resist :)

  4. interscape says:

    Actually, pink is Meghan’s favorite color (he probably gathered from her website). She got super excited when she saw it was pink.

    Hey PatriotB… the next time you feel the urge to open your mouth… maybe you should try resisting harder.

  5. fred says:

    wow. a flame war is brewing on some page about SAPI.

    people juut NEED to tell others ‘what they should do’.

    i’m gonna go get an abortion, right after i burn this flag. brb kthxbye.