Provide common date/time formatting, locale voice matching, tone synthesis, phonetic spelling, and one hardware synthesizer protocol encoder for both readers. Standardize whiskey and x ray and use the tested Fenrir DECtalk mapping. Validated the common module with GOWORK=off go test -race ./...; reader integration, serial cancellation, PCM hashes, no-CGO datetime, and speech regression checks passed. Physical keyboard, audible synthesis, and braille acceptance remain separate live checks.
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package speechdispatcher
|
|
|
|
import (
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
func NormalizeLanguage(value string) string {
|
|
value = strings.Split(strings.Split(value, ".")[0], "@")[0]
|
|
return strings.ToLower(strings.ReplaceAll(value, "_", "-"))
|
|
}
|
|
|
|
// LanguageVoices follows Cthulhu's eSpeak selection: exact dialect, then base
|
|
// language, with unmodified names before '+' variants. Empty means locale.
|
|
// Unknown locales retain the full catalog; explicit languages remain exact.
|
|
func LanguageVoices(voices []Voice, language string) []Voice {
|
|
explicit := language != ""
|
|
if !explicit {
|
|
for _, key := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
|
|
if language = os.Getenv(key); language != "" {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
language = NormalizeLanguage(language)
|
|
if !explicit && (language == "" || language == "c" || language == "posix") {
|
|
language = "en-us"
|
|
}
|
|
languages := []string{language}
|
|
if !explicit {
|
|
languages = append(languages, strings.SplitN(language, "-", 2)[0])
|
|
}
|
|
for _, match := range languages {
|
|
var selected []Voice
|
|
for _, voice := range voices {
|
|
if NormalizeLanguage(voice.Language) == match {
|
|
selected = append(selected, voice)
|
|
}
|
|
}
|
|
if len(selected) > 0 {
|
|
sort.SliceStable(selected, func(i, j int) bool {
|
|
return !strings.Contains(selected[i].Name, "+") && strings.Contains(selected[j].Name, "+")
|
|
})
|
|
return selected
|
|
}
|
|
}
|
|
if explicit {
|
|
return nil
|
|
}
|
|
return voices
|
|
}
|