Langsame Hörhilfen für Ausspracheübungen ergänzen
This commit is contained in:
@@ -16,8 +16,14 @@ export function fetchProgress() {
|
||||
return request<ProgressSummary>('/progress');
|
||||
}
|
||||
|
||||
export function fetchExerciseAudio(exerciseId: string, signal?: AbortSignal) {
|
||||
return requestBlob(`/practice/exercises/${encodeURIComponent(exerciseId)}/audio`, { signal });
|
||||
export type ExerciseAudioMode = 'normal' | 'slow' | 'focus';
|
||||
|
||||
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[] = []) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RotateCcw, Send, Volume2 } from 'lucide-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 type { AttemptResponse, Exercise, Lesson } from '../api/types';
|
||||
import type { AttemptResponse, Exercise, Lesson, WordScore } from '../api/types';
|
||||
import { AudioRecorder } from '../components/AudioRecorder';
|
||||
import { PhonemeAnalysis } from '../components/PhonemeAnalysis';
|
||||
import { PronunciationText } from '../components/PronunciationText';
|
||||
@@ -21,12 +21,12 @@ 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 [audioLoadingKey, setAudioLoadingKey] = useState<string | null>(null);
|
||||
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 referenceAudioRef = useRef<{ exerciseId: string; urls: Map<string, string>; player: HTMLAudioElement | null } | null>(null);
|
||||
const audioRequestRef = useRef<AbortController | null>(null);
|
||||
const audioGenerationRef = useRef(0);
|
||||
const recentExerciseIds = useRef<string[]>(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []);
|
||||
@@ -49,35 +49,41 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
audioGenerationRef.current += 1;
|
||||
audioRequestRef.current?.abort();
|
||||
audioRequestRef.current = null;
|
||||
referenceAudioRef.current?.player.pause();
|
||||
if (referenceAudioRef.current) URL.revokeObjectURL(referenceAudioRef.current.url);
|
||||
referenceAudioRef.current?.player?.pause();
|
||||
referenceAudioRef.current?.urls.forEach((url) => URL.revokeObjectURL(url));
|
||||
referenceAudioRef.current = null;
|
||||
}, [exercise?.id]);
|
||||
|
||||
async function playReference() {
|
||||
if (!exercise || audioLoading || audioRequestRef.current || recording) return;
|
||||
async function playReference(mode: ExerciseAudioMode, word = '') {
|
||||
if (!exercise || audioLoadingKey || audioRequestRef.current || recording) return;
|
||||
setAudioError('');
|
||||
const exerciseId = exercise.id;
|
||||
const audioKey = `${mode}:${word.toLocaleLowerCase()}`;
|
||||
const generation = audioGenerationRef.current;
|
||||
let controller: AbortController | null = null;
|
||||
try {
|
||||
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();
|
||||
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;
|
||||
const url = URL.createObjectURL(blob);
|
||||
referenceAudioRef.current = { exerciseId, url, player: new Audio(url) };
|
||||
url = URL.createObjectURL(blob);
|
||||
referenceAudioRef.current.urls.set(audioKey, url);
|
||||
}
|
||||
const player = referenceAudioRef.current.player;
|
||||
player.currentTime = 0;
|
||||
referenceAudioRef.current.player?.pause();
|
||||
const player = new Audio(url);
|
||||
referenceAudioRef.current.player = player;
|
||||
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 (audioGenerationRef.current === generation) setAudioLoadingKey(null);
|
||||
if (audioRequestRef.current === controller) audioRequestRef.current = null;
|
||||
}
|
||||
}
|
||||
@@ -87,8 +93,8 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
audioGenerationRef.current += 1;
|
||||
audioRequestRef.current?.abort();
|
||||
audioRequestRef.current = null;
|
||||
referenceAudioRef.current?.player.pause();
|
||||
setAudioLoading(false);
|
||||
referenceAudioRef.current?.player?.pause();
|
||||
setAudioLoadingKey(null);
|
||||
setLoadingNext(true);
|
||||
setError('');
|
||||
setNotice('');
|
||||
@@ -137,6 +143,8 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
);
|
||||
}
|
||||
|
||||
const focusWords = result ? practiceFocusWords(exercise.target_text, result.attempt.word_scores) : [];
|
||||
|
||||
return (
|
||||
<div className="view-stack">
|
||||
<header className="view-header">
|
||||
@@ -155,9 +163,26 @@ 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>
|
||||
<div className="action-row">
|
||||
<button className="ghost-button" type="button" disabled={loadingNext || audioLoadingKey !== null || recording} onClick={() => void playReference('normal')}>
|
||||
<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>}
|
||||
<div className="action-row">
|
||||
<AudioRecorder
|
||||
@@ -167,8 +192,8 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
audioGenerationRef.current += 1;
|
||||
audioRequestRef.current?.abort();
|
||||
audioRequestRef.current = null;
|
||||
referenceAudioRef.current?.player.pause();
|
||||
setAudioLoading(false);
|
||||
referenceAudioRef.current?.player?.pause();
|
||||
setAudioLoadingKey(null);
|
||||
setAudio(null);
|
||||
setResult(null);
|
||||
setError('');
|
||||
@@ -215,3 +240,28 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
</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, '');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user