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.
90 lines
2.6 KiB
Go
90 lines
2.6 KiB
Go
// 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
|
|
}
|