-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
167 lines (143 loc) · 3.52 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"text2voice/server"
"github.com/dyike/log"
)
var usageStr = `
Usage: text2voice [options]
单次合成模式选项:
-t <text> 待合成的文本
-p <text_file_path> 待合成的文本路径
-o <file> 音频输出路径
其他:
-h 查看帮助
`
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
}
type LogConf struct {
Enable bool `json:"enable"`
FileName string `json:"filename"`
Level string `json:"level"`
}
type TTSConf struct {
VoiceName string `json:"voice_name"`
TextEncoding string `json:"text_encoding"`
SampleRate int `json:"sample_rate"`
Speed int `json:"speed"`
Volume int `json:"volume"`
Pitch int `json:"pitch"`
Rdn int `json:"rdn"`
}
type Text2VoiceConfig struct {
Log LogConf `json:"log"`
AppId string `json:"appid"`
WorkDir string `json:"work_dir"`
Speed int `json:"speed"`
TTS TTSConf `json:"tts"`
}
func LoadConfig(fileName string) (*Text2VoiceConfig, error) {
if len(fileName) == 0 {
return nil, errors.New("配置文件为空")
}
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
decoder := json.NewDecoder(file)
text2voiceConfig := &Text2VoiceConfig{}
err = decoder.Decode(text2voiceConfig)
if err != nil {
return nil, err
}
return text2voiceConfig, nil
}
func main() {
var txt string
var txtPath string
var out string
var help bool
var confFile string
flag.StringVar(&confFile, "c", "default.json", "配置文件")
flag.StringVar(&txt, "t", "", "单次合成的文本")
flag.StringVar(&txtPath, "p", "", "合成文本文件路径")
flag.StringVar(&out, "o", "test.mp3", "单次合成的输出路径")
flag.BoolVar(&help, "h", false, "Help")
flag.Parse()
if help {
fmt.Printf("%s\n", usageStr)
return
}
config, err := LoadConfig(confFile)
if err != nil {
log.Error("加载配置文件失败:%v\n", err)
return
}
logLevel := config.Log.Level
logFile := config.Log.FileName
if config.Log.Enable {
err := configureLog(logFile, logLevel)
if err != nil {
log.Error("日志配置失败:%v", err)
}
}
if len(txtPath) == 0 && len(txt) == 0 {
log.Debug("没有输入内容")
return
}
if len(txt) == 0 && len(txtPath) > 0 {
content, err := ioutil.ReadFile(txtPath)
if err != nil {
log.Error("读取文件[%s]失败: %v", txtPath, err)
}
txt = string(content)
}
opts := &server.Options{}
opts.TTSParams = fmt.Sprintf("voice_name = %s, text_encoding = %s, sample_rate= %d, speed = %d, volume = %d, pitch = %d, rdn = %d",
config.TTS.VoiceName,
config.TTS.TextEncoding,
config.TTS.SampleRate,
config.TTS.Speed,
config.TTS.Volume,
config.TTS.Pitch,
config.TTS.Rdn)
opts.LoginParams = fmt.Sprintf("appid = %s, work_dir = %s",
config.AppId,
config.WorkDir)
opts.Speed = config.Speed
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
}
}
}