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

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