|
| 1 | +import { FC, useEffect, useMemo, useRef } from "react"; |
| 2 | +import { |
| 3 | + spectrum, |
| 4 | + spectrumToImage, |
| 5 | + useAudioBuffer, |
| 6 | + valueToColor, |
| 7 | +} from "./util"; |
| 8 | +import styles from "./Compare.module.css"; |
| 9 | + |
| 10 | +export const Compare: FC<{ a: Blob; b: Blob }> = ({ a, b }) => { |
| 11 | + const bufA = useAudioBuffer(a); |
| 12 | + const bufB = useAudioBuffer(b); |
| 13 | + |
| 14 | + const specA = useMemo(() => (bufA ? spectrum(bufA, 512) : null), [bufA]); |
| 15 | + const specB = useMemo(() => (bufB ? spectrum(bufB, 512) : null), [bufB]); |
| 16 | + |
| 17 | + // similiarity |
| 18 | + |
| 19 | + const similarity = useMemo(() => { |
| 20 | + if (specA && specB) { |
| 21 | + const aLen = specA.length / 512; |
| 22 | + const bLen = specB.length / 512; |
| 23 | + |
| 24 | + const sim = new Float32Array(aLen * bLen); |
| 25 | + |
| 26 | + for (let i = 0; i < aLen; i++) { |
| 27 | + for (let j = 0; j < bLen; j++) { |
| 28 | + let dotProduct = 0; |
| 29 | + let normA = 0; |
| 30 | + let normB = 0; |
| 31 | + for (let k = 0; k < 512; k++) { |
| 32 | + const aVal = specA[i * 512 + k]; |
| 33 | + const bVal = specB[j * 512 + k]; |
| 34 | + dotProduct += aVal * bVal; |
| 35 | + normA += aVal * aVal; |
| 36 | + normB += bVal * bVal; |
| 37 | + } |
| 38 | + sim[i * bLen + j] = |
| 39 | + dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + console.log(sim); |
| 44 | + |
| 45 | + return sim; |
| 46 | + } |
| 47 | + |
| 48 | + return null; |
| 49 | + }, [specA, specB]); |
| 50 | + |
| 51 | + if (specA && specB && similarity) { |
| 52 | + return ( |
| 53 | + <div className={styles.container}> |
| 54 | + <Foobar spec={specA} rotate /> |
| 55 | + <Baz similarity={similarity} width={specA.length / 512} /> |
| 56 | + <Foobar spec={specB} /> |
| 57 | + </div> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + return <h1>Compare</h1>; |
| 62 | +}; |
| 63 | + |
| 64 | +const Foobar: FC<{ spec: Float32Array; rotate?: true }> = ({ |
| 65 | + spec, |
| 66 | + rotate, |
| 67 | +}) => { |
| 68 | + const canvas = useRef<HTMLCanvasElement>(null); |
| 69 | + |
| 70 | + const imData = useMemo(() => spectrumToImage(spec, 512), [spec]); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + const ctx = canvas.current?.getContext("2d"); |
| 74 | + if (ctx) { |
| 75 | + const im = rotate ? rotateImageData90(imData) : imData; |
| 76 | + |
| 77 | + ctx.canvas.width = im.width; |
| 78 | + ctx.canvas.height = im.height; |
| 79 | + |
| 80 | + ctx.putImageData(im, 0, 0); |
| 81 | + } |
| 82 | + }, [imData, rotate]); |
| 83 | + |
| 84 | + return <canvas ref={canvas} />; |
| 85 | +}; |
| 86 | + |
| 87 | +const Baz: FC<{ similarity: Float32Array; width: number }> = ({ |
| 88 | + similarity, |
| 89 | + width, |
| 90 | +}) => { |
| 91 | + const canvas = useRef<HTMLCanvasElement>(null); |
| 92 | + |
| 93 | + useEffect(() => { |
| 94 | + const ctx = canvas.current?.getContext("2d"); |
| 95 | + if (ctx) { |
| 96 | + const height = similarity.length / width; |
| 97 | + const imData = new ImageData(width, height); |
| 98 | + |
| 99 | + for (let i = 0; i < similarity.length; i++) { |
| 100 | + const element = similarity[i]; |
| 101 | + |
| 102 | + const [r, g, b, a] = valueToColor(element); |
| 103 | + |
| 104 | + imData.data[i * 4] = r; |
| 105 | + imData.data[i * 4 + 1] = g; |
| 106 | + imData.data[i * 4 + 2] = b; |
| 107 | + imData.data[i * 4 + 3] = a; |
| 108 | + } |
| 109 | + |
| 110 | + ctx.canvas.width = imData.width; |
| 111 | + ctx.canvas.height = imData.height; |
| 112 | + |
| 113 | + ctx.putImageData(imData, 0, 0); |
| 114 | + } |
| 115 | + }, [similarity]); |
| 116 | + |
| 117 | + return <canvas ref={canvas} />; |
| 118 | +}; |
| 119 | + |
| 120 | +function rotateImageData90(imageData: ImageData) { |
| 121 | + const { width, height, data } = imageData; |
| 122 | + // Create a new ImageData object with swapped dimensions |
| 123 | + const rotatedImageData = new ImageData(height, width); |
| 124 | + |
| 125 | + const rotatedData = rotatedImageData.data; |
| 126 | + |
| 127 | + for (let y = 0; y < height; y++) { |
| 128 | + for (let x = 0; x < width; x++) { |
| 129 | + // Calculate the source pixel index |
| 130 | + const srcIndex = (y * width + x) * 4; |
| 131 | + |
| 132 | + // Calculate the destination pixel index |
| 133 | + // Transpose the coordinates and reverse the Y-axis |
| 134 | + const dstIndex = (x * height + (height - y - 1)) * 4; |
| 135 | + |
| 136 | + // Copy the RGBA values |
| 137 | + rotatedData[dstIndex] = data[srcIndex]; // R |
| 138 | + rotatedData[dstIndex + 1] = data[srcIndex + 1]; // G |
| 139 | + rotatedData[dstIndex + 2] = data[srcIndex + 2]; // B |
| 140 | + rotatedData[dstIndex + 3] = data[srcIndex + 3]; // A |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return rotatedImageData; |
| 145 | +} |
0 commit comments