Browse
 
Tools
Rss Categories

Streaming Callback Function

Reference Number: AA-01047 Views: 2623 0 Rating/ Voters

The callback function is called by the speech port each time there is a change in the stream status. Primarily this is used with streams performing barge-in detection and/or end-of-speech detection to notify the host application to stop playing a prompt (on barge-in) or to stop recording an utterance (on end-of-speech).


typedef void (*LV_SRE_StreamStateChangeFn)(int NewState, unsigned int TotalBytes, unsigned int RecordedBytes, void* UserData)

Parameters

NewState

New state of stream. See Stream Status.

TotalBytes

The total number of bytes of audio streamed so far.

RecordedBytes

The total number of bytes of audio recorded so far (since the time barge-in occurred).

UserData

A void pointer to a user-defined object which can provide a context when the speech port invokes the callback function.

C Code

  1. void Port_Callback(int NewState, unsigned int TotalBytes,
  2. unsigned int RecordedBytes, void *UserData)
  3. {
  4. int iDecodeStatus;
  5. int i = 0;

  6. printf("Port Callback: ");

  7. switch(StreamStatus)
  8. {
  9. case STREAM_STATUS_NOT_READY:
  10. printf("Not ready. Call StreamStart\n");
  11. break;
  12. case STREAM_STATUS_READY:
  13. printf("Ready for streaming\n");
  14. break;
  15. case STREAM_STATUS_BARGE_IN:
  16. printf("Barge in\n");
  17. break;
  18. case STREAM_STATUS_END_SPEECH:
  19. printf("End of speech\n");
  20. iDecodeStatus = LV_SRE_WaitForEngineToIdle(hport, VOICECHANNEL, 7000);
  21. if(iDecodeStatus != LV_TIME_OUT)
  22. {
  23. printf("\nDecoded Transcript:\t%s\nConfidence Score:\t%d\n
  24. Interpretation Count:\t\t%d\n\n",
  25. LV_SRE_GetInterpretationString(hport, VOICECHANNEL, i)
  26. LV_SRE_GetInterpretationScore(hport, VOICECHANNEL, i)
  27. LV_SRE_GetNumberOfInterpretations(hport, VOICECHANNEL,));
  28. }
  29. break;

  30. case STREAM_STATUS_STOPPED:
  31. printf("Stream stopped\n");
  32. break;
  33. case STREAM_STATUS_BARGE_IN_TIMEOUT:
  34. printf("Barge in timeout\n");
  35. break;
  36. case STREAM_STATUS_END_SPEECH_TIMEOUT:
  37. printf("EOS timeout\n");
  38. break;
  39. }
  40. }

Remarks

This example covers every possible stream status, but notice that only STREAM_STATUS_END_SPEECH executes anything more than a simple print notification. The code would have a comparable effect even with all of the other cases removed. It is not necessary to account for every - or any at all - status; this is only an example of something that might reasonably be in a streaming callback function.

The C version is simpler than the C++ version, but they are very similar in layout. This is a static method, so in C++ you must use the UserData to create a pointer to an instance of whatever class this function belongs to. The initialization would look something like this:

  • SpeechPort* self = (SpeechPort*)UserData;