Search in sources :

Example 6 with SynthesizeOptions

use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TranslateAndSynthesizeExample method main.

public static void main(String[] args) throws IOException {
    LanguageTranslator translator = new LanguageTranslator();
    translator.setUsernameAndPassword("username", "password");
    TextToSpeech synthesizer = new TextToSpeech();
    synthesizer.setUsernameAndPassword("username", "password");
    String text = "Greetings from Watson Developer Cloud";
    // translate
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).source(Language.ENGLISH).target(Language.SPANISH).build();
    TranslationResult translationResult = service.translate(translateOptions).execute();
    String translation = translationResult.getTranslations().get(0).getTranslation();
    // synthesize
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(translation).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
    InputStream in = service.synthesize(synthesizeOptions).execute();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
}
Also used : InputStream(java.io.InputStream) TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult) File(java.io.File) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)

Example 7 with SynthesizeOptions

use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class CustomizationsIT method testSynthesize.

/**
 * Test synthesize.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testSynthesize() throws IOException {
    model = createVoiceModel();
    final Word expected = instantiateWords().get(0);
    AddWordOptions addOptions = new AddWordOptions.Builder().word(expected.getWord()).translation(expected.getTranslation()).customizationId(model.getCustomizationId()).build();
    service.addWord(addOptions).execute();
    SynthesizeOptions synthesizeOptions1 = new SynthesizeOptions.Builder().text(expected.getWord()).voice(SynthesizeOptions.Voice.EN_US_MICHAELVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
    final InputStream stream1 = service.synthesize(synthesizeOptions1).execute();
    SynthesizeOptions synthesizeOptions2 = new SynthesizeOptions.Builder().text(expected.getWord()).voice(SynthesizeOptions.Voice.EN_US_MICHAELVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).customizationId(model.getCustomizationId()).build();
    final InputStream stream2 = service.synthesize(synthesizeOptions2).execute();
    assertFalse(TestUtils.streamContentEquals(stream1, stream2));
}
Also used : AddWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions) Word(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word) InputStream(java.io.InputStream) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 8 with SynthesizeOptions

use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TextToSpeechIT method testSynthesizeAndFixHeader.

/**
 * Test the fix wave header not having the size due to be streamed.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws UnsupportedAudioFileException the unsupported audio file exception
 */
@Test
public void testSynthesizeAndFixHeader() throws IOException, UnsupportedAudioFileException {
    String text = "one two three four five";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
    InputStream result = service.synthesize(synthesizeOptions).execute();
    assertNotNull(result);
    result = WaveUtils.reWriteWaveHeader(result);
    File tempFile = File.createTempFile("output", ".wav");
    writeInputStreamToFile(result, tempFile);
    assertNotNull(AudioSystem.getAudioFileFormat(tempFile));
}
Also used : InputStream(java.io.InputStream) File(java.io.File) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 9 with SynthesizeOptions

use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TextToSpeechIT method testSynthesize.

/**
 * Synthesize text and write it to a temporary file.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testSynthesize() throws IOException {
    String text = "This is an integration test; 1,2 !, @, #, $, %, ^, 20.";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
    InputStream result = service.synthesize(synthesizeOptions).execute();
    writeInputStreamToFile(result, File.createTempFile("tts-audio", "wav"));
}
Also used : InputStream(java.io.InputStream) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 10 with SynthesizeOptions

use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TextToSpeechTest method testSynthesize.

/**
 * Test synthesize.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@SuppressWarnings("resource")
@Test
public void testSynthesize() throws IOException, InterruptedException {
    final File audio = new File("src/test/resources/text_to_speech/sample1.wav");
    final Buffer buffer = new Buffer().write(Files.toByteArray(audio));
    server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.AUDIO_WAV).setBody(buffer));
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(HttpMediaType.AUDIO_PCM + "; rate=16000").build();
    final InputStream in = service.synthesize(synthesizeOptions).execute();
    final RecordedRequest request = server.takeRequest();
    final HttpUrl requestUrl = HttpUrl.parse("http://www.example.com" + request.getPath());
    assertEquals(request.getBody().readUtf8(), "{\"text\":\"" + text + "\"}");
    assertEquals(SYNTHESIZE_PATH, requestUrl.encodedPath());
    assertEquals(SynthesizeOptions.Voice.EN_US_LISAVOICE, requestUrl.queryParameter("voice"));
    assertEquals(HttpMediaType.AUDIO_PCM + "; rate=16000", request.getHeader("Accept"));
    assertNotNull(in);
    writeInputStreamToOutputStream(in, new FileOutputStream("build/output.wav"));
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpUrl(okhttp3.HttpUrl) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)13 File (java.io.File)9 Test (org.junit.Test)9 SynthesizeOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)8 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)7 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)4 FileOutputStream (java.io.FileOutputStream)4 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 FileInputStream (java.io.FileInputStream)3 HttpUrl (okhttp3.HttpUrl)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 AddWordOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions)2 Word (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word)2 BaseSynthesizeCallback (com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback)2 Buffer (okio.Buffer)2 TranslationResult (com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult)1