Skip to content

Commit

Permalink
Add function to retrieve the speech synthesis task status
Browse files Browse the repository at this point in the history
  • Loading branch information
vmilovanovicc committed Sep 30, 2023
1 parent 15f83b4 commit 2833441
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
39 changes: 35 additions & 4 deletions backend/text_to_speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/polly"
"github.com/aws/aws-sdk-go-v2/service/polly/types"
typesPolly "github.com/aws/aws-sdk-go-v2/service/polly/types"
"log"
"strings"
)
Expand Down Expand Up @@ -33,15 +33,15 @@ func GetSpeechSynthesisTaskId(text, bucketName, languageCode, targetVoice string
client := polly.NewFromConfig(cfg)

targetVoice, _ = GetTargetVoice(languageCode)
targetVoiceId := types.VoiceId(targetVoice)
targetVoiceId := typesPolly.VoiceId(targetVoice)
bucketLocation, _ := CreateBucket("")
bucketName, _ = GetBucketName(bucketLocation)

params := &polly.StartSpeechSynthesisTaskInput{
Text: aws.String(text),
OutputS3BucketName: aws.String(bucketName),
OutputFormat: types.OutputFormatMp3,
Engine: types.EngineStandard,
OutputFormat: typesPolly.OutputFormatMp3,
Engine: typesPolly.EngineStandard,
VoiceId: targetVoiceId,
}

Expand All @@ -57,3 +57,34 @@ func GetSpeechSynthesisTaskId(text, bucketName, languageCode, targetVoice string
fmt.Println(objectName)
return *resp.SynthesisTask.TaskId, objectName, nil
}

// GetSpeechSynthesisTaskStatus returns the current status of the individual speech synthesis task.
// Will not wait for the SpeechSynthesisTask to complete.
func GetSpeechSynthesisTaskStatus(taskId string) (typesPolly.TaskStatus, error) {
cfg := LoadAWSConfig()
client := polly.NewFromConfig(cfg)

params := &polly.GetSpeechSynthesisTaskInput{
TaskId: aws.String(taskId),
}

ctx := context.TODO()
resp, err := client.GetSpeechSynthesisTask(ctx, params)
if err != nil {
log.Fatalf("failed to retrieve task status, error: %v", err)
return "", nil
}

status := resp.SynthesisTask.TaskStatus
if status == "failed" {
fmt.Printf("current status: %v\n, reason: %v", status, resp.SynthesisTask.TaskStatusReason)
} else if status == "scheduled" || status == "inProgress" {
fmt.Printf("current status: %v", status)
} else {
fmt.Printf("current status: %v", status)
fmt.Printf("Pathway for the output speech file: %v\n", resp.SynthesisTask.OutputUri)
}

fmt.Println(status)
return status, nil
}
4 changes: 2 additions & 2 deletions backend/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestTranslateTextFromTranslate(t *testing.T) {
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := tt.expectTranslatedText, gotTranslatedText; e != a {
t.Errorf("expect %v, got %v", e, a)
if tt.expectTranslatedText != gotTranslatedText {
t.Errorf("expect %v, got %v", tt.expectTranslatedText, gotTranslatedText)
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ func main() {
locationURL, _ := backend.CreateBucket("")
backend.GetBucketName(locationURL)
backend.GetTargetVoice("fr")
backend.GetSpeechSynthesisTaskId("Testing speech to voice functionality", "", "es", "")
taskId, _, _ := backend.GetSpeechSynthesisTaskId("Testing speech to voice functionality", "", "es", "")
backend.GetSpeechSynthesisTaskStatus(taskId)
}

0 comments on commit 2833441

Please sign in to comment.