|
12 | 12 | */ |
13 | 13 | package com.ibm.watson.developer_cloud.text_to_speech.v1; |
14 | 14 |
|
| 15 | +import java.util.Arrays; |
15 | 16 | import java.io.File; |
16 | 17 | import java.io.FileOutputStream; |
17 | 18 | import java.io.IOException; |
18 | 19 | import java.io.InputStream; |
19 | 20 | import java.io.OutputStream; |
20 | 21 | import java.util.List; |
21 | 22 |
|
22 | | -import com.ibm.watson.developer_cloud.text_to_speech.v1.model.AudioFormat; |
23 | | -import com.ibm.watson.developer_cloud.text_to_speech.v1.model.CustomTranslation; |
24 | | -import com.ibm.watson.developer_cloud.text_to_speech.v1.model.CustomVoiceModel; |
| 23 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions; |
| 24 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordsOptions; |
| 25 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.CreateVoiceModelOptions; |
| 26 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteVoiceModelOptions; |
| 27 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteWordOptions; |
| 28 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetVoiceModelOptions; |
| 29 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetVoiceOptions; |
| 30 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetWordOptions; |
| 31 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListVoiceModelsOptions; |
| 32 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListWordsOptions; |
| 33 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions; |
| 34 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Translation; |
| 35 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.UpdateVoiceModelOptions; |
25 | 36 | import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice; |
| 37 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.VoiceModel; |
| 38 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.VoiceModels; |
26 | 39 | import com.ibm.watson.developer_cloud.text_to_speech.v1.util.WaveUtils; |
| 40 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word; |
| 41 | +import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Words; |
27 | 42 |
|
28 | 43 | public class CustomizationExample { |
29 | 44 |
|
30 | 45 | public static void main(String[] args) throws IOException { |
31 | 46 | TextToSpeech service = new TextToSpeech("<username>", "<password>"); |
32 | 47 |
|
33 | 48 | // create custom voice model. |
34 | | - CustomVoiceModel customVoiceModel = service.createCustomVoiceModel("my model", "en-US", "the model for testing") |
35 | | - .execute(); |
| 49 | + CreateVoiceModelOptions createOptions = new CreateVoiceModelOptions.Builder() |
| 50 | + .name("my model") |
| 51 | + .language("en-US") |
| 52 | + .description("the model for testing") |
| 53 | + .build(); |
| 54 | + VoiceModel customVoiceModel = service.createVoiceModel(createOptions).execute(); |
36 | 55 | System.out.println(customVoiceModel); |
37 | 56 |
|
38 | 57 | // list custom voice models for US English. |
39 | | - List<CustomVoiceModel> customVoiceModels = service.getCustomVoiceModels("en-US").execute(); |
| 58 | + ListVoiceModelsOptions listOptions = new ListVoiceModelsOptions.Builder() |
| 59 | + .language("en-US") |
| 60 | + .build(); |
| 61 | + VoiceModels customVoiceModels = service.listVoiceModels(listOptions); |
40 | 62 | System.out.println(customVoiceModels); |
41 | 63 |
|
42 | 64 | // update custom voice model. |
43 | | - customVoiceModel.setName("my updated model"); |
44 | | - customVoiceModel.setDescription("the updated model for testing"); |
45 | | - service.updateCustomVoiceModel(customVoiceModel).execute(); |
| 65 | + UpdateVoiceModelOptions updateOptions = new UpdateVoiceModelOptions.Builder() |
| 66 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 67 | + .name(newName) |
| 68 | + .description("the updated model for testing") |
| 69 | + .build(); |
| 70 | + service.updateVoiceModel(updateOptions).execute(); |
46 | 71 |
|
47 | 72 | // list custom voice models regardless of language. |
48 | | - customVoiceModels = service.getCustomVoiceModels(null).execute(); |
| 73 | + customVoiceModels = service.listVoiceModels().execute(); |
49 | 74 | System.out.println(customVoiceModels); |
50 | 75 |
|
51 | 76 | // create multiple custom word translations |
52 | | - CustomTranslation customTranslation1 = new CustomTranslation("hodor", "hold the door"); |
53 | | - CustomTranslation customTranslation2 = new CustomTranslation("plz", "please"); |
54 | | - service.addWords(customVoiceModel, customTranslation1, customTranslation2).execute(); |
| 77 | + Word word1 = new Word(); |
| 78 | + word1.setWord("hodor"); |
| 79 | + word1.setTranslation("hold the door"); |
| 80 | + Word word2 = new Word(); |
| 81 | + word2.setWord("plz"); |
| 82 | + word2.setTranslation("please"); |
| 83 | + List<Word> words = Arrays.asList(word1, word2); |
| 84 | + AddWordsOptions addOptions = new AddWordsOptions.Builder() |
| 85 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 86 | + .words(words) |
| 87 | + .build(); |
| 88 | + service.addWords(addOptions).execute(); |
55 | 89 |
|
56 | 90 | // create a single custom word translation |
57 | | - service.addWord(customVoiceModel, new CustomTranslation("nat", "and that")).execute(); |
| 91 | + AddWordOptions addOptions = new AddWordOptions.Builder() |
| 92 | + .word("nat") |
| 93 | + .translation("and that") |
| 94 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 95 | + .build(); |
| 96 | + service.addWord(addOptions).execute(); |
58 | 97 |
|
59 | 98 | // get custom word translations |
60 | | - List<CustomTranslation> words = service.getWords(customVoiceModel).execute(); |
| 99 | + ListWordsOptions listOptions = new ListWordsOptions.Builder() |
| 100 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 101 | + .build(); |
| 102 | + Words words = service.listWords(listOptions).execute(); |
61 | 103 | System.out.println(words); |
62 | 104 |
|
63 | 105 | // get custom word translation |
64 | | - CustomTranslation translation = service.getWord(customVoiceModel, "hodor").execute(); |
| 106 | + GetWordOptions getOptions = new GetWordOptions.Builder() |
| 107 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 108 | + .word("hodor") |
| 109 | + .build(); |
| 110 | + Translation translation = service.getWord(getOptions).execute(); |
65 | 111 | System.out.println(translation); |
66 | 112 |
|
67 | 113 | // synthesize with custom voice model |
68 | 114 | String text = "plz hodor"; |
69 | | - InputStream in = service.synthesize(text, Voice.EN_MICHAEL, AudioFormat.WAV, customVoiceModel.getId()).execute(); |
| 115 | + SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder() |
| 116 | + .text(text) |
| 117 | + .voice(SynthesizeOptions.Voice.EN_US_MICHAELVOICE) |
| 118 | + .accept(SynthesizeOptions.Accept.AUDIO_WAV) |
| 119 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 120 | + .build(); |
| 121 | + InputStream in = service.synthesize(synthesizeOptions).execute(); |
70 | 122 | writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav")); |
71 | 123 |
|
72 | 124 | // delete custom words with object and string |
73 | | - service.deleteWord(customVoiceModel, customTranslation1); |
74 | | - service.deleteWord(customVoiceModel, customTranslation2.getWord()); |
| 125 | + DeleteWordOptions deleteOptions1 = new DeleteWordOptions.Builder() |
| 126 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 127 | + .word(word1.getWord()) |
| 128 | + .build(); |
| 129 | + service.deleteWord(deleteOptions1).execute(); |
| 130 | + DeleteWordOptions deleteOptions2 = new DeleteWordOptions.Builder() |
| 131 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 132 | + .word(word2.getWord()) |
| 133 | + .build(); |
| 134 | + service.deleteWord(deleteOptions2).execute(); |
75 | 135 |
|
76 | 136 | // delete custom voice model |
77 | | - service.deleteCustomVoiceModel(customVoiceModel).execute(); |
| 137 | + DeleteVoiceModelOptions deleteOptions = new DeleteVoiceModelOptions.Builder() |
| 138 | + .customizationId(customVoiceModel.getCustomizationId()) |
| 139 | + .build(); |
| 140 | + service.deleteVoiceModel(deleteOptions).execute(); |
78 | 141 |
|
79 | 142 | // list custom voice models regardless of language. |
80 | | - customVoiceModels = service.getCustomVoiceModels(null).execute(); |
| 143 | + customVoiceModels = service.listVoiceModels().execute(); |
81 | 144 | System.out.println(customVoiceModels); |
82 | 145 | } |
83 | 146 |
|
|
0 commit comments