Sample DLL Code Example (SAPI 5.3)

Microsoft Speech API 5.3

Sample DLL Code Example

This example builds a Visual Basic ActiveX DLL in order to demonstrate general object-oriented programming techniques, and more specifically, to demonstrate the use of the ISpeechRecoGrammar_CmdLoadFromResource method.

The DLL contains one resource, called "101," which is a compiled version of the Solitaire recognition grammar. This resource is used in the ISpeechRecoGrammar CmdLoadFromResource sample.

The DLL contains one method, called "SpeakToFile," which is used in the ISpeechPhraseAlternate code example.

To create the Solitaire grammar
  • Open Notepad
  • Copy the Solitaire Grammar text below, and paste it into Notepad
  • Save it as "C:\sol.xml"
To compile the Solitaire Grammar

Click Start, and then click Run. Paste this text into the text box:

  "C:\Program Files\Microsoft Speech SDK 5.3\Bin\GC" C:\SOL.XML

If your Speech SDK is installed at a different location, adjust the path accordingly. Click OK, and the grammar compiler will compile SOL.XML into SOL.CFG

Create the DLL project

In Visual Basic, create an ActiveX DLL project.

  1. On the Project menu, select Project1 Properties.
  2. In the Project1 Properties dialog box, click the General tab, and replace the Project Name (Project1) with SpeechDocs, click OK.
  3. On the Project menu, click References.
  4. Scroll down the References list, select Microsoft Speech Object Library, click OK.
  5. Paste the DLL source code into the Declarations section of the module called Class1.
Load the Visual Basic Resource Editor
  1. On the Add-Ins menu, click Add-In Manager.
  2. From the Available Add-Ins list box, select the VB 6 Resource Editor .
  3. From the Load Behavior box, select Loaded/Unloaded, click OK.

An icon for the Resource Editor will appear on Visual Basic's standard toolbar.

Add the Grammar to the DLL as a Resource
  1. Click the Resource Editor toolbar icon.
  2. In the Resource Editor, click the Add Custom Resource toolbar button.
  3. In the Open Custom Resource dialog box, select the grammar file SOL.CFG, click OK. The Resource Editor will now display an icon captioned 101.
  4. Right-click the icon and select Properties.
  5. In the Type text box, replace the word CUSTOM with CFGGRAMMAR, click OK.
  6. Click the Save toolbar button and save file SpeechDocs.RES.
Compile the DLL

On the File menu, click Make SpeechDocs.dll.

Text of Solitaire Grammar

This is the text of the Solitaire grammar SOL.XML.

  <GRAMMAR LANGID="409">
    <DEFINE>
        <ID NAME="FROM" VAL="1"/>
        <ID NAME="TO" VAL="2"/>
        <ID NAME="SUIT" VAL="3"/>
        <ID NAME="COLOR" VAL="4"/>
        <ID NAME="RANK" VAL="5"/>
        <ID NAME="ColorRed" VAL="11101"/>
        <ID NAME="ColorBlack" VAL="10011"/>
	<ID NAME="RID_NewGame" VAL="101"/>
	<ID NAME="RID_MoveCard" VAL="102"/>
	<ID NAME="RID_Rank" VAL="103"/>
    </DEFINE>
   <RULE NAME="newgame" ID="RID_NewGame" TOPLEVEL="ACTIVE">
      <P>new +game</P><O>-please</O>
   </RULE>
   <RULE NAME="playcard" TOPLEVEL="ACTIVE" EXPORT="1">
      <O>please</O>
      <P>play the</P>
      <RULEREF NAME="card"/>
      <O>please</O>
   </RULE>
   <RULE NAME="movecard" ID="RID_MoveCard" TOPLEVEL="ACTIVE">
      <O>please</O>
      <P>
         <L>
            <P>move</P>
            <P>put</P>
         </L>
         <P>the</P>
      </P>
      <RULEREF PROPNAME="from" PROPID="FROM" NAME="card"/>
      <O>
         <L>
            <P>on</P>
            <P>to</P>
         </L>
         <P>the</P>
         <RULEREF PROPNAME="to" PROPID="TO" NAME="card"/>
      </O>
      <O>please</O>
   </RULE>
   <RULE NAME="card">
      <L>
         <P>
            <L PROPNAME="color" PROPID="COLOR">
               <P VAL="ColorRed">red</P>
               <P VAL="ColorBlack">black</P>
            </L>
            <RULEREF NAME="rank"/>
         </P>
         <P>
            <RULEREF NAME="rank"/>
            <O>
               <P>of</P>
               <L PROPNAME="suit" PROPID="SUIT">
                  <P VAL="0">clubs</P>
                  <P VAL="1">hearts</P>
                  <P VAL="2">diamonds</P>
                  <P VAL="3">spades</P>
               </L>
            </O>
         </P>
         <L PROPNAME="suit" PROPID="SUIT">
            <P VAL="0">club</P>
            <P VAL="1">heart</P>
            <P VAL="2">diamond</P>
            <P VAL="3">spade</P>
         </L>
      </L>
   </RULE>
   <RULE NAME="rank" ID="RID_Rank">
      <L PROPNAME="rank" PROPID="RANK">
         <P VAL="1">ace</P>
         <P VAL="2">two</P>
         <P VAL="3">three</P>
         <P VAL="4">four</P>
         <P VAL="5">five</P>
         <P VAL="6">six</P>
         <P VAL="7">seven</P>
         <P VAL="8">eight</P>
         <P VAL="9">nine</P>
         <P VAL="10">ten</P>
         <P VAL="11">jack</P>
         <P VAL="12">queen</P>
         <P VAL="13">king</P>
         <P VAL="12">lady</P>
         <P VAL="13">emperor</P>
      </L>
   </RULE>
</GRAMMAR>
The DLL Source Code

This is the source code for SpeechDocs DLL.

  Option Explicit

Private mV As SpeechLib.SpVoice
Private mF As SpeechLib.SpFileStream

Public Function SpeakToFile( _
        ByVal strText, _
        ByVal strFName, _
        Optional NameOrToken _
    ) As SpFileStream

    Set mV = New SpVoice            'Create voice object with default voice
    If (Not IsMissing(NameOrToken)) Then
        On Error GoTo BadNameOrToken
        Select Case TypeName(NameOrToken)   'VBA.Information.TypeName
        Case "String"               'Use string parameter as name of voice
            Set mV.Voice = mV.GetVoices("name=" & NameOrToken).Item(0)
        Case "ISpeechObjectToken"   'Use object token parameter as voice
            Set mV.Voice = NameOrToken
        End Select
    End If

BadNameOrToken:     'Use default voice if Set from NameOrToken parameter fails

    On Error GoTo OtherErrors
    Set mF = New SpFileStream                   'Create stream object
    mF.Open strFName, SSFMCreateForWrite, True  'Open as the filename
    Set mV.AudioOutputStream = mF               'Set voice output to file
    mV.Speak strText, SVSFIsXML                 'Speak synchronously
    mF.Close                                    'Close file

OtherErrors:        'Exit on illegal file path or other err
    Set mV = Nothing
    Set SpeakToFile = mF       'Return Filestream object
End Function