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"));
}
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));
}
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));
}
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"));
}
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"));
}
Aggregations