eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

speak_lib.h 20KB

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