Skip to content

enfp-dev-studio/react-native-nitro-webrtc-vad

Repository files navigation

react-native-nitro-webrtc-vad

Fast native WebRTC voice activity detection for React Native, built with Nitro Modules.

Version Downloads License

react-native-nitro-webrtc-vad runs the WebRTC VAD engine in native C/C++ code and emits simple speech state transitions to JavaScript.

Features

  • Native WebRTC VAD for iOS and Android
  • Nitro Modules JSI API
  • Built-in microphone capture with speechStart and speechEnd events
  • Direct PCM16 frame classification with processFrame
  • No WebRTC checkout, depot_tools, gn, or ninja setup required

Requirements

  • React Native 0.76 or newer
  • Node.js 18 or newer
  • react-native-nitro-modules
  • Android minSdkVersion 23 or newer
  • iOS through the React Native CocoaPods toolchain

Expo apps must use a prebuild/dev-client native build. Expo Go is not supported because this package contains native C/C++ code.

Installation

bun add react-native-nitro-webrtc-vad react-native-nitro-modules
npm install react-native-nitro-webrtc-vad react-native-nitro-modules
yarn add react-native-nitro-webrtc-vad react-native-nitro-modules

For iOS, install pods after adding the package:

cd ios && pod install

For Expo prebuild/dev-client apps:

npx expo prebuild

Permissions

Android includes RECORD_AUDIO in the library manifest, but your app still needs to request microphone permission at runtime before calling start().

import { PermissionsAndroid, Platform } from 'react-native'

async function requestMicrophonePermission() {
  if (Platform.OS !== 'android') {
    return true
  }

  const result = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
  )

  return result === PermissionsAndroid.RESULTS.GRANTED
}

On iOS, add NSMicrophoneUsageDescription to your app's Info.plist.

<key>NSMicrophoneUsageDescription</key>
<string>This app uses the microphone to detect voice activity.</string>

Usage

import { NitroWebrtcVad, type VadEventType } from 'react-native-nitro-webrtc-vad'

NitroWebrtcVad.setListener((eventType: VadEventType, timestampMs: number) => {
  if (eventType === 'speechStart') {
    console.log('Speech started', timestampMs)
  }

  if (eventType === 'speechEnd') {
    console.log('Speech ended', timestampMs)
  }
})

NitroWebrtcVad.start({
  mode: 2,
  sampleRate: 16000,
  frameMs: 20,
  speechStartFrames: 3,
  speechEndFrames: 8,
})

// Later:
NitroWebrtcVad.stop()
NitroWebrtcVad.clearListener()

Direct PCM16 Frames

Use processFrame() when you already have mono PCM16 audio frames and want to classify them yourself.

NitroWebrtcVad.start({ sampleRate: 16000, frameMs: 20 })

const frame = new Int16Array(320)
const isSpeech = NitroWebrtcVad.processFrame(frame.buffer)

The frame byte length must match the configured sampleRate and frameMs.

API

NitroWebrtcVad.start(options?)

Starts native VAD processing and microphone capture.

Options:

  • mode: WebRTC VAD aggressiveness from 0 to 3. Higher values are more aggressive. Default: 2.
  • sampleRate: Input sample rate. Supported WebRTC VAD rates include 8000, 16000, 32000, and 48000. Default: 16000.
  • frameMs: Frame duration. Must be 10, 20, or 30. Default: 20.
  • speechStartFrames: Consecutive speech frames required before speechStart. Default: 3.
  • speechEndFrames: Consecutive non-speech frames required before speechEnd. Default: 8.

NitroWebrtcVad.stop()

Stops native VAD processing and microphone capture.

NitroWebrtcVad.setListener(listener)

Registers a listener for VAD state changes.

type VadEventType = 'speechStart' | 'speechEnd'
type VadListener = (eventType: VadEventType, timestampMs: number) => void

NitroWebrtcVad.clearListener()

Removes the current listener.

NitroWebrtcVad.processFrame(pcm16)

Processes a single mono PCM16 ArrayBuffer and returns true for speech or false for non-speech.

Notes

  • Call start() before processFrame().
  • Call stop() when your screen or recording session ends.
  • start() throws if VAD is already running.
  • processFrame() throws if the frame size does not match the configured VAD frame size.

Build Contract

This package vendors the small WebRTC VAD source needed for native builds. Consumers should not need to install WebRTC-specific build tools. See Consumer Build Contract for details.

License

MIT

Releases

No releases published

Packages

 
 
 

Contributors