Skip to content

Commit

Permalink
feat:Add method AddPackTaskWithCustomOptions which allow user to us…
Browse files Browse the repository at this point in the history
…e different options
  • Loading branch information
czyt committed Jan 19, 2024
1 parent fd01efd commit 4db08c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/ttsTask/packTask.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type PackEntry struct {
Text string
// Entry name to be packed into a file
EntryName string
// EntryCommunicateOpt defines the options for communicating with the TTS engine.if note set, use the PackTask's CommunicateOpt.
EntryCommunicateOpt *communicateOption.CommunicateOption
}

type PackTask struct {
Expand All @@ -33,7 +35,12 @@ func (p *PackTask) Start(wg *sync.WaitGroup) error {
defer wg.Done()
for _, entry := range p.PackEntries {
// for zip file, the entry should be written after creation.
c, err := communicate.NewCommunicate(entry.Text, p.CommunicateOpt)
opt := p.CommunicateOpt
if entry.EntryCommunicateOpt != nil {
opt = entry.EntryCommunicateOpt

}
c, err := communicate.NewCommunicate(entry.Text, opt)
if err != nil {
log.Printf("create communicate error:%v \r\n", err)
continue
Expand Down
22 changes: 22 additions & 0 deletions speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ func (s *Speech) AddSingleTask(text string, output io.Writer) error {
// - metaData: optional parameter. It is the data which will be serialized into a json file. The name uses the key and value as the key-value pair.
// The function returns an error if there are no pack task entries.
func (s *Speech) AddPackTask(dataEntries map[string]string, entryCreator func(name string) (io.Writer, error), output io.Writer, metaData ...map[string]any) error {
return s.AddPackTaskWithCustomOptions(dataEntries, nil, entryCreator, output, metaData...)
}

// AddPackTaskWithCustomOptions adds a pack task with options to the speech.
// It takes four parameters:
// - dataEntries: a map where the key is the entry name and the value is the entry text to be synthesized.
// - entriesOption: a map where the key is the entry name and the value is the entry option to be used for the entry.
// - entryCreator: a function that creates a writer for each entry. This can be a packer context related writer, such as a zip writer.
// - output: the output of the pack task, which will finally be written into a file.
// - metaData: optional parameter. It is the data which will be serialized into a json file. The name uses the key and value as the key-value pair.
// The function returns an error if there are no pack task entries.
func (s *Speech) AddPackTaskWithCustomOptions(dataEntries map[string]string, entriesOption map[string][]Option, entryCreator func(name string) (io.Writer, error), output io.Writer, metaData ...map[string]any) error {
taskCount := len(dataEntries)
if taskCount == 0 {
return NoPackTaskEntries
Expand All @@ -85,6 +97,16 @@ func (s *Speech) AddPackTask(dataEntries map[string]string, entryCreator func(na
Text: text,
EntryName: name,
}
if entriesOption != nil {
if entryOpt, ok := entriesOption[name]; ok {
opt := &option{}
for _, apply := range entryOpt {
apply(opt)
}
packEntry.EntryCommunicateOpt = opt.toInternalOption()
}

}
packEntries = append(packEntries, packEntry)
}

Expand Down
22 changes: 22 additions & 0 deletions speech_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ func TestSpeech_StartTasksToZip(t *testing.T) {

}

func TestSpeech_StartTasksWithOptionsToZip(t *testing.T) {
opts := make([]Option, 0)
opts = append(opts, WithVoice("zh-CN-YunxiaNeural"))
speech, err := NewSpeech(opts...)
if err != nil {
t.Fatal(err)
}
w, err := os.OpenFile("testdata/tts.zip", os.O_RDWR|os.O_CREATE, 0666)
zipWriter := zip.NewWriter(w)
defer zipWriter.Close()
dataPayload := make(map[string]string)
dataPayload["test.mp3"] = "种一棵树最好的时间是十年前,其次是现在.The best time to plant"
dataPayload["test1.mp3"] = "莫听穿林打叶声,何妨吟啸且徐行.。竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。料峭春风吹酒醒,微冷,山头斜照却相迎。回首向来萧瑟处,归去,也无风雨也无晴。"

options := make(map[string][]Option)
options["test.mp3"] = []Option{WithVoice("zh-CN-YunyangNeural")}
speech.AddPackTaskWithCustomOptions(dataPayload, options, zipWriter.Create, w)
speech.StartTasks()
zipWriter.Flush()

}

func TestSpeech_GetVoiceList(t *testing.T) {
speech, err := NewSpeech()
if err != nil {
Expand Down

0 comments on commit 4db08c7

Please sign in to comment.