From 2592bda3ffa095316ca372ab53dbe4925357e429 Mon Sep 17 00:00:00 2001 From: Till Kolter Date: Fri, 27 Sep 2024 15:32:12 +0200 Subject: [PATCH] Add sampleRate option for LiveAudioVisualizer --- src/LiveAudioVisualizer/LiveAudioVisualizer.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/LiveAudioVisualizer/LiveAudioVisualizer.tsx b/src/LiveAudioVisualizer/LiveAudioVisualizer.tsx index c694ea6..a5ad06e 100644 --- a/src/LiveAudioVisualizer/LiveAudioVisualizer.tsx +++ b/src/LiveAudioVisualizer/LiveAudioVisualizer.tsx @@ -74,6 +74,12 @@ export interface Props { * Default: `0.4` */ smoothingTimeConstant?: number; + /** + * Sample rate of the internal AudioContext. Should be the same as the AudioContext associated with the media recorder. + * For more details {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext MDN AudioContext} + * Default: 44100 + */ + sampleRate?: number; } const LiveAudioVisualizer: (props: Props) => ReactElement = ({ @@ -88,6 +94,7 @@ const LiveAudioVisualizer: (props: Props) => ReactElement = ({ maxDecibels = -10, minDecibels = -90, smoothingTimeConstant = 0.4, + sampleRate = 44100, }: Props) => { const [context, setContext] = useState(); const [audioSource, setAudioSource] = useState(); @@ -97,7 +104,7 @@ const LiveAudioVisualizer: (props: Props) => ReactElement = ({ useEffect(() => { if (!mediaRecorder.stream) return; - const ctx = new AudioContext(); + const ctx = new AudioContext({ sampleRate }); const analyserNode = ctx.createAnalyser(); setAnalyser(analyserNode); analyserNode.fftSize = fftSize;