-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
114 lines (90 loc) · 2.75 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"ontts/server"
"os"
"strings"
"github.com/imroc/log"
)
var usageStr = `
Usage: ontts [options]
讯飞语音参数选项:
-tp <param> TTS合成参数[有默认值]
-lp <param> 登录参数[有默认值]
单次合成模式选项:
-t <text> 待合成的文本
-o <file> 音频输出路径
合成服务模式选项:
-d <dir> 音频保存的目录
-b <dir> 音频备份的目录
-s <digit> 合成速度级别(1-10),数值越小速度越快,越耗CPU[默认为1]
-r <addr> redis连接地址
-rp <pass> redis密码
日志选项:
-l <file> 日志输出路径[默认./ontts.log]
-ll <level> 日志输出级别(debug,info,warn,error)
其他:
-h 查看帮助
`
func main() {
opts := &server.Options{}
var txt string
var out string
var help bool
var logFile string
var logLevel string
flag.StringVar(&txt, "t", "", "单次合成的文本")
flag.StringVar(&out, "o", "", "单次合成的输出路径")
flag.StringVar(&logFile, "l", "ontts.log", "日志输出路径")
flag.StringVar(&logLevel, "ll", "debug", "日志输出级别")
flag.BoolVar(&help, "h", false, "Help")
flag.StringVar(&opts.TTSParams, "tp", "voice_name = xiaoqi, text_encoding = UTF8, sample_rate = 8000, speed = 50, volume = 50, pitch = 50, rdn = 2", "TTS合成参数")
flag.StringVar(&opts.LoginParams, "lp", "appid = 5718a335, work_dir = .", "登录参数")
flag.StringVar(&opts.RedisAddr, "r", ":6379", "redis连接地址")
flag.StringVar(&opts.RedisPass, "rp", "", "redis连接密码")
flag.StringVar(&opts.OutDir, "d", "", "音频输出目录")
flag.StringVar(&opts.BackupDir, "b", "", "音频保存目录")
flag.IntVar(&opts.Speed, "s", 1, "合成速度")
flag.Parse()
if help {
fmt.Printf("%s\n", usageStr)
return
}
err := configureLog(logFile, logLevel)
if err != nil {
log.Error("日志配置失败:%v", err)
return
}
s := server.New(opts)
if txt != "" { // 单次合成
if out == "" {
out = txt + ".wav"
}
log.Debug("合成文本:%q,输出:%s", txt, out)
if err = s.Once(txt, out); err != nil {
log.Error("%v", err)
return
}
}
s.Start()
}
func configureLog(logFile, logLevel string) error {
level := log.DEBUG
switch strings.ToLower(logLevel) {
case "debug":
level = log.DEBUG
case "info":
level = log.INFO
case "warn":
level = log.WARN
case "error":
level = log.ERROR
}
file, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
log.Set(level, file, log.Lshortfile|log.LstdFlags)
return nil
}