Add speech language to quick settings

This commit is contained in:
Storm Dragon committed 2026-11-15 12:44:12 -05:00
1 parent 09e9570c66
commit 0f5c2db158
3 files changed
+143 -7

No files matched your search

+44 -3
View File
@@ -17,6 +17,7 @@ import (
"git.stormux.org/storm/cthulhu/internal/speech"
"git.stormux.org/storm/cthulhu/internal/tentaclewire"
"git.stormux.org/storm/screenreader-common/quicksettings"
sd "git.stormux.org/storm/screenreader-common/speechdispatcher"
)
// Use the settings worker's bounded mailbox, keeping discovery, disk writes,
@@ -165,6 +166,7 @@ func (host *quickSettingsHost) categories() []quicksettings.Category {
} else {
voice = append(voice,
quicksettings.Entry{ID: "speech.module", Label: "Speech-dispatcher module", Kind: quicksettings.Choice, EmptyIsDefault: true},
quicksettings.Entry{ID: "speech.language", Label: "Speech-dispatcher language", Kind: quicksettings.Choice, EmptyIsDefault: true},
quicksettings.Entry{ID: "speech.voice", Label: "Speech-dispatcher voice", Kind: quicksettings.Choice, EmptyIsDefault: true})
}
reading := []quicksettings.Entry{
@@ -200,6 +202,13 @@ func voiceOptionID(family *config.VoiceFamily) string {
return family.Name + "\x00" + family.Lang + "\x00" + family.Variant
}
func quickVoiceLanguage(family *config.VoiceFamily) string {
if family == nil {
return ""
}
return strings.TrimSuffix(family.Lang+"-"+family.Dialect, "-")
}
func (host *quickSettingsHost) Value(entry quicksettings.Entry) quicksettings.Value {
settings := host.settings
number := func(value *float64, fallback int) quicksettings.Value {
@@ -217,6 +226,8 @@ func (host *quickSettingsHost) Value(entry quicksettings.Entry) quicksettings.Va
return number(settings.DefaultVoice.Gain, 100)
case "speech.module":
return quicksettings.Value{Choice: settings.SpeechOutputModule}
case "speech.language":
return quicksettings.Value{Choice: quickVoiceLanguage(settings.DefaultVoice.Family)}
case "speech.voice":
return quicksettings.Value{Choice: voiceOptionID(settings.DefaultVoice.Family)}
case "reading.punctuation":
@@ -274,12 +285,33 @@ func (host *quickSettingsHost) Options(entry quicksettings.Entry) []quicksetting
}
sort.Slice(options[1:], func(i, j int) bool { return options[1+i].ID < options[1+j].ID })
return options
case "speech.language":
if !host.catalog() {
return nil
}
options := []quicksettings.Option{{ID: "", Label: "default"}}
seen := map[string]bool{}
for _, voice := range host.worker.catalog[host.settings.SpeechOutputModule] {
language := voice.Language
normalized := sd.NormalizeLanguage(language)
if language != "" && !seen[normalized] {
seen[normalized] = true
options = append(options, quicksettings.Option{ID: language, Label: language})
}
}
return options
case "speech.voice":
if !host.catalog() {
return nil
}
options := []quicksettings.Option{{ID: "", Label: "default"}}
for _, voice := range quickSpeechVoices(host.settings.SpeechOutputModule, host.worker.catalog[host.settings.SpeechOutputModule]) {
voices := host.worker.catalog[host.settings.SpeechOutputModule]
if language := quickVoiceLanguage(host.settings.DefaultVoice.Family); language != "" {
voices = sd.LanguageVoices(voices, language)
} else {
voices = quickSpeechVoices(host.settings.SpeechOutputModule, voices)
}
for _, voice := range voices {
options = append(options, quicksettings.Option{ID: voiceOptionID(&config.VoiceFamily{Name: voice.Name, Lang: voice.Language, Variant: voice.Variant}), Label: voice.Name})
}
return options
@@ -321,8 +353,17 @@ func (host *quickSettingsHost) set(settings *config.Settings, entry quicksetting
settings.DefaultVoice.Family = &config.VoiceFamily{Name: voices[0].Name, Lang: voices[0].Language, Variant: voices[0].Variant}
}
}
case "speech.voice":
case "speech.language":
settings.DefaultVoice.Family = nil
if value.Choice != "" {
settings.DefaultVoice.Family = &config.VoiceFamily{Lang: value.Choice}
}
case "speech.voice":
language := quickVoiceLanguage(settings.DefaultVoice.Family)
settings.DefaultVoice.Family = nil
if language != "" {
settings.DefaultVoice.Family = &config.VoiceFamily{Lang: language}
}
for _, voice := range host.worker.catalog[settings.SpeechOutputModule] {
family := &config.VoiceFamily{Name: voice.Name, Lang: voice.Language, Variant: voice.Variant}
if voiceOptionID(family) == value.Choice {
@@ -406,7 +447,7 @@ func (quick quickSpeechSettings) apply(settings config.Settings) config.Settings
entry.Kind = quicksettings.Toggle
}
value := host.Value(entry)
if id == "speech.module" || id == "speech.voice" {
if id == "speech.module" || id == "speech.language" || id == "speech.voice" {
// A voice belongs to its synthesizer, and a hardware synthesizer also
// needs its transport. Keep this selection valid across app profiles.
settings.SpeechServerFactory = quick.settings.SpeechServerFactory
+96 -3
View File
@@ -78,6 +78,10 @@ func TestSpeechShortcutsApplySaveAndFailure(t *testing.T) {
t.Fatalf("module: %q %+v", message, applied)
}
command(input.SelectNextSpeechSetting)
if message != "Speech-dispatcher language en" {
t.Fatal(message)
}
command(input.SelectNextSpeechSetting)
if message != "Speech-dispatcher voice one" {
t.Fatal(message)
}
@@ -175,7 +179,7 @@ func TestQuickSettingsCategoriesReadingAndKeyboard(t *testing.T) {
}
func TestQuickSpeechFollowsWindowSwitchesAndSavesGlobally(t *testing.T) {
for _, id := range []string{"speech.rate", "speech.pitch", "speech.volume", "speech.module", "speech.voice", "keyboard.word-echo"} {
for _, id := range []string{"speech.rate", "speech.pitch", "speech.volume", "speech.module", "speech.language", "speech.voice", "keyboard.word-echo"} {
t.Run(id, func(t *testing.T) {
global := config.DefaultSettings()
global.Path = filepath.Join(t.TempDir(), "settings.toml")
@@ -210,7 +214,7 @@ func TestQuickSpeechFollowsWindowSwitchesAndSavesGlobally(t *testing.T) {
settingsUpdateFunc(func(_ context.Context, next speech.Settings) error { applied = next; return nil }))
worker.live = &liveSettings{}
worker.live.Store(global)
worker.catalog = speech.Catalog{"alpha": {{Name: "one", Language: "en"}, {Name: "two", Language: "en"}},
worker.catalog = speech.Catalog{"alpha": {{Name: "one", Language: "en"}, {Name: "two", Language: "en"}, {Name: "French", Language: "fr"}},
"beta": {{Name: "three", Language: "en"}}}
ctx := t.Context()
worker.switchApplication(ctx, "Cached Application")
@@ -233,6 +237,9 @@ func TestQuickSpeechFollowsWindowSwitchesAndSavesGlobally(t *testing.T) {
case "speech.module":
base.SpeechOutputModule = "beta"
base.DefaultVoice.Family = &config.VoiceFamily{Name: "three", Lang: "en"}
case "speech.language":
base.SpeechOutputModule = "alpha"
base.DefaultVoice.Family = &config.VoiceFamily{Lang: "fr"}
case "speech.voice":
base.SpeechOutputModule = "alpha"
base.DefaultVoice.Family = &config.VoiceFamily{Name: "two", Lang: "en"}
@@ -511,7 +518,7 @@ func TestSpeechShortcutESpeakDefaultAndVariants(t *testing.T) {
if applied.OutputModule != "espeak-ng" || applied.SynthesisVoice != "en-us" || applied.Language != "en-us" {
t.Fatalf("module switch: %+v", applied)
}
worker.handleSpeechCommand(context.Background(), input.SelectNextSpeechSetting)
selectQuickSetting(t, worker, "speech.voice")
// Voices wrap through the module default, as in Fenrir.
for _, tc := range []struct {
command input.Command
@@ -540,3 +547,89 @@ func TestQuickSettingsRetainPartialDiscovery(t *testing.T) {
t.Fatal(options, host.unavailable)
}
}
func TestQuickSpeechLanguageFiltersVoicesAndKeepsDefaultLanguage(t *testing.T) {
settings := config.DefaultSettings()
settings.Path = filepath.Join(t.TempDir(), "settings.toml")
settings.SpeechOutputModule = "espeak-ng"
settings.DefaultVoice.Family = &config.VoiceFamily{Name: "English", Lang: "en"}
store, err := config.NewStore(settings.Path)
if err != nil {
t.Fatal(err)
}
if err := store.Save(settings); err != nil {
t.Fatal(err)
}
original, err := os.ReadFile(settings.Path)
if err != nil {
t.Fatal(err)
}
var applied speech.Settings
var updateError error
worker := newPreferencesWorker(slog.New(slog.DiscardHandler), settings, settingsUpdateFunc(func(_ context.Context, next speech.Settings) error {
if updateError != nil {
return updateError
}
applied = next
return nil
}))
worker.catalog = speech.Catalog{"espeak-ng": {
{Name: "English", Language: "en"},
{Name: "German+variant", Language: "de"},
{Name: "German", Language: "de"},
{Name: "French", Language: "fr"},
}}
catalog := worker.catalog
worker.discover = func(context.Context, string) (speech.Catalog, error) { return catalog, nil }
message := ""
worker.announce = func(text string) { message = text }
selectQuickSetting(t, worker, "speech.language")
host := &quickSettingsHost{worker: worker, ctx: t.Context(), settings: worker.settings}
languages := host.Options(quicksettings.Entry{ID: "speech.language"})
if len(languages) != 4 {
t.Fatal("languages not deduplicated", languages)
}
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
if applied.Language != "de" || applied.SynthesisVoice != "" || message != "de" {
t.Fatal(applied, message)
}
host.settings = worker.settings
voices := host.Options(quicksettings.Entry{ID: "speech.voice"})
if len(voices) != 3 || voices[1].Label != "German" || voices[2].Label != "German+variant" {
t.Fatal(voices)
}
worker.handleSpeechCommand(t.Context(), input.SelectNextSpeechSetting)
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
if applied.Language != "de" || applied.SynthesisVoice != "German" {
t.Fatal(applied)
}
worker.handleSpeechCommand(t.Context(), input.DecreaseSpeechSetting)
if applied.Language != "de" || applied.SynthesisVoice != "" {
t.Fatal("default voice lost selected language", applied)
}
selectQuickSetting(t, worker, "speech.language")
updateError = errors.New("selection failed")
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
if quickVoiceLanguage(worker.settings.DefaultVoice.Family) != "de" || message != "Speech-dispatcher language was not changed" {
t.Fatal(worker.settings.DefaultVoice.Family, message)
}
contents, err := os.ReadFile(settings.Path)
if err != nil || string(contents) != string(original) {
t.Fatal("temporary selection changed settings file", err)
}
restarted, err := store.Load()
if err != nil || restarted.DefaultVoice.Family.Lang != "en" || restarted.DefaultVoice.Family.Name != "English" {
t.Fatal("restart did not restore saved selection", restarted.DefaultVoice.Family, err)
}
updateError = nil
worker.handleSpeechCommand(t.Context(), input.SaveSpeechSettings)
saved, err := store.Load()
if err != nil || saved.DefaultVoice.Family.Lang != "de" || saved.DefaultVoice.Family.Name != "" {
t.Fatal("explicit save lost language", saved.DefaultVoice.Family, err)
}
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
if applied.Language != "" || applied.SynthesisVoice != "" || worker.settings.DefaultVoice.Family != nil {
t.Fatal("language default did not reset selection", applied)
}
}