texttospeech/list_voices/list_voices.go (38 lines of code) (raw):
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// The list_voices command lists the available Text-to-Speech voices.
package main
import (
"context"
"fmt"
"io"
"log"
"os"
texttospeech "cloud.google.com/go/texttospeech/apiv1"
"cloud.google.com/go/texttospeech/apiv1/texttospeechpb"
)
// [START tts_list_voices]
// ListVoices lists the available text to speech voices.
func ListVoices(w io.Writer) error {
ctx := context.Background()
client, err := texttospeech.NewClient(ctx)
if err != nil {
return err
}
defer client.Close()
// Performs the list voices request.
resp, err := client.ListVoices(ctx, &texttospeechpb.ListVoicesRequest{})
if err != nil {
return err
}
for _, voice := range resp.Voices {
// Display the voice's name. Example: tpc-vocoded
fmt.Fprintf(w, "Name: %v\n", voice.Name)
// Display the supported language codes for this voice. Example: "en-US"
for _, languageCode := range voice.LanguageCodes {
fmt.Fprintf(w, " Supported language: %v\n", languageCode)
}
// Display the SSML Voice Gender.
fmt.Fprintf(w, " SSML Voice Gender: %v\n", voice.SsmlGender.String())
// Display the natural sample rate hertz for this voice. Example: 24000
fmt.Fprintf(w, " Natural Sample Rate Hertz: %v\n",
voice.NaturalSampleRateHertz)
}
return nil
}
// [END tts_list_voices]
func main() {
err := ListVoices(os.Stdout)
if err != nil {
log.Fatal(err)
}
}