190 lines
5.1 KiB
Go
190 lines
5.1 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Package voiceprofiles owns primary-voice presets, independent of reader storage
|
|
// and speech transports. Selecting a preset never changes echo or message voices.
|
|
package voiceprofiles
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
sd "git.stormux.org/storm/screenreader-common/speechdispatcher"
|
|
)
|
|
|
|
type Profile struct {
|
|
Name string `toml:"name"`
|
|
Driver string `toml:"driver"`
|
|
Module string `toml:"module,omitempty"`
|
|
Language string `toml:"language,omitempty"`
|
|
Voice string `toml:"voice,omitempty"`
|
|
Variant string `toml:"variant,omitempty"`
|
|
Rate int `toml:"rate"`
|
|
Pitch int `toml:"pitch"`
|
|
Volume int `toml:"volume"`
|
|
Device string `toml:"device,omitempty"`
|
|
BaudRate int `toml:"baud-rate,omitempty"`
|
|
}
|
|
|
|
type Collection struct {
|
|
Profiles []Profile `toml:"profiles,omitempty"`
|
|
}
|
|
|
|
func (c Collection) Clone() Collection { return Collection{Profiles: slices.Clone(c.Profiles)} }
|
|
|
|
type Driver struct {
|
|
ID, Label string
|
|
Hardware, Serial bool
|
|
}
|
|
|
|
func Drivers() []Driver {
|
|
return []Driver{{ID: "speech-dispatcher", Label: "Speech Dispatcher"},
|
|
{ID: "dectalk", Label: "DECtalk", Hardware: true, Serial: true},
|
|
{ID: "dectalk-usb", Label: "DECtalk USB", Hardware: true},
|
|
{ID: "litetalk", Label: "LiteTalk", Hardware: true, Serial: true},
|
|
{ID: "doubletalk", Label: "DoubleTalk LT", Hardware: true, Serial: true},
|
|
{ID: "tripletalk", Label: "TripleTalk", Hardware: true, Serial: true}}
|
|
}
|
|
|
|
func driverFor(id string) (Driver, bool) {
|
|
for _, driver := range Drivers() {
|
|
if driver.ID == id {
|
|
return driver, true
|
|
}
|
|
}
|
|
return Driver{}, false
|
|
}
|
|
|
|
// Normalize supplies the saved name when the editor's name field is blank.
|
|
func (p Profile) Normalize() Profile {
|
|
p.Name = strings.TrimSpace(p.Name)
|
|
if p.Name == "" {
|
|
driver, _ := driverFor(p.Driver)
|
|
if !driver.Hardware {
|
|
p.Name = strings.TrimSpace(strings.Join([]string{p.Module, p.Voice}, " "))
|
|
}
|
|
if p.Name == "" {
|
|
p.Name = driver.Label
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (p Profile) Validate() error {
|
|
if p.Name == "" || p.Name != strings.TrimSpace(p.Name) || utf8.RuneCountInString(p.Name) > 128 || !utf8.ValidString(p.Name) {
|
|
return fmt.Errorf("Enter a voice profile name of at most 128 characters")
|
|
}
|
|
for _, char := range p.Name {
|
|
if unicode.IsControl(char) {
|
|
return fmt.Errorf("Voice profile names must be single-line text")
|
|
}
|
|
}
|
|
driver, ok := driverFor(p.Driver)
|
|
if !ok {
|
|
return fmt.Errorf("Choose a supported synthesizer")
|
|
}
|
|
for _, number := range []int{p.Rate, p.Pitch, p.Volume} {
|
|
if number < -100 || number > 100 {
|
|
return fmt.Errorf("Voice rate, pitch and volume must be between -100 and 100")
|
|
}
|
|
}
|
|
if err := sd.ValidSelection(p.Module, p.Language, p.Voice); err != nil {
|
|
return err
|
|
}
|
|
if len(p.Variant) > 1024 || strings.IndexFunc(p.Variant, unicode.IsControl) >= 0 {
|
|
return fmt.Errorf("Invalid voice variant")
|
|
}
|
|
if driver.Serial {
|
|
if !filepath.IsAbs(p.Device) || len(p.Device) > 1024 || strings.IndexFunc(p.Device, unicode.IsControl) >= 0 {
|
|
return fmt.Errorf("Choose an absolute serial device path")
|
|
}
|
|
if !slices.Contains(BaudRates, p.BaudRate) {
|
|
return fmt.Errorf("Choose a supported serial baud rate")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var BaudRates = []int{1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}
|
|
|
|
func (c Collection) Validate() error {
|
|
if len(c.Profiles) > 128 {
|
|
return fmt.Errorf("At most 128 voice profiles can be saved")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, p := range c.Profiles {
|
|
if err := p.Validate(); err != nil {
|
|
return err
|
|
}
|
|
name := strings.ToLower(p.Name)
|
|
if seen[name] {
|
|
return fmt.Errorf("A voice profile named %q already exists", p.Name)
|
|
}
|
|
seen[name] = true
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Put replaces only the explicitly edited profile; duplicate names never overwrite.
|
|
func (c Collection) Put(index int, p Profile) (Collection, error) {
|
|
if index < -1 || index >= len(c.Profiles) {
|
|
return c, fmt.Errorf("Voice profile no longer exists")
|
|
}
|
|
next := c.Clone()
|
|
p = p.Normalize()
|
|
if index == -1 {
|
|
next.Profiles = append(next.Profiles, p)
|
|
} else {
|
|
next.Profiles[index] = p
|
|
}
|
|
if err := next.Validate(); err != nil {
|
|
return c, err
|
|
}
|
|
return next, nil
|
|
}
|
|
|
|
func (c Collection) Remove(index int) Collection {
|
|
next := c.Clone()
|
|
if index >= 0 && index < len(next.Profiles) {
|
|
next.Profiles = slices.Delete(next.Profiles, index, index+1)
|
|
}
|
|
return next
|
|
}
|
|
|
|
func SameVoice(a, b Profile) bool {
|
|
a.Name = ""
|
|
b.Name = ""
|
|
a.Variant = ""
|
|
b.Variant = ""
|
|
// Device and baud rate only matter to serial drivers; an editor can keep
|
|
// hidden values for them, such as a DECtalk USB profile's serial fields.
|
|
for _, p := range []*Profile{&a, &b} {
|
|
if driver, _ := driverFor(p.Driver); !driver.Serial {
|
|
p.Device, p.BaudRate = "", 0
|
|
}
|
|
}
|
|
return a == b
|
|
}
|
|
|
|
func (c Collection) Matching(p Profile) string {
|
|
for _, candidate := range c.Profiles {
|
|
if SameVoice(candidate, p) {
|
|
return candidate.Name
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (c Collection) Find(name string) (Profile, bool) {
|
|
for _, p := range c.Profiles {
|
|
if p.Name == name {
|
|
return p, true
|
|
}
|
|
}
|
|
return Profile{}, false
|
|
}
|