This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flite.go
141 lines (117 loc) · 3.13 KB
/
flite.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
// Package flite implements bindings for Flite (festival-lite)
package flite
/*
#cgo CFLAGS: -I/usr/include/flite
#cgo LDFLAGS: -lflite_cmu_us_awb -lflite_cmu_us_kal16 -lflite_cmu_us_kal -lflite_cmu_us_rms -lflite_cmu_us_slt -lflite_usenglish -lflite_cmulex -lflite -lm
#include "flite.h"
#include <stdlib.h>
cst_voice* register_cmu_us_awb();
cst_voice* register_cmu_us_kal16();
cst_voice* register_cmu_us_kal();
cst_voice* register_cmu_us_rms();
cst_voice* register_cmu_us_slt();
*/
import "C"
import (
"fmt"
"unsafe"
)
// Voice type
type Voice struct {
name *int8
features *features
ffunctions *features
init *utterance
}
func (v *Voice) cptr() *C.cst_voice {
return (*C.cst_voice)(unsafe.Pointer(v))
}
// newVoiceFromPointer returns new Voice from pointer
func newVoiceFromPointer(ptr unsafe.Pointer) *Voice {
return (*Voice)(ptr)
}
// Wave type
type Wave struct {
Type *int8
SampleRate int32
NumSamples int32
NumChannels int32
Padding [4]byte
Samples *int16
}
func (w *Wave) cptr() *C.cst_wave {
return (*C.cst_wave)(unsafe.Pointer(w))
}
// newWaveFromPointer returns new Wave from pointer
func newWaveFromPointer(ptr unsafe.Pointer) *Wave {
return (*Wave)(ptr)
}
// features type
type features struct {
head *featValPair
ctx unsafe.Pointer
}
// utterance type
type utterance struct {
features *features
ffunctions *features
relations *features
ctx unsafe.Pointer
}
// featValPair type
type featValPair struct {
name string
val *val
next *featValPair
}
// val type
type val struct {
C [16]byte
}
// VoiceSelect returns a Voice for the voice name.
// The valid names are "awb", "kal16", "kal", "rms", "slt".
// It will return error if there is no match.
func VoiceSelect(name string) (*Voice, error) {
var ret *C.struct_cst_voice_struct
switch name {
case "awb":
ret = C.register_cmu_us_awb()
case "kal16":
ret = C.register_cmu_us_kal16()
case "kal":
ret = C.register_cmu_us_kal()
case "rms":
ret = C.register_cmu_us_rms()
case "slt":
ret = C.register_cmu_us_slt()
default:
return nil, fmt.Errorf("flite: no match for voice: %s", name)
}
if ret == nil {
return nil, fmt.Errorf("flite: no voice")
}
v := newVoiceFromPointer(unsafe.Pointer(ret))
return v, nil
}
// TextToSpeech synthesizes the text with the given voice.
// outtype may be a filename where the generated wav is written to, or "play" and it will be sent to the audio device, or "none" and it will be discarded.
// The return value is the number of seconds of speech generated.
func TextToSpeech(text string, voice *Voice, outtype string) float64 {
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
cvoice := voice.cptr()
couttype := C.CString(outtype)
defer C.free(unsafe.Pointer(couttype))
ret := C.flite_text_to_speech(ctext, cvoice, couttype)
v := (float64)(ret)
return v
}
// TextToWave returns a waveform synthesized from the text with the given voice.
func TextToWave(text string, voice *Voice) *Wave {
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
cvoice := voice.cptr()
ret := C.flite_text_to_wave(ctext, cvoice)
v := newWaveFromPointer(unsafe.Pointer(ret))
return v
}