Langsame Hörhilfen für Ausspracheübungen ergänzen

This commit is contained in:
Eugenius Naumenko
2026-10-14 02:13:07 +00:00
parent 16da8cad1d
commit 83944f791e
7 changed files with 261 additions and 32 deletions
@@ -34,10 +34,13 @@ type audioSpeechService struct {
locale string
voice string
text string
mode string
word string
}
func (s *audioSpeechService) Synthesize(_ context.Context, locale, voice, text string) ([]byte, string, error) {
func (s *audioSpeechService) SynthesizeExercise(_ context.Context, locale, voice, text, mode, word string) ([]byte, string, error) {
s.locale, s.voice, s.text = locale, voice, text
s.mode, s.word = mode, word
return []byte("mp3-data"), "audio/mpeg", nil
}
@@ -58,11 +61,50 @@ func TestExerciseAudioUsesStoredSentenceAndLanguageVoice(t *testing.T) {
if recorder.Header().Get("Content-Type") != "audio/mpeg" {
t.Fatalf("content type = %q", recorder.Header().Get("Content-Type"))
}
if lookup.requestedUserID != "usr_current" || speech.locale != "en-US" || speech.voice != "en-US-JennyNeural" || speech.text != "Please pass the water." {
if lookup.requestedUserID != "usr_current" || speech.locale != "en-US" || speech.voice != "en-US-JennyNeural" || speech.text != "Please pass the water." || speech.mode != "normal" {
t.Fatalf("unexpected synthesis input: user %q, locale %q, voice %q, text %q", lookup.requestedUserID, speech.locale, speech.voice, speech.text)
}
}
func TestExerciseAudioSlowAndFocusModes(t *testing.T) {
for _, test := range []struct {
name string
url string
mode string
word string
}{
{"slow", "/practice/exercises/ex_1/audio?mode=slow", "slow", ""},
{"focus", "/practice/exercises/ex_1/audio?mode=focus&word=water", "focus", "water"},
{"focus punctuation", "/practice/exercises/ex_1/audio?mode=focus&word=water.", "focus", "water"},
} {
t.Run(test.name, func(t *testing.T) {
speech := &audioSpeechService{}
server := &Server{store: &audioExerciseStore{}, speech: speech}
req := httptest.NewRequest(http.MethodGet, test.url, nil)
req.SetPathValue("id", "ex_1")
req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current"))
recorder := httptest.NewRecorder()
server.exerciseAudio(recorder, req)
if recorder.Code != http.StatusOK || speech.mode != test.mode || speech.word != test.word {
t.Fatalf("status = %d, mode = %q, word = %q", recorder.Code, speech.mode, speech.word)
}
})
}
}
func TestExerciseAudioRejectsWordsOutsideSentence(t *testing.T) {
speech := &audioSpeechService{}
server := &Server{store: &audioExerciseStore{}, speech: speech}
req := httptest.NewRequest(http.MethodGet, "/practice/exercises/ex_1/audio?mode=focus&word=unrelated", nil)
req.SetPathValue("id", "ex_1")
req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current"))
recorder := httptest.NewRecorder()
server.exerciseAudio(recorder, req)
if recorder.Code != http.StatusBadRequest || speech.text != "" {
t.Fatalf("status = %d, synthesis text = %q", recorder.Code, speech.text)
}
}
func TestExerciseAudioRejectsInaccessibleExercise(t *testing.T) {
lookup := &audioExerciseStore{lookupErr: store.ErrNotFound}
speech := &audioSpeechService{}
+35 -1
View File
@@ -9,6 +9,7 @@ import (
"net/http"
"strings"
"time"
"unicode"
"speech-learning-api/internal/config"
"speech-learning-api/internal/domain"
@@ -306,12 +307,31 @@ func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusNotFound, err)
return
}
mode := r.URL.Query().Get("mode")
if mode == "" {
mode = "normal"
}
if mode != "normal" && mode != "slow" && mode != "focus" {
writeError(w, http.StatusBadRequest, errors.New("unsupported audio mode"))
return
}
focusWord := r.URL.Query().Get("word")
if mode == "focus" {
focusWord = exerciseFocusWord(exercise.TargetText, focusWord)
if focusWord == "" {
writeError(w, http.StatusBadRequest, errors.New("word must occur in the exercise sentence"))
return
}
} else if focusWord != "" {
writeError(w, http.StatusBadRequest, errors.New("word is only supported in focus mode"))
return
}
language, err := s.store.GetLanguage(r.Context(), exercise.TargetLanguage)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
audio, mime, err := s.speech.Synthesize(r.Context(), language.AzureLocale, language.DefaultTTSVoice, exercise.TargetText)
audio, mime, err := s.speech.SynthesizeExercise(r.Context(), language.AzureLocale, language.DefaultTTSVoice, exercise.TargetText, mode, focusWord)
if err != nil {
writeError(w, http.StatusBadGateway, err)
return
@@ -326,6 +346,20 @@ func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(audio)
}
func exerciseFocusWord(sentence, requested string) string {
requested = strings.TrimFunc(strings.TrimSpace(requested), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })
if requested == "" {
return ""
}
for _, token := range strings.Fields(sentence) {
word := strings.TrimFunc(token, func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })
if strings.EqualFold(word, requested) {
return word
}
}
return ""
}
func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(24 << 20); err != nil {
writeError(w, http.StatusBadRequest, err)