Übungssätze mit Azure-Stimme abspielbar machen
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -49,3 +49,20 @@ export async function request<T>(path: string, options: RequestInit = {}): Promi
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
export async function requestBlob(path: string, options: RequestInit = {}): Promise<Blob> {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { request } from './client';
|
||||
import { request, requestBlob } from './client';
|
||||
import type { Dashboard, Exercise, Lesson, ProgressSummary } from './types';
|
||||
|
||||
let pendingDashboardRequest: Promise<Dashboard> | null = null;
|
||||
@@ -16,6 +16,10 @@ export function fetchProgress() {
|
||||
return request<ProgressSummary>('/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 {
|
||||
|
||||
@@ -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<AbortController | null>(null);
|
||||
const audioGenerationRef = useRef(0);
|
||||
const recentExerciseIds = useRef<string[]>(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) {
|
||||
<section className="panel practice-panel">
|
||||
<p className="muted">{exercise.prompt_text}</p>
|
||||
<PronunciationText targetText={exercise.target_text} words={result?.attempt.word_scores} />
|
||||
<button className="ghost-button" type="button" disabled={loadingNext || audioLoading || recording} onClick={() => void playReference()}>
|
||||
<Volume2 size={18} /> {audioLoading ? 'Satz wird geladen...' : 'Satz anhören'}
|
||||
</button>
|
||||
{audioError && <p className="form-error">{audioError}</p>}
|
||||
<div className="action-row">
|
||||
<AudioRecorder
|
||||
key={exercise.id}
|
||||
onRecorded={setAudio}
|
||||
onRecordingStart={() => { 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'}
|
||||
|
||||
Reference in New Issue
Block a user