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.

Categories

More from Cloud

Kubernetes version 1.28 now available in IBM Cloud Kubernetes Service

2 min read - We are excited to announce the availability of Kubernetes version 1.28 for your clusters that are running in IBM Cloud Kubernetes Service. This is our 23rd release of Kubernetes. With our Kubernetes service, you can easily upgrade your clusters without the need for deep Kubernetes knowledge. When you deploy new clusters, the default Kubernetes version remains 1.27 (soon to be 1.28); you can also choose to immediately deploy version 1.28. Learn more about deploying clusters here. Kubernetes version 1.28 In…

Temenos brings innovative payments capabilities to IBM Cloud to help banks transform

3 min read - The payments ecosystem is at an inflection point for transformation, and we believe now is the time for change. As banks look to modernize their payments journeys, Temenos Payments Hub has become the first dedicated payments solution to deliver innovative payments capabilities on the IBM Cloud for Financial Services®—an industry-specific platform designed to accelerate financial institutions' digital transformations with security at the forefront. This is the latest initiative in our long history together helping clients transform. With the Temenos Payments…

Foundational models at the edge

7 min read - Foundational models (FMs) are marking the beginning of a new era in machine learning (ML) and artificial intelligence (AI), which is leading to faster development of AI that can be adapted to a wide range of downstream tasks and fine-tuned for an array of applications.  With the increasing importance of processing data where work is being performed, serving AI models at the enterprise edge enables near-real-time predictions, while abiding by data sovereignty and privacy requirements. By combining the IBM watsonx data…

The next wave of payments modernization: Minimizing complexity to elevate customer experience

3 min read - The payments ecosystem is at an inflection point for transformation, especially as we see the rise of disruptive digital entrants who are introducing new payment methods, such as cryptocurrency and central bank digital currencies (CDBC). With more choices for customers, capturing share of wallet is becoming more competitive for traditional banks. This is just one of many examples that show how the payments space has evolved. At the same time, we are increasingly seeing regulators more closely monitor the industry’s…