use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.
the class TextToSpeechTest method testWithVoiceAsWav.
/**
* Test with voice as AudioFormat.WAV.
*/
// @Test
public void testWithVoiceAsWav() {
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
final InputStream is = service.synthesize(synthesizeOptions).execute();
assertNotNull(is);
try {
writeInputStreamToOutputStream(is, new FileOutputStream("target/output.wav"));
} catch (final FileNotFoundException e) {
Assert.fail(e.getMessage());
}
}
use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.
the class TextToSpeechTest method testSynthesizeWebM.
/**
* Test synthesize for WebM.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws InterruptedException the interrupted exception
*/
@SuppressWarnings("resource")
@Test
public void testSynthesizeWebM() throws IOException, InterruptedException {
final File audio = new File("src/test/resources/text_to_speech/sample1.webm");
final Buffer buffer = new Buffer().write(Files.toByteArray(audio));
server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.AUDIO_WEBM).setBody(buffer));
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WEBM).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_WEBM, request.getHeader("Accept"));
assertNotNull(in);
writeInputStreamToOutputStream(in, new FileOutputStream("build/output.webm"));
}
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 {
Authenticator ltAuthenticator = new IamAuthenticator("<iam_api_key>");
LanguageTranslator translator = new LanguageTranslator("2019-11-22", ltAuthenticator);
Authenticator ttsAuthenticator = new IamAuthenticator("<iam_api_key>");
TextToSpeech synthesizer = new TextToSpeech(ttsAuthenticator);
String text = "Greetings from Watson Developer Cloud";
// translate
TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).source(Language.ENGLISH).target(Language.SPANISH).build();
TranslationResult translationResult = translator.translate(translateOptions).execute().getResult();
String translation = translationResult.getTranslations().get(0).getTranslation();
// synthesize
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(translation).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(HttpMediaType.AUDIO_WAV).build();
InputStream in = synthesizer.synthesize(synthesizeOptions).execute().getResult();
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 TextToSpeechIT method testSynthesizeUsingWebSocketWithSsml.
/**
* Test synthesize using web socket with ssml.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void testSynthesizeUsingWebSocketWithSsml() throws InterruptedException {
List<String> ssmlMarks = new ArrayList<>();
ssmlMarks.add("sean");
ssmlMarks.add("ricky");
String ssmlText = String.format("Thought I'd end up with <mark name=\"%s\" />Sean, <express-as type=\"Apology\"> " + "but he wasn't a match. </express-as> Wrote some songs " + "about <mark name=\"%s\" />Ricky, now I listen and " + "laugh", ssmlMarks.get(0), ssmlMarks.get(1));
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(ssmlText).voice(SynthesizeOptions.Voice.EN_US_ALLISONVOICE).accept(HttpMediaType.AUDIO_OGG).build();
service.synthesizeUsingWebSocket(synthesizeOptions, new BaseSynthesizeCallback() {
@Override
public void onMarks(Marks marks) {
returnedMarks.add(marks);
}
});
// wait for synthesis to complete
lock.await(5, TimeUnit.SECONDS);
for (Marks m : returnedMarks) {
List<MarkTiming> markList = m.getMarks();
for (MarkTiming markTiming : markList) {
assertTrue(ssmlMarks.contains(markTiming.getMark()));
}
}
}
use of com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.
the class TextToSpeechTest method testSynthesizeWOptions.
// Test the synthesize operation with a valid options model parameter
@Test
public void testSynthesizeWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "This is a mock binary response.";
String synthesizePath = "/v1/synthesize";
server.enqueue(new MockResponse().setHeader("Content-type", "audio/basic").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the SynthesizeOptions model
SynthesizeOptions synthesizeOptionsModel = new SynthesizeOptions.Builder().text("testString").accept("audio/ogg;codecs=opus").voice("en-US_MichaelV3Voice").customizationId("testString").build();
// Invoke synthesize() with a valid options model and verify the result
Response<InputStream> response = textToSpeechService.synthesize(synthesizeOptionsModel).execute();
assertNotNull(response);
InputStream responseObj = response.getResult();
assertNotNull(responseObj);
responseObj.close();
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, synthesizePath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("voice"), "en-US_MichaelV3Voice");
assertEquals(query.get("customization_id"), "testString");
}
Aggregations