feat: 增加在线语音 SDK 和音色管理后台,完善引擎适配
This commit is contained in:
1 parent
1856de97bc
commit
a532ba4e0f
71 files changed
+3189
-472
No files matched your search
+105
-21
@@ -1,8 +1,11 @@
|
||||
package tts
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -17,17 +20,19 @@ type Manifest struct {
|
||||
}
|
||||
|
||||
type VoiceEntry struct {
|
||||
Name string `json:"name"`
|
||||
Locale string `json:"locale"`
|
||||
Label string `json:"label"`
|
||||
SpeakerID int `json:"speakerId"`
|
||||
Quality int `json:"quality"`
|
||||
Dir string `json:"dir"`
|
||||
Model string `json:"model"`
|
||||
Lexicon string `json:"lexicon"`
|
||||
Tokens string `json:"tokens"`
|
||||
DataDir string `json:"dataDir"`
|
||||
RuleFsts []string `json:"ruleFsts"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Files map[string]string `json:"files"`
|
||||
Name string `json:"name"`
|
||||
Locale string `json:"locale"`
|
||||
Label string `json:"label"`
|
||||
SpeakerID int `json:"speakerId"`
|
||||
Quality int `json:"quality"`
|
||||
Dir string `json:"dir"`
|
||||
Model string `json:"model"`
|
||||
Lexicon string `json:"lexicon"`
|
||||
Tokens string `json:"tokens"`
|
||||
DataDir string `json:"dataDir"`
|
||||
RuleFsts []string `json:"ruleFsts"`
|
||||
}
|
||||
|
||||
type VoicePack struct {
|
||||
@@ -55,10 +60,17 @@ func LoadRepo(root string) (*Repo, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read manifest: %w", err)
|
||||
}
|
||||
return LoadRepoManifest(root, raw)
|
||||
}
|
||||
|
||||
func LoadRepoManifest(root string, raw []byte) (*Repo, error) {
|
||||
var m Manifest
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
return nil, fmt.Errorf("parse manifest: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(m.Version) == "" {
|
||||
return nil, fmt.Errorf("manifest version is required")
|
||||
}
|
||||
if len(m.Voices) == 0 {
|
||||
return nil, fmt.Errorf("manifest has no voices")
|
||||
}
|
||||
@@ -84,14 +96,14 @@ func LoadRepo(root string) (*Repo, error) {
|
||||
rules = append(rules, filepath.Join(base, f))
|
||||
}
|
||||
pack := &VoicePack{
|
||||
Name: e.Name,
|
||||
Locale: e.Locale,
|
||||
Label: e.Label,
|
||||
SpeakerID: e.SpeakerID,
|
||||
Quality: e.Quality,
|
||||
ModelPath: filepath.Join(base, e.Model),
|
||||
TokensPath: filepath.Join(base, tokens),
|
||||
RuleFsts: strings.Join(rules, ","),
|
||||
Name: e.Name,
|
||||
Locale: e.Locale,
|
||||
Label: e.Label,
|
||||
SpeakerID: e.SpeakerID,
|
||||
Quality: e.Quality,
|
||||
ModelPath: filepath.Join(base, e.Model),
|
||||
TokensPath: filepath.Join(base, tokens),
|
||||
RuleFsts: strings.Join(rules, ","),
|
||||
}
|
||||
if e.Lexicon != "" {
|
||||
pack.LexiconPath = filepath.Join(base, e.Lexicon)
|
||||
@@ -102,6 +114,31 @@ func LoadRepo(root string) (*Repo, error) {
|
||||
if pack.Label == "" {
|
||||
pack.Label = pack.Name
|
||||
}
|
||||
paths := []string{pack.ModelPath, pack.TokensPath}
|
||||
if pack.LexiconPath != "" {
|
||||
paths = append(paths, pack.LexiconPath)
|
||||
}
|
||||
if pack.DataDir != "" {
|
||||
paths = append(paths, pack.DataDir)
|
||||
}
|
||||
paths = append(paths, rules...)
|
||||
for _, path := range paths {
|
||||
if err := contained(root, path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := verifyHash(pack.ModelPath, e.SHA256); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for relative, hash := range e.Files {
|
||||
path := filepath.Join(base, relative)
|
||||
if err := contained(root, path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := verifyHash(path, hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := pack.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -127,9 +164,17 @@ func (p *VoicePack) validate() error {
|
||||
required = append(required, strings.Split(p.RuleFsts, ",")...)
|
||||
}
|
||||
for _, path := range required {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("voice %q: %w", p.Name, err)
|
||||
}
|
||||
if path == p.DataDir {
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("voice %q: dataDir must be a directory", p.Name)
|
||||
}
|
||||
} else if !info.Mode().IsRegular() {
|
||||
return fmt.Errorf("voice %q: resource must be a regular file", p.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -148,6 +193,11 @@ func (r *Repo) Resolve(requested string) *VoicePack {
|
||||
if p, ok := r.byName[requested]; ok {
|
||||
return p
|
||||
}
|
||||
for _, p := range r.Packs {
|
||||
if strings.EqualFold(strings.ReplaceAll(requested, "_", "-"), p.Locale) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
want := normalizeLanguage(requested)
|
||||
if want != "" {
|
||||
for _, p := range r.Packs {
|
||||
@@ -178,4 +228,38 @@ func normalizeLanguage(tag string) string {
|
||||
default:
|
||||
return tag
|
||||
}
|
||||
}
|
||||
}
|
||||
func contained(root, path string) error {
|
||||
base, err := filepath.EvalSymlinks(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
target, err := filepath.EvalSymlinks(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, err := filepath.Rel(base, target)
|
||||
if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || filepath.IsAbs(rel) {
|
||||
return fmt.Errorf("voice path outside root: %s", path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func verifyHash(path, expected string) error {
|
||||
decoded, err := hex.DecodeString(expected)
|
||||
if err != nil || len(decoded) != sha256.Size {
|
||||
return fmt.Errorf("missing/invalid SHA256: %s", path)
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
hash := sha256.New()
|
||||
if _, err := io.Copy(hash, f); err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.EqualFold(hex.EncodeToString(hash.Sum(nil)), expected) {
|
||||
return fmt.Errorf("SHA256 mismatch: %s", path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user