Advertisement
Guest User

Picospeech

a guest
Feb 14th, 2014
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. #include "TtsEngine.h"
  8.  
  9. #define OUTPUT_BUFFER_SIZE (128 * 1024)
  10.  
  11. using namespace android;
  12.  
  13. static bool synthesis_complete = false;
  14.  
  15. static FILE *outfp = stdout;
  16.  
  17. // @param [inout] void *&       - The userdata pointer set in the original
  18. //                                 synth call
  19. // @param [in]    uint32_t      - Track sampling rate in Hz
  20. // @param [in] tts_audio_format - The audio format
  21. // @param [in]    int           - The number of channels
  22. // @param [inout] int8_t *&     - A buffer of audio data only valid during the
  23. //                                execution of the callback
  24. // @param [inout] size_t  &     - The size of the buffer
  25. // @param [in] tts_synth_status - indicate whether the synthesis is done, or
  26. //                                 if more data is to be synthesized.
  27. // @return TTS_CALLBACK_HALT to indicate the synthesis must stop,
  28. //         TTS_CALLBACK_CONTINUE to indicate the synthesis must continue if
  29. //            there is more data to produce.
  30. tts_callback_status synth_done(void *& userdata, uint32_t sample_rate,
  31.         tts_audio_format audio_format, int channels, int8_t *& data, size_t& size, tts_synth_status status)
  32. {
  33.     //fprintf(stderr, "TTS callback, rate: %d, data size: %d, status: %i\n", sample_rate, size, status);
  34.  
  35.     if (status == TTS_SYNTH_DONE)
  36.     {
  37.         synthesis_complete = true;
  38.     }
  39.  
  40.     if ((size == OUTPUT_BUFFER_SIZE) || (status == TTS_SYNTH_DONE))
  41.     {
  42.         fwrite(data, size, 1, outfp);
  43.     }
  44.  
  45.     return TTS_CALLBACK_CONTINUE;
  46. }
  47.  
  48. static void usage(void)
  49. {
  50.     fprintf(stderr, "\nUsage:\n\n" \
  51.                     "testtts [-l lang] [-o filename] \"Text to speak\"\n\n" \
  52.                     "  -o\tFile to write audio to (default stdout)\n" \
  53.                         "  -l\tLanguage (en,us,de,fr,es,it)\n" );
  54.     exit(0);
  55. }
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59.     tts_result result;
  60.     TtsEngine* ttsEngine = getTtsEngine();
  61.     uint8_t* synthBuffer;
  62.     char* synthInput = NULL;
  63.     int currentOption;
  64.     char* outputFilename = NULL;
  65.  
  66.     int lang = 0;
  67.     char* langName = NULL;
  68.     static const char* languages[6][3] = {
  69.         {"en","eng","GBR"},
  70.         {"us","eng","US"},
  71.         {"de","deu","DEU"},
  72.         {"fr","fra","FRA"},
  73.         {"es","spa","ESP"},
  74.         {"it","ita","ITA"}
  75.     };
  76.  
  77.     fprintf(stderr, "Pico TTS Test App\n");
  78.  
  79.     if (argc == 1)
  80.     {
  81.         usage();
  82.     }
  83.  
  84.     while ( (currentOption = getopt(argc, argv, "o:l:h")) != -1)
  85.     {
  86.         switch (currentOption)
  87.         {
  88.         case 'o':
  89.             outputFilename = optarg;
  90.             fprintf(stderr, "Output audio to file '%s'\n", outputFilename);
  91.             break;
  92.         case 'l':
  93.                         {
  94.                             langName = optarg;
  95.                             int lnum = sizeof(languages)/sizeof(*languages);
  96.                             for( int i=0; i<lnum; ++i){
  97.                                 if( strcmp(langName, languages[i][0]) == 0 ) lang = i;
  98.                             }
  99.                         }
  100.             break;
  101.         case 'h':
  102.             usage();
  103.             break;
  104.                 case '?':
  105.                         if (optopt == 'l' || optopt == 'o')
  106.                             fprintf (stderr, "Option -%c requires an argument.\n", optopt);
  107.                 default:
  108.                         printf ("Getopt returned character code 0%o ??\n", currentOption);
  109.         }
  110.     }
  111.  
  112.     fprintf(stderr, "Language: '%s'\n", languages[lang][0] );
  113.  
  114.     if (optind < argc)
  115.     {
  116.         synthInput = argv[optind];
  117.     }
  118.  
  119.     if (!synthInput)
  120.     {
  121.         fprintf(stderr, "Error: no input string\n");
  122.         usage();
  123.             exit(1);
  124.     }
  125.  
  126.     fprintf(stderr, "Input string: \"%s\"\n", synthInput);
  127.  
  128.     synthBuffer = new uint8_t[OUTPUT_BUFFER_SIZE];
  129.  
  130.     result = ttsEngine->init(synth_done, "../lang/");
  131.  
  132.     if (result != TTS_SUCCESS)
  133.     {
  134.         fprintf(stderr, "Failed to init TTS\n");
  135.     }
  136.  
  137.     // Set language which was optional given by -l flag
  138.     result = ttsEngine->setLanguage(languages[lang][1], languages[lang][2], "");
  139.  
  140.     if (result != TTS_SUCCESS)
  141.     {
  142.         fprintf(stderr, "Failed to load language\n");
  143.     }
  144.  
  145.     if (outputFilename)
  146.     {
  147.         outfp = fopen(outputFilename, "wb");
  148.     }
  149.  
  150.     fprintf(stderr, "Synthesising text...\n");
  151.  
  152.     result = ttsEngine->synthesizeText(argv[argc-1], synthBuffer, OUTPUT_BUFFER_SIZE, NULL);
  153.  
  154.     if (result != TTS_SUCCESS)
  155.     {
  156.         fprintf(stderr, "Failed to synth text\n");
  157.     }
  158.  
  159.     while(!synthesis_complete)
  160.     {
  161.         usleep(100);
  162.     }
  163.  
  164.     fprintf(stderr, "Completed.\n");
  165.  
  166.     if (outputFilename)
  167.     {
  168.         fclose(outfp);
  169.     }
  170.  
  171.     result = ttsEngine->shutdown();
  172.  
  173.     if (result != TTS_SUCCESS)
  174.     {
  175.         fprintf(stderr, "Failed to shutdown TTS\n");
  176.     }
  177.  
  178.     delete [] synthBuffer;
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement