### 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 = `

Browser Compatibility

`; } displayCompatibility(features); ``` -------------------------------- ### Reset Web Voice Processor to Initial State Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md The `reset` function is used to clear all subscribed engines from the WebVoiceProcessor and stop any ongoing audio recording. This effectively brings the processor back to its initial state, ready for new configurations or to be completely shut down. ```javascript await WebVoiceProcessor.reset(); ``` -------------------------------- ### Check Browser Compatibility with Web Voice Processor Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md This function checks if the current browser meets the requirements for using the Web Voice Processor library. It verifies essential browser features like Web Audio API, Web Workers, and WebAssembly. The function returns an object indicating the status of each requirement, aiding in early detection of compatibility issues. ```javascript import { browserCompatibilityCheck } from '@picovoice/web-voice-processor'; browserCompatibilityCheck(); ``` ```javascript window.WebVoiceProcessor.browserCompatibilityCheck(); ``` -------------------------------- ### Set Custom AudioWorklet Processor URL - JavaScript Source: https://context7.com/picovoice/web-voice-processor/llms.txt This snippet demonstrates how to configure Web Voice Processor to use a custom AudioWorkletProcessor. It sets options like frame length and output sample rate, and specifies the URL for the custom processor script. This allows for specialized audio capture beyond the default. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; // Set custom recorder processor URL const options = { frameLength: 512, outputSampleRate: 16000, customRecorderProcessorURL: './custom-recorder-processor.js' }; WebVoiceProcessor.setOptions(options); const engine = { onmessage: (e) => { /* process */ } }; await WebVoiceProcessor.subscribe(engine); ``` -------------------------------- ### Access AudioContext with WebVoiceProcessor.audioContext Source: https://context7.com/picovoice/web-voice-processor/llms.txt A static property that provides access to the current `AudioContext` instance used by the Web Voice Processor. It returns the `AudioContext` if initialized, or `null` otherwise. This allows for advanced audio manipulation and analysis using the Web Audio API. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; console.log(WebVoiceProcessor.audioContext); // null const engine = { onmessage: (e) => { /* process */ } }; await WebVoiceProcessor.subscribe(engine); const context = WebVoiceProcessor.audioContext; console.log(`Sample rate: ${context.sampleRate} Hz`); console.log(`Audio context state: ${context.state}`); // Use context for audio analysis const analyser = context.createAnalyser(); analyser.fftSize = 2048; console.log(`Frequency bins: ${analyser.frequencyBinCount}`); ``` -------------------------------- ### Custom AudioWorklet Processor Implementation - JavaScript Source: https://context7.com/picovoice/web-voice-processor/llms.txt This JavaScript code defines a custom AudioWorkletProcessor, `CustomRecorderProcessor`. It handles incoming audio data, buffers it, and posts frames of a specified length to the main thread via `port.postMessage`. This processor is registered with the name 'recorder-processor'. ```javascript // custom-recorder-processor.js class CustomRecorderProcessor extends AudioWorkletProcessor { constructor(options) { super(); this.frameLength = options.processorOptions.frameLength; this.numberOfChannels = options.processorOptions.numberOfChannels; this.buffer = []; } process(inputs, outputs, parameters) { const input = inputs[0]; if (input && input.length > 0) { const channel = input[0]; // Use first channel for (let i = 0; i < channel.length; i++) { this.buffer.push(channel[i]); if (this.buffer.length >= this.frameLength) { // Send frame to main thread this.port.postMessage({ buffer: [new Float32Array(this.buffer)] }); this.buffer = []; } } } return true; // Keep processor alive } } registerProcessor('recorder-processor', CustomRecorderProcessor); ``` -------------------------------- ### Define Engine Interface for Web Voice Processor Source: https://github.com/picovoice/web-voice-processor/blob/master/README.md Defines the expected interface for an 'engine' object that can be subscribed to the WebVoiceProcessor. Engines must implement an `onmessage` method that handles incoming `command`s, specifically processing `inputFrame` data, which is an `Int16Array` of audio samples. ```javascript onmessage = function (e) { switch (e.data.command) { case 'process': process(e.data.inputFrame); break; } }; ``` -------------------------------- ### Reset WebVoiceProcessor: Stop Recording and Remove Engines Source: https://context7.com/picovoice/web-voice-processor/llms.txt Resets the WebVoiceProcessor by stopping audio recording and removing all subscribed engines. This is useful for cleanup or returning the processor to its initial state. It allows for resubscribing new engines after the reset. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; const engine1 = { onmessage: (e) => { /* process */ } }; const engine2 = { onmessage: (e) => { /* process */ } }; const worker = new Worker('./processor.js'); await WebVoiceProcessor.subscribe([engine1, engine2, worker]); // Stop all processing and clear all engines await WebVoiceProcessor.reset(); console.log('All engines removed, recording stopped'); // Can subscribe new engines after reset const newEngine = { onmessage: (e) => { /* process */ } }; await WebVoiceProcessor.subscribe(newEngine); ``` -------------------------------- ### Unsubscribe Engines from Audio Frames - JavaScript Source: https://context7.com/picovoice/web-voice-processor/llms.txt Unsubscribe engines from receiving audio frames. Audio recording automatically stops when all engines are unsubscribed, releasing microphone resources. Supports unsubscribing single or multiple engines. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; const audioEngine = { onmessage: function(e) { if (e.data.command === 'process') { console.log('Processing audio frame'); } } }; await WebVoiceProcessor.subscribe(audioEngine); // Later, stop processing await WebVoiceProcessor.unsubscribe(audioEngine); console.log('Audio processing stopped'); // Unsubscribe multiple engines const worker1 = new Worker('./worker1.js'); const worker2 = new Worker('./worker2.js'); await WebVoiceProcessor.subscribe([worker1, worker2]); await WebVoiceProcessor.unsubscribe([worker1, worker2]); ``` -------------------------------- ### Check Recording Status with WebVoiceProcessor.isRecording Source: https://context7.com/picovoice/web-voice-processor/llms.txt A static property that indicates whether the Web Voice Processor is currently recording audio. It returns `true` if recording is active and `false` otherwise. This property is useful for updating UI elements or controlling application logic based on the recording state. ```javascript import { WebVoiceProcessor } from '@picovoice/web-voice-processor'; console.log(`Recording: ${WebVoiceProcessor.isRecording}`); // false const engine = { onmessage: (e) => { /* process */ } }; await WebVoiceProcessor.subscribe(engine); console.log(`Recording: ${WebVoiceProcessor.isRecording}`); // true await WebVoiceProcessor.unsubscribe(engine); console.log(`Recording: ${WebVoiceProcessor.isRecording}`); // false // Use in UI to show recording state function updateRecordingIndicator() { const indicator = document.getElementById('recording-indicator'); indicator.style.display = WebVoiceProcessor.isRecording ? 'block' : 'none'; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.