PCM Audio Playback Worklet (audio-processors/playback.worklet.js)

generative-ai · gemini/multimodal-live-api/native-audio-websocket-demo-apps/plain-js-demo-app/frontend/audio-processors/playback.worklet.js · javascript · Apache-2.0
voice, realtime, lang:javascript, tag:browser

Audio worklet processor for queuing and playing raw PCM audio streams in the browser. Use for real-time voice applications.

// Source: https://github.com/GoogleCloudPlatform/generative-ai | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Audio Playback Worklet Processor for playing PCM audio
 */

class PCMProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this.audioQueue = [];

    this.port.onmessage = (event) => {
      if (event.data === "interrupt") {
        // Clear the queue on interrupt
        this.audioQueue = [];
      } else if (event.data instanceof Float32Array) {
        // Add audio data to the queue
        this.audioQueue.push(event.data);
      }
    };
  }

  process(inputs, outputs, parameters) {
    const output = outputs[0];
    if (output.length ==

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key