/native/external/espeak/src/speak_lib.h

http://eyes-free.googlecode.com/ · C Header · 599 lines · 186 code · 60 blank · 353 comment · 0 complexity · 6db1f5703123e5bc5a6ffd32e1f304b8 MD5 · raw file

  1. #ifndef SPEAK_LIB_H
  2. #define SPEAK_LIB_H
  3. /***************************************************************************
  4. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  5. * email: jonsd@users.sourceforge.net *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, see: *
  19. * <http://www.gnu.org/licenses/>. *
  20. ***************************************************************************/
  21. /*************************************************************/
  22. /* This is the header file for the library version of espeak */
  23. /* */
  24. /*************************************************************/
  25. #include <stdio.h>
  26. #include <wctype.h>
  27. #define ESPEAK_API_REVISION 3
  28. /*
  29. Revision 2
  30. Added parameter "options" to eSpeakInitialize()
  31. Revision 3
  32. Added espeakWORDGAP to espeak_PARAMETER
  33. Revision 4
  34. Added flags parameter to espeak_CompileDictionary()
  35. */
  36. /********************/
  37. /* Initialization */
  38. /********************/
  39. typedef enum {
  40. espeakEVENT_LIST_TERMINATED = 0, // Retrieval mode: terminates the event list.
  41. espeakEVENT_WORD = 1, // Start of word
  42. espeakEVENT_SENTENCE, // Start of sentence
  43. espeakEVENT_MARK, // Mark
  44. espeakEVENT_PLAY, // Audio element
  45. espeakEVENT_END, // End of sentence or clause
  46. espeakEVENT_MSG_TERMINATED, // End of message
  47. espeakEVENT_PHONEME // Phoneme, if enabled in espeak_Initialize()
  48. } espeak_EVENT_TYPE;
  49. typedef struct {
  50. espeak_EVENT_TYPE type;
  51. unsigned int unique_identifier; // message identifier (or 0 for key or character)
  52. int text_position; // the number of characters from the start of the text
  53. int length; // word length, in characters (for espeakEVENT_WORD)
  54. int audio_position; // the time in mS within the generated speech output data
  55. int sample; // sample id (internal use)
  56. void* user_data; // pointer supplied by the calling program
  57. union {
  58. int number; // used for WORD and SENTENCE events. For PHONEME events this is the phoneme mnemonic.
  59. const char *name; // used for MARK and PLAY events. UTF8 string
  60. } id;
  61. } espeak_EVENT;
  62. /*
  63. When a message is supplied to espeak_synth, the request is buffered and espeak_synth returns. When the message is really processed, the callback function will be repetedly called.
  64. In RETRIEVAL mode, the callback function supplies to the calling program the audio data and an event list terminated by 0 (LIST_TERMINATED).
  65. In PLAYBACK mode, the callback function is called as soon as an event happens.
  66. For example suppose that the following message is supplied to espeak_Synth:
  67. "hello, hello."
  68. * Once processed in RETRIEVAL mode, it could lead to 3 calls of the callback function :
  69. ** Block 1:
  70. <audio data> +
  71. List of events: SENTENCE + WORD + LIST_TERMINATED
  72. ** Block 2:
  73. <audio data> +
  74. List of events: WORD + END + LIST_TERMINATED
  75. ** Block 3:
  76. no audio data
  77. List of events: MSG_TERMINATED + LIST_TERMINATED
  78. * Once processed in PLAYBACK mode, it could lead to 5 calls of the callback function:
  79. ** SENTENCE
  80. ** WORD (call when the sounds are actually played)
  81. ** WORD
  82. ** END (call when the end of sentence is actually played.)
  83. ** MSG_TERMINATED
  84. The MSG_TERMINATED event is the last event. It can inform the calling program to clear the user data related to the message.
  85. So if the synthesis must be stopped, the callback function is called for each pending message with the MSG_TERMINATED event.
  86. A MARK event indicates a <mark> element in the text.
  87. A PLAY event indicates an <audio> element in the text, for which the calling program should play the named sound file.
  88. */
  89. typedef enum {
  90. POS_CHARACTER = 1,
  91. POS_WORD,
  92. POS_SENTENCE
  93. } espeak_POSITION_TYPE;
  94. typedef enum {
  95. /* PLAYBACK mode: plays the audio data, supplies events to the calling program*/
  96. AUDIO_OUTPUT_PLAYBACK,
  97. /* RETRIEVAL mode: supplies audio data and events to the calling program */
  98. AUDIO_OUTPUT_RETRIEVAL,
  99. /* SYNCHRONOUS mode: as RETRIEVAL but doesn't return until synthesis is completed */
  100. AUDIO_OUTPUT_SYNCHRONOUS,
  101. /* Synchronous playback */
  102. AUDIO_OUTPUT_SYNCH_PLAYBACK
  103. } espeak_AUDIO_OUTPUT;
  104. typedef enum {
  105. EE_OK=0,
  106. EE_INTERNAL_ERROR=-1,
  107. EE_BUFFER_FULL=1,
  108. EE_NOT_FOUND=2
  109. } espeak_ERROR;
  110. #ifdef __cplusplus
  111. extern "C"
  112. #endif
  113. int espeak_Initialize(espeak_AUDIO_OUTPUT output, int buflength, const char *path, int options);
  114. /* Must be called before any synthesis functions are called.
  115. output: the audio data can either be played by eSpeak or passed back by the SynthCallback function.
  116. buflength: The length in mS of sound buffers passed to the SynthCallback function.
  117. path: The directory which contains the espeak-data directory, or NULL for the default location.
  118. options: bit 0: 1=allow espeakEVENT_PHONEME events.
  119. Returns: sample rate in Hz, or -1 (EE_INTERNAL_ERROR).
  120. */
  121. typedef int (t_espeak_callback)(short*, int, espeak_EVENT*);
  122. #ifdef __cplusplus
  123. extern "C"
  124. #endif
  125. void espeak_SetSynthCallback(t_espeak_callback* SynthCallback);
  126. /* Must be called before any synthesis functions are called.
  127. This specifies a function in the calling program which is called when a buffer of
  128. speech sound data has been produced.
  129. The callback function is of the form:
  130. int SynthCallback(short *wav, int numsamples, espeak_EVENT *events);
  131. wav: is the speech sound data which has been produced.
  132. NULL indicates that the synthesis has been completed.
  133. numsamples: is the number of entries in wav. This number may vary, may be less than
  134. the value implied by the buflength parameter given in espeak_Initialize, and may
  135. sometimes be zero (which does NOT indicate end of synthesis).
  136. events: an array of espeak_EVENT items which indicate word and sentence events, and
  137. also the occurance if <mark> and <audio> elements within the text. The list of
  138. events is terminated by an event of type = 0.
  139. Callback returns: 0=continue synthesis, 1=abort synthesis.
  140. */
  141. #ifdef __cplusplus
  142. extern "C"
  143. #endif
  144. void espeak_SetUriCallback(int (*UriCallback)(int, const char*, const char*));
  145. /* This function may be called before synthesis functions are used, in order to deal with
  146. <audio> tags. It specifies a callback function which is called when an <audio> element is
  147. encountered and allows the calling program to indicate whether the sound file which
  148. is specified in the <audio> element is available and is to be played.
  149. The callback function is of the form:
  150. int UriCallback(int type, const char *uri, const char *base);
  151. type: type of callback event. Currently only 1= <audio> element
  152. uri: the "src" attribute from the <audio> element
  153. base: the "xml:base" attribute (if any) from the <speak> element
  154. Return: 1=don't play the sound, but speak the text alternative.
  155. 0=place a PLAY event in the event list at the point where the <audio> element
  156. occurs. The calling program can then play the sound at that point.
  157. */
  158. /********************/
  159. /* Synthesis */
  160. /********************/
  161. #define espeakCHARS_AUTO 0
  162. #define espeakCHARS_UTF8 1
  163. #define espeakCHARS_8BIT 2
  164. #define espeakCHARS_WCHAR 3
  165. #define espeakSSML 0x10
  166. #define espeakPHONEMES 0x100
  167. #define espeakENDPAUSE 0x1000
  168. #define espeakKEEP_NAMEDATA 0x2000
  169. #ifdef __cplusplus
  170. extern "C"
  171. #endif
  172. espeak_ERROR espeak_Synth(const void *text,
  173. size_t size,
  174. unsigned int position,
  175. espeak_POSITION_TYPE position_type,
  176. unsigned int end_position,
  177. unsigned int flags,
  178. unsigned int* unique_identifier,
  179. void* user_data);
  180. /* Synthesize speech for the specified text. The speech sound data is passed to the calling
  181. program in buffers by means of the callback function specified by espeak_SetSynthCallback(). The command is asynchronous: it is internally buffered and returns as soon as possible. If espeak_Initialize was previously called with AUDIO_OUTPUT_PLAYBACK as argument, the sound data are played by eSpeak.
  182. text: The text to be spoken, terminated by a zero character. It may be either 8-bit characters,
  183. wide characters (wchar_t), or UTF8 encoding. Which of these is determined by the "flags"
  184. parameter.
  185. size: Equal to (or greatrer than) the size of the text data, in bytes. This is used in order
  186. to allocate internal storage space for the text. This value is not used for
  187. AUDIO_OUTPUT_SYNCHRONOUS mode.
  188. position: The position in the text where speaking starts. Zero indicates speak from the
  189. start of the text.
  190. position_type: Determines whether "position" is a number of characters, words, or sentences.
  191. Values:
  192. end_position: If set, this gives a character position at which speaking will stop. A value
  193. of zero indicates no end position.
  194. flags: These may be OR'd together:
  195. Type of character codes, one of:
  196. espeakCHARS_UTF8 UTF8 encoding
  197. espeakCHARS_8BIT The 8 bit ISO-8859 character set for the particular language.
  198. espeakCHARS_AUTO 8 bit or UTF8 (this is the default)
  199. espeakCHARS_WCHAR Wide characters (wchar_t)
  200. espeakSSML Elements within < > are treated as SSML elements, or if not recognised are ignored.
  201. espeakPHONEMES Text within [[ ]] is treated as phonemes codes (in espeak's Hirshenbaum encoding).
  202. espeakENDPAUSE If set then a sentence pause is added at the end of the text. If not set then
  203. this pause is suppressed.
  204. unique_identifier: message identifier; helpful for identifying later
  205. data supplied to the callback.
  206. user_data: pointer which will be passed to the callback function.
  207. Return: EE_OK: operation achieved
  208. EE_BUFFER_FULL: the command can not be buffered;
  209. you may try after a while to call the function again.
  210. EE_INTERNAL_ERROR.
  211. */
  212. #ifdef __cplusplus
  213. extern "C"
  214. #endif
  215. espeak_ERROR espeak_Synth_Mark(const void *text,
  216. size_t size,
  217. const char *index_mark,
  218. unsigned int end_position,
  219. unsigned int flags,
  220. unsigned int* unique_identifier,
  221. void* user_data);
  222. /* Synthesize speech for the specified text. Similar to espeak_Synth() but the start position is
  223. specified by the name of a <mark> element in the text.
  224. index_mark: The "name" attribute of a <mark> element within the text which specified the
  225. point at which synthesis starts. UTF8 string.
  226. For the other parameters, see espeak_Synth()
  227. Return: EE_OK: operation achieved
  228. EE_BUFFER_FULL: the command can not be buffered;
  229. you may try after a while to call the function again.
  230. EE_INTERNAL_ERROR.
  231. */
  232. #ifdef __cplusplus
  233. extern "C"
  234. #endif
  235. espeak_ERROR espeak_Key(const char *key_name);
  236. /* Speak the name of a keyboard key.
  237. If key_name is a single character, it speaks the name of the character.
  238. Otherwise, it speaks key_name as a text string.
  239. Return: EE_OK: operation achieved
  240. EE_BUFFER_FULL: the command can not be buffered;
  241. you may try after a while to call the function again.
  242. EE_INTERNAL_ERROR.
  243. */
  244. #ifdef __cplusplus
  245. extern "C"
  246. #endif
  247. espeak_ERROR espeak_Char(wchar_t character);
  248. /* Speak the name of the given character
  249. Return: EE_OK: operation achieved
  250. EE_BUFFER_FULL: the command can not be buffered;
  251. you may try after a while to call the function again.
  252. EE_INTERNAL_ERROR.
  253. */
  254. /***********************/
  255. /* Speech Parameters */
  256. /***********************/
  257. typedef enum {
  258. espeakSILENCE=0, /* internal use */
  259. espeakRATE=1,
  260. espeakVOLUME=2,
  261. espeakPITCH=3,
  262. espeakRANGE=4,
  263. espeakPUNCTUATION=5,
  264. espeakCAPITALS=6,
  265. espeakWORDGAP=7,
  266. espeakOPTIONS=8, // reserved for misc. options. not yet used
  267. espeakINTONATION=9,
  268. espeakRESERVED1=10,
  269. espeakRESERVED2=11,
  270. espeakEMPHASIS, /* internal use */
  271. espeakLINELENGTH, /* internal use */
  272. espeakVOICETYPE, // internal, 1=mbrola
  273. N_SPEECH_PARAM /* last enum */
  274. } espeak_PARAMETER;
  275. typedef enum {
  276. espeakPUNCT_NONE=0,
  277. espeakPUNCT_ALL=1,
  278. espeakPUNCT_SOME=2
  279. } espeak_PUNCT_TYPE;
  280. #ifdef __cplusplus
  281. extern "C"
  282. #endif
  283. espeak_ERROR espeak_SetParameter(espeak_PARAMETER parameter, int value, int relative);
  284. /* Sets the value of the specified parameter.
  285. relative=0 Sets the absolute value of the parameter.
  286. relative=1 Sets a relative value of the parameter.
  287. parameter:
  288. espeakRATE: speaking speed in word per minute.
  289. espeakVOLUME: volume in range 0-100 0=silence
  290. espeakPITCH: base pitch, range 0-100. 50=normal
  291. espeakRANGE: pitch range, range 0-100. 0-monotone, 50=normal
  292. espeakPUNCTUATION: which punctuation characters to announce:
  293. value in espeak_PUNCT_TYPE (none, all, some),
  294. see espeak_GetParameter() to specify which characters are announced.
  295. espeakCAPITALS: announce capital letters by:
  296. 0=none,
  297. 1=sound icon,
  298. 2=spelling,
  299. 3 or higher, by raising pitch. This values gives the amount in Hz by which the pitch
  300. of a word raised to indicate it has a capital letter.
  301. espeakWORDGAP: pause between words, units of 10mS (at the default speed)
  302. Return: EE_OK: operation achieved
  303. EE_BUFFER_FULL: the command can not be buffered;
  304. you may try after a while to call the function again.
  305. EE_INTERNAL_ERROR.
  306. */
  307. #ifdef __cplusplus
  308. extern "C"
  309. #endif
  310. int espeak_GetParameter(espeak_PARAMETER parameter, int current);
  311. /* current=0 Returns the default value of the specified parameter.
  312. current=1 Returns the current value of the specified parameter, as set by SetParameter()
  313. */
  314. #ifdef __cplusplus
  315. extern "C"
  316. #endif
  317. espeak_ERROR espeak_SetPunctuationList(const wchar_t *punctlist);
  318. /* Specified a list of punctuation characters whose names are to be spoken when the
  319. value of the Punctuation parameter is set to "some".
  320. punctlist: A list of character codes, terminated by a zero character.
  321. Return: EE_OK: operation achieved
  322. EE_BUFFER_FULL: the command can not be buffered;
  323. you may try after a while to call the function again.
  324. EE_INTERNAL_ERROR.
  325. */
  326. #ifdef __cplusplus
  327. extern "C"
  328. #endif
  329. void espeak_SetPhonemeTrace(int value, FILE *stream);
  330. /* Controls the output of phoneme symbols for the text
  331. value=0 No phoneme output (default)
  332. value=1 Output the translated phoneme symbols for the text
  333. value=2 as (1), but also output a trace of how the translation was done (matching rules and list entries)
  334. stream output stream for the phoneme symbols (and trace). If stream=NULL then it uses stdout.
  335. */
  336. #ifdef __cplusplus
  337. extern "C"
  338. #endif
  339. void espeak_CompileDictionary(const char *path, FILE *log, int flags);
  340. /* Compile pronunciation dictionary for a language which corresponds to the currently
  341. selected voice. The required voice should be selected before calling this function.
  342. path: The directory which contains the language's '_rules' and '_list' files.
  343. 'path' should end with a path separator character ('/').
  344. log: Stream for error reports and statistics information. If log=NULL then stderr will be used.
  345. flags: Bit 0: include source line information for debug purposes (This is displayed with the
  346. -X command line option).
  347. */
  348. /***********************/
  349. /* Voice Selection */
  350. /***********************/
  351. // voice table
  352. typedef struct {
  353. const char *name; // a given name for this voice. UTF8 string.
  354. const char *languages; // list of pairs of (byte) priority + (string) language (and dialect qualifier)
  355. const char *identifier; // the filename for this voice within espeak-data/voices
  356. unsigned char gender; // 0=none 1=male, 2=female,
  357. unsigned char age; // 0=not specified, or age in years
  358. unsigned char variant; // only used when passed as a parameter to espeak_SetVoiceByProperties
  359. unsigned char xx1; // for internal use
  360. int score; // for internal use
  361. void *spare; // for internal use
  362. } espeak_VOICE;
  363. /* Note: The espeak_VOICE structure is used for two purposes:
  364. 1. To return the details of the available voices.
  365. 2. As a parameter to espeak_SetVoiceByProperties() in order to specify selection criteria.
  366. In (1), the "languages" field consists of a list of (UTF8) language names for which this voice
  367. may be used, each language name in the list is terminated by a zero byte and is also preceded by
  368. a single byte which gives a "priority" number. The list of languages is terminated by an
  369. additional zero byte.
  370. A language name consists of a language code, optionally followed by one or more qualifier (dialect)
  371. names separated by hyphens (eg. "en-uk"). A voice might, for example, have languages "en-uk" and
  372. "en". Even without "en" listed, voice would still be selected for the "en" language (because
  373. "en-uk" is related) but at a lower priority.
  374. The priority byte indicates how the voice is preferred for the language. A low number indicates a
  375. more preferred voice, a higher number indicates a less preferred voice.
  376. In (2), the "languages" field consists simply of a single (UTF8) language name, with no preceding
  377. priority byte.
  378. */
  379. #ifdef __cplusplus
  380. extern "C"
  381. #endif
  382. const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec);
  383. /* Reads the voice files from espeak-data/voices and creates an array of espeak_VOICE pointers.
  384. The list is terminated by a NULL pointer
  385. If voice_spec is NULL then all voices are listed.
  386. If voice spec is give, then only the voices which are compatible with the voice_spec
  387. are listed, and they are listed in preference order.
  388. */
  389. #ifdef __cplusplus
  390. extern "C"
  391. #endif
  392. espeak_ERROR espeak_SetVoiceByName(const char *name);
  393. /* Searches for a voice with a matching "name" field. Language is not considered.
  394. "name" is a UTF8 string.
  395. Return: EE_OK: operation achieved
  396. EE_BUFFER_FULL: the command can not be buffered;
  397. you may try after a while to call the function again.
  398. EE_INTERNAL_ERROR.
  399. */
  400. #ifdef __cplusplus
  401. extern "C"
  402. #endif
  403. espeak_ERROR espeak_SetVoiceByProperties(espeak_VOICE *voice_spec);
  404. /* An espeak_VOICE structure is used to pass criteria to select a voice. Any of the following
  405. fields may be set:
  406. name NULL, or a voice name
  407. languages NULL, or a single language string (with optional dialect), eg. "en-uk", or "en"
  408. gender 0=not specified, 1=male, 2=female
  409. age 0=not specified, or an age in years
  410. variant After a list of candidates is produced, scored and sorted, "variant" is used to index
  411. that list and choose a voice.
  412. variant=0 takes the top voice (i.e. best match). variant=1 takes the next voice, etc
  413. */
  414. #ifdef __cplusplus
  415. extern "C"
  416. #endif
  417. espeak_VOICE *espeak_GetCurrentVoice(void);
  418. /* Returns the espeak_VOICE data for the currently selected voice.
  419. This is not affected by temporary voice changes caused by SSML elements such as <voice> and <s>
  420. */
  421. #ifdef __cplusplus
  422. extern "C"
  423. #endif
  424. espeak_ERROR espeak_Cancel(void);
  425. /* Stop immediately synthesis and audio output of the current text. When this
  426. function returns, the audio output is fully stopped and the synthesizer is ready to
  427. synthesize a new message.
  428. Return: EE_OK: operation achieved
  429. EE_INTERNAL_ERROR.
  430. */
  431. #ifdef __cplusplus
  432. extern "C"
  433. #endif
  434. int espeak_IsPlaying(void);
  435. /* Returns 1 if audio is played, 0 otherwise.
  436. */
  437. #ifdef __cplusplus
  438. extern "C"
  439. #endif
  440. espeak_ERROR espeak_Synchronize(void);
  441. /* This function returns when all data have been spoken.
  442. Return: EE_OK: operation achieved
  443. EE_INTERNAL_ERROR.
  444. */
  445. #ifdef __cplusplus
  446. extern "C"
  447. #endif
  448. espeak_ERROR espeak_Terminate(void);
  449. /* last function to be called.
  450. Return: EE_OK: operation achieved
  451. EE_INTERNAL_ERROR.
  452. */
  453. #ifdef __cplusplus
  454. extern "C"
  455. #endif
  456. const char *espeak_Info(void* ptr);
  457. /* Returns the version number string.
  458. The parameter is for future use, and should be set to NULL
  459. */
  460. #endif