Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix to play audio files #153

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 78 additions & 69 deletions src/components/Viewer/Player/AudioVisualizer.tsx
Original file line number Diff line number Diff line change
@@ -1,76 +1,85 @@
/* eslint-disable react/display-name */

import React, { useCallback } from "react";

import { AudioVisualizerWrapper } from "src/components/Viewer/Player/AudioVisualizer.styled";
import React from "react";

const AudioVisualizer = React.forwardRef((_props, ref: any) => {
const canvasRef = React.useRef<HTMLCanvasElement>(null);

React.useEffect(() => {
if (!ref) return;
// Add a callback fn to the `<video>` onplay event
ref.current.onplay = audioVisualizer;
}, [ref]);

function audioVisualizer() {
const video = ref.current;
const context = new AudioContext();
const src = context.createMediaElementSource(video);
const analyser = context.createAnalyser();

const canvas = canvasRef.current;
if (!canvas) return;
canvas.width = video.offsetWidth;
canvas.height = video.offsetHeight;
const ctx = canvas.getContext("2d");

src.connect(analyser);
analyser.connect(context.destination);

analyser.fftSize = 256;

const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);

setInterval(function () {
renderFrame(
analyser,
ctx,
bufferLength,
dataArray,
canvas.width,
canvas.height,
);
}, 20);
}

function renderFrame(
analyser: any,
ctx: any,
bufferLength: number,
dataArray: Uint8Array,
width: number,
height: number,
) {
const barWidth = (width / bufferLength) * 2.6;
let barHeight;
let x = 0;

analyser.getByteFrequencyData(dataArray);

ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, width, height);

for (let i = 0; i < bufferLength; i++) {
barHeight = dataArray[i] * 2;
ctx.fillStyle = `rgba(${78}, 42, 132, 1)`;
ctx.fillRect(x, height - barHeight, barWidth, barHeight);

x += barWidth + 6;

const AudioVisualizer = React.forwardRef(
(_props, ref: React.RefObject<HTMLVideoElement>) => {
const canvasRef = React.useRef<HTMLCanvasElement>(null);

const audioVisualizer = useCallback(() => {
// Block re-initialization if audio is already playing
if (ref.current?.currentTime && ref.current?.currentTime > 0) return;

const video = ref.current;
if (!video) return;

const context = new AudioContext();
const src = context.createMediaElementSource(video);
const analyser = context.createAnalyser();

const canvas = canvasRef.current;
if (!canvas) return;
canvas.width = video.offsetWidth;
canvas.height = video.offsetHeight;
const ctx = canvas.getContext("2d");

src.connect(analyser);
analyser.connect(context.destination);

analyser.fftSize = 256;

const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);

setInterval(function () {
renderFrame(
analyser,
ctx,
bufferLength,
dataArray,
canvas.width,
canvas.height,
);
}, 20);
}, [ref]);

React.useEffect(() => {
if (!ref || !ref.current) return;
// Add a callback fn to the `<video>` onplay event

ref.current.onplay = audioVisualizer;
}, [audioVisualizer, ref]);

function renderFrame(
analyser: any,
ctx: any,
bufferLength: number,
dataArray: Uint8Array,
width: number,
height: number,
) {
const barWidth = (width / bufferLength) * 2.6;
let barHeight: number;
let x = 0;

analyser.getByteFrequencyData(dataArray);

ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, width, height);

for (let i = 0; i < bufferLength; i++) {
barHeight = dataArray[i] * 2;
ctx.fillStyle = `rgba(${78}, 42, 132, 1)`;
ctx.fillRect(x, height - barHeight, barWidth, barHeight);

x += barWidth + 6;
}
}
}

return <AudioVisualizerWrapper ref={canvasRef} />;
});
return <AudioVisualizerWrapper ref={canvasRef} />;
},
);

export default AudioVisualizer;
2 changes: 1 addition & 1 deletion src/components/Viewer/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const Player: React.FC<PlayerProps> = ({ allSources, resources, painting }) => {
style={{
maxHeight: configOptions.canvasHeight,
position: "relative",
zIndex: "0",
zIndex: "1",
}}
>
{allSources.map((painting) => (
Expand Down
Loading