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

Add sampleRate option for LiveAudioVisualizer #31

Open
wants to merge 1 commit into
base: master
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
9 changes: 8 additions & 1 deletion src/LiveAudioVisualizer/LiveAudioVisualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand All @@ -88,6 +94,7 @@ const LiveAudioVisualizer: (props: Props) => ReactElement = ({
maxDecibels = -10,
minDecibels = -90,
smoothingTimeConstant = 0.4,
sampleRate = 44100,
}: Props) => {
const [context, setContext] = useState<AudioContext>();
const [audioSource, setAudioSource] = useState<MediaStreamAudioSourceNode>();
Expand All @@ -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;
Expand Down