feat(music): visualize from data array
This commit is contained in:
Binary file not shown.
BIN
public/audio/mesmerizing_galaxy.mp3
Normal file
BIN
public/audio/mesmerizing_galaxy.mp3
Normal file
Binary file not shown.
@@ -1,15 +1,9 @@
|
|||||||
import {
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
type RefObject,
|
|
||||||
useCallback,
|
|
||||||
useEffect,
|
|
||||||
useRef,
|
|
||||||
useState,
|
|
||||||
} from "react";
|
|
||||||
import { type InnerKittyProps } from "~/utils/types";
|
import { type InnerKittyProps } from "~/utils/types";
|
||||||
import { CHAR_WIDTH } from "../Kitty";
|
import { CHAR_WIDTH } from "../Kitty";
|
||||||
import { useKitty } from "~/hooks/useKitty";
|
import { useKitty } from "~/hooks/useKitty";
|
||||||
|
|
||||||
export const Cava = (props: { audio: RefObject<HTMLAudioElement> }) => {
|
export const Cava = (_props: {}) => {
|
||||||
const kitty = useKitty();
|
const kitty = useKitty();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -21,7 +15,7 @@ export const Cava = (props: { audio: RefObject<HTMLAudioElement> }) => {
|
|||||||
gridTemplateRows: `1fr`,
|
gridTemplateRows: `1fr`,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{kitty && <InnerCava {...props} {...kitty} />}
|
{kitty && <InnerCava {...kitty} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -62,31 +56,32 @@ const FrequencyBar = (props: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const InnerCava = (props: InnerKittyProps<typeof Cava>) => {
|
const InnerCava = (props: InnerKittyProps<typeof Cava>) => {
|
||||||
const sourceRef = useRef<MediaElementAudioSourceNode | null>(null);
|
const sourceRef = useRef<AudioBufferSourceNode | null>(null);
|
||||||
const analyserRef = useRef<AnalyserNode | null>(null);
|
const analyserRef = useRef<AnalyserNode | null>(null);
|
||||||
const audioContextRef = useRef<AudioContext | null>(null);
|
const audioContextRef = useRef<AudioContext | null>(null);
|
||||||
|
const dataArray = useRef<Uint8Array | null>(null);
|
||||||
const [barHeights, setBarHeights] = useState(
|
const [barHeights, setBarHeights] = useState(
|
||||||
new Array<number>(Math.floor(props.cols / 3)).fill(0),
|
new Array<number>(Math.floor(props.cols / 3)).fill(0),
|
||||||
);
|
);
|
||||||
|
|
||||||
const requestRef = useRef<number>();
|
const requestRef = useRef<number>();
|
||||||
const calculateBarHeights = useCallback(() => {
|
const calculateBarHeights = () => {
|
||||||
if (!analyserRef.current) return;
|
if (!dataArray.current || !analyserRef.current) return;
|
||||||
|
|
||||||
const bufferLength = analyserRef.current.frequencyBinCount;
|
analyserRef.current.getByteFrequencyData(dataArray.current);
|
||||||
const dataArray = new Uint8Array(bufferLength);
|
|
||||||
analyserRef.current.getByteFrequencyData(dataArray);
|
|
||||||
|
|
||||||
const barCount = Math.floor(props.cols / 3);
|
const barCount = Math.floor(props.cols / 2);
|
||||||
const newBarHeights = [];
|
const newBarHeights = [];
|
||||||
|
|
||||||
for (let i = 0; i < barCount; i++) {
|
for (let i = 0; i < barCount; i++) {
|
||||||
const startIndex = Math.floor((i / barCount) * bufferLength);
|
const startIndex = Math.floor((i / barCount) * dataArray.current.length);
|
||||||
const endIndex = Math.floor(((i + 1) / barCount) * bufferLength);
|
const endIndex = Math.floor(
|
||||||
const slice = dataArray.slice(startIndex, endIndex);
|
((i + 1) / barCount) * dataArray.current.length,
|
||||||
|
);
|
||||||
|
const slice = dataArray.current.slice(startIndex, endIndex);
|
||||||
const sum = slice.reduce((acc, val) => acc + val, 0);
|
const sum = slice.reduce((acc, val) => acc + val, 0);
|
||||||
const average = sum / slice.length;
|
const average = sum / slice.length;
|
||||||
newBarHeights.push(average);
|
newBarHeights.push(average * 0.9);
|
||||||
}
|
}
|
||||||
|
|
||||||
const stateBarHeights =
|
const stateBarHeights =
|
||||||
@@ -95,45 +90,60 @@ const InnerCava = (props: InnerKittyProps<typeof Cava>) => {
|
|||||||
: barHeights;
|
: barHeights;
|
||||||
|
|
||||||
const smoothedBarHeights = newBarHeights.map((height, i) => {
|
const smoothedBarHeights = newBarHeights.map((height, i) => {
|
||||||
const smoothingFactor = 1;
|
const smoothingFactor = 0.8;
|
||||||
return (
|
return (
|
||||||
stateBarHeights[i]! + (height - stateBarHeights[i]!) * smoothingFactor
|
stateBarHeights[i] + (height - stateBarHeights[i]) * smoothingFactor
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
setBarHeights(smoothedBarHeights);
|
setBarHeights(smoothedBarHeights);
|
||||||
|
|
||||||
requestRef.current = requestAnimationFrame(calculateBarHeights);
|
requestRef.current = requestAnimationFrame(calculateBarHeights);
|
||||||
}, [props.cols, barHeights]);
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const audioElement = props.audio.current;
|
const fetchAudio = async () => {
|
||||||
if (!audioElement) return;
|
try {
|
||||||
|
const audioContext = new AudioContext();
|
||||||
|
audioContextRef.current = audioContext;
|
||||||
|
|
||||||
const audioContext = new AudioContext();
|
const response = await fetch("/audio/mesmerizing_galaxy.mp3");
|
||||||
audioContextRef.current = audioContext;
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
|
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
||||||
|
|
||||||
const analyser = audioContext.createAnalyser();
|
const analyserNode = audioContext.createAnalyser();
|
||||||
analyser.fftSize = 256;
|
analyserNode.fftSize = 256;
|
||||||
|
|
||||||
void audioElement.play().then(() => void audioContext.resume());
|
const source = audioContext.createBufferSource();
|
||||||
|
source.buffer = audioBuffer;
|
||||||
|
source.loop = true;
|
||||||
|
source.connect(analyserNode);
|
||||||
|
analyserNode.connect(audioContext.destination);
|
||||||
|
|
||||||
if (!sourceRef.current) {
|
analyserRef.current = analyserNode;
|
||||||
const source = audioContext.createMediaElementSource(audioElement);
|
sourceRef.current = source;
|
||||||
source.connect(analyser);
|
dataArray.current = new Uint8Array(analyserNode.frequencyBinCount);
|
||||||
analyser.connect(audioContext.destination);
|
|
||||||
sourceRef.current = source;
|
requestRef.current = requestAnimationFrame(calculateBarHeights);
|
||||||
analyserRef.current = analyser;
|
|
||||||
|
source.start();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching or decoding audio:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (audioContextRef.current) {
|
||||||
|
requestRef.current = requestAnimationFrame(calculateBarHeights);
|
||||||
|
} else {
|
||||||
|
fetchAudio();
|
||||||
}
|
}
|
||||||
|
|
||||||
requestRef.current = requestAnimationFrame(calculateBarHeights);
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (requestRef.current) cancelAnimationFrame(requestRef.current);
|
if (requestRef.current) cancelAnimationFrame(requestRef.current);
|
||||||
};
|
};
|
||||||
}, [props.cols, props.audio]);
|
}, [calculateBarHeights]);
|
||||||
|
|
||||||
return barHeights.map((value, i) => (
|
return barHeights.map((height, i) => (
|
||||||
<FrequencyBar key={i} value={value} max={255 / 2} height={props.rows} />
|
<FrequencyBar key={i} value={height} max={255} height={props.rows} />
|
||||||
));
|
));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { useState } from "react";
|
|
||||||
import { formatMMSS } from "../../utils/time";
|
import { formatMMSS } from "../../utils/time";
|
||||||
import { CharArray } from "../../utils/string";
|
import { CharArray } from "../../utils/string";
|
||||||
import { CHAR_HEIGHT, CHAR_WIDTH } from "../Kitty";
|
import { CHAR_HEIGHT, CHAR_WIDTH } from "../Kitty";
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Kitty } from "../Kitty";
|
|||||||
import { SpotifyPlayer } from "./SpotifyPlayer";
|
import { SpotifyPlayer } from "./SpotifyPlayer";
|
||||||
import { useApp } from "~/hooks/useApp";
|
import { useApp } from "~/hooks/useApp";
|
||||||
import { cn, hideIf } from "~/utils/react";
|
import { cn, hideIf } from "~/utils/react";
|
||||||
|
import { Cava } from "./Cava";
|
||||||
|
|
||||||
export type CurrentlyPlaying = {
|
export type CurrentlyPlaying = {
|
||||||
item: {
|
item: {
|
||||||
@@ -22,7 +23,7 @@ export const Music = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchCurrentlyPlaying = () =>
|
const fetchCurrentlyPlaying = () =>
|
||||||
fetch("http://213.210.20.230:3000/currently-playing?format=json")
|
fetch("https://api.pihkaal.me/currently-playing?format=json")
|
||||||
.then((r) => r.json())
|
.then((r) => r.json())
|
||||||
.then((data: CurrentlyPlaying) => {
|
.then((data: CurrentlyPlaying) => {
|
||||||
data.progress_ms = Math.max(0, data.progress_ms - 1500);
|
data.progress_ms = Math.max(0, data.progress_ms - 1500);
|
||||||
@@ -72,7 +73,7 @@ export const Music = () => {
|
|||||||
)}
|
)}
|
||||||
rows={5}
|
rows={5}
|
||||||
>
|
>
|
||||||
{/*<Cava audio={audio} />*/}
|
<Cava />
|
||||||
</Kitty>
|
</Kitty>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user