diff --git a/apps/api/internal/httpapi/exercise_audio_test.go b/apps/api/internal/httpapi/exercise_audio_test.go new file mode 100644 index 0000000..ad1f1be --- /dev/null +++ b/apps/api/internal/httpapi/exercise_audio_test.go @@ -0,0 +1,80 @@ +package httpapi + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "speech-learning-api/internal/domain" + "speech-learning-api/internal/services" + "speech-learning-api/internal/store" +) + +type audioExerciseStore struct { + DataStore + requestedUserID string + lookupErr error +} + +func (s *audioExerciseStore) GetExercise(_ context.Context, _, userID string) (domain.Exercise, error) { + s.requestedUserID = userID + if s.lookupErr != nil { + return domain.Exercise{}, s.lookupErr + } + return domain.Exercise{ID: "ex_1", TargetLanguage: "en-US", TargetText: "Please pass the water."}, nil +} + +func (s *audioExerciseStore) GetLanguage(_ context.Context, _ string) (domain.LanguageProfile, error) { + return domain.LanguageProfile{AzureLocale: "en-US", DefaultTTSVoice: "en-US-JennyNeural"}, nil +} + +type audioSpeechService struct { + services.SpeechService + locale string + voice string + text string +} + +func (s *audioSpeechService) Synthesize(_ context.Context, locale, voice, text string) ([]byte, string, error) { + s.locale, s.voice, s.text = locale, voice, text + return []byte("mp3-data"), "audio/mpeg", nil +} + +func TestExerciseAudioUsesStoredSentenceAndLanguageVoice(t *testing.T) { + lookup := &audioExerciseStore{} + speech := &audioSpeechService{} + server := &Server{store: lookup, speech: speech} + req := httptest.NewRequest(http.MethodGet, "/practice/exercises/ex_1/audio", 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 || recorder.Body.String() != "mp3-data" { + t.Fatalf("status = %d, body = %q", recorder.Code, recorder.Body.String()) + } + 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." { + t.Fatalf("unexpected synthesis input: user %q, locale %q, voice %q, text %q", lookup.requestedUserID, speech.locale, speech.voice, speech.text) + } +} + +func TestExerciseAudioRejectsInaccessibleExercise(t *testing.T) { + lookup := &audioExerciseStore{lookupErr: store.ErrNotFound} + speech := &audioSpeechService{} + server := &Server{store: lookup, speech: speech} + req := httptest.NewRequest(http.MethodGet, "/practice/exercises/ex_other/audio", nil) + req.SetPathValue("id", "ex_other") + req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current")) + recorder := httptest.NewRecorder() + + server.exerciseAudio(recorder, req) + + if recorder.Code != http.StatusNotFound || lookup.requestedUserID != "usr_current" || speech.text != "" { + t.Fatalf("status = %d, synthesis text = %q", recorder.Code, speech.text) + } +} diff --git a/apps/api/internal/httpapi/server.go b/apps/api/internal/httpapi/server.go index 0b1e601..4e731f4 100644 --- a/apps/api/internal/httpapi/server.go +++ b/apps/api/internal/httpapi/server.go @@ -61,6 +61,7 @@ func (s *Server) Routes() http.Handler { mux.Handle("GET /dashboard", s.requireAuth(http.HandlerFunc(s.dashboard))) mux.Handle("GET /progress", s.requireAuth(http.HandlerFunc(s.progress))) mux.Handle("POST /lessons/next", s.requireAuth(http.HandlerFunc(s.nextLesson))) + mux.Handle("GET /practice/exercises/{id}/audio", s.requireAuth(http.HandlerFunc(s.exerciseAudio))) mux.Handle("POST /practice/attempts", s.requireAuth(http.HandlerFunc(s.createAttempt))) mux.Handle("POST /conversation/start", s.requireAuth(http.HandlerFunc(s.startConversation))) mux.Handle("POST /conversation/{id}/turn", s.requireAuth(http.HandlerFunc(s.conversationTurn))) @@ -299,6 +300,32 @@ func (s *Server) nextAgentExercise(ctx context.Context, user domain.User, profil return lesson, exercise, err } +func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) { + exercise, err := s.store.GetExercise(r.Context(), r.PathValue("id"), currentUserID(r)) + if err != nil { + writeError(w, http.StatusNotFound, err) + 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) + if err != nil { + writeError(w, http.StatusBadGateway, err) + return + } + if len(audio) == 0 { + writeError(w, http.StatusServiceUnavailable, errors.New("audio is unavailable")) + return + } + w.Header().Set("Content-Type", mime) + w.Header().Set("Cache-Control", "private, no-store") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(audio) +} + func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) { if err := r.ParseMultipartForm(24 << 20); err != nil { writeError(w, http.StatusBadRequest, err) diff --git a/apps/web/src/api/client.ts b/apps/web/src/api/client.ts index 93961e0..1bb271b 100644 --- a/apps/web/src/api/client.ts +++ b/apps/web/src/api/client.ts @@ -49,3 +49,20 @@ export async function request(path: string, options: RequestInit = {}): Promi } return payload as T; } + +export async function requestBlob(path: string, options: RequestInit = {}): Promise { + const headers = new Headers(options.headers); + const token = getToken(); + if (token) { + headers.set('Authorization', `Bearer ${token}`); + } + const response = await fetch(`${API_BASE}${path}`, { ...options, headers }); + if (!response.ok) { + const payload = response.headers.get('content-type')?.includes('application/json') + ? await response.json() + : await response.text(); + const message = typeof payload === 'object' && payload?.error ? payload.error : response.statusText; + throw new ApiError(response.status, message, response.headers.get('x-request-id') ?? '', payload); + } + return response.blob(); +} diff --git a/apps/web/src/api/lessons.ts b/apps/web/src/api/lessons.ts index 9b26301..95284d1 100644 --- a/apps/web/src/api/lessons.ts +++ b/apps/web/src/api/lessons.ts @@ -1,4 +1,4 @@ -import { request } from './client'; +import { request, requestBlob } from './client'; import type { Dashboard, Exercise, Lesson, ProgressSummary } from './types'; let pendingDashboardRequest: Promise | null = null; @@ -16,6 +16,10 @@ export function fetchProgress() { return request('/progress'); } +export function fetchExerciseAudio(exerciseId: string, signal?: AbortSignal) { + return requestBlob(`/practice/exercises/${encodeURIComponent(exerciseId)}/audio`, { signal }); +} + export async function fetchNextLesson(fresh = false, recentExerciseIds: string[] = []) { if (!fresh && pendingDashboardRequest) { try { diff --git a/apps/web/src/pages/PracticePage.tsx b/apps/web/src/pages/PracticePage.tsx index 444bbc4..7facce1 100644 --- a/apps/web/src/pages/PracticePage.tsx +++ b/apps/web/src/pages/PracticePage.tsx @@ -1,6 +1,6 @@ -import { RotateCcw, Send } from 'lucide-react'; +import { RotateCcw, Send, Volume2 } from 'lucide-react'; import { useEffect, useRef, useState } from 'react'; -import { fetchNextLesson } from '../api/lessons'; +import { fetchExerciseAudio, fetchNextLesson } from '../api/lessons'; import { submitAttempt } from '../api/practice'; import type { AttemptResponse, Exercise, Lesson } from '../api/types'; import { AudioRecorder } from '../components/AudioRecorder'; @@ -21,9 +21,14 @@ export function PracticePage({ initialSelection }: Props) { const [loading, setLoading] = useState(false); const [loadingNext, setLoadingNext] = useState(false); const [recording, setRecording] = useState(false); + const [audioLoading, setAudioLoading] = useState(false); + const [audioError, setAudioError] = useState(''); const [error, setError] = useState(''); const [notice, setNotice] = useState(''); const submittingRef = useRef(false); + const referenceAudioRef = useRef<{ exerciseId: string; url: string; player: HTMLAudioElement } | null>(null); + const audioRequestRef = useRef(null); + const audioGenerationRef = useRef(0); const recentExerciseIds = useRef(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []); useEffect(() => { @@ -40,11 +45,54 @@ export function PracticePage({ initialSelection }: Props) { return () => URL.revokeObjectURL(url); }, [audio]); + useEffect(() => () => { + audioGenerationRef.current += 1; + audioRequestRef.current?.abort(); + audioRequestRef.current = null; + referenceAudioRef.current?.player.pause(); + if (referenceAudioRef.current) URL.revokeObjectURL(referenceAudioRef.current.url); + referenceAudioRef.current = null; + }, [exercise?.id]); + + async function playReference() { + if (!exercise || audioLoading || audioRequestRef.current || recording) return; + setAudioError(''); + const exerciseId = exercise.id; + const generation = audioGenerationRef.current; + let controller: AbortController | null = null; + try { + if (!referenceAudioRef.current || referenceAudioRef.current.exerciseId !== exerciseId) { + setAudioLoading(true); + controller = new AbortController(); + audioRequestRef.current = controller; + const blob = await fetchExerciseAudio(exerciseId, controller.signal); + if (controller.signal.aborted || audioGenerationRef.current !== generation) return; + const url = URL.createObjectURL(blob); + referenceAudioRef.current = { exerciseId, url, player: new Audio(url) }; + } + const player = referenceAudioRef.current.player; + player.currentTime = 0; + await player.play(); + } catch { + if (audioGenerationRef.current !== generation || controller?.signal.aborted) return; + setAudioError('Satz konnte nicht abgespielt werden. Bitte erneut versuchen.'); + } finally { + if (audioGenerationRef.current === generation) setAudioLoading(false); + if (audioRequestRef.current === controller) audioRequestRef.current = null; + } + } + async function loadNext(fresh = false) { if (loadingNext || recording || submittingRef.current) return; + audioGenerationRef.current += 1; + audioRequestRef.current?.abort(); + audioRequestRef.current = null; + referenceAudioRef.current?.player.pause(); + setAudioLoading(false); setLoadingNext(true); setError(''); setNotice(''); + setAudioError(''); setAudio(null); setResult(null); try { @@ -107,11 +155,24 @@ export function PracticePage({ initialSelection }: Props) {

{exercise.prompt_text}

+ + {audioError &&

{audioError}

}
{ setAudio(null); setResult(null); setError(''); }} + onRecordingStart={() => { + audioGenerationRef.current += 1; + audioRequestRef.current?.abort(); + audioRequestRef.current = null; + referenceAudioRef.current?.player.pause(); + setAudioLoading(false); + setAudio(null); + setResult(null); + setError(''); + }} onRecordingChange={setRecording} disabled={loading || loadingNext} buttonLabel={loading ? 'Bewertung läuft...' : audio || result ? 'Erneut aufnehmen' : 'Aufnehmen'} diff --git a/docs/api.md b/docs/api.md index ad0703c..ebbf856 100644 --- a/docs/api.md +++ b/docs/api.md @@ -29,6 +29,7 @@ Authorization: Bearer ## Lessons and Practice - `POST /lessons/next` +- `GET /practice/exercises/{id}/audio` (Bearer-Token erforderlich; liefert den Satz der zugänglichen Übung als `audio/mpeg`) - `POST /practice/attempts` `POST /practice/attempts` expects multipart form data: