### Quick Start with Script Tags for Browser VAD Source: https://docs.vad.ricky0123.com/user-guide/browser This example demonstrates how to quickly integrate the voice activity detector (VAD) into a web page using script tags. It loads the necessary ONNX Runtime Web and VAD web bundles from CDNs. The `onSpeechEnd` callback captures audio data when speech is detected, and `start()` initiates the VAD process. Ensure correct paths for `onnxWASMBasePath` and `baseAssetPath` are provided. ```javascript ``` -------------------------------- ### Install @ricky0123/vad-web via NPM Source: https://docs.vad.ricky0123.com/user-guide/browser This command installs the `@ricky0123/vad-web` package using NPM, a common package manager for Node.js projects. This allows for managing the VAD library as a project dependency. ```bash npm i @ricky0123/vad-web ``` -------------------------------- ### Webpack Configuration for Serving VAD Assets Source: https://docs.vad.ricky0123.com/user-guide/browser This Webpack configuration example utilizes the `copy-webpack-plugin` to ensure that necessary VAD and ONNX Runtime Web files are copied to the output directory during the build process. This is crucial when you choose to serve these assets locally instead of relying on CDNs. Ensure `copy-webpack-plugin` is installed (`npm i -D copy-webpack-plugin`). ```javascript const CopyPlugin = require("copy-webpack-plugin") module.exports = { // ... plugins: [ // ... new CopyPlugin({ patterns: [ // ... { from: "node_modules/@ricky0123/vad-web/dist/vad.worklet.bundle.min.js", to: "[name][ext]", }, { from: "node_modules/@ricky0123/vad-web/dist/*.onnx", to: "[name][ext]", }, { from: "node_modules/onnxruntime-web/dist/*.wasm", to: "[name][ext]" }, { from: "node_modules/onnxruntime-web/dist/*.mjs", to: "[name][ext]" }, ], }), ], } ``` -------------------------------- ### Install Vad-React Package Source: https://docs.vad.ricky0123.com/user-guide/react Install the Vad-React package using npm. This is the first step to integrate voice activity detection into your React application. ```bash npm i @ricky0123/vad-react ``` -------------------------------- ### Install Dependencies and Build Project with npm Source: https://docs.vad.ricky0123.com/developer-guide/hacking Commands to set up the development environment by installing project dependencies and building all packages using npm. These commands should be executed from the root of the repository. ```shell npm install npm run build ``` -------------------------------- ### Initialize and Use MicVAD with Callbacks (JavaScript) Source: https://docs.vad.ricky0123.com/user-guide/api Demonstrates how to initialize the MicVAD API with custom callbacks for speech events and start processing audio. It requires the '@ricky0123/vad-web' package and returns audio data on speech end. ```javascript import { MicVAD } from "@ricky0123/vad-web" const myvad = await MicVAD.new({ onSpeechEnd: (audio) => { // do something with `audio` (Float32Array of audio samples at sample rate 16000)... }, }) myvad.start() ``` -------------------------------- ### Customize Audio Stream Constraints with MicVAD Source: https://docs.vad.ricky0123.com/user-guide/browser This example demonstrates how to override the default microphone stream acquisition in MicVAD by providing a custom 'getStream' function. This allows for specific audio constraints such as channel count, sample rate, and disabling features like echo cancellation, auto gain control, and noise suppression. The onSpeechEnd callback is also shown for processing audio data upon speech detection. ```javascript import { MicVAD } from "@ricky0123/vad-web" const myvad = await MicVAD.new({ getStream: async () => { return await navigator.mediaDevices.getUserMedia({ audio: { channelCount: 2, // Stereo instead of mono echoCancellation: false, // Disable echo cancellation autoGainControl: false, // Disable auto gain control noiseSuppression: false, // Disable noise suppression sampleRate: 48000, // Custom sample rate }, }) }, onSpeechEnd: (audio) => { // do something with `audio` (Float32Array of audio samples at sample rate 16000)... }, }) myvad.start() ``` -------------------------------- ### Configure Browser VAD with Custom Asset Paths Source: https://docs.vad.ricky0123.com/user-guide/browser This JavaScript code snippet shows how to initialize the VAD when serving model and runtime files locally. By setting `baseAssetPath` and `onnxWASMBasePath`, you can specify the locations from which the VAD and ONNX Runtime Web assets should be loaded. The `onSpeechEnd` callback is used to process detected speech audio. ```javascript import { MicVAD } from "@ricky0123/vad-web" const myvad = await MicVAD.new({ baseAssetPath: "/", // or whatever you want onnxWASMBasePath: "/", // or whatever you want onSpeechEnd: (audio) => { // do something with `audio` (Float32Array of audio samples at sample rate 16000)... }, }) myvad.start() ``` -------------------------------- ### Custom Audio Configuration with useMicVAD Source: https://docs.vad.ricky0123.com/user-guide/react Configure custom audio stream settings for the useMicVAD hook. This allows for advanced control over audio constraints like channel count, echo cancellation, and noise suppression. ```javascript import { useMicVAD } from "@ricky0123/vad-react" const MyComponent = () => { const vad = useMicVAD({ getStream: async () => { return await navigator.mediaDevices.getUserMedia({ audio: { channelCount: 2, // Stereo echoCancellation: false, autoGainControl: false, noiseSuppression: false, }, }) }, onSpeechEnd: (audio) => { console.log("User stopped talking") }, }) return