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

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