-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpeak.go
181 lines (152 loc) · 4.1 KB
/
vpeak.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package vpeak
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var VoicepeakPath string
func init() {
if runtime.GOOS == "darwin" {
VoicepeakPath = "/Applications/voicepeak.app/Contents/MacOS/voicepeak"
} else if runtime.GOOS == "windows" {
VoicepeakPath = "C:\\Program Files\\VOICEPEAK\\voicepeak.exe"
} else {
log.Fatal("Unsupported operating system")
}
}
const (
WavName = "output.wav"
)
var (
narratorMap = map[string]string{
"f1": "Japanese Female 1",
"f2": "Japanese Female 2",
"f3": "Japanese Female 3",
"m1": "Japanese Male 1",
"m2": "Japanese Male 2",
"m3": "Japanese Male 3",
"c": "Japanese Female Child",
}
emotionMap = map[string]string{
"happy": "happy=100",
"fun": "fun=100",
"angry": "angry=100",
"sad": "sad=100",
}
)
// Options struct holds the settings for speech generation
type Options struct {
Narrator string
Emotion string
Output string
Silent bool
}
// GenerateSpeech generates speech audio from the given text and options
func GenerateSpeech(text string, opts Options) error {
options := buildOptions(text, opts)
output := opts.Output
if output == "" {
output = WavName
}
cmd1 := vpCmd(options)
if err := cmd1.Run(); err != nil {
return fmt.Errorf("voicepeak command failed: %v", err)
}
if !opts.Silent {
if err := PlayAudio(output); err != nil {
return err
}
// if the output is not specified, delete the generated wav file
if runtime.GOOS != "windows" && output == WavName {
if err := os.Remove(WavName); err != nil {
return fmt.Errorf("failed to delete %s: %v", WavName, err)
}
}
}
return nil
}
// PlayAudio plays the specified audio file
func PlayAudio(wavName string) error {
var cmd *exec.Cmd
if runtime.GOOS == "darwin" {
cmd = exec.Command("afplay", wavName)
} else if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "start", "", wavName)
} else {
return fmt.Errorf("unsupported operating system")
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("wav file play failed: %v", err)
}
return nil
}
// ProcessTextFiles processes text files in a directory and generates audio files
func ProcessTextFiles(dir string, opts Options) error {
files, err := ioutil.ReadDir(dir)
if err != nil {
return fmt.Errorf("error reading directory: %v", err)
}
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".txt" {
filePath := filepath.Join(dir, file.Name())
content, err := readTextFile(filePath)
if err != nil {
log.Printf("Error reading file (%s): %v", file.Name(), err)
continue
}
outputName := convertWavExt(file.Name())
outputPath := filepath.Join(dir, outputName)
if opts.Output != "" {
// Override directory output
outputPath = filepath.Join(opts.Output, outputName)
}
localOpts := opts
localOpts.Output = outputPath
if err := GenerateSpeech(content, localOpts); err != nil {
log.Printf("Error generating speech for file (%s): %v", file.Name(), err)
continue
}
}
}
return nil
}
func vpCmd(options []string) *exec.Cmd {
_, err := exec.LookPath(VoicepeakPath)
if err != nil {
log.Fatalf("Command not found: %v", err)
}
return exec.Command(VoicepeakPath, options...)
}
func convertWavExt(filename string) string {
oldExt := filepath.Ext(filename)
return strings.TrimSuffix(filename, oldExt) + ".wav"
}
func buildOptions(text string, opts Options) []string {
options := []string{"-s", text}
if narrator, ok := narratorMap[opts.Narrator]; ok {
options = append([]string{"--narrator", narrator}, options...)
} else if opts.Narrator != "" {
log.Fatalf("Invalid narrator option: %s", opts.Narrator)
}
if emotion, ok := emotionMap[opts.Emotion]; ok {
options = append([]string{"--emotion", emotion}, options...)
} else if opts.Emotion != "" {
log.Fatalf("Invalid emotion option: %s", opts.Emotion)
}
if opts.Output != "" {
options = append([]string{"-o", opts.Output}, options...)
}
return options
}
func readTextFile(filePath string) (string, error) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(content), nil
}