Ausspracheaufnahme vor Bewertung prüfbar machen
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<MediaStream | null>(null);
|
||||
const contextRef = useRef<AudioContext | null>(null);
|
||||
const processorRef = useRef<ScriptProcessorNode | null>(null);
|
||||
@@ -16,11 +24,31 @@ export function AudioRecorder({ onRecorded, disabled }: Props) {
|
||||
const chunks = useRef<Float32Array[]>([]);
|
||||
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 (
|
||||
<div className="recorder">
|
||||
{recording ? (
|
||||
<button className="danger-button" onClick={stop} type="button">
|
||||
<Square size={18} /> Stop
|
||||
<Square size={18} /> Stoppen
|
||||
</button>
|
||||
) : (
|
||||
<button className="primary-button" onClick={start} type="button" disabled={disabled}>
|
||||
<Mic size={18} /> Aufnehmen
|
||||
<button className={secondary ? 'ghost-button' : 'primary-button'} onClick={start} type="button" disabled={disabled || starting}>
|
||||
<Mic size={18} /> {starting ? 'Mikrofon wird gestartet...' : buttonLabel}
|
||||
</button>
|
||||
)}
|
||||
{error && <span className="inline-error">{error}</span>}
|
||||
|
||||
@@ -16,19 +16,32 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
const [lesson, setLesson] = useState<Lesson | null>(initialSelection?.lesson ?? null);
|
||||
const [exercise, setExercise] = useState<Exercise | null>(initialSelection?.exercise ?? null);
|
||||
const [audio, setAudio] = useState<Blob | null>(null);
|
||||
const [audioUrl, setAudioUrl] = useState<string | null>(null);
|
||||
const [result, setResult] = useState<AttemptResponse | null>(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<string[]>(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) {
|
||||
<p className="eyebrow">{lesson.title}</p>
|
||||
<h1>Aussprache</h1>
|
||||
</div>
|
||||
<button className="ghost-button" disabled={loadingNext || loading} onClick={() => void loadNext(true)}>
|
||||
<button className="ghost-button" disabled={loadingNext || loading || recording} onClick={() => void loadNext(true)}>
|
||||
<RotateCcw size={18} /> {loadingNext ? 'Lade neue Uebung...' : 'Andere Uebung'}
|
||||
</button>
|
||||
</header>
|
||||
@@ -92,12 +108,28 @@ export function PracticePage({ initialSelection }: Props) {
|
||||
<p className="muted">{exercise.prompt_text}</p>
|
||||
<PronunciationText targetText={exercise.target_text} words={result?.attempt.word_scores} />
|
||||
<div className="action-row">
|
||||
<AudioRecorder onRecorded={setAudio} disabled={loading || loadingNext} />
|
||||
<button className="primary-button" disabled={!audio || loading || loadingNext} onClick={submit}>
|
||||
<Send size={18} /> Bewerten
|
||||
</button>
|
||||
<AudioRecorder
|
||||
key={exercise.id}
|
||||
onRecorded={setAudio}
|
||||
onRecordingStart={() => { 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 && (
|
||||
<button className="primary-button" disabled={loading || loadingNext} onClick={() => void evaluateRecording()} type="button">
|
||||
<Send size={18} /> Bewerten
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{audio && !result && <p className="muted">Aufnahme bereit. Jetzt bewerten.</p>}
|
||||
{audio && !recording && (
|
||||
<div className="recording-preview">
|
||||
<p className="muted">Aufnahme noch nicht gesendet. Anhören, erneut aufnehmen oder bewerten.</p>
|
||||
{audioUrl && <audio controls src={audioUrl} aria-label="Eigene Aufnahme anhören" />}
|
||||
</div>
|
||||
)}
|
||||
{loading && <p className="muted">Die Aussprache wird bewertet...</p>}
|
||||
</section>
|
||||
|
||||
{result && (
|
||||
|
||||
Reference in New Issue
Block a user