speech.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Generic Speech Recognition API
  20. */
  21. #ifndef _ASTERISK_SPEECH_H
  22. #define _ASTERISK_SPEECH_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. /* Speech structure flags */
  27. enum ast_speech_flags {
  28. AST_SPEECH_QUIET = (1 << 0), /* Quiet down output... they are talking */
  29. AST_SPEECH_SPOKE = (1 << 1), /* Speaker spoke! */
  30. AST_SPEECH_HAVE_RESULTS = (1 << 2), /* Results are present */
  31. };
  32. /* Speech structure states - in order of expected change */
  33. enum ast_speech_states {
  34. AST_SPEECH_STATE_NOT_READY = 0, /* Not ready to accept audio */
  35. AST_SPEECH_STATE_READY, /* Accepting audio */
  36. AST_SPEECH_STATE_WAIT, /* Wait for results to become available */
  37. AST_SPEECH_STATE_DONE, /* Processing is all done */
  38. };
  39. enum ast_speech_results_type {
  40. AST_SPEECH_RESULTS_TYPE_NORMAL = 0,
  41. AST_SPEECH_RESULTS_TYPE_NBEST,
  42. };
  43. /* Speech structure */
  44. struct ast_speech {
  45. /*! Structure lock */
  46. ast_mutex_t lock;
  47. /*! Set flags */
  48. unsigned int flags;
  49. /*! Processing sound (used when engine is processing audio and getting results) */
  50. char *processing_sound;
  51. /*! Current state of structure */
  52. int state;
  53. /*! Expected write format */
  54. struct ast_format *format;
  55. /*! Data for speech engine */
  56. void *data;
  57. /*! Cached results */
  58. struct ast_speech_result *results;
  59. /*! Type of results we want */
  60. enum ast_speech_results_type results_type;
  61. /*! Pointer to the engine used by this speech structure */
  62. struct ast_speech_engine *engine;
  63. };
  64. /* Speech recognition engine structure */
  65. struct ast_speech_engine {
  66. /*! Name of speech engine */
  67. char *name;
  68. /*! Set up the speech structure within the engine */
  69. int (*create)(struct ast_speech *speech, struct ast_format *format);
  70. /*! Destroy any data set on the speech structure by the engine */
  71. int (*destroy)(struct ast_speech *speech);
  72. /*! Load a local grammar on the speech structure */
  73. int (*load)(struct ast_speech *speech, const char *grammar_name, const char *grammar);
  74. /*! Unload a local grammar */
  75. int (*unload)(struct ast_speech *speech, const char *grammar_name);
  76. /*! Activate a loaded grammar */
  77. int (*activate)(struct ast_speech *speech, const char *grammar_name);
  78. /*! Deactivate a loaded grammar */
  79. int (*deactivate)(struct ast_speech *speech, const char *grammar_name);
  80. /*! Write audio to the speech engine */
  81. int (*write)(struct ast_speech *speech, void *data, int len);
  82. /*! Signal DTMF was received */
  83. int (*dtmf)(struct ast_speech *speech, const char *dtmf);
  84. /*! Prepare engine to accept audio */
  85. int (*start)(struct ast_speech *speech);
  86. /*! Change an engine specific setting */
  87. int (*change)(struct ast_speech *speech, const char *name, const char *value);
  88. /*! Get an engine specific setting */
  89. int (*get_setting)(struct ast_speech *speech, const char *name, char *buf, size_t len);
  90. /*! Change the type of results we want back */
  91. int (*change_results_type)(struct ast_speech *speech, enum ast_speech_results_type results_type);
  92. /*! Try to get results */
  93. struct ast_speech_result *(*get)(struct ast_speech *speech);
  94. /*! Accepted formats by the engine */
  95. struct ast_format_cap *formats;
  96. AST_LIST_ENTRY(ast_speech_engine) list;
  97. };
  98. /* Result structure */
  99. struct ast_speech_result {
  100. /*! Recognized text */
  101. char *text;
  102. /*! Result score */
  103. int score;
  104. /*! NBest Alternative number if in NBest results type */
  105. int nbest_num;
  106. /*! Matched grammar */
  107. char *grammar;
  108. /*! List information */
  109. AST_LIST_ENTRY(ast_speech_result) list;
  110. };
  111. /*! \brief Activate a grammar on a speech structure */
  112. int ast_speech_grammar_activate(struct ast_speech *speech, const char *grammar_name);
  113. /*! \brief Deactivate a grammar on a speech structure */
  114. int ast_speech_grammar_deactivate(struct ast_speech *speech, const char *grammar_name);
  115. /*! \brief Load a grammar on a speech structure (not globally) */
  116. int ast_speech_grammar_load(struct ast_speech *speech, const char *grammar_name, const char *grammar);
  117. /*! \brief Unload a grammar */
  118. int ast_speech_grammar_unload(struct ast_speech *speech, const char *grammar_name);
  119. /*! \brief Get speech recognition results */
  120. struct ast_speech_result *ast_speech_results_get(struct ast_speech *speech);
  121. /*! \brief Free a set of results */
  122. int ast_speech_results_free(struct ast_speech_result *result);
  123. /*! \brief Indicate to the speech engine that audio is now going to start being written */
  124. void ast_speech_start(struct ast_speech *speech);
  125. /*! \brief Create a new speech structure */
  126. struct ast_speech *ast_speech_new(const char *engine_name, const struct ast_format_cap *formats);
  127. /*! \brief Destroy a speech structure */
  128. int ast_speech_destroy(struct ast_speech *speech);
  129. /*! \brief Write audio to the speech engine */
  130. int ast_speech_write(struct ast_speech *speech, void *data, int len);
  131. /*! \brief Signal to the engine that DTMF was received */
  132. int ast_speech_dtmf(struct ast_speech *speech, const char *dtmf);
  133. /*! \brief Change an engine specific attribute */
  134. int ast_speech_change(struct ast_speech *speech, const char *name, const char *value);
  135. /*! \brief Get an engine specific attribute */
  136. int ast_speech_get_setting(struct ast_speech *speech, const char *name, char *buf, size_t len);
  137. /*! \brief Change the type of results we want */
  138. int ast_speech_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type);
  139. /*! \brief Change state of a speech structure */
  140. int ast_speech_change_state(struct ast_speech *speech, int state);
  141. /*! \brief Register a speech recognition engine */
  142. int ast_speech_register(struct ast_speech_engine *engine);
  143. /*! \brief Unregister a speech recognition engine */
  144. int ast_speech_unregister(const char *engine_name);
  145. #if defined(__cplusplus) || defined(c_plusplus)
  146. }
  147. #endif
  148. #endif /* _ASTERISK_SPEECH_H */