Search in sources :

Example 1 with Voice

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

the class TextToSpeech method getVoice.

/**
 * Retrieves a specific voice available for speech synthesis.
 *
 * Lists information about the voice specified with the `voice` path parameter. Specify the `customization_id` query
 * parameter to obtain information for that custom voice model of the specified voice. Use the `GET /v1/voices` method
 * to see a list of all available voices.
 *
 * @param getVoiceOptions the {@link GetVoiceOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Voice}
 */
public ServiceCall<Voice> getVoice(GetVoiceOptions getVoiceOptions) {
    Validator.notNull(getVoiceOptions, "getVoiceOptions cannot be null");
    String[] pathSegments = { "v1/voices" };
    String[] pathParameters = { getVoiceOptions.voice() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    if (getVoiceOptions.customizationId() != null) {
        builder.query("customization_id", getVoiceOptions.customizationId());
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Voice.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Voice(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice)

Example 2 with Voice

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

the class CustomizationsTest method testGetVoiceCustomization.

/**
 * Test get voice with custom voice model.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testGetVoiceCustomization() throws InterruptedException {
    server.enqueue(jsonResponse(voice));
    GetVoiceOptions getOptions = new GetVoiceOptions.Builder().voice("en-US_TestMaleVoice").customizationId("cafebabe-1234-5678-9abc-def012345678").build();
    final Voice result = service.getVoice(getOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(VOICES_PATH + "/en-US_TestMaleVoice?customization_id=" + CUSTOMIZATION_ID, request.getPath());
    assertEquals("GET", request.getMethod());
    assertEquals(voice, result);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Voice(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice) GetVoiceOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetVoiceOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with Voice

use of com.ibm.watson.text_to_speech.v1.model.Voice 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 4 with Voice

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

the class TextToSpeechIT method testGetVoice.

/**
 * Test get voice.
 */
@Test
public void testGetVoice() {
    GetVoiceOptions getOptions = new GetVoiceOptions.Builder().voice(voiceName).build();
    Voice voice = service.getVoice(getOptions).execute().getResult();
    assertNotNull(voice);
    assertNotNull(voice.getDescription());
    assertNotNull(voice.getGender());
    assertNotNull(voice.getLanguage());
    assertNotNull(voice.getName());
    assertNotNull(voice.getUrl());
}
Also used : Voice(com.ibm.watson.text_to_speech.v1.model.Voice) GetVoiceOptions(com.ibm.watson.text_to_speech.v1.model.GetVoiceOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 5 with Voice

use of com.ibm.watson.text_to_speech.v1.model.Voice 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)

Aggregations

Test (org.junit.Test)10 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)7 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)6 Voice (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice)5 InputStream (java.io.InputStream)5 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)4 GetVoiceOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetVoiceOptions)4 File (java.io.File)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 Pronunciation (com.ibm.watson.text_to_speech.v1.model.Pronunciation)3 Voice (com.ibm.watson.text_to_speech.v1.model.Voice)3 Test (org.testng.annotations.Test)3 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 GetPronunciationOptions (com.ibm.watson.text_to_speech.v1.model.GetPronunciationOptions)2 GetVoiceOptions (com.ibm.watson.text_to_speech.v1.model.GetVoiceOptions)2 BaseSynthesizeCallback (com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback)2