Use shared quick settings and voice discovery from reader core v0.5.0
This commit is contained in:
1 parent
24840d355c
commit
5aef0be169
10 files changed
+151
-244
No files matched your search
@@ -98,10 +98,7 @@ func (a *application) load_application_profile(selected *applicationOverride) {
|
||||
func (a *application) capture_override(terminal int) overrideSnapshot {
|
||||
settings := a.settings
|
||||
settings.remoteFields = maps.Clone(settings.remoteFields)
|
||||
quick := *a.quick
|
||||
quick.fields = maps.Clone(quick.fields)
|
||||
quick.categories = slices.Clone(quick.categories)
|
||||
return overrideSnapshot{settings, quick, a.engine.Capture_terminal_session(terminal), a.muted, a.temporarySilence}
|
||||
return overrideSnapshot{settings, a.quick.clone(), a.engine.Capture_terminal_session(terminal), a.muted, a.temporarySilence}
|
||||
}
|
||||
|
||||
func (a *application) install_override(saved overrideSnapshot, terminal int) {
|
||||
@@ -109,7 +106,7 @@ func (a *application) install_override(saved overrideSnapshot, terminal int) {
|
||||
live.Presentation.Speech.Enabled = !saved.muted
|
||||
a.apply_remote_state(live, false)
|
||||
a.settings = saved.settings
|
||||
*a.quick = saved.quick
|
||||
*a.quick = saved.quick.clone()
|
||||
a.muted, a.temporarySilence = saved.muted, saved.silence
|
||||
a.configure_reader()
|
||||
a.engine.Restore_session(saved.runtime, terminal)
|
||||
|
||||
+127
-140
@@ -20,9 +20,9 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -31,18 +31,13 @@ import (
|
||||
"git.stormux.org/storm/fenrir/internal/i18n"
|
||||
"git.stormux.org/storm/fenrir/internal/preferences"
|
||||
"git.stormux.org/storm/fenrir/internal/speech"
|
||||
"git.stormux.org/storm/screenreader-common/quicksettings"
|
||||
)
|
||||
|
||||
type quickCategory struct {
|
||||
label string
|
||||
entries []config.Definition
|
||||
position int
|
||||
}
|
||||
|
||||
type quickSettings struct {
|
||||
selectionChanged bool
|
||||
categories []quickCategory
|
||||
category int
|
||||
menu *quicksettings.Menu
|
||||
definitions map[string]config.Definition
|
||||
changed bool
|
||||
discovery preferences.Discovery
|
||||
selectSpeech func(config.Speech, func(error, bool))
|
||||
@@ -61,9 +56,10 @@ func new_quick_settings(driverID string) *quickSettings {
|
||||
}
|
||||
}
|
||||
visit(config.Sections())
|
||||
menu := &quickSettings{}
|
||||
menu := &quickSettings{definitions: make(map[string]config.Definition)}
|
||||
var categories []quicksettings.Category
|
||||
for _, section := range config.Ordered_sections(sections, []string{"presentation.speech", "reading", "keyboard"}) {
|
||||
category := quickCategory{label: section.Label}
|
||||
category := quicksettings.Category{ID: section.ID, Label: section.Label}
|
||||
for _, definition := range section.Settings {
|
||||
if definition.ChoiceSource == "speech-module" && driver.Modules == nil {
|
||||
continue
|
||||
@@ -71,23 +67,88 @@ func new_quick_settings(driverID string) *quickSettings {
|
||||
if (definition.ChoiceSource == "speech-language" || definition.ChoiceSource == "speech-voice") && driver.Voices == nil {
|
||||
continue
|
||||
}
|
||||
if definition.QuickLabel != "" {
|
||||
category.entries = append(category.entries, definition)
|
||||
if definition.QuickLabel == "" {
|
||||
continue
|
||||
}
|
||||
entry := quicksettings.Entry{ID: definition.Path, Label: definition.QuickLabel}
|
||||
switch {
|
||||
case definition.Kind == reflect.Bool:
|
||||
entry.Kind = quicksettings.Toggle
|
||||
case definition.ChoiceSource != "":
|
||||
entry.Kind, entry.EmptyIsDefault = quicksettings.Choice, speech_choice(definition.ChoiceSource)
|
||||
default:
|
||||
// Python's 0.05 normalized step on the 200-point SSIP scale.
|
||||
entry.Kind, entry.Min, entry.Max, entry.Step = quicksettings.Range, int(definition.Min), int(definition.Max), 10
|
||||
}
|
||||
menu.definitions[entry.ID] = definition
|
||||
category.Entries = append(category.Entries, entry)
|
||||
}
|
||||
if section.ID == "presentation.speech" {
|
||||
ranks := map[string]int{"rate": 0, "pitch": 1, "volume": 2, "module": 3, "language": 4, "voice": 5}
|
||||
sort.SliceStable(category.entries, func(i, j int) bool {
|
||||
return ranks[strings.TrimPrefix(category.entries[i].Path, "presentation.speech.")] < ranks[strings.TrimPrefix(category.entries[j].Path, "presentation.speech.")]
|
||||
sort.SliceStable(category.Entries, func(i, j int) bool {
|
||||
return ranks[strings.TrimPrefix(category.Entries[i].ID, "presentation.speech.")] < ranks[strings.TrimPrefix(category.Entries[j].ID, "presentation.speech.")]
|
||||
})
|
||||
}
|
||||
if len(category.entries) > 0 {
|
||||
menu.categories = append(menu.categories, category)
|
||||
}
|
||||
categories = append(categories, category)
|
||||
}
|
||||
menu.menu = quicksettings.New(categories, quicksettings.Messages{Translate: i18n.T})
|
||||
return menu
|
||||
}
|
||||
|
||||
// clone keeps override snapshots independent of later navigation.
|
||||
func (q quickSettings) clone() quickSettings {
|
||||
q.fields = maps.Clone(q.fields)
|
||||
q.menu = q.menu.Clone()
|
||||
return q
|
||||
}
|
||||
|
||||
// quickHost reads Fenrir's typed settings for the shared menu.
|
||||
type quickHost struct {
|
||||
quick *quickSettings
|
||||
settings *config.Settings
|
||||
}
|
||||
|
||||
func (h quickHost) Value(entry quicksettings.Entry) quicksettings.Value {
|
||||
switch value := h.quick.definitions[entry.ID].Value(*h.settings).(type) {
|
||||
case bool:
|
||||
return quicksettings.Value{Checked: value}
|
||||
case string:
|
||||
return quicksettings.Value{Choice: value}
|
||||
case int:
|
||||
return quicksettings.Value{Number: value}
|
||||
}
|
||||
panic("unsupported quick setting: " + entry.ID)
|
||||
}
|
||||
|
||||
func (h quickHost) Options(entry quicksettings.Entry) []quicksettings.Option {
|
||||
definition := h.quick.definitions[entry.ID]
|
||||
choices := definition.Choices(*h.settings)
|
||||
if speech_choice(definition.ChoiceSource) {
|
||||
choices = h.quick.discovery.Choices(*h.settings)[entry.ID]
|
||||
}
|
||||
options := make([]quicksettings.Option, 0, len(choices))
|
||||
for _, choice := range choices {
|
||||
options = append(options, quicksettings.Option{ID: choice.ID, Label: choice.Label})
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
func (h quickHost) set(entry quicksettings.Entry, value quicksettings.Value, settings *config.Settings) {
|
||||
definition := h.quick.definitions[entry.ID]
|
||||
var typed any
|
||||
switch entry.Kind {
|
||||
case quicksettings.Toggle:
|
||||
typed = value.Checked
|
||||
case quicksettings.Choice:
|
||||
typed = value.Choice
|
||||
default:
|
||||
typed = value.Number
|
||||
}
|
||||
if err := definition.Set(settings, typed); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Overlay only the curated quick settings, preserving file-only and session
|
||||
// choices such as automatic reading, mute and the attribute feedback override.
|
||||
func copy_quick_settings(target *config.Settings, source config.Settings) {
|
||||
@@ -105,140 +166,66 @@ func speech_parameters(settings config.Settings) speech.Parameters {
|
||||
return speech.Parameters{Rate: voice.Rate, Pitch: voice.Pitch, Volume: voice.Volume}
|
||||
}
|
||||
|
||||
var quickCommands = map[command.ID]quicksettings.Command{
|
||||
command.CurrentQuickEntry: quicksettings.CurrentEntry,
|
||||
command.CurrentQuickValue: quicksettings.CurrentValue,
|
||||
command.NextQuickEntry: quicksettings.NextEntry,
|
||||
command.PreviousQuickEntry: quicksettings.PreviousEntry,
|
||||
command.NextQuickValue: quicksettings.NextValue,
|
||||
command.PreviousQuickValue: quicksettings.PreviousValue,
|
||||
command.NextQuickCategory: quicksettings.NextCategory,
|
||||
command.PreviousQuickCategory: quicksettings.PreviousCategory,
|
||||
}
|
||||
|
||||
func (q *quickSettings) command(id command.ID, settings *config.Settings, voice Speaker) bool {
|
||||
switch id {
|
||||
case command.CurrentQuickEntry, command.CurrentQuickValue, command.NextQuickEntry, command.PreviousQuickEntry, command.NextQuickValue, command.PreviousQuickValue, command.NextQuickCategory, command.PreviousQuickCategory:
|
||||
default:
|
||||
quickCommand, ok := quickCommands[id]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if len(q.categories) == 0 {
|
||||
voice.Say(i18n.T("Quick menu not available"))
|
||||
host := quickHost{quick: q, settings: settings}
|
||||
result := q.menu.Handle(quickCommand, host)
|
||||
if !result.Changed {
|
||||
voice.Say(result.Text)
|
||||
return true
|
||||
}
|
||||
categoryChanged := id == command.NextQuickCategory || id == command.PreviousQuickCategory
|
||||
if categoryChanged {
|
||||
step := 1
|
||||
if id == command.PreviousQuickCategory {
|
||||
step = -1
|
||||
definition := q.definitions[result.Entry.ID]
|
||||
if speech_choice(definition.ChoiceSource) && q.selectSpeech != nil {
|
||||
candidate := *settings
|
||||
host.set(result.Entry, result.Value, &candidate)
|
||||
if definition.ChoiceSource == "speech-module" {
|
||||
candidate.Presentation.Speech.Language = ""
|
||||
candidate.Presentation.Speech.Voice = ""
|
||||
}
|
||||
q.category = (q.category + step + len(q.categories)) % len(q.categories)
|
||||
}
|
||||
category := &q.categories[q.category]
|
||||
switch id {
|
||||
case command.NextQuickEntry:
|
||||
category.position = (category.position + 1) % len(category.entries)
|
||||
case command.PreviousQuickEntry:
|
||||
category.position = (category.position + len(category.entries) - 1) % len(category.entries)
|
||||
}
|
||||
entry := category.entries[category.position]
|
||||
previous := entry.Value(*settings)
|
||||
adjusting := id == command.NextQuickValue || id == command.PreviousQuickValue
|
||||
step := 1
|
||||
if id == command.PreviousQuickValue {
|
||||
step = -1
|
||||
}
|
||||
var text string
|
||||
switch {
|
||||
case entry.Kind == reflect.Bool:
|
||||
value := entry.Value(*settings).(bool)
|
||||
if adjusting {
|
||||
value = !value
|
||||
if err := entry.Set(settings, value); err != nil {
|
||||
panic(err)
|
||||
if definition.ChoiceSource == "speech-language" {
|
||||
candidate.Presentation.Speech.Voice = ""
|
||||
}
|
||||
q.selectSpeech(candidate.Presentation.Speech, func(err error, present bool) {
|
||||
if err != nil {
|
||||
slog.Warn("speech selection failed", "error", err, "at", time.Now())
|
||||
if present {
|
||||
voice.Say(i18n.T("Voice test failed"))
|
||||
}
|
||||
return
|
||||
}
|
||||
copy_speech_selection(&settings.Presentation.Speech, candidate.Presentation.Speech)
|
||||
q.changed = true
|
||||
}
|
||||
text = "unchecked"
|
||||
if value {
|
||||
text = "checked"
|
||||
}
|
||||
text = i18n.T(text)
|
||||
case entry.ChoiceSource != "":
|
||||
choices := entry.Choices(*settings)
|
||||
dynamic := entry.ChoiceSource == "speech-module" || entry.ChoiceSource == "speech-language" || entry.ChoiceSource == "speech-voice"
|
||||
if dynamic {
|
||||
choices = q.discovery.Choices(*settings)[entry.Path]
|
||||
}
|
||||
position := 0
|
||||
for index, choice := range choices {
|
||||
if choice.ID == entry.Value(*settings).(string) {
|
||||
position = index
|
||||
break
|
||||
q.selectionChanged = true
|
||||
for _, path := range []string{"presentation.speech.module", "presentation.speech.language", "presentation.speech.voice"} {
|
||||
q.mark_changed(path)
|
||||
}
|
||||
}
|
||||
if adjusting {
|
||||
position = (position + step + len(choices)) % len(choices)
|
||||
if dynamic && q.selectSpeech != nil {
|
||||
candidate := *settings
|
||||
if err := entry.Set(&candidate, choices[position].ID); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if entry.ChoiceSource == "speech-module" {
|
||||
candidate.Presentation.Speech.Language = ""
|
||||
candidate.Presentation.Speech.Voice = ""
|
||||
}
|
||||
if entry.ChoiceSource == "speech-language" {
|
||||
candidate.Presentation.Speech.Voice = ""
|
||||
}
|
||||
message := i18n.T(choices[position].Label)
|
||||
if choices[position].ID == "" {
|
||||
message = i18n.T(entry.QuickLabel) + " " + i18n.T("default")
|
||||
}
|
||||
q.selectSpeech(candidate.Presentation.Speech, func(err error, present bool) {
|
||||
if err != nil {
|
||||
slog.Warn("speech selection failed", "error", err, "at", time.Now())
|
||||
if present {
|
||||
voice.Say(i18n.T("Voice test failed"))
|
||||
}
|
||||
return
|
||||
}
|
||||
copy_speech_selection(&settings.Presentation.Speech, candidate.Presentation.Speech)
|
||||
q.changed = true
|
||||
q.selectionChanged = true
|
||||
for _, path := range []string{"presentation.speech.module", "presentation.speech.language", "presentation.speech.voice"} {
|
||||
q.mark_changed(path)
|
||||
}
|
||||
if present {
|
||||
voice.Say(message)
|
||||
}
|
||||
})
|
||||
return true
|
||||
if present {
|
||||
voice.Say(result.Text)
|
||||
}
|
||||
if err := entry.Set(settings, choices[position].ID); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
q.changed = true
|
||||
}
|
||||
text = i18n.T(choices[position].Label)
|
||||
default:
|
||||
value := entry.Value(*settings).(int)
|
||||
if adjusting {
|
||||
// Python's 0.05 normalized step on the 200-point SSIP scale.
|
||||
next := max(int(entry.Min), min(int(entry.Max), value+step*10))
|
||||
if next != value {
|
||||
if err := entry.Set(settings, next); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
voice.Configure(speech_parameters(*settings))
|
||||
q.changed = true
|
||||
value = next
|
||||
}
|
||||
}
|
||||
text = strconv.Itoa(value)
|
||||
})
|
||||
return true
|
||||
}
|
||||
if !reflect.DeepEqual(previous, entry.Value(*settings)) {
|
||||
q.mark_changed(entry.Path)
|
||||
host.set(result.Entry, result.Value, settings)
|
||||
if result.Entry.Kind == quicksettings.Range {
|
||||
voice.Configure(speech_parameters(*settings))
|
||||
}
|
||||
defaultSpeech := speech_choice(entry.ChoiceSource) && entry.Value(*settings).(string) == ""
|
||||
if defaultSpeech {
|
||||
text = i18n.T(entry.QuickLabel) + " " + i18n.T("default")
|
||||
} else if categoryChanged || id == command.CurrentQuickEntry || id == command.NextQuickEntry || id == command.PreviousQuickEntry {
|
||||
text = i18n.T(entry.QuickLabel) + " " + text
|
||||
}
|
||||
if categoryChanged {
|
||||
text = i18n.T(category.label) + ". " + text
|
||||
}
|
||||
voice.Say(text)
|
||||
q.changed = true
|
||||
q.mark_changed(result.Entry.ID)
|
||||
voice.Say(result.Text)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -366,7 +366,8 @@ func TestQuickEchoCombinationsAndMenuCancelOrSave(t *testing.T) {
|
||||
menu.command(command.PreviousQuickValue, &settings, voice) // punctuation none
|
||||
menu.command(command.NextQuickCategory, &settings, voice)
|
||||
for index := range 3 {
|
||||
entry := menu.categories[menu.category].entries[index]
|
||||
selected, _ := menu.menu.Selected()
|
||||
entry := menu.definitions[selected.ID]
|
||||
if entry.Value(settings).(bool) != (mask&(1<<index) != 0) {
|
||||
menu.command(command.NextQuickValue, &settings, voice)
|
||||
}
|
||||
|
||||
@@ -6,35 +6,19 @@ package speech
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
sd "git.stormux.org/storm/screenreader-common/speechdispatcher"
|
||||
)
|
||||
|
||||
type Voice = sd.Voice
|
||||
|
||||
type Catalog struct {
|
||||
Modules []string
|
||||
Voices map[string][]Voice
|
||||
Errors map[string]error
|
||||
}
|
||||
// Catalog is the shared discovery result: modules, voices keyed "" for the
|
||||
// server default, and per-module errors.
|
||||
type Catalog = sd.Discovery
|
||||
|
||||
func Validate_selection(module, language, voice string) error {
|
||||
if !valid_module(module) || !valid_module(language) {
|
||||
return fmt.Errorf("invalid speech module or language")
|
||||
}
|
||||
if !utf8.ValidString(voice) || len(voice) > 1024 || voice != "" && strings.TrimSpace(voice) == "" {
|
||||
return fmt.Errorf("invalid speech voice")
|
||||
}
|
||||
for _, char := range voice {
|
||||
if unicode.IsControl(char) {
|
||||
return fmt.Errorf("invalid speech voice")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return sd.ValidSelection(module, language, voice)
|
||||
}
|
||||
|
||||
// Discover never speaks or changes the active client. Each query has a short
|
||||
@@ -49,68 +33,7 @@ func Discover(options Options) (Catalog, error) {
|
||||
defer w.Close()
|
||||
ctx, cancel := context.WithTimeout(option_context(options), 4*time.Second)
|
||||
defer cancel()
|
||||
query := func(command string) ([]string, error) { return w.Command(ctx, command) }
|
||||
|
||||
modules, err := query("LIST OUTPUT_MODULES")
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, module := range modules {
|
||||
if module == "" || !valid_module(module) || seen[module] {
|
||||
return result, fmt.Errorf("invalid Speech Dispatcher module list")
|
||||
}
|
||||
seen[module] = true
|
||||
}
|
||||
result.Modules = modules
|
||||
// Query the server default before selecting any named module.
|
||||
for _, module := range append([]string{""}, modules...) {
|
||||
var rows []string
|
||||
var err error
|
||||
if module != "" {
|
||||
_, err = query("SET SELF OUTPUT_MODULE " + module)
|
||||
}
|
||||
if err == nil {
|
||||
rows, err = query("LIST SYNTHESIS_VOICES")
|
||||
}
|
||||
if err == nil {
|
||||
result.Voices[module], err = parse_voices(rows)
|
||||
}
|
||||
if err != nil {
|
||||
result.Errors[module] = err
|
||||
// A rejected command ends a complete reply. A broken stream must
|
||||
// never be reused: a late reply could belong to the previous module.
|
||||
if _, ok := err.(*Rejection); !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func parse_voices(rows []string) ([]Voice, error) {
|
||||
voices := make([]Voice, 0, len(rows))
|
||||
seen := map[string]bool{}
|
||||
for _, row := range rows {
|
||||
fields := strings.Split(row, "\t")
|
||||
if len(fields) < 2 || len(fields) > 3 || fields[0] == "" || fields[1] == "" {
|
||||
return nil, fmt.Errorf("invalid speech voice list")
|
||||
}
|
||||
if err := Validate_selection("", fields[1], fields[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Some modules repeat aliases. Stable IDs are the synthesis names.
|
||||
if seen[fields[0]] {
|
||||
continue
|
||||
}
|
||||
seen[fields[0]] = true
|
||||
voice := Voice{Name: fields[0], Language: fields[1]}
|
||||
if len(fields) == 3 {
|
||||
voice.Variant = fields[2]
|
||||
}
|
||||
voices = append(voices, voice)
|
||||
}
|
||||
return voices, nil
|
||||
return sd.DiscoverQuery(ctx, w)
|
||||
}
|
||||
|
||||
var Normalize_language = sd.NormalizeLanguage
|
||||
@@ -191,5 +114,5 @@ func discover_voices(options Options) ([]Voice, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parse_voices(rows)
|
||||
return sd.ParseVoices(rows)
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
sd "git.stormux.org/storm/screenreader-common/speechdispatcher"
|
||||
)
|
||||
|
||||
func TestLargeVoiceCatalogAndSilentPreparation(t *testing.T) {
|
||||
@@ -51,7 +53,7 @@ func TestLargeVoiceCatalogAndSilentPreparation(t *testing.T) {
|
||||
|
||||
func TestBoundedRepliesAndInjection(t *testing.T) {
|
||||
for _, row := range []string{"Missing tabs", "name\ten\tvariant\textra", "name\ten\rCANCEL ALL", "bad\x00name\ten"} {
|
||||
if _, err := parse_voices([]string{row}); err == nil {
|
||||
if _, err := sd.ParseVoices([]string{row}); err == nil {
|
||||
t.Fatal("accepted malformed catalog row", row)
|
||||
}
|
||||
}
|
||||
@@ -78,7 +80,7 @@ func TestCandidateRejectionLeavesOriginalClientUsable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCatalogPreservesEspeakVoiceWhitespace(t *testing.T) {
|
||||
voices, err := parse_voices([]string{"Cherokee \tchr\tnone"})
|
||||
voices, err := sd.ParseVoices([]string{"Cherokee \tchr\tnone"})
|
||||
if err != nil || len(voices) != 1 || voices[0].Name != "Cherokee " {
|
||||
t.Fatal(voices, err)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ package speech
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sd "git.stormux.org/storm/screenreader-common/speechdispatcher"
|
||||
)
|
||||
|
||||
// Driver binds a stable file/CLI ID to its implementation. Availability must
|
||||
@@ -131,11 +133,4 @@ func discover_modules(options Options) ([]string, error) {
|
||||
return modules, nil
|
||||
}
|
||||
|
||||
func valid_module(module string) bool {
|
||||
for _, char := range module {
|
||||
if !(char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z' || char >= '0' && char <= '9' || char == '.' || char == '_' || char == '+' || char == '-') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func valid_module(module string) bool { return sd.ValidModule(module) }
|
||||
Reference in new issue
Block a user