Skip to content

Move to Functional component #1

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
90 changes: 38 additions & 52 deletions src/components/audio-analyser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from 'preact';
import {useEffect, useState} from 'preact/hooks';
import {AudioVisualiser} from './audio-visualiser';

export interface AudioAnalyserProps {
Expand All @@ -7,55 +7,41 @@ export interface AudioAnalyserProps {
width?: number;
height?: number;
}

export interface AudioAnalyserState {
audioData: Uint8Array;
}

export class AudioAnalyser extends Component<AudioAnalyserProps, AudioAnalyserState> {
public audioContext?: AudioContext;
public analyser?: AnalyserNode;
public dataArray?: Uint8Array;
public source?: MediaStreamAudioSourceNode;
public rafId?: number;

constructor(props: AudioAnalyserProps) {
super(props);

this.state = {audioData: new Uint8Array(0)};

this.tick = this.tick.bind(this);
}

componentDidMount() {
this.audioContext = new AudioContext();
this.analyser = this.audioContext.createAnalyser();
this.dataArray = new Uint8Array(this.analyser.frequencyBinCount);
this.source = this.audioContext.createMediaStreamSource(this.props.audio);
this.source.connect(this.analyser);
this.rafId = requestAnimationFrame(this.tick);
}

tick() {
this.analyser!.getByteTimeDomainData(this.dataArray!);
this.setState({audioData: this.dataArray});
this.rafId = requestAnimationFrame(this.tick);
}

componentWillUnmount() {
cancelAnimationFrame(this.rafId!);
this.analyser!.disconnect();
this.source!.disconnect();
}

render() {
return (
<AudioVisualiser
width={this.props.width}
height={this.props.height}
classes={this.props.classses}
audioData={this.state.audioData}
/>
);
}
export function AudioAnalyser({
width,
height,
classses,
audio,
}: AudioAnalyserProps) {
const [audioData, setAudioData] = useState(new Uint8Array(0));

useEffect(() => {
const audioContext = new AudioContext();
const analyser = audioContext.createAnalyser();
const dataArray = new Uint8Array(analyser.frequencyBinCount);
const source = audioContext.createMediaStreamSource(audio);

source.connect(analyser);

let rafId = requestAnimationFrame(function tick() {
analyser.getByteTimeDomainData(dataArray);
setAudioData(dataArray);
rafId = requestAnimationFrame(tick);
});

return () => {
cancelAnimationFrame(rafId);
analyser.disconnect();
source.disconnect();
};
}, [audioData]);

return (
<AudioVisualiser
width={width}
height={height}
classes={classses}
audioData={audioData}
/>
);
}