### Install Web Voice Processor via npm or yarn Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Instructions for installing the Web Voice Processor library using either npm or yarn package managers. These commands download and add the library to your project's dependencies, making its functionalities available for use. ```bash npm install @picovoice/web-voice-processor ``` ```bash yarn add @picovoice/web-voice-processor ``` -------------------------------- ### Initialize and Control WebVoiceProcessor VU Meter (JavaScript) Source: https://github.com/picovoice/web-voice-processor/blob/master/demo/index.html This snippet initializes the VuMeterEngine from WebVoiceProcessor, subscribes it to receive audio data, and updates a visual VU bar based on the RMS dB level. It also handles starting and stopping the processor, along with error handling. ```javascript let handle; document.addEventListener('DOMContentLoaded', function () { console.log('DOMContentLoaded'); let vuBar = document.getElementById('audio-rms-level'); function vuMeterCallback(dB) { const MIN_DB = -60; let pct = (Math.max(dB, MIN_DB) * 100) / -MIN_DB + 100; let cssPercentage = pct + '%'; vuBar.style.width = cssPercentage; } function writeMessage(message) { console.log(message); let p = document.createElement('p'); let text = document.createTextNode(message); p.appendChild(text); document.body.appendChild(p); } const vuMeterEngine = new WebVoiceProcessor.VuMeterEngine(vuMeterCallback); document.getElementById('start-audio-dump').onclick = () => { writeMessage('Starting Audio dump ...'); let blobPromise = WebVoiceProcessor.WebVoiceProcessor.audioDump(10000); document.getElementById('start-audio-dump').disabled = true; let downloadLink = document.getElementById('audio-download-link'); downloadLink.style.visibility = 'hidden'; blobPromise.then(blob => { let wav = new wavefile.WaveFile() blob.arrayBuffer().then(data => { const dataInt16Array = new Int16Array(data) wav.fromScratch(1, 16000, "16", dataInt16Array) downloadLink.href = wav.toDataURI(); writeMessage( 'Audio dump complete. Click to download the PCM binary data.', ); document.getElementById('start-audio-dump').disabled = false; downloadLink.style.visibility = 'visible'; }) }); }; document.getElementById('start').onclick = () => { document.getElementById('start').disabled = true; writeMessage('Starting ...'); WebVoiceProcessor.WebVoiceProcessor.subscribe(vuMeterEngine).catch(error => { writeMessage('Error initializing WebVoiceProcessor: ' + error); }) document.getElementById('stop').disabled = false; document.getElementById('start-audio-dump').disabled = false; }; document.getElementById('stop').onclick = () => { document.getElementById('stop').disabled = true; writeMessage('Stopping ...'); WebVoiceProcessor.WebVoiceProcessor.unsubscribe(vuMeterEngine); document.getElementById('start').disabled = false; document.getElementById('start-audio-dump').disabled = true; }; }); ``` -------------------------------- ### Subscribe Engines to Web Voice Processor for Audio Processing Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md This code snippet illustrates how to subscribe engines (either Web Workers or custom objects) to the WebVoiceProcessor. Subscription automatically starts audio recording. The library processes audio frames and sends them to the subscribed engines for further analysis. Handles potential rejections during microphone permission requests. ```javascript const worker = new Worker('${WORKER_PATH}'); const engine = { onmessage: function(e) { /// ... handle inputFrame } } await WebVoiceProcessor.subscribe(engine); await WebVoiceProcessor.subscribe(worker); // or await WebVoiceProcessor.subscribe([engine, worker]); ``` -------------------------------- ### Subscribe Engines to Receive Audio Frames - JavaScript Source: https://context7.com/picovoice/web-voice-processor/llms.txt Subscribe engines (Web Workers or objects with onmessage) to receive real-time microphone audio frames. Audio recording starts automatically upon the first subscription. Handles errors like permission denial or device not found. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; // Create a custom processing engine const audioEngine = { onmessage: function(e) { if (e.data.command === 'process') { const audioFrame = e.data.inputFrame; // Int16Array console.log(`Received ${audioFrame.length} audio samples`); // Process the audio frame const volume = calculateVolume(audioFrame); console.log(`Audio volume: ${volume}`); } } }; function calculateVolume(frame) { let sum = 0; for (let i = 0; i < frame.length; i++) { sum += Math.abs(frame[i]); } return sum / frame.length; } // Subscribe the engine and start recording try { await WebVoiceProcessor.subscribe(audioEngine); console.log('Microphone recording started'); } catch (error) { console.error('Failed to start recording:', error.message); // Handle permission denied or device not found errors } // Subscribe multiple engines at once const worker = new Worker('./audio-processor-worker.js'); await WebVoiceProcessor.subscribe([audioEngine, worker]); ``` -------------------------------- ### Subscribe to VuMeter Engine (JavaScript) Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Integrates a VU meter to capture audio levels. A callback function is provided to handle the VU meter data, which is expected in dBFS format within the range of [-96, 0]. ```javascript function vuMeterCallback(dB) { console.log(dB) } const vuMeterEngine = new VuMeterEngine(vuMeterCallback); WebVoiceProcessor.subscribe(vuMeterEngine); ``` -------------------------------- ### Import Web Voice Processor via ES Modules Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Demonstrates how to import the WebVoiceProcessor class from the library when using ES Modules, commonly found in modern JavaScript development environments like Create React App, Angular, or Webpack projects. This allows direct access to the library's core functionalities. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; ``` -------------------------------- ### Build WebVoiceProcessor from Source (Console) Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Builds the WebVoiceProcessor project using either Yarn or npm. The build process generates minified and non-minified versions of IIFE and ESM formats, along with TypeScript type definitions in the 'dist' folder. ```console yarn yarn build ``` ```console npm install npm run-script build ``` -------------------------------- ### Include Web Voice Processor via HTML Script Tag Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Shows how to include the Web Voice Processor library in an HTML file using a script tag. This method makes the library's functionalities available globally under the `window.WebVoiceProcessor` object, suitable for projects not using module bundlers. ```html ``` -------------------------------- ### Audio Resampling with WebAssembly (JavaScript) Source: https://context7.com/picovoice/web-voice-processor/llms.txt The Resampler class provides low-level audio resampling capabilities using a WebAssembly-based engine. It allows converting audio streams from one sample rate to another, which is crucial for applications that need to process audio at a specific rate, irrespective of the microphone's native sample rate. It offers methods for initialization, processing frames, calculating required input, resetting state, and releasing resources. ```javascript import { Resampler } from '@picovoice/web-voice-processor'; // Create resampler instance const inputSampleRate = 48000; // Browser microphone sample rate const outputSampleRate = 16000; // Target sample rate for speech processing const filterOrder = 50; // Resampler filter quality const frameLength = 512; // Output frame length const resampler = await Resampler.create( inputSampleRate, outputSampleRate, filterOrder, frameLength ); console.log(`Resampler version: ${resampler.version}`); console.log(`Input buffer length: ${resampler.inputBufferLength}`); console.log(`Output frame length: ${resampler.frameLength}`); // Process audio frame const inputFrame = new Float32Array(1024); // Microphone data // ... fill inputFrame with audio data ... const outputBuffer = new Int16Array(frameLength); const samplesProcessed = resampler.process(inputFrame, outputBuffer); console.log(`Processed ${samplesProcessed} samples`); // Calculate required input samples for desired output const requiredInputSamples = resampler.getNumRequiredInputSamples(512); console.log(`Need ${requiredInputSamples} input samples for 512 output samples`); // Reset resampler state resampler.reset(); // Clean up resampler.release(); ``` -------------------------------- ### Custom Web Worker for Audio Processing (JavaScript) Source: https://context7.com/picovoice/web-voice-processor/llms.txt This pattern demonstrates how to offload audio processing tasks to a separate Web Worker, preventing the main browser thread from becoming blocked during computationally intensive operations. The main script initializes the Web Voice Processor, subscribes a worker to receive audio frames, and handles results sent back from the worker. The worker script, responsible for the actual processing, receives audio frames, performs calculations (like energy and zero-crossing rate), and posts results back to the main thread. ```javascript // main.js import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; const worker = new Worker('./audio-processor-worker.js'); worker.onmessage = function(e) { if (e.data.command === 'result') { console.log('Processing result:', e.data.result); // Update UI with results document.getElementById('output').textContent = JSON.stringify(e.data.result); } }; // Subscribe worker to receive audio frames await WebVoiceProcessor.subscribe(worker); // Stop processing await WebVoiceProcessor.unsubscribe(worker); worker.terminate(); ``` ```javascript // audio-processor-worker.js let frameCount = 0; onmessage = function(e) { if (e.data.command === 'process') { const inputFrame = e.data.inputFrame; // Int16Array of audio samples // Perform audio processing const energy = calculateEnergy(inputFrame); const zeroCrossings = countZeroCrossings(inputFrame); frameCount++; // Send results back to main thread if (frameCount % 10 === 0) { postMessage({ command: 'result', result: { frameCount: frameCount, energy: energy, zeroCrossings: zeroCrossings } }); } } }; function calculateEnergy(frame) { let sum = 0; for (let i = 0; i < frame.length; i++) { sum += frame[i] * frame[i]; } return Math.sqrt(sum / frame.length); } function countZeroCrossings(frame) { let count = 0; for (let i = 1; i < frame.length; i++) { if ((frame[i - 1] >= 0 && frame[i] < 0) || (frame[i - 1] < 0 && frame[i] >= 0)) { count++; } } return count; } ``` -------------------------------- ### Use VuMeterEngine for Real-time Audio Level Monitoring Source: https://context7.com/picovoice/web-voice-processor/llms.txt The `VuMeterEngine` is a built-in engine that calculates and returns the VU (Volume Unit) meter value in dBFS for real-time audio level monitoring. It accepts a callback function that receives the dBFS value, allowing for UI updates or detection of silence and clipping. ```javascript import { WebVoiceProcessor, VuMeterEngine } from '@picovoice/web-voice-processor'; // Create VU meter with callback function vuMeterCallback(dB) { // dB is in range [-96, 0] dBFS console.log(`Audio level: ${dB.toFixed(2)} dBFS`); // Update UI volume meter const percentage = Math.max(0, (dB + 96) / 96 * 100); document.getElementById('volume-bar').style.width = `${percentage}%`; // Detect silence if (dB < -60) { console.log('Silence detected'); } // Detect clipping if (dB > -3) { console.warn('Audio level too high, may clip'); } } const vuMeterEngine = new VuMeterEngine(vuMeterCallback); await WebVoiceProcessor.subscribe(vuMeterEngine); // Subscribe other engines alongside VU meter const speechEngine = { onmessage: (e) => { /* speech processing */ } }; await WebVoiceProcessor.subscribe([vuMeterEngine, speechEngine]); // Stop VU meter await WebVoiceProcessor.unsubscribe(vuMeterEngine); ``` -------------------------------- ### Configure Custom Recorder Processor (JavaScript) Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Enables the use of a custom recorder processor by adding the 'customRecorderProcessorURL' option. This allows for custom audio processing beyond the default implementation. Refer to MDN's AudioWorkletProcessor documentation for more details. ```javascript let options = { frameLength: 512, outputSampleRate: 16000, deviceId: null, filterOrder: 50, customRecorderProcessorURL: "${URL_PATH_TO_RECORDER_PROCESSOR}" }; WebVoiceProcessor.setOptions(options); ``` -------------------------------- ### Record Audio with WebVoiceProcessor.audioDump() Source: https://context7.com/picovoice/web-voice-processor/llms.txt Records raw signed 16-bit PCM audio data for a specified duration and returns it as a Blob. This functionality is valuable for debugging, testing, or saving audio samples. The resulting Blob can be downloaded or sent to a server. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; // Record 3 seconds of audio try { const audioBlob = await WebVoiceProcessor.audioDump(3000); console.log(`Recorded ${audioBlob.size} bytes of audio data`); // Download the audio file const url = URL.createObjectURL(audioBlob); const a = document.createElement('a'); a.href = url; a.download = 'audio-dump.pcm'; a.click(); URL.revokeObjectURL(url); // Or send to server const formData = new FormData(); formData.append('audio', audioBlob, 'recording.pcm'); await fetch('/api/upload-audio', { method: 'POST', body: formData }); } catch (error) { console.error('Audio dump failed:', error); } // Record 5 seconds const longerRecording = await WebVoiceProcessor.audioDump(5000); // File format: Signed 16-bit PCM, 16000 Hz, mono // Can be opened in Audacity: File > Import > Raw Data ``` -------------------------------- ### Set WebVoiceProcessor Options (JavaScript) Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Updates audio settings for WebVoiceProcessor by overriding default options. This function accepts an object with various configuration parameters. ```javascript let options = { frameLength: 512, outputSampleRate: 16000, deviceId: null, filterOrder: 50, }; WebVoiceProcessor.setOptions(options); ``` -------------------------------- ### Unsubscribe Engines from Web Voice Processor Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Demonstrates how to unsubscribe engines from the WebVoiceProcessor. Unsubscribing stops the audio recording process. Multiple engines can be unsubscribed individually or as an array. ```javascript await WebVoiceProcessor.unsubscribe(engine); await WebVoiceProcessor.unsubscribe(worker); //or await WebVoiceProcessor.unsubscribe([engine, worker]); ``` -------------------------------- ### Set Audio Processing Options - JavaScript Source: https://context7.com/picovoice/web-voice-processor/llms.txt Configure audio processing settings including frame length, output sample rate, microphone device, and resampler filter order. Options can be set before subscribing or updated dynamically with the forceUpdate flag. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; // Set options before starting const options = { frameLength: 512, // Number of samples per frame (default: 512) outputSampleRate: 16000, // Target sample rate in Hz (default: 16000) deviceId: null, // Specific microphone ID or null for default filterOrder: 50, // Resampler filter order (default: 50) customRecorderProcessorURL: null // Custom AudioWorklet processor URL }; WebVoiceProcessor.setOptions(options); // Get available audio devices const devices = await navigator.mediaDevices.enumerateDevices(); const microphones = devices.filter(device => device.kind === 'audioinput'); console.log('Available microphones:', microphones); // Use a specific microphone if (microphones.length > 1) { WebVoiceProcessor.setOptions({ deviceId: microphones[1].deviceId }); } const engine = { onmessage: (e) => { /* process */ } }; await WebVoiceProcessor.subscribe(engine); // Update options while recording (causes brief interruption) WebVoiceProcessor.setOptions({ frameLength: 1024, outputSampleRate: 16000 }, true); // forceUpdate = true ``` -------------------------------- ### Check Browser Compatibility for Web Voice Processor (JavaScript) Source: https://context7.com/picovoice/web-voice-processor/llms.txt This function checks if the browser meets the minimum requirements for the Web Voice Processor, such as WebAssembly, Web Audio API, Web Workers, and secure context. It returns an object detailing the compatibility of each feature, which can be used to inform the user or tailor the application's behavior. ```javascript import { browserCompatibilityCheck } from '@picovoice/web-voice-processor'; const features = browserCompatibilityCheck(); console.log('Browser compatibility:', features); // Check overall compatibility if (features._picovoice) { console.log('Browser fully supports Web Voice Processor'); } else { console.error('Browser missing required features'); } // Check individual features if (!features.WebAssembly) { console.error('WebAssembly not supported'); } if (!features.mediaDevices) { console.error('Media Devices API not available'); } if (!features.Worker) { console.error('Web Workers not supported'); } if (!features.isSecureContext) { console.warn('Not a secure context (HTTPS required for production)'); } // Display compatibility report to user function displayCompatibility(features) { const report = document.getElementById('compatibility-report'); report.innerHTML = `