Skip to content

Commit e284411

Browse files
authored
Add cleanup. Bump version to 1.2.0 (#27)
1 parent a0dc505 commit e284411

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "react-audio-visualize",
33
"private": false,
4-
"version": "1.1.3",
4+
"version": "1.2.0",
55
"license": "MIT",
6-
"author": "",
6+
"author": "Samhir Tarif",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/samhirtarif/react-audio-visualize.git"

src/LiveAudioVisualizer/LiveAudioVisualizer.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,31 @@ const LiveAudioVisualizer: (props: Props) => ReactElement = ({
8989
minDecibels = -90,
9090
smoothingTimeConstant = 0.4,
9191
}: Props) => {
92-
const [context] = useState(() => new AudioContext());
92+
const [context, setContext] = useState<AudioContext>();
93+
const [audioSource, setAudioSource] = useState<MediaStreamAudioSourceNode>();
9394
const [analyser, setAnalyser] = useState<AnalyserNode>();
9495
const canvasRef = useRef<HTMLCanvasElement>(null);
9596

9697
useEffect(() => {
9798
if (!mediaRecorder.stream) return;
9899

99-
const analyserNode = context.createAnalyser();
100+
const ctx = new AudioContext();
101+
const analyserNode = ctx.createAnalyser();
100102
setAnalyser(analyserNode);
101103
analyserNode.fftSize = fftSize;
102104
analyserNode.minDecibels = minDecibels;
103105
analyserNode.maxDecibels = maxDecibels;
104106
analyserNode.smoothingTimeConstant = smoothingTimeConstant;
105-
const source = context.createMediaStreamSource(mediaRecorder.stream);
107+
const source = ctx.createMediaStreamSource(mediaRecorder.stream);
106108
source.connect(analyserNode);
109+
setContext(ctx);
110+
setAudioSource(source);
111+
112+
return () => {
113+
source.disconnect();
114+
analyserNode.disconnect();
115+
ctx.state !== "closed" && ctx.close();
116+
};
107117
}, [mediaRecorder.stream]);
108118

109119
useEffect(() => {
@@ -113,7 +123,7 @@ const LiveAudioVisualizer: (props: Props) => ReactElement = ({
113123
}, [analyser, mediaRecorder.state]);
114124

115125
const report = useCallback(() => {
116-
if (!analyser) return;
126+
if (!analyser || !context) return;
117127

118128
const data = new Uint8Array(analyser?.frequencyBinCount);
119129

@@ -129,14 +139,16 @@ const LiveAudioVisualizer: (props: Props) => ReactElement = ({
129139
) {
130140
context.close();
131141
}
132-
}, [analyser, context.state]);
142+
}, [analyser, context?.state]);
133143

134144
useEffect(() => {
135145
return () => {
136-
if (context.state !== "closed") {
146+
if (context && context.state !== "closed") {
137147
context.close();
138148
}
139-
}
149+
audioSource?.disconnect();
150+
analyser?.disconnect();
151+
};
140152
}, []);
141153

142154
const processFrequencyData = (data: Uint8Array): void => {

0 commit comments

Comments
 (0)