Provide common date/time formatting, locale voice matching, tone synthesis, phonetic spelling, and one hardware synthesizer protocol encoder for both readers. Standardize whiskey and x ray and use the tested Fenrir DECtalk mapping. Validated the common module with GOWORK=off go test -race ./...; reader integration, serial cancellation, PCM hashes, no-CGO datetime, and speech regression checks passed. Physical keyboard, audible synthesis, and braille acceptance remain separate live checks.
80 lines
3.0 KiB
Go
80 lines
3.0 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package tones
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"math"
|
|
)
|
|
|
|
// 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 ProgressFrequency(percentage float64) float64 { return 110 * math.Pow(2, percentage/25) }
|
|
|
|
func ProgressWAV(percentage float64) []byte {
|
|
return NVDAWAV(ProgressFrequency(percentage), 40)
|
|
}
|
|
|
|
// NVDAWAV also supplies the capital-letter beep (2000 Hz, 50 ms).
|
|
func NVDAWAV(hz float64, milliseconds int) []byte {
|
|
samples := Samples(hz, milliseconds, false)
|
|
const sampleRate = SampleRate
|
|
sampleCount := len(samples)
|
|
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))
|
|
for _, value := range samples {
|
|
_ = binary.Write(buffer, binary.LittleEndian, value)
|
|
_ = binary.Write(buffer, binary.LittleEndian, value)
|
|
}
|
|
return buffer.Bytes()
|
|
}
|
|
|
|
// SampleRate is the native synthesis rate; streaming readers may resample it.
|
|
const SampleRate = 44100
|
|
|
|
// Samples generates the common clipped sine, or its indentation triangle variant.
|
|
// Invalid frequencies and durations produce no samples.
|
|
func Samples(hz float64, milliseconds int, triangle bool) []int16 {
|
|
if math.IsNaN(hz) || math.IsInf(hz, 0) || hz < 1 || hz > SampleRate || milliseconds <= 0 || milliseconds > 10000 {
|
|
return nil
|
|
}
|
|
const sampleRate = SampleRate
|
|
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
|
|
|
|
samples := make([]int16, sampleCount)
|
|
sinFrequency := 2 * math.Pi / cycleLength
|
|
for index := range samples {
|
|
phase := float64(index%sampleRate) * sinFrequency
|
|
wave := min(1.0, max(-1.0, math.Sin(phase)*2))
|
|
if triangle {
|
|
wave = 2 / math.Pi * math.Asin(math.Sin(phase))
|
|
}
|
|
samples[index] = int16(wave * 7000)
|
|
}
|
|
return samples
|
|
}
|