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.

388 lines
15 KiB

  1. From 8bec80dccc9f4fe147a500486813f4e89a0d56d8 Mon Sep 17 00:00:00 2001
  2. From: Mathieu Parent <math.parent@gmail.com>
  3. Date: Sun, 25 Oct 2009 15:19:01 +0100
  4. Subject: [PATCH 3/7] pico2wave: Convert text to .wav using svox text-to-speech system.
  5. ---
  6. pico/.gitignore | 1 +
  7. pico/Makefile.am | 7 +
  8. pico/bin/pico2wave.c | 341 ++++++++++++++++++++++++++++++++++++++++++++++++++
  9. pico/configure.in | 3 +
  10. 4 files changed, 352 insertions(+), 0 deletions(-)
  11. create mode 100644 pico/bin/pico2wave.c
  12. --- a/pico/.gitignore
  13. +++ b/pico/.gitignore
  14. @@ -29,4 +29,5 @@ libtool
  15. *.lo
  16. .libs
  17. libttspico.la
  18. +pico2wave
  19. --- a/pico/Makefile.am
  20. +++ b/pico/Makefile.am
  21. @@ -34,3 +34,10 @@ libttspico_la_SOURCES = \
  22. lib/picotrns.c \
  23. lib/picowa.c
  24. +bin_PROGRAMS = pico2wave
  25. +pico2wave_SOURCES = \
  26. + bin/pico2wave.c
  27. +pico2wave_LDADD = \
  28. + libttspico.la -lm -lpopt
  29. +pico2wave_CFLAGS = -Wall -I lib
  30. +
  31. --- /dev/null
  32. +++ b/pico/bin/pico2wave.c
  33. @@ -0,0 +1,342 @@
  34. +/* pico2wave.c
  35. +
  36. + * Copyright (C) 2009 Mathieu Parent <math.parent@gmail.com>
  37. + *
  38. + * Licensed under the Apache License, Version 2.0 (the "License");
  39. + * you may not use this file except in compliance with the License.
  40. + * You may obtain a copy of the License at
  41. + *
  42. + * http://www.apache.org/licenses/LICENSE-2.0
  43. + *
  44. + * Unless required by applicable law or agreed to in writing, software
  45. + * distributed under the License is distributed on an "AS IS" BASIS,
  46. + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  47. + * See the License for the specific language governing permissions and
  48. + * limitations under the License.
  49. + *
  50. + * Convert text to .wav using svox text-to-speech system.
  51. + *
  52. + */
  53. +
  54. +
  55. +#include <popt.h>
  56. +#include <stdio.h>
  57. +#include <stdint.h>
  58. +#include <stdlib.h>
  59. +#include <string.h>
  60. +
  61. +#include <picoapi.h>
  62. +#include <picoapid.h>
  63. +#include <picoos.h>
  64. +
  65. +
  66. +/* adaptation layer defines */
  67. +#define PICO_MEM_SIZE 2500000
  68. +#define DummyLen 100000000
  69. +
  70. +/* string constants */
  71. +#define MAX_OUTBUF_SIZE 128
  72. +const char * PICO_LINGWARE_PATH = "./lang/";
  73. +const char * PICO_VOICE_NAME = "PicoVoice";
  74. +
  75. +/* supported voices
  76. + Pico does not separately specify the voice and locale. */
  77. +const char * picoSupportedLangIso3[] = { "eng", "eng", "deu", "spa", "fra", "ita" };
  78. +const char * picoSupportedCountryIso3[] = { "USA", "GBR", "DEU", "ESP", "FRA", "ITA" };
  79. +const char * picoSupportedLang[] = { "en-US", "en-GB", "de-DE", "es-ES", "fr-FR", "it-IT" };
  80. +const char * picoInternalLang[] = { "en-US", "en-GB", "de-DE", "es-ES", "fr-FR", "it-IT" };
  81. +const char * picoInternalTaLingware[] = { "en-US_ta.bin", "en-GB_ta.bin", "de-DE_ta.bin", "es-ES_ta.bin", "fr-FR_ta.bin", "it-IT_ta.bin" };
  82. +const char * picoInternalSgLingware[] = { "en-US_lh0_sg.bin", "en-GB_kh0_sg.bin", "de-DE_gl0_sg.bin", "es-ES_zl0_sg.bin", "fr-FR_nk0_sg.bin", "it-IT_cm0_sg.bin" };
  83. +const char * picoInternalUtppLingware[] = { "en-US_utpp.bin", "en-GB_utpp.bin", "de-DE_utpp.bin", "es-ES_utpp.bin", "fr-FR_utpp.bin", "it-IT_utpp.bin" };
  84. +const int picoNumSupportedVocs = 6;
  85. +
  86. +/* adapation layer global variables */
  87. +void * picoMemArea = NULL;
  88. +pico_System picoSystem = NULL;
  89. +pico_Resource picoTaResource = NULL;
  90. +pico_Resource picoSgResource = NULL;
  91. +pico_Resource picoUtppResource = NULL;
  92. +pico_Engine picoEngine = NULL;
  93. +pico_Char * picoTaFileName = NULL;
  94. +pico_Char * picoSgFileName = NULL;
  95. +pico_Char * picoUtppFileName = NULL;
  96. +pico_Char * picoTaResourceName = NULL;
  97. +pico_Char * picoSgResourceName = NULL;
  98. +pico_Char * picoUtppResourceName = NULL;
  99. +int picoSynthAbort = 0;
  100. +
  101. +
  102. +int main(int argc, const char *argv[]) {
  103. + char * wavefile = NULL;
  104. + char * lang = "en-US";
  105. + int langIndex = -1, langIndexTmp = -1;
  106. + char * text;
  107. + int8_t * buffer;
  108. + size_t bufferSize = 256;
  109. +
  110. + /* Parsing options */
  111. + poptContext optCon; /* context for parsing command-line options */
  112. + int opt; /* used for argument parsing */
  113. +
  114. + struct poptOption optionsTable[] = {
  115. + { "wave", 'w', POPT_ARG_STRING, &wavefile, 0,
  116. + "Write output to this WAV file (extension SHOULD be .wav)", "filename.wav" },
  117. + { "lang", 'l', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT, &lang, 0,
  118. + "Language", "lang" },
  119. + POPT_AUTOHELP
  120. + POPT_TABLEEND
  121. + };
  122. + optCon = poptGetContext(NULL, argc, argv, optionsTable, POPT_CONTEXT_POSIXMEHARDER);
  123. + poptSetOtherOptionHelp(optCon, "<words>");
  124. +
  125. + /* Reporting about invalid extra options */
  126. + while ((opt = poptGetNextOpt(optCon)) != -1) {
  127. + switch (opt) {
  128. + default:
  129. + fprintf(stderr, "Invalid option %s: %s\n",
  130. + poptBadOption(optCon, 0), poptStrerror(opt));
  131. + poptPrintHelp(optCon, stderr, 0);
  132. + exit(1);
  133. + }
  134. + }
  135. +
  136. + /* Mandatory option: --wave */
  137. + if(!wavefile) {
  138. + fprintf(stderr, "Mandatory option: %s\n\n",
  139. + "--wave=filename.wav");
  140. + poptPrintHelp(optCon, stderr, 0);
  141. + exit(1);
  142. + }
  143. + /* option: --lang */
  144. + for(langIndexTmp =0; langIndexTmp<picoNumSupportedVocs; langIndexTmp++) {
  145. + if(!strcmp(picoSupportedLang[langIndexTmp], lang)) {
  146. + langIndex = langIndexTmp;
  147. + break;
  148. + }
  149. + }
  150. + if(langIndex == -1) {
  151. + fprintf(stderr, "Unknown language: %s\nValid languages:\n",
  152. + lang);
  153. + for(langIndexTmp =0; langIndexTmp<picoNumSupportedVocs; langIndexTmp++) {
  154. + fprintf(stderr, "%s\n", picoSupportedLang[langIndexTmp]);
  155. + }
  156. + lang = "en-US";
  157. + fprintf(stderr, "\n");
  158. + poptPrintHelp(optCon, stderr, 0);
  159. + exit(1);
  160. + }
  161. +
  162. + /* Remaining argument is <words> */
  163. + const char **extra_argv;
  164. + extra_argv = poptGetArgs(optCon);
  165. + if(extra_argv) {
  166. + text = (char *) &(*extra_argv)[0];
  167. + } else {
  168. + //TODO: stdin not supported yet.
  169. + fprintf(stderr, "Missing argument: %s\n\n",
  170. + "<words>");
  171. + poptPrintHelp(optCon, stderr, 0);
  172. + exit(1);
  173. + }
  174. +
  175. + poptFreeContext(optCon);
  176. +
  177. + buffer = malloc( bufferSize );
  178. +
  179. + int ret, getstatus;
  180. + pico_Char * inp = NULL;
  181. + pico_Char * local_text = NULL;
  182. + short outbuf[MAX_OUTBUF_SIZE/2];
  183. + pico_Int16 bytes_sent, bytes_recv, text_remaining, out_data_type;
  184. + pico_Retstring outMessage;
  185. +
  186. + picoSynthAbort = 0;
  187. +
  188. + picoMemArea = malloc( PICO_MEM_SIZE );
  189. + if((ret = pico_initialize( picoMemArea, PICO_MEM_SIZE, &picoSystem ))) {
  190. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  191. + fprintf(stderr, "Cannot initialize pico (%i): %s\n", ret, outMessage);
  192. + goto terminate;
  193. + }
  194. +
  195. + /* Load the text analysis Lingware resource file. */
  196. + picoTaFileName = (pico_Char *) malloc( PICO_MAX_DATAPATH_NAME_SIZE + PICO_MAX_FILE_NAME_SIZE );
  197. + strcpy((char *) picoTaFileName, PICO_LINGWARE_PATH);
  198. + strcat((char *) picoTaFileName, (const char *) picoInternalTaLingware[langIndex]);
  199. + if((ret = pico_loadResource( picoSystem, picoTaFileName, &picoTaResource ))) {
  200. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  201. + fprintf(stderr, "Cannot load text analysis resource file (%i): %s\n", ret, outMessage);
  202. + goto unloadTaResource;
  203. + }
  204. +
  205. + /* Load the signal generation Lingware resource file. */
  206. + picoSgFileName = (pico_Char *) malloc( PICO_MAX_DATAPATH_NAME_SIZE + PICO_MAX_FILE_NAME_SIZE );
  207. + strcpy((char *) picoSgFileName, PICO_LINGWARE_PATH);
  208. + strcat((char *) picoSgFileName, (const char *) picoInternalSgLingware[langIndex]);
  209. + if((ret = pico_loadResource( picoSystem, picoSgFileName, &picoSgResource ))) {
  210. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  211. + fprintf(stderr, "Cannot load signal generation Lingware resource file (%i): %s\n", ret, outMessage);
  212. + goto unloadSgResource;
  213. + }
  214. +
  215. + /* Load the utpp Lingware resource file if exists - NOTE: this file is optional
  216. + and is currently not used. Loading is only attempted for future compatibility.
  217. + If this file is not present the loading will still succeed. //
  218. + picoUtppFileName = (pico_Char *) malloc( PICO_MAX_DATAPATH_NAME_SIZE + PICO_MAX_FILE_NAME_SIZE );
  219. + strcpy((char *) picoUtppFileName, PICO_LINGWARE_PATH);
  220. + strcat((char *) picoUtppFileName, (const char *) picoInternalUtppLingware[langIndex]);
  221. + ret = pico_loadResource( picoSystem, picoUtppFileName, &picoUtppResource );
  222. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  223. + printf("pico_loadResource: %i: %s\n", ret, outMessage);
  224. + */
  225. +
  226. + /* Get the text analysis resource name. */
  227. + picoTaResourceName = (pico_Char *) malloc( PICO_MAX_RESOURCE_NAME_SIZE );
  228. + if((ret = pico_getResourceName( picoSystem, picoTaResource, (char *) picoTaResourceName ))) {
  229. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  230. + fprintf(stderr, "Cannot get the text analysis resource name (%i): %s\n", ret, outMessage);
  231. + goto unloadUtppResource;
  232. + }
  233. +
  234. + /* Get the signal generation resource name. */
  235. + picoSgResourceName = (pico_Char *) malloc( PICO_MAX_RESOURCE_NAME_SIZE );
  236. + if((ret = pico_getResourceName( picoSystem, picoSgResource, (char *) picoSgResourceName ))) {
  237. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  238. + fprintf(stderr, "Cannot get the signal generation resource name (%i): %s\n", ret, outMessage);
  239. + goto unloadUtppResource;
  240. + }
  241. +
  242. +
  243. + /* Create a voice definition. */
  244. + if((ret = pico_createVoiceDefinition( picoSystem, (const pico_Char *) PICO_VOICE_NAME ))) {
  245. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  246. + fprintf(stderr, "Cannot create voice definition (%i): %s\n", ret, outMessage);
  247. + goto unloadUtppResource;
  248. + }
  249. +
  250. + /* Add the text analysis resource to the voice. */
  251. + if((ret = pico_addResourceToVoiceDefinition( picoSystem, (const pico_Char *) PICO_VOICE_NAME, picoTaResourceName ))) {
  252. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  253. + fprintf(stderr, "Cannot add the text analysis resource to the voice (%i): %s\n", ret, outMessage);
  254. + goto unloadUtppResource;
  255. + }
  256. +
  257. + /* Add the signal generation resource to the voice. */
  258. + if((ret = pico_addResourceToVoiceDefinition( picoSystem, (const pico_Char *) PICO_VOICE_NAME, picoSgResourceName ))) {
  259. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  260. + fprintf(stderr, "Cannot add the signal generation resource to the voice (%i): %s\n", ret, outMessage);
  261. + goto unloadUtppResource;
  262. + }
  263. +
  264. + /* Create a new Pico engine. */
  265. + if((ret = pico_newEngine( picoSystem, (const pico_Char *) PICO_VOICE_NAME, &picoEngine ))) {
  266. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  267. + fprintf(stderr, "Cannot create a new pico engine (%i): %s\n", ret, outMessage);
  268. + goto disposeEngine;
  269. + }
  270. +
  271. + local_text = (pico_Char *) text ;
  272. + text_remaining = strlen((const char *) local_text) + 1;
  273. +
  274. + inp = (pico_Char *) local_text;
  275. +
  276. + size_t bufused = 0;
  277. +
  278. + picoos_Common common = (picoos_Common) pico_sysGetCommon(picoSystem);
  279. +
  280. + picoos_SDFile sdOutFile = NULL;
  281. +
  282. + picoos_bool done = TRUE;
  283. + if(TRUE != (done = picoos_sdfOpenOut(common, &sdOutFile,
  284. + (picoos_char *) wavefile, SAMPLE_FREQ_16KHZ, PICOOS_ENC_LIN)))
  285. + {
  286. + fprintf(stderr, "Cannot open output wave file\n");
  287. + ret = 1;
  288. + goto disposeEngine;
  289. + }
  290. +
  291. + /* synthesis loop */
  292. + while (text_remaining) {
  293. + /* Feed the text into the engine. */
  294. + if((ret = pico_putTextUtf8( picoEngine, inp, text_remaining, &bytes_sent ))) {
  295. + pico_getSystemStatusMessage(picoSystem, ret, outMessage);
  296. + fprintf(stderr, "Cannot put Text (%i): %s\n", ret, outMessage);
  297. + goto disposeEngine;
  298. + }
  299. +
  300. + text_remaining -= bytes_sent;
  301. + inp += bytes_sent;
  302. +
  303. + do {
  304. + if (picoSynthAbort) {
  305. + goto disposeEngine;
  306. + }
  307. + /* Retrieve the samples and add them to the buffer. */
  308. + getstatus = pico_getData( picoEngine, (void *) outbuf,
  309. + MAX_OUTBUF_SIZE, &bytes_recv, &out_data_type );
  310. + if((getstatus !=PICO_STEP_BUSY) && (getstatus !=PICO_STEP_IDLE)){
  311. + pico_getSystemStatusMessage(picoSystem, getstatus, outMessage);
  312. + fprintf(stderr, "Cannot get Data (%i): %s\n", getstatus, outMessage);
  313. + goto disposeEngine;
  314. + }
  315. + if (bytes_recv) {
  316. + if ((bufused + bytes_recv) <= bufferSize) {
  317. + memcpy(buffer+bufused, (int8_t *) outbuf, bytes_recv);
  318. + bufused += bytes_recv;
  319. + } else {
  320. + done = picoos_sdfPutSamples(
  321. + sdOutFile,
  322. + bufused / 2,
  323. + (picoos_int16*) (buffer));
  324. + bufused = 0;
  325. + memcpy(buffer, (int8_t *) outbuf, bytes_recv);
  326. + bufused += bytes_recv;
  327. + }
  328. + }
  329. + } while (PICO_STEP_BUSY == getstatus);
  330. + /* This chunk of synthesis is finished; pass the remaining samples. */
  331. + if (!picoSynthAbort) {
  332. + done = picoos_sdfPutSamples(
  333. + sdOutFile,
  334. + bufused / 2,
  335. + (picoos_int16*) (buffer));
  336. + }
  337. + picoSynthAbort = 0;
  338. + }
  339. +
  340. + if(TRUE != (done = picoos_sdfCloseOut(common, &sdOutFile)))
  341. + {
  342. + fprintf(stderr, "Cannot close output wave file\n");
  343. + ret = 1;
  344. + goto disposeEngine;
  345. + }
  346. +
  347. +disposeEngine:
  348. + if (picoEngine) {
  349. + pico_disposeEngine( picoSystem, &picoEngine );
  350. + pico_releaseVoiceDefinition( picoSystem, (pico_Char *) PICO_VOICE_NAME );
  351. + picoEngine = NULL;
  352. + }
  353. +unloadUtppResource:
  354. + if (picoUtppResource) {
  355. + pico_unloadResource( picoSystem, &picoUtppResource );
  356. + picoUtppResource = NULL;
  357. + }
  358. +unloadSgResource:
  359. + if (picoSgResource) {
  360. + pico_unloadResource( picoSystem, &picoSgResource );
  361. + picoSgResource = NULL;
  362. + }
  363. +unloadTaResource:
  364. + if (picoTaResource) {
  365. + pico_unloadResource( picoSystem, &picoTaResource );
  366. + picoTaResource = NULL;
  367. + }
  368. +terminate:
  369. + if (picoSystem) {
  370. + pico_terminate(&picoSystem);
  371. + picoSystem = NULL;
  372. + }
  373. + exit(ret);
  374. +}
  375. +
  376. --- a/pico/configure.in
  377. +++ b/pico/configure.in
  378. @@ -14,3 +14,6 @@ AC_CONFIG_FILES([Makefile])
  379. AC_OUTPUT
  380. AC_CONFIG_MACRO_DIR([m4])
  381. +
  382. +AC_CHECK_LIB(popt, poptGetContext)
  383. +