clipper/frontend/src/Waveform.tsx

156 lines
4.4 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react';
2021-11-25 18:02:37 +00:00
import { Frames, VideoPosition, newRPC } from './App';
import { MediaSetServiceClientImpl, MediaSet } from './generated/media_set';
2021-10-08 14:38:35 +00:00
import { WaveformCanvas } from './WaveformCanvas';
2022-01-24 19:33:16 +00:00
import { HudCanvas, SelectionChangeEvent } from './HudCanvas';
import { EmptySelectionAction, SelectionMode } from './HudCanvasState';
2021-11-06 20:52:47 +00:00
import { from, Observable } from 'rxjs';
import { bufferCount } from 'rxjs/operators';
2021-10-08 14:38:35 +00:00
interface Props {
mediaSet: MediaSet;
2021-11-25 18:02:37 +00:00
position: VideoPosition;
2021-10-08 14:38:35 +00:00
viewport: Frames;
2022-01-24 19:33:16 +00:00
onSelectionChange: (selectionState: SelectionChangeEvent) => void;
2021-10-08 14:38:35 +00:00
}
export const CanvasLogicalWidth = 2000;
export const CanvasLogicalHeight = 500;
export const Waveform: React.FC<Props> = ({
mediaSet,
position,
viewport,
onSelectionChange,
2021-10-08 14:38:35 +00:00
}: Props) => {
2021-11-06 20:52:47 +00:00
const [peaks, setPeaks] = useState<Observable<number[]>>(from([]));
const [selectedFrames, setSelectedFrames] = useState({ start: 0, end: 0 });
const [selectedPixels, setSelectedPixels] = useState({
start: 0,
end: 0,
});
const [positionPixels, setPositionPixels] = useState<number | null>(0);
2021-10-08 14:38:35 +00:00
// effects
// load peaks on MediaSet change
useEffect(() => {
(async function () {
if (mediaSet == null) {
return;
}
if (viewport.start >= viewport.end) {
return;
2021-10-08 14:38:35 +00:00
}
const service = new MediaSetServiceClientImpl(newRPC());
const segment = await service.GetPeaksForSegment({
id: mediaSet.id,
numBins: CanvasLogicalWidth,
startFrame: viewport.start,
endFrame: viewport.end,
});
console.log('got segment', segment);
const peaks = from(segment.peaks).pipe(
bufferCount(mediaSet.audioChannels)
);
setPeaks(peaks);
2021-10-08 14:38:35 +00:00
})();
}, [viewport, mediaSet]);
2021-10-08 14:38:35 +00:00
// convert position to canvas pixels
2021-10-08 14:38:35 +00:00
useEffect(() => {
const frame = Math.round(position.currentTime * mediaSet.audioSampleRate);
if (frame < viewport.start || frame > viewport.end) {
setPositionPixels(null);
2021-10-08 14:38:35 +00:00
return;
}
2021-12-13 04:10:07 +00:00
const pixelsPerFrame = CanvasLogicalWidth / (viewport.end - viewport.start);
const positionPixels = (frame - viewport.start) * pixelsPerFrame;
setPositionPixels(positionPixels);
}, [mediaSet, position, viewport]);
2021-10-08 14:38:35 +00:00
// update selectedPixels on viewport change
useEffect(() => {
2021-12-13 04:10:07 +00:00
const start = Math.max(frameToCanvasX(selectedFrames.start), 0);
const end = Math.min(
frameToCanvasX(selectedFrames.end),
CanvasLogicalWidth
);
setSelectedPixels({ start, end });
}, [viewport, selectedFrames]);
2021-10-08 14:38:35 +00:00
// handlers
2021-10-08 14:38:35 +00:00
2022-01-24 19:33:16 +00:00
// convert selection change from canvas pixels to frames, and trigger callback.
const handleSelectionChange = (selectionState: SelectionChangeEvent) => {
const { mode, prevMode, selection } = selectionState;
const framesPerPixel = (viewport.end - viewport.start) / CanvasLogicalWidth;
const selectedFrames = {
start: Math.round(viewport.start + selection.start * framesPerPixel),
end: Math.round(viewport.start + selection.end * framesPerPixel),
};
2022-01-24 19:33:16 +00:00
if (mode == SelectionMode.Normal && prevMode != SelectionMode.Normal) {
setSelectedPixels(selection);
setSelectedFrames(selectedFrames);
}
2022-01-24 19:33:16 +00:00
onSelectionChange({
...selectionState,
selection: selectedFrames,
});
};
2021-10-08 14:38:35 +00:00
// helpers
const frameToCanvasX = (frame: number): number => {
2022-01-25 19:06:15 +00:00
const numFrames = viewport.end - viewport.start;
if (numFrames == 0) {
return 0;
}
const pixelsPerFrame = CanvasLogicalWidth / numFrames;
return Math.round((frame - viewport.start) * pixelsPerFrame);
};
2021-10-08 14:38:35 +00:00
// render component
const hudStyles = {
borderLineWidth: 0,
borderStrokeStyle: 'transparent',
positionLineWidth: 6,
positionStrokeStyle: 'red',
2022-01-17 17:58:50 +00:00
hoverPositionStrokeStyle: '#666666',
};
2021-10-08 14:38:35 +00:00
return (
<>
<div className={`relative grow`}>
<WaveformCanvas
peaks={peaks}
channels={mediaSet.audioChannels}
width={CanvasLogicalWidth}
height={CanvasLogicalHeight}
strokeStyle="green"
fillStyle="black"
alpha={1}
></WaveformCanvas>
<HudCanvas
2021-10-08 14:38:35 +00:00
width={CanvasLogicalWidth}
height={CanvasLogicalHeight}
2021-12-12 10:04:23 +00:00
emptySelectionAction={EmptySelectionAction.SelectNothing}
styles={hudStyles}
position={positionPixels}
selection={selectedPixels}
onSelectionChange={handleSelectionChange}
/>
2021-10-08 14:38:35 +00:00
</div>
</>
);
};