GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-sdk/text-to-speech at master · jaymicrocode/java-sdk

Expand file tree

/

text-to-speech

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

text-to-speech

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
src
src
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradle.properties
gradle.properties
 
 

README.md

Outline

Text to Speech

Installation

Maven
<dependency>
  <groupId>com.ibm.watson</groupId>
  <artifactId>text-to-speech</artifactId>
  <version>8.6.0</version>
</dependency>
Gradle
'com.ibm.watson:text-to-speech:8.6.0'

Usage

Use the Text to Speech service to get the available voices to synthesize.

Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
TextToSpeech service = new TextToSpeech(authenticator);

Voices voices = service.listVoices().execute().getResult();
System.out.println(voices);

Usage with WebSockets

The Watson Text to Speech service supports the use of WebSockets as an alternative to the synthesize() method, which converts text to speech. Here is an example of using the WebSocket version of the method to get an audio file:

Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
TextToSpeech service = new TextToSpeech(authenticator);

String text = "It's beginning to look a lot like Christmas";
SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder()
  .text(text)
  .accept(SynthesizeOptions.Accept.AUDIO_OGG_CODECS_OPUS)
  .build();

// a callback is defined to handle certain events, like an audio transmission or a timing marker
// in this case, we'll build up a byte array of all the received bytes to build the resulting file
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
service.synthesizeUsingWebSocket(synthesizeOptions, new BaseSynthesizeCallback() {
  @Override
  public void onAudioStream(byte[] bytes) {
    // append to our byte array
    try {
      byteArrayOutputStream.write(bytes);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
});

// quick way to wait for synthesis to complete, since synthesizeUsingWebSocket() runs asynchronously
Thread.sleep(5000);

// create file with audio data
String filename = "synthesize_websocket_test.ogg";
OutputStream fileOutputStream = new FileOutputStream(filename);
byteArrayOutputStream.writeTo(fileOutputStream);

// clean up
byteArrayOutputStream.close();
fileOutputStream.close();
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.