in experiments/babel/main.go [419:483]
func generateSpeech(voices []*texttospeechpb.Voice, translations map[string]string) []BabelOutput {
ctx := context.Background()
var wg sync.WaitGroup
//results := []string{}
results := []BabelOutput{}
resultChan := make(chan BabelOutput, len(voices))
timestamp := time.Now().Format(timeformat)
for _, voice := range voices {
wg.Add(1)
lang := voice.GetLanguageCodes()[0]
text := translations[lang]
//log.Printf("%s %s %s: %s", voice.GetName(), lang, voice.GetSsmlGender(), text)
go func(voice *texttospeechpb.Voice, text, timestamp string) {
defer wg.Done()
outputmetadata := BabelOutput{
VoiceName: voice.GetName(),
LanguageCode: voice.GetLanguageCodes()[0],
Text: text,
Gender: voice.GetSsmlGender().String(),
}
audiobytes, err := synthesizeWithVoice(ctx, voice, text)
if err != nil {
outputmetadata.Error = fmt.Sprintf("error goroutine: text %s; voice: %s", text, voice.GetName())
resultChan <- outputmetadata
//resultChan <- fmt.Sprintf("error goroutine: text %s; voice: %s", text, voice.GetName())
}
filename := fmt.Sprintf("%s-%s-%s-%s.wav", timestamp, voice.GetName(), voice.GetLanguageCodes()[0], voice.GetSsmlGender())
outputmetadata.AudioPath = filename
outputmetadata.Length = len(audiobytes)
if len(audiobytes) == 0 {
//log.Printf("%s is zero bytes", filename)
outputmetadata.Error = fmt.Sprintf("%s voice generated 0 bytes", voice.GetName())
resultChan <- outputmetadata
} else {
err = os.WriteFile(filename, audiobytes, 0644)
if err != nil {
//resultChan <- fmt.Sprintf("unable to write to %s: %v", filename, err)
outputmetadata.Error = fmt.Sprintf("unable to write to %s: %v", filename, err)
}
}
/* log.Printf(" %s Audio content (%7d bytes) written to file: %v",
voice.GetName(),
len(audiobytes),
filename,
) */
//resultChan <- filename
resultChan <- outputmetadata
}(voice, text, timestamp)
}
go func() {
wg.Wait()
close(resultChan)
}()
for r := range resultChan {
results = append(results, r)
}
return results
}