Ausspracheaufnahme vor Bewertung prüfbar machen
This commit is contained in:
@@ -312,6 +312,15 @@ input:focus {
|
|||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.recording-preview {
|
||||||
|
display: grid;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.recording-preview audio {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
.result-grid {
|
.result-grid {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
|
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
|
||||||
|
|||||||
@@ -1,14 +1,22 @@
|
|||||||
import { Mic, Square } from 'lucide-react';
|
import { Mic, Square } from 'lucide-react';
|
||||||
import { useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
onRecorded: (blob: Blob) => void;
|
onRecorded: (blob: Blob) => void;
|
||||||
|
onRecordingStart?: () => void;
|
||||||
|
onRecordingChange?: (recording: boolean) => void;
|
||||||
disabled?: boolean;
|
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 [recording, setRecording] = useState(false);
|
||||||
|
const [starting, setStarting] = useState(false);
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
|
const startingRef = useRef(false);
|
||||||
|
const recordingRef = useRef(false);
|
||||||
|
const mountedRef = useRef(true);
|
||||||
const streamRef = useRef<MediaStream | null>(null);
|
const streamRef = useRef<MediaStream | null>(null);
|
||||||
const contextRef = useRef<AudioContext | null>(null);
|
const contextRef = useRef<AudioContext | null>(null);
|
||||||
const processorRef = useRef<ScriptProcessorNode | null>(null);
|
const processorRef = useRef<ScriptProcessorNode | null>(null);
|
||||||
@@ -16,11 +24,31 @@ export function AudioRecorder({ onRecorded, disabled }: Props) {
|
|||||||
const chunks = useRef<Float32Array[]>([]);
|
const chunks = useRef<Float32Array[]>([]);
|
||||||
const sampleRate = useRef(44100);
|
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() {
|
async function start() {
|
||||||
|
if (startingRef.current || recordingRef.current || disabled) return;
|
||||||
|
startingRef.current = true;
|
||||||
|
setStarting(true);
|
||||||
|
onRecordingChange?.(true);
|
||||||
setError('');
|
setError('');
|
||||||
|
let stream: MediaStream | null = null;
|
||||||
|
let audioContext: AudioContext | null = null;
|
||||||
try {
|
try {
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
const audioContext = new AudioContext();
|
if (!mountedRef.current) {
|
||||||
|
stream.getTracks().forEach((track) => track.stop());
|
||||||
|
onRecordingChange?.(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
audioContext = new AudioContext();
|
||||||
const source = audioContext.createMediaStreamSource(stream);
|
const source = audioContext.createMediaStreamSource(stream);
|
||||||
const processor = audioContext.createScriptProcessor(4096, 1, 1);
|
const processor = audioContext.createScriptProcessor(4096, 1, 1);
|
||||||
chunks.current = [];
|
chunks.current = [];
|
||||||
@@ -35,33 +63,52 @@ export function AudioRecorder({ onRecorded, disabled }: Props) {
|
|||||||
contextRef.current = audioContext;
|
contextRef.current = audioContext;
|
||||||
processorRef.current = processor;
|
processorRef.current = processor;
|
||||||
sourceRef.current = source;
|
sourceRef.current = source;
|
||||||
|
recordingRef.current = true;
|
||||||
setRecording(true);
|
setRecording(true);
|
||||||
|
onRecordingStart?.();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
stream?.getTracks().forEach((track) => track.stop());
|
||||||
|
if (audioContext) void audioContext.close();
|
||||||
setError(err instanceof Error ? err.message : 'Mikrofon nicht verfuegbar');
|
setError(err instanceof Error ? err.message : 'Mikrofon nicht verfuegbar');
|
||||||
|
onRecordingChange?.(false);
|
||||||
|
} finally {
|
||||||
|
startingRef.current = false;
|
||||||
|
setStarting(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function stop() {
|
function stop() {
|
||||||
|
if (!recordingRef.current) return;
|
||||||
|
recordingRef.current = false;
|
||||||
processorRef.current?.disconnect();
|
processorRef.current?.disconnect();
|
||||||
sourceRef.current?.disconnect();
|
sourceRef.current?.disconnect();
|
||||||
streamRef.current?.getTracks().forEach((track) => track.stop());
|
streamRef.current?.getTracks().forEach((track) => track.stop());
|
||||||
void contextRef.current?.close();
|
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);
|
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 downsampled = downsampleBuffer(merged, sampleRate.current, 16000);
|
||||||
const wav = encodeWav(downsampled, 16000);
|
const wav = encodeWav(downsampled, 16000);
|
||||||
onRecorded(new Blob([wav], { type: 'audio/wav' }));
|
onRecorded(new Blob([wav], { type: 'audio/wav' }));
|
||||||
setRecording(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="recorder">
|
<div className="recorder">
|
||||||
{recording ? (
|
{recording ? (
|
||||||
<button className="danger-button" onClick={stop} type="button">
|
<button className="danger-button" onClick={stop} type="button">
|
||||||
<Square size={18} /> Stop
|
<Square size={18} /> Stoppen
|
||||||
</button>
|
</button>
|
||||||
) : (
|
) : (
|
||||||
<button className="primary-button" onClick={start} type="button" disabled={disabled}>
|
<button className={secondary ? 'ghost-button' : 'primary-button'} onClick={start} type="button" disabled={disabled || starting}>
|
||||||
<Mic size={18} /> Aufnehmen
|
<Mic size={18} /> {starting ? 'Mikrofon wird gestartet...' : buttonLabel}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
{error && <span className="inline-error">{error}</span>}
|
{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 [lesson, setLesson] = useState<Lesson | null>(initialSelection?.lesson ?? null);
|
||||||
const [exercise, setExercise] = useState<Exercise | null>(initialSelection?.exercise ?? null);
|
const [exercise, setExercise] = useState<Exercise | null>(initialSelection?.exercise ?? null);
|
||||||
const [audio, setAudio] = useState<Blob | null>(null);
|
const [audio, setAudio] = useState<Blob | null>(null);
|
||||||
|
const [audioUrl, setAudioUrl] = useState<string | null>(null);
|
||||||
const [result, setResult] = useState<AttemptResponse | null>(null);
|
const [result, setResult] = useState<AttemptResponse | null>(null);
|
||||||
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 [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
const [notice, setNotice] = useState('');
|
const [notice, setNotice] = useState('');
|
||||||
|
const submittingRef = useRef(false);
|
||||||
const recentExerciseIds = useRef<string[]>(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []);
|
const recentExerciseIds = useRef<string[]>(initialSelection?.exercise.id ? [initialSelection.exercise.id] : []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!initialSelection) void loadNext();
|
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) {
|
async function loadNext(fresh = false) {
|
||||||
if (loadingNext) return;
|
if (loadingNext || recording || submittingRef.current) return;
|
||||||
setLoadingNext(true);
|
setLoadingNext(true);
|
||||||
setError('');
|
setError('');
|
||||||
setNotice('');
|
setNotice('');
|
||||||
@@ -51,8 +64,10 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function submit() {
|
async function evaluateRecording() {
|
||||||
if (!exercise || !audio) return;
|
if (!exercise || !audio || submittingRef.current) return;
|
||||||
|
submittingRef.current = true;
|
||||||
|
setAudio(null);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError('');
|
setError('');
|
||||||
try {
|
try {
|
||||||
@@ -60,6 +75,7 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Bewertung fehlgeschlagen');
|
setError(err instanceof Error ? err.message : 'Bewertung fehlgeschlagen');
|
||||||
} finally {
|
} finally {
|
||||||
|
submittingRef.current = false;
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,7 +96,7 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
<p className="eyebrow">{lesson.title}</p>
|
<p className="eyebrow">{lesson.title}</p>
|
||||||
<h1>Aussprache</h1>
|
<h1>Aussprache</h1>
|
||||||
</div>
|
</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'}
|
<RotateCcw size={18} /> {loadingNext ? 'Lade neue Uebung...' : 'Andere Uebung'}
|
||||||
</button>
|
</button>
|
||||||
</header>
|
</header>
|
||||||
@@ -92,12 +108,28 @@ export function PracticePage({ initialSelection }: Props) {
|
|||||||
<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} />
|
||||||
<div className="action-row">
|
<div className="action-row">
|
||||||
<AudioRecorder onRecorded={setAudio} disabled={loading || loadingNext} />
|
<AudioRecorder
|
||||||
<button className="primary-button" disabled={!audio || loading || loadingNext} onClick={submit}>
|
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
|
<Send size={18} /> Bewerten
|
||||||
</button>
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</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>
|
</section>
|
||||||
|
|
||||||
{result && (
|
{result && (
|
||||||
|
|||||||
Reference in New Issue
Block a user