How to Call the Watson Speech Service with Watson SDK for Golang

1 min read

The IBM Text to Speech service provides APIs that use IBM's speech-synthesis capabilities to turn text into a very natural-sounding speech in many different languages, dialects, and voices.

Text to Speech provides at least one male or female voice (and sometimes both) for each language, and the audio is streamed back to you with minimal delay. 

Watson SDKs abstract much of the complexity associated with application development. By providing programming interfaces in languages that you already know, they can help you get up and running quickly with IBM Watson services.

Go SDK is one of the IBM-supported Watson SDKs, and this post will outline how to use Go SDK to access the IBM Text to Speech service. Users who are interested in using Golang to access Text to Speech may benefit from this content.

Note: As a prerequisite, please install the latest version of Golang. The following example is using version go1.14.

golang1

Step 1: Create a sample script

  • Create a new folder named demo.
  • Use any text editor to create a file called testSpeech.go under the new folder. Use the following for the content of the script:
    package main
    
    import (  
    "bytes"  
    "fmt"  
    "github.com/IBM/go-sdk-core/core"  
    "github.com/watson-developer-cloud/go-sdk/texttospeechv1"  
    "os"
    )
    
    func ConvertTextToSpeech(audioFilePath string, audioFileName string, text string, apikey string, url string) {
    
    // Instantiate the Watson Text To Speech service
    authenticator := &core.IamAuthenticator{ApiKey: apikey,
    }
    service, serviceErr := texttospeechv1.NewTextToSpeechV1(&texttospeechv1.TextToSpeechV1Options{
    Authenticator: authenticator,})
    
    // Check successful instantiation
    if serviceErr != nil {  panic(serviceErr)}
    
    /* SYNTHESIZE */
    synthesizeOptions := service.NewSynthesizeOptions(text).SetAccept("audio/mp3").SetVoice("en-GB_KateVoice")
    
    // Call the textToSpeech Synthesize method
    synthesizeResult, _, responseErr := service.Synthesize(synthesizeOptions)
    
    // Check successful call
    if responseErr != nil {    panic(responseErr)}
    
    // Check successful casting
    if synthesizeResult != nil {  
    buff := new(bytes.Buffer)  
    buff.ReadFrom(synthesizeResult)  
    file, _ := os.Create(audioFilePath + audioFileName)  
    file.Write(buff.Bytes())  
    file.Close()  
    }  
    synthesizeResult.Close()
    }
    
    func main() {
      Speech()
    }
    
    func Speech() string {
     var audioFileName,audioFilePath,text,apikey,url  string
    
     audioFilePath ="output"  
    audioFileName ="speech.wav"  
    text ="hello world"  
    apikey ="<api key>"  
    url ="https://api.us-south.text-to-speech.watson.cloud.ibm.com"
    
    ConvertTextToSpeech(audioFilePath, audioFileName, text, apikey, url)  
    fmt.Println("Conversion from Text to Speech completed")
    return("Success")
    }

Step 2: Create a test script 

  • Use any text editor to create a file called speech_test.go under the new folder. Use the following for the content of the script:
package main

import (
  "testing"
)

func TestSpeech(t *testing.T) {
    want := "Success"
    if got := Speech(); got != want {
        t.Errorf("Speech() = %q, want %q", got, want)
    }
}

Step 3: Update the Go SDK library

  • Under the demo folder, run the following command:
    go mod download github.com/watson-developer-cloud/go-sdk@v1.4.1
  • Update the go.mod file
    • A file called go.mod will be created with the following content:
      module speech
      go 1.14
      require (        
      github.com/IBM/go-sdk-core v1.1.0        
      github.com/watson-developer-cloud/go-sdk v1.4.1
      )
    • Update the content to the following:
      module speech
      go 1.14
      require (        
      github.com/IBM/go-sdk-core v0.0.0-20200217212347-fe152a0e9e89     
      github.com/watson-developer-cloud/go-sdk v1.4.1
      )
    • Note: If you don't update the go.mod file, you may encounter this error later:
      Note: If you don't update the go.mod file, you may encounter this error later:

  • Run the following test:
    go test
    ww2

  • Then, run this script:
    go run testSpeech.go
    The outputspeech.wav should be created successfully.
    golang2

Summary

It's easy to get started with IBM Watson SDK for Golang, and the GitHub repository is actively developed. If you encounter any issue with the SDK, you may first try the StackOverflow, but if you don't have any luck there, please feel free to raise a GitHub issue at the repository and the SDK developers will investigate it.

Be the first to hear about news, product updates, and innovation from IBM Cloud