forked from happyalu/goflite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voices.go
86 lines (69 loc) · 1.85 KB
/
voices.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
// Copyright 2013, Carnegie Mellon University. All Rights Reserved.
// Use of this code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Author: Alok Parlikar <[email protected]>
package goflite
/*
#include <flite.h>
*/
import "C"
import (
"errors"
"sync"
"unsafe"
)
// Type for pointers to Flite Voice Structures
type flitevoice *C.cst_voice
const defaultVoiceName = "slt"
// Voice db
type voxbase struct {
flitevox map[string]flitevoice // List of voices available for use
mutex sync.RWMutex
}
func newVoxBase() *voxbase {
s := &voxbase{flitevox: make(map[string]flitevoice)}
// Add Default Voice
name := C.CString(defaultVoiceName)
v := C.flite_voice_select(name)
C.free(unsafe.Pointer(name))
if v != nil {
name := C.GoString(v.name)
if name == defaultVoiceName {
s.flitevox[defaultVoiceName] = v
} else {
C.delete_voice(v)
}
}
return s
}
// Add a voice to list of available voices, given a name the voice
// will be known as, and the path to the flitevox file. Preferably use
// absolute voice paths. If no voices are added, the "slt" voice is
// always supported
func (voices *voxbase) addVoice(name, path string) error {
voices.mutex.Lock()
defer voices.mutex.Unlock()
_, present := voices.flitevox[name]
if present {
return errors.New("Voice with given name already present")
}
pathC := C.CString("file://" + path)
defer C.free(unsafe.Pointer(pathC))
v := C.flite_voice_select(pathC)
if v == nil {
return errors.New("Voice File could not be loaded")
}
voices.flitevox[name] = v
return nil
}
// Voices stored in voxbase are C structures that should be freed
func (voices *voxbase) free() {
voices.mutex.Lock()
defer voices.mutex.Unlock()
for name, voice := range voices.flitevox {
if name != defaultVoiceName {
// Default voice is linked in, don't remove it
C.delete_voice(voice)
}
}
}