From 9e6347400a9029777daba848dd3a2f438a417d95 Mon Sep 17 00:00:00 2001 From: Eugenius Naumenko Date: Wed, 14 Oct 2026 00:57:56 +0000 Subject: [PATCH] =?UTF-8?q?Ausspracheaufnahme=20vor=20Bewertung=20pr=C3=BC?= =?UTF-8?q?fbar=20machen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/src/app/styles.css | 9 ++++ apps/web/src/components/AudioRecorder.tsx | 63 ++++++++++++++++++++--- apps/web/src/pages/PracticePage.tsx | 50 ++++++++++++++---- 3 files changed, 105 insertions(+), 17 deletions(-) diff --git a/apps/web/src/app/styles.css b/apps/web/src/app/styles.css index fde66df..c1d76c3 100644 --- a/apps/web/src/app/styles.css +++ b/apps/web/src/app/styles.css @@ -312,6 +312,15 @@ input:focus { flex-wrap: wrap; } +.recording-preview { + display: grid; + gap: 8px; +} + +.recording-preview audio { + max-width: 100%; +} + .result-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); diff --git a/apps/web/src/components/AudioRecorder.tsx b/apps/web/src/components/AudioRecorder.tsx index 48f75f8..e33398b 100644 --- a/apps/web/src/components/AudioRecorder.tsx +++ b/apps/web/src/components/AudioRecorder.tsx @@ -1,14 +1,22 @@ import { Mic, Square } from 'lucide-react'; -import { useRef, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; type Props = { onRecorded: (blob: Blob) => void; + onRecordingStart?: () => void; + onRecordingChange?: (recording: boolean) => void; disabled?: boolean; + buttonLabel?: string; + secondary?: boolean; }; -export function AudioRecorder({ onRecorded, disabled }: Props) { +export function AudioRecorder({ onRecorded, onRecordingStart, onRecordingChange, disabled, buttonLabel = 'Aufnehmen', secondary }: Props) { const [recording, setRecording] = useState(false); + const [starting, setStarting] = useState(false); const [error, setError] = useState(''); + const startingRef = useRef(false); + const recordingRef = useRef(false); + const mountedRef = useRef(true); const streamRef = useRef(null); const contextRef = useRef(null); const processorRef = useRef(null); @@ -16,11 +24,31 @@ export function AudioRecorder({ onRecorded, disabled }: Props) { const chunks = useRef([]); const sampleRate = useRef(44100); + useEffect(() => { + mountedRef.current = true; + return () => { + mountedRef.current = false; + streamRef.current?.getTracks().forEach((track) => track.stop()); + void contextRef.current?.close(); + }; + }, []); + async function start() { + if (startingRef.current || recordingRef.current || disabled) return; + startingRef.current = true; + setStarting(true); + onRecordingChange?.(true); setError(''); + let stream: MediaStream | null = null; + let audioContext: AudioContext | null = null; try { - const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); - const audioContext = new AudioContext(); + stream = await navigator.mediaDevices.getUserMedia({ audio: true }); + if (!mountedRef.current) { + stream.getTracks().forEach((track) => track.stop()); + onRecordingChange?.(false); + return; + } + audioContext = new AudioContext(); const source = audioContext.createMediaStreamSource(stream); const processor = audioContext.createScriptProcessor(4096, 1, 1); chunks.current = []; @@ -35,33 +63,52 @@ export function AudioRecorder({ onRecorded, disabled }: Props) { contextRef.current = audioContext; processorRef.current = processor; sourceRef.current = source; + recordingRef.current = true; setRecording(true); + onRecordingStart?.(); } catch (err) { + stream?.getTracks().forEach((track) => track.stop()); + if (audioContext) void audioContext.close(); setError(err instanceof Error ? err.message : 'Mikrofon nicht verfuegbar'); + onRecordingChange?.(false); + } finally { + startingRef.current = false; + setStarting(false); } } function stop() { + if (!recordingRef.current) return; + recordingRef.current = false; processorRef.current?.disconnect(); sourceRef.current?.disconnect(); streamRef.current?.getTracks().forEach((track) => track.stop()); void contextRef.current?.close(); + processorRef.current = null; + sourceRef.current = null; + streamRef.current = null; + contextRef.current = null; + setRecording(false); + onRecordingChange?.(false); const merged = mergeBuffers(chunks.current); + if (merged.length === 0) { + setError('Keine Aufnahme erkannt. Bitte erneut versuchen.'); + return; + } const downsampled = downsampleBuffer(merged, sampleRate.current, 16000); const wav = encodeWav(downsampled, 16000); onRecorded(new Blob([wav], { type: 'audio/wav' })); - setRecording(false); } return (
{recording ? ( ) : ( - )} {error && {error}} diff --git a/apps/web/src/pages/PracticePage.tsx b/apps/web/src/pages/PracticePage.tsx index c9d3a77..444bbc4 100644 --- a/apps/web/src/pages/PracticePage.tsx +++ b/apps/web/src/pages/PracticePage.tsx @@ -16,19 +16,32 @@ export function PracticePage({ initialSelection }: Props) { const [lesson, setLesson] = useState(initialSelection?.lesson ?? null); const [exercise, setExercise] = useState(initialSelection?.exercise ?? null); const [audio, setAudio] = useState(null); + const [audioUrl, setAudioUrl] = useState(null); const [result, setResult] = useState(null); const [loading, setLoading] = useState(false); const [loadingNext, setLoadingNext] = useState(false); + const [recording, setRecording] = useState(false); const [error, setError] = useState(''); const [notice, setNotice] = useState(''); + const submittingRef = useRef(false); const recentExerciseIds = useRef(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []); useEffect(() => { if (!initialSelection) void loadNext(); }, []); + useEffect(() => { + if (!audio) { + setAudioUrl(null); + return; + } + const url = URL.createObjectURL(audio); + setAudioUrl(url); + return () => URL.revokeObjectURL(url); + }, [audio]); + async function loadNext(fresh = false) { - if (loadingNext) return; + if (loadingNext || recording || submittingRef.current) return; setLoadingNext(true); setError(''); setNotice(''); @@ -51,8 +64,10 @@ export function PracticePage({ initialSelection }: Props) { } } - async function submit() { - if (!exercise || !audio) return; + async function evaluateRecording() { + if (!exercise || !audio || submittingRef.current) return; + submittingRef.current = true; + setAudio(null); setLoading(true); setError(''); try { @@ -60,6 +75,7 @@ export function PracticePage({ initialSelection }: Props) { } catch (err) { setError(err instanceof Error ? err.message : 'Bewertung fehlgeschlagen'); } finally { + submittingRef.current = false; setLoading(false); } } @@ -80,7 +96,7 @@ export function PracticePage({ initialSelection }: Props) {

{lesson.title}

Aussprache

- @@ -92,12 +108,28 @@ export function PracticePage({ initialSelection }: Props) {

{exercise.prompt_text}

- - + { setAudio(null); setResult(null); setError(''); }} + onRecordingChange={setRecording} + disabled={loading || loadingNext} + buttonLabel={loading ? 'Bewertung läuft...' : audio || result ? 'Erneut aufnehmen' : 'Aufnehmen'} + secondary={Boolean(audio)} + /> + {audio && !recording && ( + + )}
- {audio && !result &&

Aufnahme bereit. Jetzt bewerten.

} + {audio && !recording && ( +
+

Aufnahme noch nicht gesendet. Anhören, erneut aufnehmen oder bewerten.

+ {audioUrl &&
+ )} + {loading &&

Die Aussprache wird bewertet...

} {result && (