Skip to content

Commit

Permalink
Merge pull request #3 from hqnna/main
Browse files Browse the repository at this point in the history
Get audio working with the visualizer
  • Loading branch information
lukeocodes authored Aug 7, 2024
2 parents c3055dc + e56adb3 commit 01dfe54
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/api/speak/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function POST(request: NextRequest) {
response.headers.set("Surrogate-Control", "no-store");
response.headers.set(
"Cache-Control",
"s-maxage=0, no-store, no-cache, must-revalidate, proxy-revalidate"
"s-maxage=0, no-store, no-cache, must-revalidate, proxy-revalidate",
);
response.headers.set("Expires", "0");

Expand Down
2 changes: 1 addition & 1 deletion app/components/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Controls = ({
playAudio(await response.blob(), "audio/mp3");
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[text]
[text],
);

return (
Expand Down
10 changes: 6 additions & 4 deletions app/components/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ type AudioInput = MediaStream | HTMLAudioElement;
const interpolateColor = (
startColor: number[],
endColor: number[],
factor: number
factor: number,
): number[] => {
const result = [];
for (let i = 0; i < startColor.length; i++) {
result[i] = Math.round(
startColor[i] + factor * (endColor[i] - startColor[i])
startColor[i] + factor * (endColor[i] - startColor[i]),
);
}
return result;
Expand All @@ -27,16 +27,18 @@ const Visualizer: React.FC<VisualizerProps> = ({ source, context }) => {
if (!context) {
context = new (window.AudioContext || window.webkitAudioContext)();
}

const analyser = context.createAnalyser();
const dataArray = new Uint8Array(analyser.frequencyBinCount);

useEffect(() => {
let audioSource: AudioNode;

if (source instanceof MediaStream) {
audioSource = context.createMediaStreamSource(source);
audioSource = context!.createMediaStreamSource(source);
} else {
audioSource = context.createMediaElementSource(source);
audioSource = context!.createMediaElementSource(source);
audioSource.connect(context!.destination);
}

audioSource.connect(analyser);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 01dfe54

Please sign in to comment.