Adopt common v0.8.0 and preserve canceled hardware settings

This commit is contained in:
Storm Dragon committed 2026-11-16 20:10:47 -05:00
1 parent b21fceb311
commit 5dfa038706
410 files changed
+5353 -4803

No files matched your search

+3 -3
View File
@@ -13,13 +13,13 @@ import (
const indentationCuePrefix = "generated-indentation-tone:"
func IndentationCueName(frequency int) string { return indentationCuePrefix + strconv.Itoa(frequency) }
func indentation_frequency(name string) (int, bool) {
func indentationFrequency(name string) (int, bool) {
if !strings.HasPrefix(name, indentationCuePrefix) {
return 0, false
}
frequency, err := strconv.Atoi(strings.TrimPrefix(name, indentationCuePrefix))
return frequency, err == nil && frequency >= 100 && frequency <= 2750
}
func indentation_tone_wav(frequency int) []byte {
return tone_wav(float64(frequency), 150*time.Millisecond, .7, 5*time.Millisecond)
func indentationToneWav(frequency int) []byte {
return toneWav(float64(frequency), 150*time.Millisecond, .7, 5*time.Millisecond)
}
+3 -3
View File
@@ -18,7 +18,7 @@ func TestIndentationToneIsBoundedAndHonorsSoundPolicy(t *testing.T) {
if err != nil || len(data) != 44+3307*2 || binary.LittleEndian.Uint32(data[24:]) != 22050 {
t.Fatalf("indentation WAV: bytes %d, error %v", len(data), err)
}
if !service.cue_available(name) || !generated_tone(name) {
if !service.cueAvailable(name) || !generatedTone(name) {
t.Fatal("generated indentation unavailable")
}
}
@@ -26,11 +26,11 @@ func TestIndentationToneIsBoundedAndHonorsSoundPolicy(t *testing.T) {
t.Fatal("unbounded pitch accepted")
}
service.Configure(Configuration{Enabled: false, Theme: "default", Volume: .5})
if service.cue_available(IndentationCueName(200)) {
if service.cueAvailable(IndentationCueName(200)) {
t.Fatal("disabled sound played indentation")
}
service.Configure(Configuration{Enabled: true, Theme: "none", Volume: .5})
if service.cue_available(IndentationCueName(200)) {
if service.cueAvailable(IndentationCueName(200)) {
t.Fatal("None theme played indentation")
}
}
+3 -3
View File
@@ -18,7 +18,7 @@ func PositionCueName(position float64) string {
return positionCuePrefix + strconv.FormatFloat(position, 'g', -1, 64)
}
func set_position(cue string) (float64, bool) {
func setPosition(cue string) (float64, bool) {
if !strings.HasPrefix(cue, positionCuePrefix) {
return 0, false
}
@@ -26,8 +26,8 @@ func set_position(cue string) (float64, bool) {
return position, err == nil && !math.IsNaN(position) && !math.IsInf(position, 0) && position >= 0 && position <= 1
}
func position_tone_wav(position float64) []byte {
func positionToneWav(position float64) []byte {
// Two octaves distinguish the ends of a set. Smooth the edges of this
// short cue; the service supplies the user's volume and stereo position.
return tone_wav(220*math.Pow(4, position), 60*time.Millisecond, 0.5, 5*time.Millisecond)
return toneWav(220*math.Pow(4, position), 60*time.Millisecond, 0.5, 5*time.Millisecond)
}
+3 -3
View File
@@ -18,7 +18,7 @@ func ProgressCueName(percentage float64) string {
return progressCuePrefix + strconv.FormatFloat(percentage, 'g', -1, 64)
}
func progress_percentage(cue string) (float64, bool) {
func progressPercentage(cue string) (float64, bool) {
if !strings.HasPrefix(cue, progressCuePrefix) {
return 0, false
}
@@ -26,5 +26,5 @@ func progress_percentage(cue string) (float64, bool) {
return percentage, err == nil && !math.IsNaN(percentage) && !math.IsInf(percentage, 0) && percentage >= 0 && percentage <= 100
}
func progress_tone_wav(percentage float64) []byte { return tones.ProgressWAV(percentage) }
func nvda_tone_wav(hz float64, milliseconds int) []byte { return tones.NVDAWAV(hz, milliseconds) }
func progressToneWav(percentage float64) []byte { return tones.ProgressWAV(percentage) }
func nvdaToneWav(hz float64, milliseconds int) []byte { return tones.NVDAWAV(hz, milliseconds) }
+13 -13
View File
@@ -32,7 +32,7 @@ func (player *progressTestPlayer) Play(ctx context.Context, data []byte, _, _ fl
}
}
func new_progress_test_service(t *testing.T) (*Service, *progressTestPlayer) {
func newProgressTestService(t *testing.T) (*Service, *progressTestPlayer) {
t.Helper()
player := &progressTestPlayer{started: make(chan progressPlayback, 16), release: make(chan struct{}, 16)}
service := New(slog.New(slog.DiscardHandler), player, "none", 0)
@@ -49,7 +49,7 @@ func new_progress_test_service(t *testing.T) (*Service, *progressTestPlayer) {
return service, player
}
func next_progress_playback(t *testing.T, player *progressTestPlayer) progressPlayback {
func nextProgressPlayback(t *testing.T, player *progressTestPlayer) progressPlayback {
t.Helper()
select {
case playback := <-player.started:
@@ -61,9 +61,9 @@ func next_progress_playback(t *testing.T, player *progressTestPlayer) progressPl
}
func TestRapidProgressFinishesActiveToneAndPlaysLatestValue(t *testing.T) {
service, player := new_progress_test_service(t)
service, player := newProgressTestService(t)
service.Enqueue(Cue{Name: ProgressCueName(0)})
first := next_progress_playback(t, player)
first := nextProgressPlayback(t, player)
for percentage := 1; percentage <= 100; percentage++ {
if !service.EnqueueBatch([]Cue{{Name: ProgressCueName(float64(percentage))}}) {
t.Fatal("rapid progress update rejected")
@@ -73,32 +73,32 @@ func TestRapidProgressFinishesActiveToneAndPlaysLatestValue(t *testing.T) {
t.Fatalf("rapid updates canceled the active progress tone: %v", err)
}
player.release <- struct{}{}
latest := next_progress_playback(t, player)
if !bytes.Equal(latest.data, progress_tone_wav(100)) {
latest := nextProgressPlayback(t, player)
if !bytes.Equal(latest.data, progressToneWav(100)) {
t.Fatal("played an obsolete percentage instead of the latest value")
}
}
func TestProgressPreservesActiveAndPendingRoleSounds(t *testing.T) {
service, player := new_progress_test_service(t)
service, player := newProgressTestService(t)
service.mutex.Lock()
service.enabled, service.theme, service.volume = true, "default", 0.5
service.available = map[string]bool{"button": true, "link": true}
service.mutex.Unlock()
service.EnqueueBatch([]Cue{{Name: "button"}, {Name: "link"}})
first := next_progress_playback(t, player)
first := nextProgressPlayback(t, player)
service.Enqueue(Cue{Name: ProgressCueName(100)})
if err := first.context.Err(); err != nil {
t.Fatalf("progress interrupted a role sound: %v", err)
}
player.release <- struct{}{}
second := next_progress_playback(t, player)
want, err := load_sound("default", "link")
second := nextProgressPlayback(t, player)
want, err := loadSound("default", "link")
if err != nil || !bytes.Equal(second.data, want) {
t.Fatal("progress displaced a pending role sound")
}
player.release <- struct{}{}
if got := next_progress_playback(t, player); !bytes.Equal(got.data, progress_tone_wav(100)) {
if got := nextProgressPlayback(t, player); !bytes.Equal(got.data, progressToneWav(100)) {
t.Fatal("latest progress did not follow the role sounds")
}
}
@@ -106,9 +106,9 @@ func TestProgressPreservesActiveAndPendingRoleSounds(t *testing.T) {
func TestProgressCancellationDiscardsPendingValue(t *testing.T) {
for _, action := range []string{"presentation", "configure", "stop"} {
t.Run(action, func(t *testing.T) {
service, player := new_progress_test_service(t)
service, player := newProgressTestService(t)
service.Enqueue(Cue{Name: ProgressCueName(0)})
first := next_progress_playback(t, player)
first := nextProgressPlayback(t, player)
service.Enqueue(Cue{Name: ProgressCueName(100)})
switch action {
case "presentation":
+27 -27
View File
@@ -99,7 +99,7 @@ func (service *Service) Configure(configuration Configuration) {
func (service *Service) Available(name string) bool {
service.mutex.Lock()
defer service.mutex.Unlock()
return service.running && service.cue_available(name)
return service.running && service.cueAvailable(name)
}
func (service *Service) Start(ctx context.Context) error {
@@ -140,12 +140,12 @@ func (service *Service) EnqueueBatch(cues []Cue) bool {
return false
}
for _, cue := range cues {
if !service.cue_available(cue.Name) {
if !service.cueAvailable(cue.Name) {
return false
}
}
if len(cues) == 1 {
if _, progress := progress_percentage(cues[0].Name); progress {
if _, progress := progressPercentage(cues[0].Name); progress {
cue := cues[0]
cue.Pan = normalizedPan(cue.Pan)
service.pendingProgress = &queuedCue{Cue: cue, generation: service.generation}
@@ -184,7 +184,7 @@ func (service *Service) discardPending() {
func (service *Service) run(ctx context.Context, done chan struct{}) {
defer close(done)
for {
cue, ok := service.next_cue(ctx)
cue, ok := service.nextCue(ctx)
if !ok {
return
}
@@ -199,7 +199,7 @@ func (service *Service) run(ctx context.Context, done chan struct{}) {
// Preserve the order of ordinary presentation cues before taking the newest
// progress value. progressWake is only a notification; mutex owns the slot.
func (service *Service) next_cue(ctx context.Context) (queuedCue, bool) {
func (service *Service) nextCue(ctx context.Context) (queuedCue, bool) {
for {
if ctx.Err() != nil {
return queuedCue{}, false
@@ -229,12 +229,12 @@ func (service *Service) next_cue(ctx context.Context) (queuedCue, bool) {
func (service *Service) playQueued(ctx context.Context, cue queuedCue) error {
service.mutex.Lock()
if cue.generation != service.generation || !service.running || !service.cue_available(cue.Name) {
if cue.generation != service.generation || !service.running || !service.cueAvailable(cue.Name) {
service.mutex.Unlock()
return nil
}
player, theme, volume := service.player, service.theme, service.volume
if _, valid := progress_percentage(cue.Name); valid {
if _, valid := progressPercentage(cue.Name); valid {
volume = 1
}
cacheKey := theme + "\x00" + cue.Name
@@ -259,7 +259,7 @@ func (service *Service) playQueued(ctx context.Context, cue queuedCue) error {
return err
}
service.mutex.Lock()
if !generated_tone(cue.Name) {
if !generatedTone(cue.Name) {
service.cache[cacheKey] = data
}
service.mutex.Unlock()
@@ -274,7 +274,7 @@ func (service *Service) playQueued(ctx context.Context, cue queuedCue) error {
func (service *Service) play(ctx context.Context, cue Cue) error {
service.mutex.Lock()
player, theme, volume := service.player, service.theme, service.volume
if _, valid := progress_percentage(cue.Name); valid {
if _, valid := progressPercentage(cue.Name); valid {
volume = 1
}
cacheKey := theme + "\x00" + cue.Name
@@ -287,7 +287,7 @@ func (service *Service) play(ctx context.Context, cue Cue) error {
return err
}
service.mutex.Lock()
if !generated_tone(cue.Name) {
if !generatedTone(cue.Name) {
service.cache[cacheKey] = data
}
service.mutex.Unlock()
@@ -313,7 +313,7 @@ var namedCues = []string{
func cueAvailability(theme string) map[string]bool {
available := make(map[string]bool, len(namedCues))
for _, name := range namedCues {
if data, err := load_sound(theme, name); err == nil && len(data) <= maxSoundBytes {
if data, err := loadSound(theme, name); err == nil && len(data) <= maxSoundBytes {
available[name] = true
}
}
@@ -322,49 +322,49 @@ func cueAvailability(theme string) map[string]bool {
// Called under the service mutex. Progress output is controlled by its own
// output mode, independent of role-sound enablement, theme and volume.
func (service *Service) cue_available(name string) bool {
if _, valid := indentation_frequency(name); valid {
func (service *Service) cueAvailable(name string) bool {
if _, valid := indentationFrequency(name); valid {
return service.enabled && service.theme != "none" && service.volume > 0
}
if _, valid := progress_percentage(name); valid {
if _, valid := progressPercentage(name); valid {
return true
}
if _, valid := set_position(name); valid {
if _, valid := setPosition(name); valid {
return service.enabled && service.theme != "none" && service.volume > 0
}
return service.enabled && service.available[name]
}
func generated_tone(name string) bool {
_, indentation := indentation_frequency(name)
_, progress := progress_percentage(name)
_, position := set_position(name)
func generatedTone(name string) bool {
_, indentation := indentationFrequency(name)
_, progress := progressPercentage(name)
_, position := setPosition(name)
return progress || position || indentation
}
func soundData(theme, cue string) ([]byte, error) {
if strings.HasPrefix(cue, indentationCuePrefix) {
frequency, valid := indentation_frequency(cue)
frequency, valid := indentationFrequency(cue)
if !valid {
return nil, errors.New("invalid generated indentation tone")
}
return indentation_tone_wav(frequency), nil
return indentationToneWav(frequency), nil
}
if strings.HasPrefix(cue, positionCuePrefix) {
position, valid := set_position(cue)
position, valid := setPosition(cue)
if !valid {
return nil, errors.New("invalid generated position tone")
}
return position_tone_wav(position), nil
return positionToneWav(position), nil
}
if strings.HasPrefix(cue, progressCuePrefix) {
percentage, valid := progress_percentage(cue)
percentage, valid := progressPercentage(cue)
if !valid {
return nil, errors.New("invalid generated progress tone")
}
return progress_tone_wav(percentage), nil
return progressToneWav(percentage), nil
}
return load_sound(theme, cue)
return loadSound(theme, cue)
}
// Stop cancels any active cue, discards pending cues, and plays the stop cue
@@ -405,7 +405,7 @@ func (service *Service) Stop(ctx context.Context) error {
return nil
}
func load_sound(theme, cue string) ([]byte, error) {
func loadSound(theme, cue string) ([]byte, error) {
if filepath.Base(theme) != theme || theme == "." || theme == ".." || filepath.Base(cue) != cue || cue == "." || cue == ".." {
return nil, errors.New("invalid sound theme or cue name")
}
+10 -10
View File
@@ -19,7 +19,7 @@ func TestBundledSoundsDecodeInProcess(t *testing.T) {
}
for _, cue := range []string{"start", "stop", "autocomplete", "button", "checkbox_checked", "checkbox_mixed", "checkbox_unchecked", "combobox", "link", "link_visited", "radiobutton_checked", "radiobutton_unchecked", "togglebutton_checked", "togglebutton_unchecked", "switch_on", "switch_off", "browse_mode", "focus_mode"} {
t.Run(cue, func(t *testing.T) {
data, err := load_sound("default", cue)
data, err := loadSound("default", cue)
if err != nil {
t.Fatal(err)
}
@@ -53,7 +53,7 @@ func TestGeneratedProgressSoundsDecodeInProcess(t *testing.T) {
}
func TestBundledSoundPanDecodes(t *testing.T) {
data, err := load_sound("default", "button")
data, err := loadSound("default", "button")
if err != nil {
t.Fatal(err)
}
@@ -70,10 +70,10 @@ func TestBundledSoundPanDecodes(t *testing.T) {
func TestThemePathsAndSizeAreBounded(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
for _, name := range []string{"..", "../other", "/tmp/other"} {
if _, err := load_sound(name, "start"); err == nil {
if _, err := loadSound(name, "start"); err == nil {
t.Fatalf("accepted theme %q", name)
}
if _, err := load_sound("default", name); err == nil {
if _, err := loadSound("default", name); err == nil {
t.Fatalf("accepted cue %q", name)
}
}
@@ -84,14 +84,14 @@ func TestThemePathsAndSizeAreBounded(t *testing.T) {
if err := os.WriteFile(filepath.Join(path, "start.wav"), []byte("custom"), 0o600); err != nil {
t.Fatal(err)
}
data, err := load_sound("custom", "start")
data, err := loadSound("custom", "start")
if err != nil || string(data) != "custom" {
t.Fatalf("custom theme = %q, %v", data, err)
}
if err := os.WriteFile(filepath.Join(path, "start.wav"), make([]byte, maxSoundBytes+1), 0o600); err != nil {
t.Fatal(err)
}
if _, err := load_sound("custom", "start"); err == nil {
if _, err := loadSound("custom", "start"); err == nil {
t.Fatal("accepted oversized sound")
}
}
@@ -191,7 +191,7 @@ func TestNewPresentationInterruptsOldCueAndDiscardsPendingCues(t *testing.T) {
case <-time.After(time.Second):
t.Fatal("initial cue was not interrupted")
}
want, err := load_sound("default", "link")
want, err := loadSound("default", "link")
if err != nil {
t.Fatal(err)
}
@@ -481,7 +481,7 @@ func TestSoundWorkerBoundsQueueAndStopsActiveCue(t *testing.T) {
}
func TestStopPlaysShutdownCueAfterOversizedPresentation(t *testing.T) {
stopData, err := load_sound("default", "stop")
stopData, err := loadSound("default", "stop")
if err != nil {
t.Fatal(err)
}
@@ -522,7 +522,7 @@ func TestStopPlaysShutdownCueAfterOversizedPresentation(t *testing.T) {
}
func TestStopTimeoutPreservesShutdownCueForRetry(t *testing.T) {
stopData, err := load_sound("default", "stop")
stopData, err := loadSound("default", "stop")
if err != nil {
t.Fatal(err)
}
@@ -602,7 +602,7 @@ func TestLiveSound(t *testing.T) {
t.Skip("explicit live audio test")
}
for _, cue := range []string{"start", "stop"} {
data, err := load_sound("default", cue)
data, err := loadSound("default", cue)
if err != nil {
t.Fatal(err)
}
+5 -5
View File
@@ -9,23 +9,23 @@ import (
"time"
)
var speechBeepWAV = tone_wav(440, 40*time.Millisecond, 0.35, 5*time.Millisecond)
var capitalBeepWAV = nvda_tone_wav(2000, 50)
var speechBeepWAV = toneWav(440, 40*time.Millisecond, 0.35, 5*time.Millisecond)
var capitalBeepWAV = nvdaToneWav(2000, 50)
// SpeechBeep replaces an utterance, independently of role-sound enablement or
// theme availability. The speech worker owns its ordering and cancellation;
// sound preferences still supply the output sink and volume.
func (service *Service) SpeechBeep(ctx context.Context) error {
return service.play_speech_beep(ctx, speechBeepWAV)
return service.playSpeechBeep(ctx, speechBeepWAV)
}
// CapitalBeep is ordered and canceled by the speech worker, just like speech.
// It uses sound volume and sink, independently of the role sound theme.
func (service *Service) CapitalBeep(ctx context.Context) error {
return service.play_speech_beep(ctx, capitalBeepWAV)
return service.playSpeechBeep(ctx, capitalBeepWAV)
}
func (service *Service) play_speech_beep(ctx context.Context, data []byte) error {
func (service *Service) playSpeechBeep(ctx context.Context, data []byte) error {
service.mutex.Lock()
player, volume := service.player, service.volume
service.mutex.Unlock()
+1 -1
View File
@@ -10,6 +10,6 @@ import (
"git.stormux.org/storm/screenreader-common/tones"
)
func tone_wav(frequency float64, duration time.Duration, amplitude float64, ramp time.Duration) []byte {
func toneWav(frequency float64, duration time.Duration, amplitude float64, ramp time.Duration) []byte {
return tones.SineWAV(frequency, duration, amplitude, ramp)
}