Search code examples
text-to-speechibm-cloudibm-watson

How do I call the synthesize method of the TextToSpeech service within the java-sdk API?


The API documentation can be found here: https://github.com/watson-developer-cloud/java-sdk

When I try and use the service, it authenticates properly and then fails on the synthesize method.

 TextToSpeech tts_service = new TextToSpeech();
 tts_service.setUsernameAndPassword("<username>", "<password>");

 tts_service.synthesize("The cat sat on the mat", Voice.EN_LISA, "audio/ogg; codecs=opus");

The stack trace for the error is shown below. I have tried the synthesize method without the voice and format arguments too (since it has a default) but the service fails with the same error when I do this.

Nov 25, 2015 4:58:55 PM com.ibm.watson.developer_cloud.service.WatsonService execute SEVERE: https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=The%20cat%20sat%20on%20the%20mat&voice=en-US_LisaVoice&Accept=audio%2Fogg%3B%20codecs%3Dopus, status: 400, error: The argument(s) [u'Accept'] are not allowed. Nov 25, 2015 4:58:55 PM com.vaadin.server.DefaultErrorHandler doDefault SEVERE: com.ibm.watson.developer_cloud.service.BadRequestException: The argument(s) [u'Accept'] are not allowed. at com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:128) at com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech.synthesize(TextToSpeech.java:119)

I'd appreciate some help with this please and want to use the java API rather than the REST calls.

Thanks.


Solution

  • The Java-SDK is sending Accept instead of accept(rocky mistake).

    A workaround until our next release is to extend the TextToSpeech class and override:

    InputStream synthesize(final String text, final Voice voice, final String outputFormat)
    

    Example:

    public class TextToSpeechHotFix extends TextToSpeech {
    
      @Override
      public InputStream synthesize(final String text, final Voice voice, final String outputFormat) {
        final RequestBuilder request = RequestBuilder.get(PATH_SYNTHESIZE);
        request.withQuery(TEXT, text);
        request.withQuery(VOICE, voice.getName());
    
        if (outputFormat != null && !outputFormat.startsWith("audio/"))
          throw new IllegalArgumentException(
              "format needs to be an audio mime type, for example: audio/wav or audio/ogg; codecs=opus");
    
        request.withQuery(ACCEPT, outputFormat != null ? outputFormat : HttpMediaType.AUDIO_WAV);
    
        final Response response = execute(request.build());
        return ResponseUtil.getInputStream(response);
      }
    }