#ifndef __ASTERISK_SPEECH_SPHINX_H
#define __ASTERISK_SPEECH_SPHINX_H 1
/* Asterisk-Sphinx Generic Speech API Engine
 * (c) 2009, Christopher Jansen
 *
 * Distributed under the GPL, or whatever license is Freest that Digium will allow.
 *
 */

/* Create/Destroy the Speech Engine itself */
int sphinx_create(struct ast_speech *speech);
int sphinx_destroy(struct ast_speech *speech);
/* Load/Unload a particular grammar 
 * - neither of these has any implementation; they do nothing. */
int sphinx_load(struct ast_speech *speech, char *grammar_name, char *grammar);
int sphinx_unload(struct ast_speech *speech, char *grammar_name);
/* Activate/Deactivate a particular grammar 
 * - valid grammars are determined by your settings on the Sphinx server. */
int sphinx_activate(struct ast_speech *speech, char *grammar_name);
/* Note: Deactivating a grammar has the side-effect of telling the engine 
 * this is its last chance to provide results (and doesn't actually deactive a grammar) */
int sphinx_deactivate(struct ast_speech *speech, char *grammar_name);
/* sphinx_write: send some data to engine for processing - data is required to be SLINEAR,
 * 8khz, which is OK since that's what Asterisk sends */
int sphinx_write(struct ast_speech *speech, void *data, int len);
/* sphinx_dtmf - unimplemented */
int sphinx_dtmf(struct ast_speech *speech, const char *dtmf);
/* sphinx_start: prepare to proces incoming speech data */
int sphinx_start(struct ast_speech *speech);
/* sphinx_change: unimplemented */
int sphinx_change(struct ast_speech *speech, char *name, const char *value);
/* sphinx_change_results_type: unimplemented */
int sphinx_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type);
/* sphinx_get: retrieve results */
struct ast_speech_result* sphinx_get(struct ast_speech *speech);

/* used to store "internal state" */
struct sphinx_state 
{
  int s;               // Socket connection to Sphinx Server
  int heardspeech;     // True if we have detected speech 
  int noiseframes;     // Number of consecutive non-silent frames
  int final;           // True if we have recieved final results
  struct ast_dsp *dsp; // Holds our silence-detection DSP
  char *sbuf;          // Data pending to send
  char *rbuf;          // Data pending to read
  int  preads;         // Number of outstanding requests
  int  prbytes;        // Bytes pending within a request
  int  rbufused;       // How full is rbuf?
  int  pwbytes;        // Bytes pending to write
};

/* Client and Server do not use the same header, but it's critical this structure is
 * identical between them.
 */
enum e_reqtype
{
  REQTYPE_GRAMMAR,
  REQTYPE_START,
  REQTYPE_DATA,
  REQTYPE_FINISH
};

/* Sphinx Server packet definition, basically */
struct sphinx_request
{
  int dlen;
  enum e_reqtype rtype;
  char *data;
};

#endif

