Basic error handling when reading peaks

This commit is contained in:
Rob Watson 2021-11-12 13:36:03 +01:00
parent 97db31209c
commit 99659adb9e
1 changed files with 42 additions and 38 deletions

View File

@ -26,6 +26,7 @@ export const WaveformCanvas: React.FC<Props> = (props: Props) => {
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => { useEffect(() => {
(async function () {
const canvas = canvasRef.current; const canvas = canvasRef.current;
if (canvas == null) { if (canvas == null) {
console.error('no canvas ref available'); console.error('no canvas ref available');
@ -49,7 +50,8 @@ export const WaveformCanvas: React.FC<Props> = (props: Props) => {
const chanHeight = canvas.height / props.channels; const chanHeight = canvas.height / props.channels;
let frameIndex = 0; let frameIndex = 0;
props.peaks.forEach((peaks) => { await props.peaks
.forEach((peaks) => {
for (let chanIndex = 0; chanIndex < peaks.length; chanIndex++) { for (let chanIndex = 0; chanIndex < peaks.length; chanIndex++) {
const yOffset = chanHeight * chanIndex; const yOffset = chanHeight * chanIndex;
const val = peaks[chanIndex]; const val = peaks[chanIndex];
@ -64,7 +66,9 @@ export const WaveformCanvas: React.FC<Props> = (props: Props) => {
ctx.globalAlpha = 1; ctx.globalAlpha = 1;
} }
frameIndex++; frameIndex++;
}); })
.catch(console.error);
})();
}, [props.peaks]); }, [props.peaks]);
const canvasStyles = { const canvasStyles = {