How to integrate krisp noise cancellation in react
# ask-ai
b
How to integrate krisp noise cancellation in react
t
Thanks for reaching out, Rahul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're interested in integrating Krisp noise cancellation into your React application! Let's walk through the steps together to make it as smooth as possible: 1. First, install the necessary package:
npm install @livekit/krisp-noise-filter
2. Next, use the React Hook provided by LiveKit: They offer a handy
useKrispNoiseFilter
hook that simplifies the integration. Here's a little example to get you started:
Copy code
import { useKrispNoiseFilter } from '@livekit/components-react';

function NoiseFilterControl() {
  const krisp = useKrispNoiseFilter();
  
  return (
    <input type="checkbox" onChange={(ev) => krisp.setNoiseFilterEnabled(ev.target.checked)}
      checked={krisp.isNoiseFilterEnabled}
      disabled={krisp.isNoiseFilterPending} />
  );
}
3. If you want more advanced control: You can directly use the
KrispNoiseFilter
class with the LiveKit SDK. Here's an example that might help:
Copy code
import { Room, RoomEvent, Track, LocalAudioTrack } from "livekit-client";

const room = useMemo(() => {
  const r = new Room();
  r.on(RoomEvent.LocalTrackPublished, async (trackPublication) => {
    if (trackPublication.source === Track.Source.Microphone &&
        trackPublication.track instanceof LocalAudioTrack) {
      const { KrispNoiseFilter, isKrispNoiseFilterSupported } = await import("@livekit/krisp-noise-filter");
      if (!isKrispNoiseFilterSupported()) {
        console.error("Enhanced noise filter is not supported for this browser");
        return;
      }
      try {
        await trackPublication.track?.setProcessor(KrispNoiseFilter());
      } catch (e) {
        console.warn("Background noise reduction could not be enabled");
      }
    }
  });
  return r;
}, []);
One important thing to keep in mind is browser compatibility. You can check this by using
isKrispNoiseFilterSupported()
. This feature works only on LiveKit Cloud and requires the
@livekit/krisp-noise-filter
package to be installed. If you run into any issues or have questions along the way, feel free to reach out—I'm here to help! Sources: Enhanced noise cancellation | LiveKit Docs | components/room.tsx | packages/react/src/index.docs.ts
b
The latest package version doesn't have the useKrispNoiseFilter
t
Hey there! If you don’t see the
useKrispNoiseFilter
hook in the latest package version, no worries at all. You can still add Krisp noise cancellation by using the base JavaScript SDK directly. Here’s a friendly alternative you can try out: 1. First, install the required package:
npm install @livekit/krisp-noise-filter
2. Next, use Room Event-Based Implementation: This approach applies noise cancellation right when the microphone track is published:
Copy code
import { Room, RoomEvent, Track, LocalAudioTrack } from "livekit-client";

const room = useMemo(() => {
  const r = new Room();
  r.on(RoomEvent.LocalTrackPublished, async (trackPublication) => {
    if (trackPublication.source === Track.Source.Microphone &&
        trackPublication.track instanceof LocalAudioTrack) {
      const { KrispNoiseFilter, isKrispNoiseFilterSupported } = await import("@livekit/krisp-noise-filter");
      if (!isKrispNoiseFilterSupported()) {
        console.error("Enhanced noise filter is not supported for this browser");
        return;
      }
      try {
        await trackPublication.track?.setProcessor(KrispNoiseFilter());
      } catch (e) {
        console.warn("Background noise reduction could not be enabled");
      }
    }
  });
  return r;
}, []);
3. Or, for more control, try Manual Track Processing: This way, you can manage the noise filter lifecycle yourself:
Copy code
import React from 'react';
import { LocalAudioTrack } from 'livekit-client';

export function ManualKrispIntegration() {
  const [isNoiseFilterEnabled, setIsNoiseFilterEnabled] = React.useState(false);
  const [krispProcessor, setKrispProcessor] = React.useState();

  const toggleNoiseFilter = async (enable) => {
    const { KrispNoiseFilter, isKrispNoiseFilterSupported } = await import('livekit/krisp-noise-filter');
    if (!isKrispNoiseFilterSupported()) {
      console.warn('Noise filter not supported');
      return;
    }
    if (!krispProcessor) {
      setKrispProcessor(KrispNoiseFilter());
    }
    // Apply or disable the processor
  };
  return (
    <button onClick={() => toggleNoiseFilter(!isNoiseFilterEnabled)}>
      {isNoiseFilterEnabled ? 'Disable' : 'Enable'} Noise Cancellation
    </button>
  );
}
Just make sure your project is running on LiveKit Cloud, and don’t forget to check if your browser supports this feature with
isKrispNoiseFilterSupported()
. If you have any questions or need a hand with anything else, I’m here to help! Sources: packages/react/etc/components-react.api.md | Enhanced noise cancellation | LiveKit Docs | components/room.tsx | lib/MicrophoneSettings.tsx
b
Does this implementation work? import { KrispNoiseFilter } from "@livekit/krisp-noise-filter"; const krisp = KrispNoiseFilter(); useEffect(() => { krisp.setEnabled(true); }, []);
t
Yes, your implementation can definitely work with just a few tweaks. The
KrispNoiseFilter
can be instantiated directly, and you can use its
setEnabled
method to turn on noise cancellation. Just keep in mind a couple of important points: a. isKrispNoiseFilterSupported() before enabling the filter. b. Then, apply the filter to a specific audio track by calling
setProcessor
on your
LocalAudioTrack
. Here’s a quick example:
Copy code
import { KrispNoiseFilter, isKrispNoiseFilterSupported } from "@livekit/krisp-noise-filter";

useEffect(() => {
  if (!isKrispNoiseFilterSupported()) {
    console.error("Enhanced noise filter is not supported for this browser");
    return;
  }
  const krisp = KrispNoiseFilter();
  // Assuming you have a LocalAudioTrack instance
  localAudioTrack.setProcessor(krisp);
  krisp.setEnabled(true);
}, []);
Sources: Enhanced noise cancellation | LiveKit Docs | components/room.tsx | packages/react/src/components/participant/AudioTrack.tsx
b
Audio is directly controlled with the help of these import { ControlBar, RoomAudioRenderer, StartAudio, useTranscriptions, useVoiceAssistant, } from "@livekit/components-react";
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
b
There is no need of Api key to set up this krisp right