Share date, voice, tone, spelling, and hardware behavior
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.
This commit is contained in:
1 parent
b9a33f7578
commit
e7bd1d0043
15 files changed
+809
-3
No files matched your search
@@ -0,0 +1,89 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Package hardware encodes hardware speech protocols without owning devices.
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Protocol selects a device, not a reader-specific dialect. The zero value is
|
||||
// the LiteTalk-compatible protocol used by LiteTalk, DoubleTalk and TripleTalk.
|
||||
type Protocol struct{ dectalk, usb bool }
|
||||
|
||||
func Serial(dectalk bool) Protocol { return Protocol{dectalk: dectalk} }
|
||||
func DECtalkUSB() Protocol { return Protocol{dectalk: true, usb: true} }
|
||||
|
||||
// Parameters uses the same -100..100 scale as Speech Dispatcher. Nil retains
|
||||
// the device's existing setting. Readers convert legacy saved scales at entry.
|
||||
type Parameters struct{ Rate, Pitch, Volume *float64 }
|
||||
|
||||
func scale(value float64, minimum, maximum int) int {
|
||||
fraction := (math.Max(-100, math.Min(100, value)) + 100) / 200
|
||||
return int(math.RoundToEven(float64(minimum) + fraction*float64(maximum-minimum)))
|
||||
}
|
||||
func centered(value float64, minimum, normal, maximum int) int {
|
||||
if value <= 0 {
|
||||
return scale(value*2+100, minimum, normal)
|
||||
}
|
||||
return scale(value*2-100, normal, maximum)
|
||||
}
|
||||
|
||||
// Commands applies one common mapping and encoding for each synthesizer.
|
||||
func (p Protocol) Commands(parameters Parameters) [3]string {
|
||||
var commands [3]string
|
||||
for index, value := range []*float64{parameters.Rate, parameters.Pitch, parameters.Volume} {
|
||||
if value == nil {
|
||||
continue
|
||||
}
|
||||
native := scale(*value, 0, []int{9, 99, 9}[index])
|
||||
if p.dectalk {
|
||||
switch index {
|
||||
case 0:
|
||||
native = centered(*value, 75, 180, 650)
|
||||
case 1:
|
||||
native = centered(*value, 50, 122, 350)
|
||||
case 2:
|
||||
native = scale(*value, 60, 86)
|
||||
}
|
||||
commands[index] = fmt.Sprintf("[:%s %d]", []string{"ra", "dv ap", "dv g5"}[index], native)
|
||||
} else {
|
||||
commands[index] = fmt.Sprintf("\x01%d%c", native, []byte{'S', 'P', 'V'}[index])
|
||||
}
|
||||
}
|
||||
return commands
|
||||
}
|
||||
|
||||
// Text replaces non-ASCII/control runes and prevents DECtalk command injection.
|
||||
func (p Protocol) Text(text string) []byte {
|
||||
var clean strings.Builder
|
||||
for _, char := range text {
|
||||
if char < 0x20 || char > 0x7e {
|
||||
char = ' '
|
||||
}
|
||||
clean.WriteRune(char)
|
||||
}
|
||||
text = clean.String()
|
||||
if p.dectalk {
|
||||
return append([]byte(strings.ReplaceAll(text, "[:", "[ :")), 0x0b)
|
||||
}
|
||||
return append([]byte(text), '\r')
|
||||
}
|
||||
func (p Protocol) Cancel() []byte {
|
||||
if p.usb {
|
||||
return []byte{6}
|
||||
}
|
||||
if p.dectalk {
|
||||
return []byte{3}
|
||||
}
|
||||
return []byte{0x18}
|
||||
}
|
||||
|
||||
// LegacyValue converts a saved 0..maximum reader value to the common scale.
|
||||
func LegacyValue(value, maximum float64) float64 {
|
||||
return 200*math.Max(0, math.Min(maximum, value))/maximum - 100
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package hardware
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDeviceProtocolsAndNormalizedSettings(t *testing.T) {
|
||||
for _, row := range []struct {
|
||||
value float64
|
||||
lite, dectalk [3]string
|
||||
}{
|
||||
{-100, [3]string{"\x010S", "\x010P", "\x010V"}, [3]string{"[:ra 75]", "[:dv ap 50]", "[:dv g5 60]"}},
|
||||
{0, [3]string{"\x014S", "\x0150P", "\x014V"}, [3]string{"[:ra 180]", "[:dv ap 122]", "[:dv g5 73]"}},
|
||||
{100, [3]string{"\x019S", "\x0199P", "\x019V"}, [3]string{"[:ra 650]", "[:dv ap 350]", "[:dv g5 86]"}},
|
||||
} {
|
||||
for _, device := range []struct {
|
||||
protocol Protocol
|
||||
want [3]string
|
||||
}{{Serial(false), row.lite}, {Serial(true), row.dectalk}, {DECtalkUSB(), row.dectalk}} {
|
||||
value := row.value
|
||||
if got := device.protocol.Commands(Parameters{&value, &value, &value}); got != device.want {
|
||||
t.Fatalf("%v: got %q, want %q", value, got, device.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, row := range []struct {
|
||||
protocol Protocol
|
||||
text string
|
||||
cancel byte
|
||||
}{
|
||||
{Serial(false), "A [ :x] [:x]\r", 0x18},
|
||||
{Serial(true), "A [ :x] [ :x]\x0b", 3},
|
||||
{DECtalkUSB(), "A [ :x] [ :x]\x0b", 6},
|
||||
} {
|
||||
if got := string(row.protocol.Text("A\n猫[ :x] [:x]")); got != row.text {
|
||||
t.Fatalf("unsafe or changed text: %q", got)
|
||||
}
|
||||
if got := row.protocol.Cancel(); len(got) != 1 || got[0] != row.cancel {
|
||||
t.Fatalf("cancel: %q", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestLegacySettingsAndOmittedFields(t *testing.T) {
|
||||
for _, row := range []struct{ value, maximum, want float64 }{{0, 99, -100}, {49.5, 99, 0}, {99, 99, 100}, {4.5, 9, 0}, {1000, 9, 100}, {-3, 9, -100}} {
|
||||
if got := LegacyValue(row.value, row.maximum); got != row.want {
|
||||
t.Fatal(row, got)
|
||||
}
|
||||
}
|
||||
value := float64(0)
|
||||
if got := Serial(true).Commands(Parameters{Pitch: &value}); got != [3]string{"", "[:dv ap 122]", ""} {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user