2022-11-24 01:00:06 -07:00
|
|
|
import { useRef } from "react";
|
|
|
|
import { useFrame, useThree } from "@react-three/fiber";
|
2022-11-24 15:54:25 -07:00
|
|
|
import { QuadraticBezierLine, Plane } from "@react-three/drei";
|
2022-11-24 01:00:06 -07:00
|
|
|
|
|
|
|
import { InferenceResult } from "../types";
|
2022-11-24 11:43:11 -07:00
|
|
|
import HeightMapImage from "./HeightMapImage";
|
|
|
|
import ImagePlane from "./ImagePlane";
|
2022-11-24 01:00:06 -07:00
|
|
|
|
|
|
|
interface SpectrogramViewerProps {
|
|
|
|
paused: boolean;
|
|
|
|
inferenceResults: InferenceResult[];
|
2022-11-24 15:54:25 -07:00
|
|
|
getTime: () => number;
|
2022-11-24 11:43:11 -07:00
|
|
|
use_height_map?: boolean;
|
2022-11-24 15:54:25 -07:00
|
|
|
audioLength: number;
|
2022-11-24 01:00:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spectrogram drawing code.
|
|
|
|
*/
|
2022-11-24 11:43:11 -07:00
|
|
|
export default function SpectrogramViewer({
|
|
|
|
paused,
|
|
|
|
inferenceResults,
|
2022-11-24 15:54:25 -07:00
|
|
|
getTime,
|
|
|
|
audioLength,
|
2022-11-24 11:43:11 -07:00
|
|
|
use_height_map = true,
|
|
|
|
}: SpectrogramViewerProps) {
|
2022-11-24 01:00:06 -07:00
|
|
|
const camera = useThree((state) => state.camera);
|
|
|
|
|
|
|
|
const playheadRef = useRef(null);
|
|
|
|
|
2022-11-24 15:54:25 -07:00
|
|
|
// Move the camera based on the clock
|
2022-11-24 01:00:06 -07:00
|
|
|
useFrame(() => {
|
2022-11-24 15:54:25 -07:00
|
|
|
const time = getTime();
|
2022-11-24 01:00:06 -07:00
|
|
|
|
2022-11-24 15:54:25 -07:00
|
|
|
const velocity = -1.0; // [m/s]
|
|
|
|
const position = velocity * time; // [m]
|
|
|
|
|
|
|
|
camera.position.y = position;
|
|
|
|
playheadRef.current.position.y = camera.position.y;
|
2022-11-24 01:00:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<group>
|
|
|
|
{inferenceResults.map((value: InferenceResult, index: number) => {
|
2022-11-24 15:54:25 -07:00
|
|
|
const height = audioLength * (-0.48 - value.counter);
|
2022-11-24 11:43:11 -07:00
|
|
|
|
|
|
|
if (use_height_map) {
|
|
|
|
return (
|
|
|
|
<HeightMapImage
|
|
|
|
url={value.image}
|
|
|
|
position={[0, height, 0]}
|
|
|
|
rotation={[0, 0, -Math.PI / 2]}
|
2022-11-24 15:54:25 -07:00
|
|
|
scale={[audioLength, 5, 1]}
|
2022-11-24 11:43:11 -07:00
|
|
|
key={index}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return <ImagePlane url={value.image} height={height} key={index} />;
|
|
|
|
}
|
2022-11-24 01:00:06 -07:00
|
|
|
})}
|
|
|
|
|
|
|
|
{/* TODO make into playhead component */}
|
|
|
|
<group ref={playheadRef}>
|
2022-11-24 15:54:25 -07:00
|
|
|
<Plane
|
|
|
|
args={[5.5, 2.0]}
|
|
|
|
rotation={[Math.PI / 2 - 0.4, 0, 0]}
|
|
|
|
position={[0, 0, -0.5]}
|
|
|
|
>
|
|
|
|
<meshBasicMaterial color="#ee2211" transparent opacity={0.8} />
|
|
|
|
</Plane>
|
|
|
|
|
|
|
|
{/* <QuadraticBezierLine
|
2022-11-24 01:00:06 -07:00
|
|
|
start={[-3, 0, 1]} // Starting point, can be an array or a vec3
|
|
|
|
end={[3, 0, 1]} // Ending point, can be an array or a vec3
|
|
|
|
mid={[0, -0.8, 0.4]} // Optional control point, can be an array or a vec3
|
2022-11-24 12:41:56 -07:00
|
|
|
color="#DD1C1A" // Default
|
2022-11-24 01:00:06 -07:00
|
|
|
lineWidth={5} // In pixels (default)
|
|
|
|
dashed={false} // Default
|
2022-11-24 15:54:25 -07:00
|
|
|
/> */}
|
2022-11-24 01:00:06 -07:00
|
|
|
</group>
|
|
|
|
</group>
|
|
|
|
);
|
|
|
|
}
|