Skip to content

Commit

Permalink
feat: add max edge silence parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
airenas committed Oct 30, 2024
1 parent 1d03a45 commit e57eb94
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
3 changes: 2 additions & 1 deletion internal/pkg/service/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Input struct {
Voice string `json:"voice,omitempty"`
Priority int `json:"priority,omitempty"`
//Possible values are: word
SpeechMarkTypes []string `json:"speechMarkTypes,omitempty"`
SpeechMarkTypes []string `json:"speechMarkTypes,omitempty"`
MaxEdgeSilenceMillis *int64 `json:"maxEdgeSilenceMillis,omitempty"`
}

// SpeechMark
Expand Down
29 changes: 15 additions & 14 deletions internal/pkg/service/api/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ func (e AudioFormatEnum) String() string {

// TTSRequestConfig config for request
type TTSRequestConfig struct {
Text string
RequestID string
OutputFormat AudioFormatEnum
OutputTextFormat TextFormatEnum
OutputMetadata []string
AllowCollectData bool
SaveTags []string
Speed float32
Voice string
Priority int
AllowedMaxLen int
SSMLParts []ssml.Part
AudioSuffix string
SpeechMarkTypes map[string]bool
Text string
RequestID string
OutputFormat AudioFormatEnum
OutputTextFormat TextFormatEnum
OutputMetadata []string
AllowCollectData bool
SaveTags []string
Speed float32
Voice string
Priority int
AllowedMaxLen int
SSMLParts []ssml.Part
AudioSuffix string
SpeechMarkTypes map[string]bool
MaxEdgeSilenceMillis int64
}
17 changes: 16 additions & 1 deletion internal/pkg/service/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ func (c *TTSConfigutaror) Configure(r *http.Request, inText *api.Input) (*api.TT
if err != nil {
return nil, err
}
goapp.Log.Info().Str("in", goapp.Sanitize(inText.Voice)).Str("mapped", res.Voice).Msg("voice")
res.SpeechMarkTypes, err = getSpeechMarkTypes(inText.SpeechMarkTypes)
if err != nil {
return nil, err
}
goapp.Log.Info().Msgf("Voice '%s' -> '%s'", goapp.Sanitize(inText.Voice), res.Voice)
res.MaxEdgeSilenceMillis, err = getMaxEdgeSilence(inText.MaxEdgeSilenceMillis)
if err != nil {
return nil, err
}
goapp.Log.Info().Int64("edgeSil", res.MaxEdgeSilenceMillis).Any("speechMarks", res.SpeechMarkTypes).Send()
if inText.Priority < 0 {
return nil, errors.Errorf("wrong priority (>=0) value: %d", inText.Priority)
}
Expand All @@ -163,6 +168,16 @@ func (c *TTSConfigutaror) Configure(r *http.Request, inText *api.Input) (*api.TT
return res, nil
}

func getMaxEdgeSilence(value *int64) (int64, error) {
if value == nil {
return -1, nil
}
if *value < 0 {
return -1, errors.Errorf("maxEdgeSilenceMillis must be >= 0")
}
return *value, nil
}

func getSpeechMarkTypes(s []string) (map[string]bool, error) {
res := make(map[string]bool)
for _, v := range s {
Expand Down
57 changes: 57 additions & 0 deletions internal/pkg/service/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"fmt"
"net/http/httptest"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -402,3 +403,59 @@ func Test_getMaxLen(t *testing.T) {
})
}
}

func Test_getSpeechMarkTypes(t *testing.T) {
type args struct {
s []string
}
tests := []struct {
name string
args args
want map[string]bool
wantErr bool
}{
{name: "OK", args: args{s: []string{"word"}}, want: map[string]bool{"word": true}, wantErr: false},
{name: "Fail", args: args{s: []string{"word1"}}, want: nil, wantErr: true},
{name: "Empty", args: args{s: nil}, want: map[string]bool{}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getSpeechMarkTypes(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("getSpeechMarkTypes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getSpeechMarkTypes() = %v, want %v", got, tt.want)
}
})
}
}

func Test_getMaxEdgeSilence(t *testing.T) {
type args struct {
value *int64
}
tests := []struct {
name string
args args
want int64
wantErr bool
}{
{name: "OK", args: args{value: nil}, want: -1, wantErr: false},
{name: "OK", args: args{value: &[]int64{10}[0]}, want: 10, wantErr: false},
{name: "Fail", args: args{value: &[]int64{-10}[0]}, want: -1, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getMaxEdgeSilence(tt.args.value)
if (err != nil) != tt.wantErr {
t.Errorf("getMaxEdgeSilence() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getMaxEdgeSilence() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e57eb94

Please sign in to comment.