Search in sources :

Example 1 with SynthesizeOptions

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

the class CustomizationExample method main.

public static void main(String[] args) throws IOException {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    TextToSpeech service = new TextToSpeech(authenticator);
    // create custom voice model.
    CreateCustomModelOptions createOptions = new CreateCustomModelOptions.Builder().name("my model").language("en-US").description("the model for testing").build();
    CustomModel customVoiceModel = service.createCustomModel(createOptions).execute().getResult();
    System.out.println(customVoiceModel);
    // list custom voice models for US English.
    ListCustomModelsOptions listOptions = new ListCustomModelsOptions.Builder().language("en-US").build();
    CustomModels customVoiceModels = service.listCustomModels(listOptions).execute().getResult();
    System.out.println(customVoiceModels);
    // update custom voice model.
    String newName = "my updated model";
    UpdateCustomModelOptions updateOptions = new UpdateCustomModelOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).name(newName).description("the updated model for testing").build();
    service.updateCustomModel(updateOptions).execute();
    // list custom voice models regardless of language.
    customVoiceModels = service.listCustomModels().execute().getResult();
    System.out.println(customVoiceModels);
    // create multiple custom word translations
    Word word1 = new Word.Builder().word("hodor").translation("hold the door").build();
    Word word2 = new Word.Builder().word("plz").translation("please").build();
    List<Word> words = Arrays.asList(word1, word2);
    AddWordsOptions addOptions = new AddWordsOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).words(words).build();
    service.addWords(addOptions).execute();
    // create a single custom word translation
    AddWordOptions addWordOptions = new AddWordOptions.Builder().word("nat").translation("and that").customizationId(customVoiceModel.getCustomizationId()).build();
    service.addWord(addWordOptions).execute();
    // get custom word translations
    ListWordsOptions listWordsOptions = new ListWordsOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).build();
    Words customWords = service.listWords(listWordsOptions).execute().getResult();
    System.out.println(customWords);
    // get custom word translation
    GetWordOptions getOptions = new GetWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word("hodor").build();
    Translation translation = service.getWord(getOptions).execute().getResult();
    System.out.println(translation);
    // synthesize with custom voice model
    String text = "plz hodor";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_MICHAELVOICE).accept(HttpMediaType.AUDIO_WAV).customizationId(customVoiceModel.getCustomizationId()).build();
    InputStream in = service.synthesize(synthesizeOptions).execute().getResult();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
    // delete custom words with object and string
    DeleteWordOptions deleteOptions1 = new DeleteWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word(word1.word()).build();
    service.deleteWord(deleteOptions1).execute();
    DeleteWordOptions deleteOptions2 = new DeleteWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word(word2.word()).build();
    service.deleteWord(deleteOptions2).execute();
    // delete custom voice model
    DeleteCustomModelOptions deleteOptions = new DeleteCustomModelOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).build();
    service.deleteCustomModel(deleteOptions).execute();
    // list custom voice models regardless of language.
    customVoiceModels = service.listCustomModels().execute().getResult();
    System.out.println(customVoiceModels);
}
Also used : CreateCustomModelOptions(com.ibm.watson.text_to_speech.v1.model.CreateCustomModelOptions) Word(com.ibm.watson.text_to_speech.v1.model.Word) CustomModels(com.ibm.watson.text_to_speech.v1.model.CustomModels) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) AddWordOptions(com.ibm.watson.text_to_speech.v1.model.AddWordOptions) DeleteWordOptions(com.ibm.watson.text_to_speech.v1.model.DeleteWordOptions) ListCustomModelsOptions(com.ibm.watson.text_to_speech.v1.model.ListCustomModelsOptions) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions) Translation(com.ibm.watson.text_to_speech.v1.model.Translation) InputStream(java.io.InputStream) GetWordOptions(com.ibm.watson.text_to_speech.v1.model.GetWordOptions) CustomModel(com.ibm.watson.text_to_speech.v1.model.CustomModel) AddWordsOptions(com.ibm.watson.text_to_speech.v1.model.AddWordsOptions) Words(com.ibm.watson.text_to_speech.v1.model.Words) ListWordsOptions(com.ibm.watson.text_to_speech.v1.model.ListWordsOptions) UpdateCustomModelOptions(com.ibm.watson.text_to_speech.v1.model.UpdateCustomModelOptions) DeleteCustomModelOptions(com.ibm.watson.text_to_speech.v1.model.DeleteCustomModelOptions) File(java.io.File)

Example 2 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(HttpMediaType.AUDIO_WAV).build();
    InputStream result = service.synthesize(synthesizeOptions).execute().getResult();
    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.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 3 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(HttpMediaType.AUDIO_WAV).build();
    InputStream result = service.synthesize(synthesizeOptions).execute().getResult();
    writeInputStreamToFile(result, File.createTempFile("tts-audio", "wav"));
}
Also used : InputStream(java.io.InputStream) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 4 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 testSynthesizeUsingWebSocket.

/**
 * Test synthesize using web socket.
 *
 * @throws InterruptedException the interrupted exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testSynthesizeUsingWebSocket() throws InterruptedException, IOException {
    String basicText = "One taught me love. One taught me patience, and one taught me pain. Now, I'm so amazing. Say " + "I've loved and I've lost, but that's not what I see. So, look what I got." + " Look what you taught me. And for that, I say... thank u, next.";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(basicText).voice(SynthesizeOptions.Voice.EN_US_ALLISONVOICE).accept(HttpMediaType.AUDIO_OGG).timings(Collections.singletonList("words")).build();
    service.synthesizeUsingWebSocket(synthesizeOptions, new BaseSynthesizeCallback() {

        @Override
        public void onContentType(String contentType) {
            returnedContentType = contentType;
        }

        @Override
        public void onAudioStream(byte[] bytes) {
            // build byte array of synthesized text
            try {
                byteArrayOutputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onTimings(Timings timings) {
            returnedTimings.add(timings);
        }
    });
    // wait for synthesis to complete
    lock.await(5, TimeUnit.SECONDS);
    String filename = "synthesize_websocket_test.ogg";
    OutputStream fileOutputStream = new FileOutputStream(filename);
    byteArrayOutputStream.writeTo(fileOutputStream);
    File createdFile = new File(filename);
    assertTrue(createdFile.exists());
    assertTrue(returnedContentType.contains("audio/ogg"));
    for (Timings t : returnedTimings) {
        List<WordTiming> wordTimings = t.getWords();
        for (WordTiming wordTiming : wordTimings) {
            assertTrue(basicText.contains(wordTiming.getWord()));
        }
    }
    // clean up
    byteArrayOutputStream.close();
    fileOutputStream.close();
    if (createdFile.delete()) {
        System.out.println("File deleted successfully!");
    } else {
        System.out.println("File could not be deleted");
    }
}
Also used : Timings(com.ibm.watson.text_to_speech.v1.model.Timings) BaseSynthesizeCallback(com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) WordTiming(com.ibm.watson.text_to_speech.v1.model.WordTiming) File(java.io.File) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 5 with SynthesizeOptions

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

the class CustomizationExample method main.

public static void main(String[] args) throws IOException {
    TextToSpeech service = new TextToSpeech("<username>", "<password>");
    // create custom voice model.
    CreateVoiceModelOptions createOptions = new CreateVoiceModelOptions.Builder().name("my model").language("en-US").description("the model for testing").build();
    VoiceModel customVoiceModel = service.createVoiceModel(createOptions).execute();
    System.out.println(customVoiceModel);
    // list custom voice models for US English.
    ListVoiceModelsOptions listOptions = new ListVoiceModelsOptions.Builder().language("en-US").build();
    VoiceModels customVoiceModels = service.listVoiceModels(listOptions);
    System.out.println(customVoiceModels);
    // update custom voice model.
    UpdateVoiceModelOptions updateOptions = new UpdateVoiceModelOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).name(newName).description("the updated model for testing").build();
    service.updateVoiceModel(updateOptions).execute();
    // list custom voice models regardless of language.
    customVoiceModels = service.listVoiceModels().execute();
    System.out.println(customVoiceModels);
    // create multiple custom word translations
    Word word1 = new Word();
    word1.setWord("hodor");
    word1.setTranslation("hold the door");
    Word word2 = new Word();
    word2.setWord("plz");
    word2.setTranslation("please");
    List<Word> words = Arrays.asList(word1, word2);
    AddWordsOptions addOptions = new AddWordsOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).words(words).build();
    service.addWords(addOptions).execute();
    // create a single custom word translation
    AddWordOptions addOptions = new AddWordOptions.Builder().word("nat").translation("and that").customizationId(customVoiceModel.getCustomizationId()).build();
    service.addWord(addOptions).execute();
    // get custom word translations
    ListWordsOptions listOptions = new ListWordsOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).build();
    Words words = service.listWords(listOptions).execute();
    System.out.println(words);
    // get custom word translation
    GetWordOptions getOptions = new GetWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word("hodor").build();
    Translation translation = service.getWord(getOptions).execute();
    System.out.println(translation);
    // synthesize with custom voice model
    String text = "plz hodor";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_MICHAELVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).customizationId(customVoiceModel.getCustomizationId()).build();
    InputStream in = service.synthesize(synthesizeOptions).execute();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
    // delete custom words with object and string
    DeleteWordOptions deleteOptions1 = new DeleteWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word(word1.getWord()).build();
    service.deleteWord(deleteOptions1).execute();
    DeleteWordOptions deleteOptions2 = new DeleteWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word(word2.getWord()).build();
    service.deleteWord(deleteOptions2).execute();
    // delete custom voice model
    DeleteVoiceModelOptions deleteOptions = new DeleteVoiceModelOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).build();
    service.deleteVoiceModel(deleteOptions).execute();
    // list custom voice models regardless of language.
    customVoiceModels = service.listVoiceModels().execute();
    System.out.println(customVoiceModels);
}
Also used : Word(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word) UpdateVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.UpdateVoiceModelOptions) CreateVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.CreateVoiceModelOptions) VoiceModels(com.ibm.watson.developer_cloud.text_to_speech.v1.model.VoiceModels) AddWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions) ListVoiceModelsOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListVoiceModelsOptions) DeleteWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteWordOptions) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) Translation(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Translation) DeleteVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteVoiceModelOptions) InputStream(java.io.InputStream) GetWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetWordOptions) VoiceModel(com.ibm.watson.developer_cloud.text_to_speech.v1.model.VoiceModel) AddWordsOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordsOptions) Words(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Words) ListWordsOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListWordsOptions) File(java.io.File)

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