March 26, 2020 By Jason Zhou 3 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.

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:
  • Run the following test:
    go test
  • Then, run this script:
    go run testSpeech.go

    The outputspeech.wav should be created successfully.

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.

Was this article helpful?
YesNo

More from Cloud

IBM Cloud Reference Architectures unleashed

2 min read - The ability to onboard workloads to cloud quickly and seamlessly is paramount to accelerate enterprises digital transformation journey. At IBM Cloud, we're thrilled to introduce the IBM Cloud® Reference Architectures designed to empower clients, technical architects, strategists and partners to revolutionize the way businesses harness the power of the cloud. VPC resiliency: Strengthening your foundation Explore the resilience of IBM Cloud Virtual Private Cloud through our comprehensive resources. Dive into our VPC Resiliency white paper, a blueprint for building robust…

Enhance your data security posture with a no-code approach to application-level encryption

4 min read - Data is the lifeblood of every organization. As your organization’s data footprint expands across the clouds and between your own business lines to drive value, it is essential to secure data at all stages of the cloud adoption and throughout the data lifecycle. While there are different mechanisms available to encrypt data throughout its lifecycle (in transit, at rest and in use), application-level encryption (ALE) provides an additional layer of protection by encrypting data at its source. ALE can enhance…

Attention new clients: exciting financial incentives for VMware Cloud Foundation on IBM Cloud

4 min read - New client specials: Get up to 50% off when you commit to a 1- or 3-year term contract on new VCF-as-a-Service offerings, plus an additional value of up to USD 200K in credits through 30 June 2025 when you migrate your VMware workloads to IBM Cloud®.1 Low starting prices: On-demand VCF-as-a-Service deployments begin under USD 200 per month.2 The IBM Cloud benefit: See the potential for a 201%3 return on investment (ROI) over 3 years with reduced downtime, cost and…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters