/
voispire.go
388 lines (354 loc) · 8.92 KB
/
voispire.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package voispire
import (
"fmt"
"io/ioutil"
"log"
"math"
"sort"
"strings"
"time"
"github.com/but80/voispire/internal/buffer"
"github.com/but80/voispire/internal/formant"
"github.com/but80/voispire/internal/wav"
"github.com/but80/voispire/internal/world"
"github.com/gordonklaus/portaudio"
"github.com/saintfish/chardet"
"github.com/xlab/closer"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
"golang.org/x/xerrors"
)
func autoToUTF8(s string) string {
r, err := chardet.NewTextDetector().DetectBest([]byte(s))
if err != nil {
return s
}
var e encoding.Encoding
switch r.Charset {
case "UTF-8", "ISO-8859-1", "ISO-8859-9":
return s
case "EUCJP":
e = japanese.EUCJP
case "ISO-2022-JP":
e = japanese.ISO2022JP
case "Shift_JIS", "windows-1252", "windows-1254":
e = japanese.ShiftJIS
default:
log.Printf("info: unsupported charset = %s", r.Charset)
return s
}
t, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(s), e.NewDecoder()))
if err != nil {
return s
}
return string(t)
}
func lessDevice(devs []*portaudio.DeviceInfo) func(i, j int) bool {
return func(i, j int) bool {
a := devs[i]
b := devs[j]
if a.DefaultLowInputLatency < b.DefaultLowInputLatency {
return true
}
if b.DefaultLowInputLatency < a.DefaultLowInputLatency {
return false
}
if a.DefaultHighInputLatency < b.DefaultHighInputLatency {
return true
}
if b.DefaultHighInputLatency < a.DefaultHighInputLatency {
return false
}
if b.DefaultSampleRate < a.DefaultSampleRate {
return true
}
if a.DefaultSampleRate < b.DefaultSampleRate {
return false
}
if b.MaxInputChannels < a.MaxInputChannels {
return true
}
if a.MaxInputChannels < b.MaxInputChannels {
return false
}
if b.MaxOutputChannels < a.MaxOutputChannels {
return true
}
if a.MaxOutputChannels < b.MaxOutputChannels {
return false
}
return a.Name < b.Name
}
}
func listDevices() (ins, outs []*portaudio.DeviceInfo, err error) {
devs, err := portaudio.Devices()
if err != nil {
return nil, nil, err
}
for _, dev := range devs {
dev.Name = autoToUTF8(dev.Name)
if 0 < dev.MaxInputChannels {
ins = append(ins, dev)
}
if 0 < dev.MaxOutputChannels {
outs = append(outs, dev)
}
}
sort.Slice(ins, lessDevice(ins))
sort.Slice(outs, lessDevice(outs))
return
}
// ListDevices は、オーディオデバイスの一覧を表示します。
func ListDevices() error {
portaudio.Initialize()
closer.Bind(func() {
portaudio.Terminate()
})
ins, outs, err := listDevices()
if err != nil {
return xerrors.Errorf("オーディオデバイス情報の取得に失敗しました: %w", err)
}
defaultIn, err := portaudio.DefaultInputDevice()
if err != nil {
return xerrors.Errorf("オーディオデバイス情報の取得に失敗しました: %w", err)
}
defaultOut, err := portaudio.DefaultOutputDevice()
if err != nil {
return xerrors.Errorf("オーディオデバイス情報の取得に失敗しました: %w", err)
}
fmt.Println("INPUTS:")
for i, dev := range ins {
if dev == defaultIn {
fmt.Print("* ")
} else {
fmt.Print(" ")
}
fmt.Printf("[%2d]", i+1)
fmt.Printf(" %-48s:", dev.Name)
fmt.Printf(" %s", dev.HostApi.Name)
fmt.Printf(" %s", dev.DefaultLowInputLatency)
fmt.Printf(" %vHz", dev.DefaultSampleRate)
fmt.Println()
}
fmt.Println()
fmt.Println("OUTPUTS:")
for i, dev := range outs {
if dev == defaultOut {
fmt.Print("* ")
} else {
fmt.Print(" ")
}
fmt.Printf("[%2d]", i+1)
fmt.Printf(" %-48s:", dev.Name)
fmt.Printf(" %s", dev.HostApi.Name)
fmt.Printf(" %s", dev.DefaultLowOutputLatency)
fmt.Printf(" %vHz", dev.DefaultSampleRate)
fmt.Println()
}
fmt.Println()
return nil
}
func join(input <-chan buffer.Shape) <-chan float64 {
out := make(chan float64, 4096)
go func() {
msg := 0
for s := range input {
for _, v := range s.Data() {
out <- v
msg++
}
}
log.Printf("debug: join: %d messages", msg)
close(out)
}()
return out
}
func render(params portaudio.StreamParameters, input *buffer.WaveSource, outCh <-chan float64) (chan struct{}, *portaudio.Stream, error) {
endCh := make(chan struct{})
onIn := func(in [][]float32) {
if len(in) == 0 {
return
}
n := len(in[0])
buf := make([]float64, n)
for i := 0; i < n; i++ {
buf[i] = float64(in[0][i])
}
input.Append(buf)
}
onOut := func(out [][]float32) {
i := 0
n := len(out[0])
for ; i < n; i++ {
select {
case v, ok := <-outCh:
if !ok {
if endCh != nil {
close(endCh)
endCh = nil
}
break
}
out[0][i] = float32(v)
out[1][i] = float32(v)
default:
break
}
}
for ; i < n; i++ {
out[0][i] = 0
out[1][i] = 0
}
}
var onProcess interface{} = onOut
if input != nil {
if outCh != nil {
onProcess = func(in, out [][]float32) {
onIn(in)
onOut(out)
}
} else {
onProcess = onIn
}
}
stream, err := portaudio.OpenStream(params, onProcess)
if err != nil {
return nil, nil, err
}
log.Printf("info: Sample rate: %f\n", stream.Info().SampleRate)
log.Printf("info: Input latency: %s\n", stream.Info().InputLatency.String())
log.Printf("info: Output latency: %s\n", stream.Info().OutputLatency.String())
if err := stream.Start(); err != nil {
return nil, nil, err
}
return endCh, stream, nil
}
const (
f0Floor = 71.0
f0Ceil = 800.0
)
// Options は、 Start 関数のオプションです。
type Options struct {
Formant float64
Transpose float64
FramePeriodMsec float64
Rate int
InDevID int
OutDevID int
InFile string
OutFile string
}
// Start は、音声変換を開始します。
func Start(o Options) error {
var f0 []float64
if o.Transpose != 0 {
log.Print("info: 基本周波数を推定中...")
src, fs, err := wav.Load(o.InFile)
if err != nil {
return xerrors.Errorf("音声ファイルの読み込みに失敗しました: %w", err)
}
log.Printf("debug: IN: %d samples, fs=%d", len(src), fs)
f0, _ = world.Harvest(src, fs, o.FramePeriodMsec, f0Floor, f0Ceil)
}
var params portaudio.StreamParameters
if o.InFile == "" || o.OutFile == "" {
portaudio.Initialize()
closer.Bind(func() {
portaudio.Terminate()
})
hostapi, err := portaudio.DefaultHostApi()
if err != nil {
return xerrors.Errorf("オーディオデバイスのオープンに失敗しました: %w", err)
}
ins, outs, err := listDevices()
if err != nil {
return xerrors.Errorf("オーディオデバイス情報の取得に失敗しました: %w", err)
}
var inDev *portaudio.DeviceInfo
if o.InFile == "" {
inDev = hostapi.DefaultInputDevice
if 1 <= o.InDevID && o.InDevID <= len(ins) {
inDev = ins[o.InDevID-1]
}
log.Printf("info: Input device: %s\n", inDev.Name)
}
var outDev *portaudio.DeviceInfo
if o.OutFile == "" {
outDev = hostapi.DefaultOutputDevice
if 1 <= o.OutDevID && o.OutDevID <= len(outs) {
outDev = outs[o.OutDevID-1]
}
log.Printf("info: Output device: %s\n", outDev.Name)
}
params = portaudio.LowLatencyParameters(inDev, outDev)
}
var input *buffer.WaveSource
var paInput *buffer.WaveSource
var fs int
if o.InFile == "" {
paInput = buffer.NewWaveSource()
input = paInput
} else {
var err error
input, fs, err = wav.NewWavFileSource(o.InFile)
if err != nil {
return xerrors.Errorf("音声ファイルのオープンに失敗しました: %w", err)
}
}
fsOut := fs
if 0 < o.Rate {
fsOut = o.Rate
}
pitchCoef := math.Pow(2.0, o.Transpose/12.0)
formantCoef := math.Pow(2.0, (o.Formant-o.Transpose)/12.0)
mod1 := formant.NewCepstralShifter(input, fs, 1024, formantCoef)
var mod2 *f0Splitter
var mod3 *stretcher
var lastmod interface{ Start() }
var outCh <-chan float64
if o.Transpose == 0 {
log.Print("info: フォルマントシフタのみを使用します")
outCh = mod1.Output()
lastmod = mod1
} else {
log.Print("info: フォルマントシフタとストレッチャを使用します")
mod2 = newF0Splitter(f0, float64(fs), o.FramePeriodMsec)
mod3 = newStretcher(pitchCoef, 1.0, float64(fsOut)/float64(fs))
mod2.input = mod1.Output()
mod3.input = mod2.output
outCh = join(mod3.output)
mod1.Start()
mod2.Start()
lastmod = mod3
}
if o.OutFile == "" {
endCh, stream, err := render(params, paInput, outCh)
if err != nil {
return xerrors.Errorf("出力ストリームのオープンに失敗しました: %w", err)
}
if mod3 != nil {
mod3.resampleCoef = float64(stream.Info().SampleRate) / float64(fs)
}
log.Print("info: 変換を開始しました")
lastmod.Start()
<-endCh
time.Sleep(time.Second)
} else {
lastmod.Start()
result := make([]float64, 0)
log.Print("info: 変換中...")
for {
v, ok := <-outCh
if !ok {
break
}
result = append(result, v)
}
log.Printf("debug: OUT: %d samples, fs=%d", len(result), fsOut)
log.Print("info: 保存中...")
wav.Save(o.OutFile, fsOut, result)
log.Print("info: 完了")
}
return nil
}