Give read-aloud a Portuguese voice, and a slower one
Phase 21's infra half. Two things the pt-PT pair needs from TTS, and one thing every learner has wanted since Phase 11. **A language is no longer a code change.** The handler knew exactly two languages, named in the Config struct: English on TTS_ENDPOINT and Chinese on TTS_ENDPOINT_ZH. Petal now discovers its Piper instances from the environment — English keeps the unsuffixed pair it has always had, and every other language is a TTS_ENDPOINT_<LANG>/TTS_VOICE_<LANG> pair — so fr and es cost a compose service and two lines of .env. <LANG> is the base tag, because an environment variable name cannot hold pt-PT's hyphen and only one Portuguese model is loaded either way. A language configured by halves is dropped rather than routed: half a configuration should reach the client as "no voice here, use Web Speech", not as an instance that errors on every tap. The startup line now names the voices it actually resolved rather than the English endpoint it was handed — the same lesson the dictionary line learned last week. **pt_PT-tugão-medium is the only European voice Piper ships.** The other five pt models in the catalogue are Brazilian, so the default anyone reaches for is the wrong country — the same trap as `dictionary-pt` packaging VERO, arriving through the catalogue rather than through the model. Named explicitly in compose, with the query that checks it in the deploy README. **The slow replay** (SUGGESTIONS §5e) is `slow: true` on /api/tts, raising Piper's length_scale to ~4/3. Piper stretches durations rather than resampling, so it stays a voice instead of a groan. The pace is part of the cache key — without it the slow replay of a word already heard at normal speed would be served back at normal speed, which is the one request where the difference is the whole point. 🐢 sits beside 🔊 on the word card, the selection bubble and the garden flashcard; the Web Speech fallback slows too, so the button means the same thing when Piper is down. **And the other reading gets her own voice.** The `alsoIn` block — the Portuguese sense of a word that is also English — now speaks in the pair's locale, which the pack names (`locale`) rather than anything inferring it from the letters. "comum" is spelled identically in both halves; a detector would have to guess, and this is the same reason the gloss shows both directions instead of picking one. Tests: config discovery (both existing deployment shapes, half-configured languages dropped, the pre-map voice defaults preserved), the slow scale and its separate cache entry, pt routing on the base tag with pt-BR landing on the European instance, and speech.ts's request body. The i18n shape suite now asserts every pack names a speakable locale in its own language — and that pt-PT's is not pt-BR. Verified: go build/vet/test, tsc, vitest 125/125, vite build. Live smoke against two fake Piper servers: en/pt × normal/slow all reached the right instance at the right length_scale with four distinct cache entries, and an unconfigured language still 404s.
This commit is contained in:
@@ -22,6 +22,7 @@ func newStubPiper(t *testing.T, body []byte) (*httptest.Server, *int32, *synthEc
|
||||
_ = json.NewDecoder(r.Body).Decode(&req)
|
||||
last.voice, _ = req["voice"].(string)
|
||||
last.text, _ = req["text"].(string)
|
||||
last.scale, _ = req["length_scale"].(float64)
|
||||
w.Header().Set("Content-Type", "audio/wav")
|
||||
_, _ = w.Write(body)
|
||||
}))
|
||||
@@ -29,7 +30,10 @@ func newStubPiper(t *testing.T, body []byte) (*httptest.Server, *int32, *synthEc
|
||||
return srv, &calls, last
|
||||
}
|
||||
|
||||
type synthEcho struct{ voice, text string }
|
||||
type synthEcho struct {
|
||||
voice, text string
|
||||
scale float64
|
||||
}
|
||||
|
||||
// newHandler builds a wav-format handler (no ffmpeg) pointed at a stub server.
|
||||
func newHandler(t *testing.T, endpoint string) *Handler {
|
||||
@@ -88,7 +92,12 @@ func TestSynthPathNormalisation(t *testing.T) {
|
||||
|
||||
func post(t *testing.T, h *Handler, text, lang string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
b, _ := json.Marshal(synthRequest{Text: text, Lang: lang})
|
||||
return postReq(t, h, synthRequest{Text: text, Lang: lang})
|
||||
}
|
||||
|
||||
func postReq(t *testing.T, h *Handler, body synthRequest) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
b, _ := json.Marshal(body)
|
||||
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(b))
|
||||
rr := httptest.NewRecorder()
|
||||
h.synth(rr, req)
|
||||
@@ -192,8 +201,87 @@ func TestTextIsCapped(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The slow replay is the whole of SUGGESTIONS §5e: same text, same voice, more
|
||||
// time per phoneme.
|
||||
func TestSlowRequestStretchesTheVoice(t *testing.T) {
|
||||
srv, _, last := newStubPiper(t, []byte("RIFF....fake-wav"))
|
||||
h := newHandler(t, srv.URL)
|
||||
|
||||
if rr := postReq(t, h, synthRequest{Text: "reception", Lang: "en-US"}); rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rr.Code)
|
||||
}
|
||||
if last.scale != lengthScale {
|
||||
t.Fatalf("normal length_scale = %v, want %v", last.scale, lengthScale)
|
||||
}
|
||||
|
||||
if rr := postReq(t, h, synthRequest{Text: "reception", Lang: "en-US", Slow: true}); rr.Code != http.StatusOK {
|
||||
t.Fatalf("slow status = %d, want 200", rr.Code)
|
||||
}
|
||||
if last.scale != slowLengthScale {
|
||||
t.Fatalf("slow length_scale = %v, want %v", last.scale, slowLengthScale)
|
||||
}
|
||||
if slowLengthScale <= lengthScale {
|
||||
t.Fatalf("slowLengthScale %v is not slower than %v", slowLengthScale, lengthScale)
|
||||
}
|
||||
}
|
||||
|
||||
// The pace has to be part of the cache key. Without it, asking for the slow
|
||||
// replay of a word already heard at normal speed serves the normal clip — the
|
||||
// one request where hearing the difference is the entire point.
|
||||
func TestSlowClipIsNotServedFromTheNormalCache(t *testing.T) {
|
||||
srv, calls, last := newStubPiper(t, []byte("RIFF....fake-wav"))
|
||||
h := newHandler(t, srv.URL)
|
||||
|
||||
postReq(t, h, synthRequest{Text: "reception", Lang: "en-US"})
|
||||
postReq(t, h, synthRequest{Text: "reception", Lang: "en-US", Slow: true})
|
||||
if *calls != 2 {
|
||||
t.Fatalf("piper calls = %d, want 2 (the slow clip is a different clip)", *calls)
|
||||
}
|
||||
if last.scale != slowLengthScale {
|
||||
t.Fatalf("second call length_scale = %v, want the slow one", last.scale)
|
||||
}
|
||||
|
||||
// …and each pace still caches on its own.
|
||||
postReq(t, h, synthRequest{Text: "reception", Lang: "en-US", Slow: true})
|
||||
postReq(t, h, synthRequest{Text: "reception", Lang: "en-US"})
|
||||
if *calls != 2 {
|
||||
t.Fatalf("piper calls = %d, want 2 (both paces now cached)", *calls)
|
||||
}
|
||||
}
|
||||
|
||||
// A Portuguese request must reach the Portuguese instance on the base tag alone:
|
||||
// env var names cannot hold the hyphen in pt-PT, so config keys the map on "pt"
|
||||
// and the handler has to meet it there. pt-BR resolves to the same instance
|
||||
// because there is only one Portuguese voice loaded — and it is the European one.
|
||||
func TestPortugueseRoutesOnTheBaseTag(t *testing.T) {
|
||||
enSrv, enCalls, _ := newStubPiper(t, []byte("EN-wav"))
|
||||
ptSrv, ptCalls, ptLast := newStubPiper(t, []byte("PT-wav"))
|
||||
h := &Handler{
|
||||
routes: map[string]route{
|
||||
"en": {strings.TrimRight(enSrv.URL, "/"), "en_US-amy-medium"},
|
||||
"pt": {strings.TrimRight(ptSrv.URL, "/"), "pt_PT-tugão-medium"},
|
||||
},
|
||||
cacheDir: t.TempDir(),
|
||||
format: formats["wav"],
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
|
||||
// Distinct text per tag, so a cache hit can't stand in for a route.
|
||||
for i, tag := range []string{"pt-PT", "pt", "pt-BR"} {
|
||||
if rr := post(t, h, strings.Repeat("receção ", i+1), tag); rr.Code != http.StatusOK {
|
||||
t.Fatalf("%s status = %d, want 200", tag, rr.Code)
|
||||
}
|
||||
}
|
||||
if *ptCalls != 3 || *enCalls != 0 {
|
||||
t.Fatalf("calls en=%d pt=%d, want en=0 pt=3", *enCalls, *ptCalls)
|
||||
}
|
||||
if ptLast.voice != "pt_PT-tugão-medium" {
|
||||
t.Fatalf("pt voice = %q, want the European Portuguese voice", ptLast.voice)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBaseLang(t *testing.T) {
|
||||
cases := map[string]string{"en-US": "en", "EN_gb": "en", "zh-CN": "zh", "en": "en", "": ""}
|
||||
cases := map[string]string{"en-US": "en", "EN_gb": "en", "zh-CN": "zh", "pt-PT": "pt", "en": "en", "": ""}
|
||||
for in, want := range cases {
|
||||
if got := baseLang(in); got != want {
|
||||
t.Errorf("baseLang(%q) = %q, want %q", in, got, want)
|
||||
|
||||
Reference in New Issue
Block a user