Skip to content

Commit

Permalink
feat: 优化 音乐律动更新频率
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq7 committed Jan 9, 2025
1 parent 1944a54 commit 03fdbe6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/pages/marching-music/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function Component() {
const [file, setFile] = useState<File>();
const [rhythmMode, setRhythmMode] = useState<RhythmMode>('spectrogram');
const rhythmModeRef = useRef<RhythmMode>();
const lightInterval = useRef<any>();

const handleSelectAudio = () => {
inputRef.current.click();
Expand Down Expand Up @@ -50,20 +51,24 @@ export default function Component() {

source.onended = () => {
marchingMusicRef.current.closeLight();
clearInterval(lightInterval.current);
lightInterval.current = null;
};

function animate() {
requestAnimationFrame(animate);
if (lightInterval.current) {
clearInterval(lightInterval.current);
lightInterval.current = null;
}

lightInterval.current = setInterval(() => {
if (rhythmModeRef.current === 'spectrogram') {
const dataArray = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(dataArray);
marchingMusicRef.current.drawBarLight(dataArray);
} else {
// marchingMusicRef.current.drawWaveLight();
}
}
animate();
}, 96);
};

reader.readAsArrayBuffer(file);
Expand Down

0 comments on commit 03fdbe6

Please sign in to comment.