Langsame Hörhilfen für Ausspracheübungen ergänzen
This commit is contained in:
@@ -34,10 +34,13 @@ type audioSpeechService struct {
|
|||||||
locale string
|
locale string
|
||||||
voice string
|
voice string
|
||||||
text 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.locale, s.voice, s.text = locale, voice, text
|
||||||
|
s.mode, s.word = mode, word
|
||||||
return []byte("mp3-data"), "audio/mpeg", nil
|
return []byte("mp3-data"), "audio/mpeg", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,11 +61,50 @@ func TestExerciseAudioUsesStoredSentenceAndLanguageVoice(t *testing.T) {
|
|||||||
if recorder.Header().Get("Content-Type") != "audio/mpeg" {
|
if recorder.Header().Get("Content-Type") != "audio/mpeg" {
|
||||||
t.Fatalf("content type = %q", recorder.Header().Get("Content-Type"))
|
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)
|
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) {
|
func TestExerciseAudioRejectsInaccessibleExercise(t *testing.T) {
|
||||||
lookup := &audioExerciseStore{lookupErr: store.ErrNotFound}
|
lookup := &audioExerciseStore{lookupErr: store.ErrNotFound}
|
||||||
speech := &audioSpeechService{}
|
speech := &audioSpeechService{}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"speech-learning-api/internal/config"
|
"speech-learning-api/internal/config"
|
||||||
"speech-learning-api/internal/domain"
|
"speech-learning-api/internal/domain"
|
||||||
@@ -306,12 +307,31 @@ func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeError(w, http.StatusNotFound, err)
|
writeError(w, http.StatusNotFound, err)
|
||||||
return
|
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)
|
language, err := s.store.GetLanguage(r.Context(), exercise.TargetLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusInternalServerError, err)
|
writeError(w, http.StatusInternalServerError, err)
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
writeError(w, http.StatusBadGateway, err)
|
writeError(w, http.StatusBadGateway, err)
|
||||||
return
|
return
|
||||||
@@ -326,6 +346,20 @@ func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
|
|||||||
_, _ = w.Write(audio)
|
_, _ = 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) {
|
func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := r.ParseMultipartForm(24 << 20); err != nil {
|
if err := r.ParseMultipartForm(24 << 20); err != nil {
|
||||||
writeError(w, http.StatusBadRequest, err)
|
writeError(w, http.StatusBadRequest, err)
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
type SpeechService interface {
|
type SpeechService interface {
|
||||||
AssessPronunciation(ctx context.Context, input PronunciationInput) (domain.PronunciationResult, error)
|
AssessPronunciation(ctx context.Context, input PronunciationInput) (domain.PronunciationResult, error)
|
||||||
Synthesize(ctx context.Context, locale, voice, text string) ([]byte, string, error)
|
Synthesize(ctx context.Context, locale, voice, text string) ([]byte, string, error)
|
||||||
|
SynthesizeExercise(ctx context.Context, locale, voice, text, mode, focusWord string) ([]byte, string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PronunciationInput struct {
|
type PronunciationInput struct {
|
||||||
@@ -98,13 +99,41 @@ func (s *AzureSpeechService) Synthesize(ctx context.Context, locale, voice, text
|
|||||||
if strings.TrimSpace(text) == "" {
|
if strings.TrimSpace(text) == "" {
|
||||||
return nil, "", errors.New("tts text is required")
|
return nil, "", errors.New("tts text is required")
|
||||||
}
|
}
|
||||||
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
|
||||||
return nil, "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
locale = valueOr(locale, "en-US")
|
locale = valueOr(locale, "en-US")
|
||||||
voice = valueOr(voice, "en-US-JennyNeural")
|
voice = valueOr(voice, "en-US-JennyNeural")
|
||||||
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, xmlEscape(text))
|
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, xmlEscape(text))
|
||||||
|
return s.synthesizeSSML(ctx, ssml)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *AzureSpeechService) SynthesizeExercise(ctx context.Context, locale, voice, text, mode, focusWord string) ([]byte, string, error) {
|
||||||
|
if strings.TrimSpace(text) == "" {
|
||||||
|
return nil, "", errors.New("tts text is required")
|
||||||
|
}
|
||||||
|
if mode == "normal" {
|
||||||
|
return s.Synthesize(ctx, locale, voice, text)
|
||||||
|
}
|
||||||
|
locale = valueOr(locale, "en-US")
|
||||||
|
voice = valueOr(voice, "en-US-JennyNeural")
|
||||||
|
var content string
|
||||||
|
switch mode {
|
||||||
|
case "slow":
|
||||||
|
content = fmt.Sprintf(`<prosody rate="0.8">%s</prosody>`, xmlEscape(text))
|
||||||
|
case "focus":
|
||||||
|
if strings.TrimSpace(focusWord) == "" {
|
||||||
|
return nil, "", errors.New("focus word is required")
|
||||||
|
}
|
||||||
|
content = fmt.Sprintf(`<prosody rate="0.7">%s</prosody><break time="400ms"/><prosody rate="0.8">%s</prosody>`, xmlEscape(focusWord), xmlEscape(text))
|
||||||
|
default:
|
||||||
|
return nil, "", errors.New("unsupported audio mode")
|
||||||
|
}
|
||||||
|
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, content)
|
||||||
|
return s.synthesizeSSML(ctx, ssml)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *AzureSpeechService) synthesizeSSML(ctx context.Context, ssml string) ([]byte, string, error) {
|
||||||
|
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
||||||
|
return nil, "", nil
|
||||||
|
}
|
||||||
endpoint := fmt.Sprintf("https://%s.tts.speech.microsoft.com/cognitiveservices/v1", s.cfg.AzureRegion)
|
endpoint := fmt.Sprintf("https://%s.tts.speech.microsoft.com/cognitiveservices/v1", s.cfg.AzureRegion)
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(ssml))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(ssml))
|
||||||
|
|||||||
@@ -1,6 +1,72 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"speech-learning-api/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type speechRoundTripFunc func(*http.Request) (*http.Response, error)
|
||||||
|
|
||||||
|
func (fn speechRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
return fn(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSynthesizeExerciseSendsValidSSML(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
mode string
|
||||||
|
word string
|
||||||
|
rates []string
|
||||||
|
}{
|
||||||
|
{"slow", "", []string{"0.8"}},
|
||||||
|
{"focus", "chips", []string{"0.7", "0.8"}},
|
||||||
|
} {
|
||||||
|
t.Run(test.mode, func(t *testing.T) {
|
||||||
|
var body string
|
||||||
|
service := &AzureSpeechService{
|
||||||
|
cfg: config.Config{AzureSpeechKey: "test-key", AzureRegion: "test-region"},
|
||||||
|
client: &http.Client{Transport: speechRoundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
payload, err := io.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
body = string(payload)
|
||||||
|
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("mp3")), Header: make(http.Header)}, nil
|
||||||
|
})},
|
||||||
|
}
|
||||||
|
_, _, err := service.SynthesizeExercise(context.Background(), "en-US", "en-US-JennyNeural", "Fish & chips are tasty.", test.mode, test.word)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
decoder := xml.NewDecoder(strings.NewReader(body))
|
||||||
|
for {
|
||||||
|
_, err := decoder.Token()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("invalid SSML: %v; body %q", err, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "Fish & chips are tasty.") {
|
||||||
|
t.Fatalf("sentence was not XML-escaped: %q", body)
|
||||||
|
}
|
||||||
|
for _, rate := range test.rates {
|
||||||
|
if !strings.Contains(body, `rate="`+rate+`"`) {
|
||||||
|
t.Fatalf("missing rate %s: %q", rate, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if test.mode == "focus" && (!strings.Contains(body, `<break time="400ms"/>`) || !strings.Contains(body, `>chips</prosody>`)) {
|
||||||
|
t.Fatalf("focus clip lacks the word or pause: %q", body)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMockPronunciationReturnsWordScores(t *testing.T) {
|
func TestMockPronunciationReturnsWordScores(t *testing.T) {
|
||||||
result := mockPronunciation("Hello, my name is Anna.")
|
result := mockPronunciation("Hello, my name is Anna.")
|
||||||
|
|||||||
@@ -16,8 +16,14 @@ export function fetchProgress() {
|
|||||||
return request<ProgressSummary>('/progress');
|
return request<ProgressSummary>('/progress');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchExerciseAudio(exerciseId: string, signal?: AbortSignal) {
|
export type ExerciseAudioMode = 'normal' | 'slow' | 'focus';
|
||||||
return requestBlob(`/practice/exercises/${encodeURIComponent(exerciseId)}/audio`, { signal });
|
|
||||||
|
export function fetchExerciseAudio(exerciseId: string, mode: ExerciseAudioMode = 'normal', word = '', signal?: AbortSignal) {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
if (mode !== 'normal') params.set('mode', mode);
|
||||||
|
if (mode === 'focus') params.set('word', word);
|
||||||
|
const query = params.size ? `?${params.toString()}` : '';
|
||||||
|
return requestBlob(`/practice/exercises/${encodeURIComponent(exerciseId)}/audio${query}`, { signal });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchNextLesson(fresh = false, recentExerciseIds: string[] = []) {
|
export async function fetchNextLesson(fresh = false, recentExerciseIds: string[] = []) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { RotateCcw, Send, Volume2 } from 'lucide-react';
|
import { RotateCcw, Send, Volume2 } from 'lucide-react';
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { fetchExerciseAudio, fetchNextLesson } from '../api/lessons';
|
import { fetchExerciseAudio, fetchNextLesson, type ExerciseAudioMode } from '../api/lessons';
|
||||||
import { submitAttempt } from '../api/practice';
|
import { submitAttempt } from '../api/practice';
|
||||||
import type { AttemptResponse, Exercise, Lesson } from '../api/types';
|
import type { AttemptResponse, Exercise, Lesson, WordScore } from '../api/types';
|
||||||
import { AudioRecorder } from '../components/AudioRecorder';
|
import { AudioRecorder } from '../components/AudioRecorder';
|
||||||
import { PhonemeAnalysis } from '../components/PhonemeAnalysis';
|
import { PhonemeAnalysis } from '../components/PhonemeAnalysis';
|
||||||
import { PronunciationText } from '../components/PronunciationText';
|
import { PronunciationText } from '../components/PronunciationText';
|
||||||
@@ -21,12 +21,12 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [loadingNext, setLoadingNext] = useState(false);
|
const [loadingNext, setLoadingNext] = useState(false);
|
||||||
const [recording, setRecording] = useState(false);
|
const [recording, setRecording] = useState(false);
|
||||||
const [audioLoading, setAudioLoading] = useState(false);
|
const [audioLoadingKey, setAudioLoadingKey] = useState<string | null>(null);
|
||||||
const [audioError, setAudioError] = useState('');
|
const [audioError, setAudioError] = useState('');
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
const [notice, setNotice] = useState('');
|
const [notice, setNotice] = useState('');
|
||||||
const submittingRef = useRef(false);
|
const submittingRef = useRef(false);
|
||||||
const referenceAudioRef = useRef<{ exerciseId: string; url: string; player: HTMLAudioElement } | null>(null);
|
const referenceAudioRef = useRef<{ exerciseId: string; urls: Map<string, string>; player: HTMLAudioElement | null } | null>(null);
|
||||||
const audioRequestRef = useRef<AbortController | null>(null);
|
const audioRequestRef = useRef<AbortController | null>(null);
|
||||||
const audioGenerationRef = useRef(0);
|
const audioGenerationRef = useRef(0);
|
||||||
const recentExerciseIds = useRef<string[]>(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []);
|
const recentExerciseIds = useRef<string[]>(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []);
|
||||||
@@ -49,35 +49,41 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
audioGenerationRef.current += 1;
|
audioGenerationRef.current += 1;
|
||||||
audioRequestRef.current?.abort();
|
audioRequestRef.current?.abort();
|
||||||
audioRequestRef.current = null;
|
audioRequestRef.current = null;
|
||||||
referenceAudioRef.current?.player.pause();
|
referenceAudioRef.current?.player?.pause();
|
||||||
if (referenceAudioRef.current) URL.revokeObjectURL(referenceAudioRef.current.url);
|
referenceAudioRef.current?.urls.forEach((url) => URL.revokeObjectURL(url));
|
||||||
referenceAudioRef.current = null;
|
referenceAudioRef.current = null;
|
||||||
}, [exercise?.id]);
|
}, [exercise?.id]);
|
||||||
|
|
||||||
async function playReference() {
|
async function playReference(mode: ExerciseAudioMode, word = '') {
|
||||||
if (!exercise || audioLoading || audioRequestRef.current || recording) return;
|
if (!exercise || audioLoadingKey || audioRequestRef.current || recording) return;
|
||||||
setAudioError('');
|
setAudioError('');
|
||||||
const exerciseId = exercise.id;
|
const exerciseId = exercise.id;
|
||||||
|
const audioKey = `${mode}:${word.toLocaleLowerCase()}`;
|
||||||
const generation = audioGenerationRef.current;
|
const generation = audioGenerationRef.current;
|
||||||
let controller: AbortController | null = null;
|
let controller: AbortController | null = null;
|
||||||
try {
|
try {
|
||||||
if (!referenceAudioRef.current || referenceAudioRef.current.exerciseId !== exerciseId) {
|
if (!referenceAudioRef.current || referenceAudioRef.current.exerciseId !== exerciseId) {
|
||||||
setAudioLoading(true);
|
referenceAudioRef.current = { exerciseId, urls: new Map(), player: null };
|
||||||
|
}
|
||||||
|
let url = referenceAudioRef.current.urls.get(audioKey);
|
||||||
|
if (!url) {
|
||||||
|
setAudioLoadingKey(audioKey);
|
||||||
controller = new AbortController();
|
controller = new AbortController();
|
||||||
audioRequestRef.current = controller;
|
audioRequestRef.current = controller;
|
||||||
const blob = await fetchExerciseAudio(exerciseId, controller.signal);
|
const blob = await fetchExerciseAudio(exerciseId, mode, word, controller.signal);
|
||||||
if (controller.signal.aborted || audioGenerationRef.current !== generation) return;
|
if (controller.signal.aborted || audioGenerationRef.current !== generation) return;
|
||||||
const url = URL.createObjectURL(blob);
|
url = URL.createObjectURL(blob);
|
||||||
referenceAudioRef.current = { exerciseId, url, player: new Audio(url) };
|
referenceAudioRef.current.urls.set(audioKey, url);
|
||||||
}
|
}
|
||||||
const player = referenceAudioRef.current.player;
|
referenceAudioRef.current.player?.pause();
|
||||||
player.currentTime = 0;
|
const player = new Audio(url);
|
||||||
|
referenceAudioRef.current.player = player;
|
||||||
await player.play();
|
await player.play();
|
||||||
} catch {
|
} catch {
|
||||||
if (audioGenerationRef.current !== generation || controller?.signal.aborted) return;
|
if (audioGenerationRef.current !== generation || controller?.signal.aborted) return;
|
||||||
setAudioError('Satz konnte nicht abgespielt werden. Bitte erneut versuchen.');
|
setAudioError('Satz konnte nicht abgespielt werden. Bitte erneut versuchen.');
|
||||||
} finally {
|
} finally {
|
||||||
if (audioGenerationRef.current === generation) setAudioLoading(false);
|
if (audioGenerationRef.current === generation) setAudioLoadingKey(null);
|
||||||
if (audioRequestRef.current === controller) audioRequestRef.current = null;
|
if (audioRequestRef.current === controller) audioRequestRef.current = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,8 +93,8 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
audioGenerationRef.current += 1;
|
audioGenerationRef.current += 1;
|
||||||
audioRequestRef.current?.abort();
|
audioRequestRef.current?.abort();
|
||||||
audioRequestRef.current = null;
|
audioRequestRef.current = null;
|
||||||
referenceAudioRef.current?.player.pause();
|
referenceAudioRef.current?.player?.pause();
|
||||||
setAudioLoading(false);
|
setAudioLoadingKey(null);
|
||||||
setLoadingNext(true);
|
setLoadingNext(true);
|
||||||
setError('');
|
setError('');
|
||||||
setNotice('');
|
setNotice('');
|
||||||
@@ -137,6 +143,8 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const focusWords = result ? practiceFocusWords(exercise.target_text, result.attempt.word_scores) : [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="view-stack">
|
<div className="view-stack">
|
||||||
<header className="view-header">
|
<header className="view-header">
|
||||||
@@ -155,9 +163,26 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
<section className="panel practice-panel">
|
<section className="panel practice-panel">
|
||||||
<p className="muted">{exercise.prompt_text}</p>
|
<p className="muted">{exercise.prompt_text}</p>
|
||||||
<PronunciationText targetText={exercise.target_text} words={result?.attempt.word_scores} />
|
<PronunciationText targetText={exercise.target_text} words={result?.attempt.word_scores} />
|
||||||
<button className="ghost-button" type="button" disabled={loadingNext || audioLoading || recording} onClick={() => void playReference()}>
|
<div className="action-row">
|
||||||
<Volume2 size={18} /> {audioLoading ? 'Satz wird geladen...' : 'Satz anhören'}
|
<button className="ghost-button" type="button" disabled={loadingNext || audioLoadingKey !== null || recording} onClick={() => void playReference('normal')}>
|
||||||
</button>
|
<Volume2 size={18} /> {audioLoadingKey === 'normal:' ? 'Satz wird geladen...' : 'Satz anhören'}
|
||||||
|
</button>
|
||||||
|
<button className="ghost-button" type="button" disabled={loadingNext || audioLoadingKey !== null || recording} onClick={() => void playReference('slow')}>
|
||||||
|
<Volume2 size={18} /> {audioLoadingKey === 'slow:' ? 'Satz wird geladen...' : 'Langsam anhören'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{focusWords.length > 0 && (
|
||||||
|
<div className="recording-preview">
|
||||||
|
<p className="muted">Diese Wörter kannst du gezielt anhören:</p>
|
||||||
|
<div className="action-row">
|
||||||
|
{focusWords.map((word) => (
|
||||||
|
<button className="ghost-button" type="button" key={word} disabled={loadingNext || audioLoadingKey !== null || recording} onClick={() => void playReference('focus', word)}>
|
||||||
|
<Volume2 size={18} /> {audioLoadingKey === `focus:${word.toLocaleLowerCase()}` ? 'Wort wird geladen...' : `${word} langsam üben`}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{audioError && <p className="form-error">{audioError}</p>}
|
{audioError && <p className="form-error">{audioError}</p>}
|
||||||
<div className="action-row">
|
<div className="action-row">
|
||||||
<AudioRecorder
|
<AudioRecorder
|
||||||
@@ -167,8 +192,8 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
audioGenerationRef.current += 1;
|
audioGenerationRef.current += 1;
|
||||||
audioRequestRef.current?.abort();
|
audioRequestRef.current?.abort();
|
||||||
audioRequestRef.current = null;
|
audioRequestRef.current = null;
|
||||||
referenceAudioRef.current?.player.pause();
|
referenceAudioRef.current?.player?.pause();
|
||||||
setAudioLoading(false);
|
setAudioLoadingKey(null);
|
||||||
setAudio(null);
|
setAudio(null);
|
||||||
setResult(null);
|
setResult(null);
|
||||||
setError('');
|
setError('');
|
||||||
@@ -215,3 +240,28 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function practiceFocusWords(sentence: string, scores: WordScore[]): string[] {
|
||||||
|
const sentenceWords = sentence.split(/\s+/).map(cleanWord);
|
||||||
|
const seen = new Set<string>();
|
||||||
|
return scores
|
||||||
|
.map((score, index) => ({
|
||||||
|
word: sentenceWords.find((word) => word.toLocaleLowerCase() === cleanWord(score.word).toLocaleLowerCase()) ?? '',
|
||||||
|
score: Math.min(score.accuracy, ...(score.phoneme_scores ?? []).map((phoneme) => phoneme.accuracy)),
|
||||||
|
index
|
||||||
|
}))
|
||||||
|
.filter((item) => item.word && item.score < 75)
|
||||||
|
.sort((a, b) => a.score - b.score || a.index - b.index)
|
||||||
|
.filter((item) => {
|
||||||
|
const key = item.word.toLocaleLowerCase();
|
||||||
|
if (seen.has(key)) return false;
|
||||||
|
seen.add(key);
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.slice(0, 3)
|
||||||
|
.map((item) => item.word);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanWord(value: string): string {
|
||||||
|
return value.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ Authorization: Bearer <token>
|
|||||||
- `GET /practice/exercises/{id}/audio` (Bearer-Token erforderlich; liefert den Satz der zugänglichen Übung als `audio/mpeg`)
|
- `GET /practice/exercises/{id}/audio` (Bearer-Token erforderlich; liefert den Satz der zugänglichen Übung als `audio/mpeg`)
|
||||||
- `POST /practice/attempts`
|
- `POST /practice/attempts`
|
||||||
|
|
||||||
|
`GET /practice/exercises/{id}/audio` unterstützt `mode=normal` (Standard), `mode=slow` und `mode=focus&word=water`. Im Fokusmodus spricht Azure zuerst das Wort, dann nach einer Pause den Satz langsam. Das Wort muss im gespeicherten Übungssatz vorkommen.
|
||||||
|
|
||||||
`POST /practice/attempts` expects multipart form data:
|
`POST /practice/attempts` expects multipart form data:
|
||||||
|
|
||||||
- `exercise_id`
|
- `exercise_id`
|
||||||
|
|||||||
Reference in New Issue
Block a user