UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

52 lines (44 loc) 1.82 kB
/** * Copyright 2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ class AudioProcessor extends AudioWorkletProcessor { constructor() { super(); this.targetSampleRate = 22000; // Change to your desired rate this.originalSampleRate = sampleRate; // Browser's sample rate this.resampleRatio = this.originalSampleRate / this.targetSampleRate; } process(inputs, outputs, parameters) { const input = inputs[0]; if (input.length > 0) { let audioData = input[0]; // Get first channel's data if (this.resampleRatio !== 1) { audioData = this.resample(audioData); } this.port.postMessage(audioData); } return true; // Keep processor alive } resample(audioData) { const newLength = Math.round(audioData.length / this.resampleRatio); const resampled = new Float32Array(newLength); for (let i = 0; i < newLength; i++) { const srcIndex = Math.floor(i * this.resampleRatio); resampled[i] = audioData[srcIndex]; // Nearest neighbor resampling } return resampled; } } registerProcessor('audio-processor', AudioProcessor);