Use shared reader core v0.4.0

Use common date/time formatting, locale voice selection, tone synthesis, phonetic spelling, and hardware command encoding. Share whiskey and x ray spelling and retain reader-owned device I/O and presentation.

Validated affected race-enabled speech, sound, presentation, date/time, application, review, and preferences tests and standalone builds with GOWORK=off against the published module. Physical hardware, audible synthesis, keyboard, and braille remain separate live checks.
This commit is contained in:
ak47 committed 2026-11-12 23:45:10 -05:00
1 parent 0dd8e2f9c7
commit 24840d355c
9 files changed
+43 -272

No files matched your search

+2 -46
View File
@@ -6,8 +6,6 @@ package speech
import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"
"unicode"
@@ -115,50 +113,8 @@ func parse_voices(rows []string) ([]Voice, error) {
return voices, nil
}
func Normalize_language(value string) string {
value = strings.Split(strings.Split(value, ".")[0], "@")[0]
return strings.ToLower(strings.ReplaceAll(value, "_", "-"))
}
// Language_voices 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 Language_voices(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 = Normalize_language(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 Normalize_language(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
}
var Normalize_language = sd.NormalizeLanguage
var Language_voices = sd.LanguageVoices
// Prepare checks membership because SSIP SET may accept unknown synthesis
// names without verifying them. The resulting client has not spoken anything.
-27
View File
@@ -5,7 +5,6 @@ package speech
import (
"fmt"
"reflect"
"strings"
"testing"
)
@@ -58,32 +57,6 @@ func TestBoundedRepliesAndInjection(t *testing.T) {
}
}
func TestEspeakLocaleDialectBaseAndExplicitLanguage(t *testing.T) {
voices := []Voice{{Name: "US+Variant", Language: "en-US"}, {Name: "German", Language: "de"}, {Name: "US", Language: "en-US"}, {Name: "English", Language: "en"}}
t.Setenv("LC_ALL", "en_US.UTF-8")
got := Language_voices(voices, "")
if !reflect.DeepEqual(got, []Voice{voices[2], voices[0]}) {
t.Fatal(got)
}
t.Setenv("LC_ALL", "en_AU.UTF-8")
if got := Language_voices(voices, ""); !reflect.DeepEqual(got, []Voice{voices[3]}) {
t.Fatal(got)
}
if got := Language_voices(voices, "de"); !reflect.DeepEqual(got, []Voice{voices[1]}) {
t.Fatal(got)
}
if got := Language_voices(voices, "unknown"); len(got) != 0 {
t.Fatal(got)
}
t.Setenv("LC_ALL", "zz_ZZ")
if got := Language_voices(voices, ""); !reflect.DeepEqual(got, voices) {
t.Fatal(got)
}
if voices[0].Name != "US+Variant" {
t.Fatal("catalog reordered in place")
}
}
func TestCandidateRejectionLeavesOriginalClientUsable(t *testing.T) {
server := new_fake_server(t, false)
original, err := Open(Options{Socket: server.path})
+11 -43
View File
@@ -6,12 +6,13 @@ package speech
import (
"errors"
"fmt"
"math"
"path/filepath"
"slices"
"strings"
"sync"
"sync/atomic"
"git.stormux.org/storm/screenreader-common/hardware"
)
var errHardwareCanceled = errors.New("hardware speech write canceled")
@@ -100,25 +101,15 @@ func hardware_parameters(options Options) error {
return parameters.Validate()
}
// Python's normalized 0..1 values correspond to the shared -100..100 scale.
// RoundToEven matches Python round, including LiteTalk's midpoint volume 4.
func hardware_scale(value, minimum, maximum int) int {
return int(math.RoundToEven(float64(minimum) + float64(value+100)*float64(maximum-minimum)/200))
}
func dectalk_scale(value, minimum, normal, maximum int) int {
if value <= 0 {
return hardware_scale(value*2+100, minimum, normal)
func (c *HardwareClient) profile() hardware.Protocol {
if c.usb {
return hardware.DECtalkUSB()
}
return hardware_scale(value*2-100, normal, maximum)
return hardware.Serial(c.dectalk)
}
func (c *HardwareClient) commands() [3]string {
p := c.parameters
if c.dectalk {
return [3]string{fmt.Sprintf("[:ra %d]", dectalk_scale(p.Rate, 75, 180, 650)), fmt.Sprintf("[:dv ap %d]", dectalk_scale(p.Pitch, 50, 122, 350)), fmt.Sprintf("[:dv g5 %d]", hardware_scale(p.Volume, 60, 86))}
}
return [3]string{fmt.Sprintf("\x01%dS", hardware_scale(p.Rate, 0, 9)), fmt.Sprintf("\x01%dP", hardware_scale(p.Pitch, 0, 99)), fmt.Sprintf("\x01%dV", hardware_scale(p.Volume, 0, 9))}
rate, pitch, volume := float64(c.parameters.Rate), float64(c.parameters.Pitch), float64(c.parameters.Volume)
return c.profile().Commands(hardware.Parameters{Rate: &rate, Pitch: &pitch, Volume: &volume})
}
func (c *HardwareClient) write(data []byte, generation uint64) error {
@@ -179,20 +170,7 @@ func (c *HardwareClient) Say(text string) error {
if err := c.configure(generation); err != nil {
return ignore_hardware_cancel(err)
}
var clean strings.Builder
for _, char := range text {
if char < 0x20 || char > 0x7e {
char = ' '
}
clean.WriteRune(char)
}
prepared := clean.String()
if c.dectalk {
prepared = strings.ReplaceAll(prepared, "[:", "[ :") + "\x0b"
} else {
prepared += "\r"
}
return ignore_hardware_cancel(c.write([]byte(prepared), generation))
return ignore_hardware_cancel(c.write(c.profile().Text(text), generation))
}
func (c *HardwareClient) Cancel() error {
@@ -200,12 +178,7 @@ func (c *HardwareClient) Cancel() error {
if c.closed.Load() {
return nil
}
command := []byte{0x18}
if c.usb {
command = []byte{0x06}
} else if c.dectalk {
command = []byte{0x03}
}
command := c.profile().Cancel()
return c.port.Cancel(command)
}
@@ -216,12 +189,7 @@ func (c *HardwareClient) Close() {
c.generation.Add(1)
// Cancel audible device buffers before releasing ownership. Port writes
// are bounded, including when the device is disconnected or flow-stopped.
command := []byte{0x18}
if c.usb {
command = []byte{0x06}
} else if c.dectalk {
command = []byte{0x03}
}
command := c.profile().Cancel()
_ = c.port.Cancel(command)
c.port.Close()
}