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.

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