Use shared reader core v0.4.0

Use common date/time formatting, locale voice selection, tone synthesis, phonetic spelling, and hardware command encoding. DECtalk now follows the common Fenrir mapping and command filtering; legacy saved settings are converted at the boundary.

Validated affected race-enabled tests and standalone builds with GOWORK=off against the published module. Common PCM hashes, serial cancellation, no-CGO datetime, and speech regression checks passed. Physical hardware, audible synthesis, keyboard, and braille remain separate live checks.
This commit is contained in:
ak47 committed 2026-11-12 23:45:10 -05:00
1 parent 8ab9d3395a
commit d95dc2d00d
18 files changed
+121 -452

No files matched your search

+4 -43
View File
@@ -5,11 +5,11 @@
package sound
import (
"bytes"
"encoding/binary"
"math"
"strconv"
"strings"
"git.stormux.org/storm/screenreader-common/tones"
)
const progressCuePrefix = "generated-progress-tone:"
@@ -26,44 +26,5 @@ func progress_percentage(cue string) (float64, bool) {
return percentage, err == nil && !math.IsNaN(percentage) && !math.IsInf(percentage, 0) && percentage >= 0 && percentage <= 100
}
// Match NVDA's default ProgressBar event_valueChange and generateBeep:
// 110 * 2^(percentage/25) Hz, 40 ms padded to the next integer cycle,
// 44100 Hz stereo PCM, and a sine clipped after multiplying by two.
// NVDA passes frequency through a C float and defaults each channel to 50%.
// Reference: nvaccess/nvda a4a89720ccbf3973a0e1e50a76dd8bff38db3d15,
// source/NVDAObjects/behaviors.py and nvdaHelper/local/beeps.cpp.
func progress_tone_wav(percentage float64) []byte {
return nvda_tone_wav(110*math.Pow(2, percentage/25), 40)
}
// nvda_tone_wav also supplies the capital-letter beep (2000 Hz, 50 ms).
func nvda_tone_wav(hz float64, milliseconds int) []byte {
const sampleRate = 44100
frequency := float64(float32(hz))
// sampleRate/hz is evaluated as a float in NVDA's C++ generator.
cycleLength := float64(float32(sampleRate) / float32(frequency))
samplesPerCycle := int(cycleLength)
sampleCount := int((float64(milliseconds) / 1000) / (1.0 / sampleRate))
sampleCount += samplesPerCycle - sampleCount%samplesPerCycle
dataSize := sampleCount * 4
buffer := bytes.NewBuffer(make([]byte, 0, 44+dataSize))
buffer.WriteString("RIFF")
_ = binary.Write(buffer, binary.LittleEndian, uint32(36+dataSize))
buffer.WriteString("WAVEfmt ")
_ = binary.Write(buffer, binary.LittleEndian, uint32(16))
_ = binary.Write(buffer, binary.LittleEndian, uint16(1))
_ = binary.Write(buffer, binary.LittleEndian, uint16(2))
_ = binary.Write(buffer, binary.LittleEndian, uint32(sampleRate))
_ = binary.Write(buffer, binary.LittleEndian, uint32(sampleRate*4))
_ = binary.Write(buffer, binary.LittleEndian, uint16(4))
_ = binary.Write(buffer, binary.LittleEndian, uint16(16))
buffer.WriteString("data")
_ = binary.Write(buffer, binary.LittleEndian, uint32(dataSize))
sinFrequency := 2 * math.Pi / cycleLength
for sample := 0; sample < sampleCount; sample++ {
value := int16(min(1.0, max(-1.0, math.Sin(float64(sample%sampleRate)*sinFrequency)*2)) * 7000)
_ = binary.Write(buffer, binary.LittleEndian, value)
_ = binary.Write(buffer, binary.LittleEndian, value)
}
return buffer.Bytes()
}
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) }
-27
View File
@@ -7,38 +7,11 @@ package sound
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
"log/slog"
"testing"
"time"
)
func TestProgressAudioMatchesNVDAGenerator(t *testing.T) {
// PCM digests from the unmodified generateBeep in the reference commit
// named in progress.go, compiled locally with a small output-only harness.
all := sha256.New()
for percentage := 0; percentage <= 100; percentage++ {
data := progress_tone_wav(float64(percentage))
if binary.LittleEndian.Uint32(data[24:]) != 44100 || binary.LittleEndian.Uint16(data[22:]) != 2 {
t.Fatal("progress tone is not 44100 Hz stereo")
}
all.Write(data[44:])
}
if got := fmt.Sprintf("%x", all.Sum(nil)); got != "ef56e46be365269af544e22d94eb1de7d171c5946d1dd5b168cc7e570c0a159f" {
t.Fatalf("0 through 100 percent PCM differs from NVDA: %s", got)
}
for percentage, digest := range map[float64]string{
12.5: "4fc87ad5310038ed281a4516e9431d5dc247b7ce75662e4fe51cb4d2ab616a36",
50.25: "6b4a9a7d254686b2fb52ead07763ff596ccdcbdd30d5e9d4ce956df13cc1bdf5",
} {
if got := fmt.Sprintf("%x", sha256.Sum256(progress_tone_wav(percentage)[44:])); got != digest {
t.Fatalf("%g percent PCM differs from NVDA: %s", percentage, got)
}
}
}
type progressPlayback struct {
context context.Context
data []byte
+3 -33
View File
@@ -5,41 +5,11 @@
package sound
import (
"bytes"
"encoding/binary"
"math"
"time"
"git.stormux.org/storm/screenreader-common/tones"
)
// tone_wav generates mono, 16-bit PCM at 22050 Hz for the existing Player.
// Callers supply a positive frequency/duration, amplitude in [0, 1], and a
// nonnegative edge ramp. A zero ramp preserves an unsmoothed sine tone.
// Playback volume, pan, queuing, and cancellation belong to the sound service.
func tone_wav(frequency float64, duration time.Duration, amplitude float64, ramp time.Duration) []byte {
const sampleRate = 22050
sampleCount := int(float64(sampleRate) * duration.Seconds())
rampSamples := int(float64(sampleRate) * ramp.Seconds())
dataSize := sampleCount * 2
buffer := bytes.NewBuffer(make([]byte, 0, 44+dataSize))
buffer.WriteString("RIFF")
_ = binary.Write(buffer, binary.LittleEndian, uint32(36+dataSize))
buffer.WriteString("WAVEfmt ")
_ = binary.Write(buffer, binary.LittleEndian, uint32(16))
_ = binary.Write(buffer, binary.LittleEndian, uint16(1))
_ = binary.Write(buffer, binary.LittleEndian, uint16(1))
_ = binary.Write(buffer, binary.LittleEndian, uint32(sampleRate))
_ = binary.Write(buffer, binary.LittleEndian, uint32(sampleRate*2))
_ = binary.Write(buffer, binary.LittleEndian, uint16(2))
_ = binary.Write(buffer, binary.LittleEndian, uint16(16))
buffer.WriteString("data")
_ = binary.Write(buffer, binary.LittleEndian, uint32(dataSize))
for sample := 0; sample < sampleCount; sample++ {
envelope := 1.0
if rampSamples > 0 {
envelope = min(1.0, float64(min(sample, sampleCount-1-sample))/float64(rampSamples))
}
value := int16(math.Sin(2*math.Pi*frequency*float64(sample)/sampleRate) * amplitude * envelope * math.MaxInt16)
_ = binary.Write(buffer, binary.LittleEndian, value)
}
return buffer.Bytes()
return tones.SineWAV(frequency, duration, amplitude, ramp)
}