Thu Apr 28 2011 16:57:18

Asterisk developer's documentation


speech.h File Reference

Generic Speech Recognition API. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_speech
struct  ast_speech_engine
struct  ast_speech_result

Enumerations

enum  ast_speech_flags { AST_SPEECH_QUIET = (1 << 0), AST_SPEECH_SPOKE = (1 << 1), AST_SPEECH_HAVE_RESULTS = (1 << 2) }
enum  ast_speech_results_type { AST_SPEECH_RESULTS_TYPE_NORMAL = 0, AST_SPEECH_RESULTS_TYPE_NBEST }
enum  ast_speech_states { AST_SPEECH_STATE_NOT_READY = 0, AST_SPEECH_STATE_READY, AST_SPEECH_STATE_WAIT, AST_SPEECH_STATE_DONE }

Functions

int ast_speech_change (struct ast_speech *speech, char *name, const char *value)
 Change an engine specific attribute.
int ast_speech_change_results_type (struct ast_speech *speech, enum ast_speech_results_type results_type)
 Change the type of results we want.
int ast_speech_change_state (struct ast_speech *speech, int state)
 Change state of a speech structure.
int ast_speech_destroy (struct ast_speech *speech)
 Destroy a speech structure.
int ast_speech_dtmf (struct ast_speech *speech, const char *dtmf)
 Signal to the engine that DTMF was received.
int ast_speech_grammar_activate (struct ast_speech *speech, char *grammar_name)
 Activate a grammar on a speech structure.
int ast_speech_grammar_deactivate (struct ast_speech *speech, char *grammar_name)
 Deactivate a grammar on a speech structure.
int ast_speech_grammar_load (struct ast_speech *speech, char *grammar_name, char *grammar)
 Load a grammar on a speech structure (not globally)
int ast_speech_grammar_unload (struct ast_speech *speech, char *grammar_name)
 Unload a grammar.
struct ast_speechast_speech_new (char *engine_name, int formats)
 Create a new speech structure.
int ast_speech_register (struct ast_speech_engine *engine)
 Register a speech recognition engine.
int ast_speech_results_free (struct ast_speech_result *result)
 Free a set of results.
struct ast_speech_resultast_speech_results_get (struct ast_speech *speech)
 Get speech recognition results.
void ast_speech_start (struct ast_speech *speech)
 Indicate to the speech engine that audio is now going to start being written.
int ast_speech_unregister (char *engine_name)
 Unregister a speech recognition engine.
int ast_speech_write (struct ast_speech *speech, void *data, int len)
 Write audio to the speech engine.

Detailed Description

Generic Speech Recognition API.

Definition in file speech.h.


Enumeration Type Documentation

Enumerator:
AST_SPEECH_QUIET 
AST_SPEECH_SPOKE 
AST_SPEECH_HAVE_RESULTS 

Definition at line 31 of file speech.h.

                      {
   AST_SPEECH_QUIET = (1 << 0),        /* Quiet down output... they are talking */
   AST_SPEECH_SPOKE = (1 << 1),        /* Speaker spoke! */
   AST_SPEECH_HAVE_RESULTS = (1 << 2), /* Results are present */
};
Enumerator:
AST_SPEECH_RESULTS_TYPE_NORMAL 
AST_SPEECH_RESULTS_TYPE_NBEST 

Definition at line 45 of file speech.h.

Enumerator:
AST_SPEECH_STATE_NOT_READY 
AST_SPEECH_STATE_READY 
AST_SPEECH_STATE_WAIT 
AST_SPEECH_STATE_DONE 

Definition at line 38 of file speech.h.

                       {
   AST_SPEECH_STATE_NOT_READY = 0, /* Not ready to accept audio */
   AST_SPEECH_STATE_READY, /* Accepting audio */
   AST_SPEECH_STATE_WAIT, /* Wait for results to become available */
   AST_SPEECH_STATE_DONE, /* Processing is all done */
};

Function Documentation

int ast_speech_change ( struct ast_speech speech,
char *  name,
const char *  value 
)

Change an engine specific attribute.

Definition at line 166 of file res_speech.c.

References ast_speech_engine::change, and ast_speech::engine.

Referenced by handle_speechset(), and speech_engine_write().

{
   return (speech->engine->change ? speech->engine->change(speech, name, value) : -1);
}
int ast_speech_change_results_type ( struct ast_speech speech,
enum ast_speech_results_type  results_type 
)

Change the type of results we want.

Definition at line 262 of file res_speech.c.

References ast_speech_engine::change_results_type, ast_speech::engine, and ast_speech::results_type.

Referenced by speech_results_type_write().

{
   speech->results_type = results_type;

   return (speech->engine->change_results_type ? speech->engine->change_results_type(speech, results_type) : 0);
}
int ast_speech_change_state ( struct ast_speech speech,
int  state 
)

Change state of a speech structure.

Definition at line 245 of file res_speech.c.

References ast_set_flag, AST_SPEECH_SPOKE, AST_SPEECH_STATE_WAIT, and ast_speech::state.

Referenced by ast_speech_new(), handle_speechrecognize(), and speech_background().

{
   int res = 0;

   switch (state) {
   case AST_SPEECH_STATE_WAIT:
      /* The engine heard audio, so they spoke */
      ast_set_flag(speech, AST_SPEECH_SPOKE);
   default:
      speech->state = state;
      break;
   }

   return res;
}
int ast_speech_destroy ( struct ast_speech speech)

Destroy a speech structure.

Definition at line 220 of file res_speech.c.

References ast_free, ast_mutex_destroy(), ast_speech_results_free(), ast_speech_engine::destroy, ast_speech::engine, ast_speech::lock, ast_speech::processing_sound, and ast_speech::results.

Referenced by destroy_callback(), handle_speechdestroy(), launch_asyncagi(), run_agi(), speech_background(), speech_create(), and speech_destroy().

{
   int res = 0;

   /* Call our engine so we are destroyed properly */
   speech->engine->destroy(speech);

   /* Deinitialize the lock */
   ast_mutex_destroy(&speech->lock);

   /* If results exist on the speech structure, destroy them */
   if (speech->results)
      ast_speech_results_free(speech->results);

   /* If a processing sound is set - free the memory used by it */
   if (speech->processing_sound)
      ast_free(speech->processing_sound);

   /* Aloha we are done */
   ast_free(speech);

   return res;
}
int ast_speech_dtmf ( struct ast_speech speech,
const char *  dtmf 
)

Signal to the engine that DTMF was received.

Definition at line 151 of file res_speech.c.

References AST_SPEECH_STATE_READY, ast_speech_engine::dtmf, ast_speech::engine, and ast_speech::state.

Referenced by speech_background().

{
   int res = 0;

   if (speech->state != AST_SPEECH_STATE_READY)
      return -1;

   if (speech->engine->dtmf != NULL) {
      res = speech->engine->dtmf(speech, dtmf);
   }

   return res;
}
int ast_speech_grammar_activate ( struct ast_speech speech,
char *  grammar_name 
)

Activate a grammar on a speech structure.

Definition at line 63 of file res_speech.c.

References ast_speech_engine::activate, and ast_speech::engine.

Referenced by handle_speechactivategrammar(), and speech_activate().

{
   return (speech->engine->activate ? speech->engine->activate(speech, grammar_name) : -1);
}
int ast_speech_grammar_deactivate ( struct ast_speech speech,
char *  grammar_name 
)

Deactivate a grammar on a speech structure.

Definition at line 69 of file res_speech.c.

References ast_speech_engine::deactivate, and ast_speech::engine.

Referenced by handle_speechdeactivategrammar(), and speech_deactivate().

{
   return (speech->engine->deactivate ? speech->engine->deactivate(speech, grammar_name) : -1);
}
int ast_speech_grammar_load ( struct ast_speech speech,
char *  grammar_name,
char *  grammar 
)

Load a grammar on a speech structure (not globally)

Definition at line 75 of file res_speech.c.

References ast_speech::engine, and ast_speech_engine::load.

Referenced by handle_speechloadgrammar(), and speech_load().

{
   return (speech->engine->load ? speech->engine->load(speech, grammar_name, grammar) : -1);
}
int ast_speech_grammar_unload ( struct ast_speech speech,
char *  grammar_name 
)

Unload a grammar.

Definition at line 81 of file res_speech.c.

References ast_speech::engine, and ast_speech_engine::unload.

Referenced by handle_speechunloadgrammar(), and speech_unload().

{
   return (speech->engine->unload ? speech->engine->unload(speech, grammar_name) : -1);
}
struct ast_speech* ast_speech_new ( char *  engine_name,
int  formats 
) [read]

Create a new speech structure.

Definition at line 172 of file res_speech.c.

References ast_best_codec(), ast_calloc, AST_FORMAT_SLINEAR, ast_free, ast_mutex_destroy(), ast_mutex_init(), ast_speech_change_state(), AST_SPEECH_STATE_NOT_READY, ast_speech_engine::create, ast_speech::engine, find_engine(), ast_speech::format, format, ast_speech_engine::formats, ast_speech::lock, and ast_speech::results.

Referenced by handle_speechcreate(), and speech_create().

{
   struct ast_speech_engine *engine = NULL;
   struct ast_speech *new_speech = NULL;
   int format = AST_FORMAT_SLINEAR;

   /* Try to find the speech recognition engine that was requested */
   if (!(engine = find_engine(engine_name)))
      return NULL;

   /* Before even allocating the memory below do some codec negotiation, we choose the best codec possible and fall back to signed linear if possible */
   if ((format = (engine->formats & formats)))
      format = ast_best_codec(format);
   else if ((engine->formats & AST_FORMAT_SLINEAR))
      format = AST_FORMAT_SLINEAR;
   else
      return NULL;

   /* Allocate our own speech structure, and try to allocate a structure from the engine too */
   if (!(new_speech = ast_calloc(1, sizeof(*new_speech))))
      return NULL;

   /* Initialize the lock */
   ast_mutex_init(&new_speech->lock);

   /* Make sure no results are present */
   new_speech->results = NULL;

   /* Copy over our engine pointer */
   new_speech->engine = engine;

   /* Can't forget the format audio is going to be in */
   new_speech->format = format;

   /* We are not ready to accept audio yet */
   ast_speech_change_state(new_speech, AST_SPEECH_STATE_NOT_READY);

   /* Pass ourselves to the engine so they can set us up some more and if they error out then do not create a structure */
   if (engine->create(new_speech, format)) {
      ast_mutex_destroy(&new_speech->lock);
      ast_free(new_speech);
      new_speech = NULL;
   }

   return new_speech;
}
int ast_speech_register ( struct ast_speech_engine engine)

Register a speech recognition engine.

Definition at line 270 of file res_speech.c.

References ast_log(), AST_RWLIST_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, ast_speech_engine::create, ast_speech_engine::destroy, find_engine(), ast_speech_engine::list, LOG_WARNING, ast_speech_engine::name, and ast_speech_engine::write.

{
   struct ast_speech_engine *existing_engine = NULL;
   int res = 0;

   /* Confirm the engine meets the minimum API requirements */
   if (!engine->create || !engine->write || !engine->destroy) {
      ast_log(LOG_WARNING, "Speech recognition engine '%s' did not meet minimum API requirements.\n", engine->name);
      return -1;
   }

   /* If an engine is already loaded with this name, error out */
   if ((existing_engine = find_engine(engine->name))) {
      ast_log(LOG_WARNING, "Speech recognition engine '%s' already exists.\n", engine->name);
      return -1;
   }

   ast_verb(2, "Registered speech recognition engine '%s'\n", engine->name);

   /* Add to the engine linked list and make default if needed */
   AST_RWLIST_WRLOCK(&engines);
   AST_RWLIST_INSERT_HEAD(&engines, engine, list);
   if (!default_engine) {
      default_engine = engine;
      ast_verb(2, "Made '%s' the default speech recognition engine\n", engine->name);
   }
   AST_RWLIST_UNLOCK(&engines);

   return res;
}
int ast_speech_results_free ( struct ast_speech_result result)

Free a set of results.

Definition at line 93 of file res_speech.c.

References ast_free, AST_LIST_NEXT, ast_speech_result::grammar, ast_speech_result::list, and ast_speech_result::text.

Referenced by ast_speech_destroy(), and ast_speech_start().

{
   struct ast_speech_result *current_result = result, *prev_result = NULL;
   int res = 0;

   while (current_result != NULL) {
      prev_result = current_result;
      /* Deallocate what we can */
      if (current_result->text != NULL) {
         ast_free(current_result->text);
         current_result->text = NULL;
      }
      if (current_result->grammar != NULL) {
         ast_free(current_result->grammar);
         current_result->grammar = NULL;
      }
      /* Move on and then free ourselves */
      current_result = AST_LIST_NEXT(current_result, list);
      ast_free(prev_result);
      prev_result = NULL;
   }

   return res;
}
struct ast_speech_result* ast_speech_results_get ( struct ast_speech speech) [read]

Get speech recognition results.

Definition at line 87 of file res_speech.c.

References ast_speech::engine, and ast_speech_engine::get.

Referenced by handle_speechrecognize(), and speech_background().

{
   return (speech->engine->get ? speech->engine->get(speech) : NULL);
}
void ast_speech_start ( struct ast_speech speech)

Indicate to the speech engine that audio is now going to start being written.

Definition at line 119 of file res_speech.c.

References ast_clear_flag, AST_SPEECH_HAVE_RESULTS, AST_SPEECH_QUIET, ast_speech_results_free(), AST_SPEECH_SPOKE, ast_speech::engine, ast_speech::results, and ast_speech_engine::start.

Referenced by handle_speechrecognize(), speech_background(), and speech_start().

{

   /* Clear any flags that may affect things */
   ast_clear_flag(speech, AST_SPEECH_SPOKE);
   ast_clear_flag(speech, AST_SPEECH_QUIET);
   ast_clear_flag(speech, AST_SPEECH_HAVE_RESULTS);

   /* If results are on the structure, free them since we are starting again */
   if (speech->results) {
      ast_speech_results_free(speech->results);
      speech->results = NULL;
   }

   /* If the engine needs to start stuff up, do it */
   if (speech->engine->start)
      speech->engine->start(speech);

   return;
}
int ast_speech_unregister ( char *  engine_name)

Unregister a speech recognition engine.

Definition at line 302 of file res_speech.c.

References AST_RWLIST_FIRST, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strlen_zero(), ast_verb, ast_speech_engine::list, and ast_speech_engine::name.

{
   struct ast_speech_engine *engine = NULL;
   int res = -1;

   if (ast_strlen_zero(engine_name))
      return -1;

   AST_RWLIST_WRLOCK(&engines);
   AST_RWLIST_TRAVERSE_SAFE_BEGIN(&engines, engine, list) {
      if (!strcasecmp(engine->name, engine_name)) {
         /* We have our engine... removed it */
         AST_RWLIST_REMOVE_CURRENT(list);
         /* If this was the default engine, we need to pick a new one */
         if (engine == default_engine) {
            default_engine = AST_RWLIST_FIRST(&engines);
         }
         ast_verb(2, "Unregistered speech recognition engine '%s'\n", engine_name);
         /* All went well */
         res = 0;
         break;
      }
   }
   AST_RWLIST_TRAVERSE_SAFE_END;
   AST_RWLIST_UNLOCK(&engines);

   return res;
}
int ast_speech_write ( struct ast_speech speech,
void *  data,
int  len 
)

Write audio to the speech engine.

Definition at line 141 of file res_speech.c.

References AST_SPEECH_STATE_READY, ast_speech::engine, ast_speech::state, and ast_speech_engine::write.

Referenced by handle_speechrecognize(), and speech_background().

{
   /* Make sure the speech engine is ready to accept audio */
   if (speech->state != AST_SPEECH_STATE_READY)
      return -1;

   return speech->engine->write(speech, data, len);
}