- android-engine:TextToSpeechService 引擎,含设置页、语音包导入(SHA-256 校验)与预热 - server:Go CPU 合成服务与部署文件 - test-client:端到端验证客户端 - 模型与语音包不入库,见 models/README.md
181 lines
4.3 KiB
Go
181 lines
4.3 KiB
Go
package tts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Manifest mirrors android-engine voices.example/manifest.json so a single
|
|
// file configures both the Android engine and this server.
|
|
type Manifest struct {
|
|
Version string `json:"version"`
|
|
Default string `json:"default"`
|
|
Voices []VoiceEntry `json:"voices"`
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
type VoicePack struct {
|
|
Name string
|
|
Locale string
|
|
Label string
|
|
SpeakerID int
|
|
Quality int
|
|
ModelPath string
|
|
LexiconPath string
|
|
TokensPath string
|
|
DataDir string
|
|
RuleFsts string
|
|
}
|
|
|
|
type Repo struct {
|
|
Version string
|
|
Default string
|
|
Packs []*VoicePack
|
|
byName map[string]*VoicePack
|
|
}
|
|
|
|
func LoadRepo(root string) (*Repo, error) {
|
|
raw, err := os.ReadFile(filepath.Join(root, "manifest.json"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read manifest: %w", err)
|
|
}
|
|
var m Manifest
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return nil, fmt.Errorf("parse manifest: %w", err)
|
|
}
|
|
if len(m.Voices) == 0 {
|
|
return nil, fmt.Errorf("manifest has no voices")
|
|
}
|
|
|
|
repo := &Repo{Version: m.Version, Default: m.Default, byName: make(map[string]*VoicePack, len(m.Voices))}
|
|
for _, e := range m.Voices {
|
|
if e.Name == "" {
|
|
return nil, fmt.Errorf("voice entry without name")
|
|
}
|
|
if _, dup := repo.byName[e.Name]; dup {
|
|
return nil, fmt.Errorf("duplicate voice %q", e.Name)
|
|
}
|
|
base := root
|
|
if e.Dir != "" {
|
|
base = filepath.Join(root, e.Dir)
|
|
}
|
|
tokens := e.Tokens
|
|
if tokens == "" {
|
|
tokens = "tokens.txt"
|
|
}
|
|
rules := make([]string, 0, len(e.RuleFsts))
|
|
for _, f := range e.RuleFsts {
|
|
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, ","),
|
|
}
|
|
if e.Lexicon != "" {
|
|
pack.LexiconPath = filepath.Join(base, e.Lexicon)
|
|
}
|
|
if e.DataDir != "" {
|
|
pack.DataDir = filepath.Join(base, e.DataDir)
|
|
}
|
|
if pack.Label == "" {
|
|
pack.Label = pack.Name
|
|
}
|
|
if err := pack.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
repo.Packs = append(repo.Packs, pack)
|
|
repo.byName[pack.Name] = pack
|
|
}
|
|
|
|
if _, ok := repo.byName[repo.Default]; !ok {
|
|
repo.Default = repo.Packs[0].Name
|
|
}
|
|
return repo, nil
|
|
}
|
|
|
|
func (p *VoicePack) validate() error {
|
|
required := []string{p.ModelPath, p.TokensPath}
|
|
if p.LexiconPath != "" {
|
|
required = append(required, p.LexiconPath)
|
|
}
|
|
if p.DataDir != "" {
|
|
required = append(required, p.DataDir)
|
|
}
|
|
if p.RuleFsts != "" {
|
|
required = append(required, strings.Split(p.RuleFsts, ",")...)
|
|
}
|
|
for _, path := range required {
|
|
if _, err := os.Stat(path); err != nil {
|
|
return fmt.Errorf("voice %q: %w", p.Name, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Repo) DefaultPack() *VoicePack {
|
|
return r.byName[r.Default]
|
|
}
|
|
|
|
// Resolve accepts a voice name or a BCP-47 language tag and falls back to the
|
|
// default pack, matching VoiceRepository.resolve on the Android side.
|
|
func (r *Repo) Resolve(requested string) *VoicePack {
|
|
requested = strings.TrimSpace(requested)
|
|
if requested == "" {
|
|
return r.DefaultPack()
|
|
}
|
|
if p, ok := r.byName[requested]; ok {
|
|
return p
|
|
}
|
|
want := normalizeLanguage(requested)
|
|
if want != "" {
|
|
for _, p := range r.Packs {
|
|
if languageOf(p.Locale) == want {
|
|
return p
|
|
}
|
|
}
|
|
}
|
|
return r.DefaultPack()
|
|
}
|
|
|
|
func languageOf(locale string) string {
|
|
tag := strings.ToLower(strings.ReplaceAll(locale, "_", "-"))
|
|
if i := strings.IndexByte(tag, '-'); i >= 0 {
|
|
tag = tag[:i]
|
|
}
|
|
return normalizeLanguage(tag)
|
|
}
|
|
|
|
func normalizeLanguage(tag string) string {
|
|
tag = strings.ToLower(strings.ReplaceAll(strings.TrimSpace(tag), "_", "-"))
|
|
if i := strings.IndexByte(tag, '-'); i >= 0 {
|
|
tag = tag[:i]
|
|
}
|
|
switch tag {
|
|
case "cmn", "zho", "zh":
|
|
return "zh"
|
|
default:
|
|
return tag
|
|
}
|
|
} |