-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
196 lines (152 loc) · 4.36 KB
/
engine.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package picogo
/*
#cgo CFLAGS: -I./picopi/pico/lib -I./picopi/pico/tts
#cgo linux LDFLAGS: -lm
#include <stdlib.h>
#include <tts_engine.c>
#include <langfiles.c>
bool cgo_speak(void *user, uint32_t rate, uint32_t format, int channels, uint8_t *audio, uint32_t audio_bytes, bool final) {
bool speak(void*, uint8_t*, uint32_t, bool); // callback.go:speak
return speak(user, audio, audio_bytes, final);
}
*/
import "C"
import (
"bytes"
"errors"
"fmt"
"os"
"runtime"
"sync"
"unsafe"
)
const (
//LangDefault is default engine's language.
LangDefault = "en-GB"
//LangDirDefault is default directory that should contains language files.
LangDirDefault = "/usr/share/pico/lang/"
//RateDefault is default speech rate value.
RateDefault = 100
//RateMin is min speech rate value.
RateMin = 20
//RateMax is max speech rate value.
RateMax = 500
//PitchDefault is default speech pitch value.
PitchDefault = 100
//PitchMin is default min pitch value.
PitchMin = 50
//PitchMax is default max pitch value.
PitchMax = 200
//VolumeDefault is default speech volume value.
VolumeDefault = 100
//VolumeMin is min speech volume value.
VolumeMin = 0
//VolumeMax is max speech volume value.
VolumeMax = 500
//AudioRate is audio sample rate constant.
AudioRate = 16000
//AudioDepth is audio sample format depth constant.
AudioDepth = 16
//AudioChannels is audio channels number constant.
AudioChannels = 1
)
var (
//ErrBadLanguage represents not supported language value error.
ErrBadLanguage = errors.New("language is not supported")
//ErrBadDirectory represents not existed languages directory error.
ErrBadDirectory = errors.New("directory is not exists")
//ErrCreate represents create engine failure.
ErrCreate = errors.New("failed to create engine")
//ErrSpeak represents speech generation failure.
ErrSpeak = errors.New("speak failure")
supportedLangs map[string]struct{} = map[string]struct{}{
"en-GB": struct{}{},
"en-US": struct{}{},
"de-DE": struct{}{},
"es-ES": struct{}{},
"fr-FR": struct{}{},
"it-IT": struct{}{},
}
)
//NewEngine returns PicoTTS engine.
func NewEngine(lang, dir string) (*Engine, error) {
if _, ok := supportedLangs[lang]; !ok {
return nil, fmt.Errorf("%s: %w", lang, ErrBadLanguage)
}
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return nil, fmt.Errorf("%s: %w", dir, ErrBadDirectory)
}
cdir := C.CString(dir)
defer C.free(unsafe.Pointer(cdir))
clang := C.CString(lang)
defer C.free(unsafe.Pointer(clang))
e := &Engine{
tts: C.TtsEngine_Create(cdir, clang, C.tts_callback_t(C.cgo_speak)),
}
if e.tts == nil {
return nil, ErrCreate
}
runtime.SetFinalizer(e, func(e *Engine) {
e.mu.Lock()
defer e.mu.Unlock()
C.TtsEngine_Destroy(e.tts)
})
return e, nil
}
//Callback receives PCM audio chunks as they are being produced.
type Callback func(pcm []byte, final bool) bool
//Engine provides PicoTTS bindings.
type Engine struct {
mu sync.Mutex
tts *C.TTS_Engine
}
//Speak produces PCM audio output of text value.
func (e *Engine) Speak(text string) ([]byte, error) {
var b bytes.Buffer
err := e.SpeakCB(text, func(pcm []byte, final bool) bool {
_, err := b.Write(pcm)
return err == nil
})
return b.Bytes(), err
}
//SpeakCB produces PCM audio output of text value to specified callback function.
func (e *Engine) SpeakCB(text string, cb Callback) error {
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
ptr := userDataCreate(cb)
defer userDataDestroy(ptr)
e.mu.Lock()
defer e.mu.Unlock()
if !C.TtsEngine_Speak(e.tts, ctext, unsafe.Pointer(ptr)) {
return fmt.Errorf("%s: %w", text, ErrSpeak)
}
return nil
}
//Abort sends an abort signal to the underlying Speak/SpeakCB audio synth routine.
func (e *Engine) Abort() {
C.TtsEngine_Stop(e.tts)
}
//Rate gets speech rate.
func (e *Engine) Rate() int {
return int(C.TtsEngine_GetRate(e.tts))
}
//SetRate sets speech rate.
func (e *Engine) SetRate(rate int) {
C.TtsEngine_SetRate(e.tts, C.int(rate))
}
//Volume gets speech volume.
func (e *Engine) Volume() int {
return int(C.TtsEngine_GetVolume(e.tts))
}
//SetVolume sets speech volume.
func (e *Engine) SetVolume(volume int) {
C.TtsEngine_SetVolume(e.tts, C.int(volume))
}
//Pitch gets speech pitch.
func (e *Engine) Pitch() int {
return int(C.TtsEngine_GetPitch(e.tts))
}
//SetPitch sets speech pitch.
func (e *Engine) SetPitch(pitch int) {
C.TtsEngine_SetPitch(e.tts, C.int(pitch))
}