-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (64 loc) · 1.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"sync"
"github.com/lingua-sensei/laverna/synthesize"
)
var (
filenamePath = flag.String("file", "", "filename path that is used for reading YAML file")
maxWorkers = flag.Int("workers", runtime.GOMAXPROCS(0), "maximum number of concurrent downloads")
generationNumber = flag.Int("n", 1, "generation number that is used in output filenames")
)
func main() {
flag.Parse()
if *filenamePath == "" {
flag.Usage()
os.Exit(0)
}
raw, err := os.ReadFile(*filenamePath)
if err != nil {
log.Fatalf("[ERR] failed to read filename path: %v", err)
}
opts, err := synthesize.UnmarshalYAML(raw)
if err != nil {
log.Fatalf("[ERR] failed to unmarshal YAML: %v", err)
}
c := &http.Client{}
for err := range batchSave(c, *maxWorkers, *generationNumber, opts) {
log.Printf("[WARN] failed to batch save: %v", err)
}
}
func batchSave(client *http.Client, workerCount, generationNumber int, opts []synthesize.Opt) <-chan error {
errChan := make(chan error, len(opts))
throttle := make(chan struct{}, workerCount)
var wg sync.WaitGroup
for i := range opts {
wg.Add(1)
go func(generationNumber int) {
defer wg.Done()
throttle <- struct{}{}
defer func() {
<-throttle
}()
audio, err := synthesize.Run(client, opts[i])
if err != nil {
errChan <- fmt.Errorf("failed to run opt(%s): %w", opts[i].Text, err)
return
}
filename := fmt.Sprintf("audio_%d.mp3", generationNumber)
if err := os.WriteFile(filename, audio, 0644); err != nil {
errChan <- fmt.Errorf("failed to write file(%s): %w", filename, err)
}
}(generationNumber + i)
}
go func() {
wg.Wait()
close(errChan)
}()
return errChan
}