Last reviewed: 3/23/2024 8:08:52 AM

Event Handling

To receive compiler warnings, error message, and completion, applications request event notifications. Event availability varies among Speech APIs.

EventEvent ArgumentsDescription
ApiErrorChantAPIErrorEventArgsNotifies the application of an API error
CompileDoneCompileEventArgsNotifies the application that the compile process is done
CompileErrorCompileEventArgsNotifies the application that the compiler detected a grammar error

Some events provide data values that are returned in argument objects. Argument data availability varies among Speech APIs.

  • ChantAPIErrorEventArgs
    • Function - API funtion name
    • Message - API error message
    • RC - API error return code
  • CompileEventArgs
    • Error - Compiler error return code
    • Description - Compiler error description
    • File - Grammar file name
    • AudioStreamOffset - Audio stream offset
    • AudioTimeOffset - Audio time offset

Event notifications are recieved in callback routines as follows:


_Recognizer = _GrammarKit.CreateSAPI5Recognizer();
if (_Recognizer != null)
{
    // Register for the event notifications
    _Recognizer.CompileDone += this.Compile_CompileDone;
    _Recognizer.CompileError += this.Compile_CompileError;
}

// Static event handler declaration
void CALLBACK CompileDone(void* Sender, CCompileEventArgs* Args);
void CALLBACK CompileError(void* Sender, CCompileEventArgs* Args);
...
_Recognizer = _GrammarKit->CreateSAPI5Recognizer();
if (_Recognizer != null)
{
    // Register for the event notifications
    _Recognizer->SetCompileDone(CompileDone);
    _Recognizer->CompileError(CompileError);
}
    

// Static event handler declaration
void CALLBACK CompileDone(void* Sender, CCompileEventArgs* Args);
void CALLBACK CompileError(void* Sender, CCompileEventArgs* Args);
...
_Recognizer = _GrammarKit->CreateSAPI5Recognizer();
if (_Recognizer != null)
{
    // Register for the event notifications
    _Recognizer->SetCompileDone(CompileDone);
    _Recognizer->CompileError(CompileError);
}
    

// Procedure event handler declaration
procedure CompileDone(Sender: TObject; Args: TCompileEventArgs);
procedure CompileError(Sender: TObject; Args: TCompileEventArgs);
...
_Recognizer := _GrammarKit.CreateSAPI5Recognizer();
if (_Recognizer <> null)
begin
    // Register for the event notifications
    _Recognizer.CompileDone := CompileDone;
    _Recognizer.CompileError := CompileError;
end;
    

// Add implements for event handlers
public class Frame1 extends JFrame implements com.grammarkit.JChantGrammarKitEvents
...
_Recognizer = _GrammarKit.createSAPI5Recognizer();
if (_Recognizer != null)
{
    // Set the callback object
    _Recognizer.setChantGrammarKitEvents(this);
    // Register for callbacks
    _Recognizer.registerCallback(ChantGrammarKitCallback.CCGMDone);
    _Recognizer.registerCallback(ChantGrammarKitCallback.CCGMError);
}
    

_Recognizer = _GrammarKit.CreateSAPI5Recognizer()
// Declaring the event handlers routines with Handles clause in VB automatically registers for the event notifications
Private Sub Compile_CompileDone(ByVal sender As System.Object, ByVal e As CompileEventArgs) Handles _Recognizer.CompileDone
Private Sub Compile_CompileError(ByVal sender As System.Object, ByVal e As CompileEventArgs) Handles _Recognizer.CompileError
    

The recognizer object sends all notifications to the event handlers. All event data is contained in a arguments object.


private void Compile_CompileDone(object sender, CompileEventArgs e)
{
    ...
}
private void Compile_CompileError(object sender, CompileEventArgs e)
{
    // Format transcript message
    string errorMsg = "";
    errorMsg = "(" + e.Error.ToString() + ") ";
    errorMsg += "error " + e.LineNumber.ToString() + " ";
    errorMsg += e.Description;
}
    

void CALLBACK CompileDone(void* Sender, CCompileEventArgs* Args)
{
    ...
}
void CALLBACK CompileError(void* Sender, CCompileEventArgs* Args)
{
    // Format transcript message
    CString strErrorMsg;
    strErrorMsg.Format(L"(%d) error %d %s\r\n", Args->GetError(), Args->GetLineNumber(), Args->GetDescription());
    ...
}
    

void CALLBACK CompileDone(void* Sender, CCompileEventArgs* Args)
{
    ...
}
void CALLBACK CompileError(void* Sender, CCompileEventArgs* Args)
{
    // Format transcript message
    String strErrorMsg = "(" + IntToStr(Args->GetError()) + ") ";
    strErrorMsg = strErrorMsg + "error " + IntToStr(Args->GetLineNumber()) + " "  + Args->GetDescription() + "\r\n";
    ...
}
    

procedure TForm1.CompileDone(Sender: TObject; Args: TCompileEventArgs);
begin
    ...
end;
procedure TForm1.CompileError(Sender: TObject; Args: TCompileEventArgs);
var
    strErrorMsg: string;
begin
    strErrorMsg := '';
    // Format transcript message
    strErrorMsg := '(' + IntToStr(Args.Error) + ') ';
    strErrorMsg := strErrorMsg + 'error ' + IntToStr(Args.LineNumber) + ' ' + Args.Description +  #13 + #10;
    ...
end;
    

// Event Handlers
public void apiError(Object sender, ChantAPIErrorEventArgs args)
{
    // Optional check for API errors
}
public void compileDone(Object sender, CompileEventArgs args)
{
    ...
}
public void compileError(Object sender, CompileEventArgs args)
{
    errorMsg = "(" + args.getError() + ") ";
    errorMsg = errorMsg + "error " + args.getLineNumber() + " ";
    errorMsg = errorMsg + args.getDescription() + "\r\n";
    ...
}
    

Private Sub Compile_CompileDone(ByVal sender As System.Object, ByVal e As CompileEventArgs) Handles _Recognizer.CompileDone
    ...
End Sub
Private Sub Compile_CompileError(ByVal sender As System.Object, ByVal e As CompileEventArgs) Handles _Recognizer.CompileError
    Dim errorMsg As String
    errorMsg = "(" + e.Error.ToString() + ") "
    errorMsg += "error " + e.LineNumber.ToString() + " "
    errorMsg += e.Description
    ...
End Sub