11/**
2- * Copyright 2026 Google LLC
2+ * Copyright 2025 Google LLC
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717class AudioProcessor extends AudioWorkletProcessor {
1818 constructor ( ) {
1919 super ( ) ;
20- this . targetSampleRate = 22000 ; // Change to your desired rate
20+ this . targetSampleRate = 16000 ; // Live API expects 16 kHz PCM input
2121 this . originalSampleRate = sampleRate ; // Browser's sample rate
2222 this . resampleRatio = this . originalSampleRate / this . targetSampleRate ;
2323 }
@@ -26,7 +26,7 @@ class AudioProcessor extends AudioWorkletProcessor {
2626 const input = inputs [ 0 ] ;
2727 if ( input . length > 0 ) {
2828 let audioData = input [ 0 ] ; // Get first channel's data
29-
29+
3030 if ( this . resampleRatio !== 1 ) {
3131 audioData = this . resample ( audioData ) ;
3232 }
@@ -40,9 +40,15 @@ class AudioProcessor extends AudioWorkletProcessor {
4040 const newLength = Math . round ( audioData . length / this . resampleRatio ) ;
4141 const resampled = new Float32Array ( newLength ) ;
4242
43+ // Linear interpolation resampling (higher quality than nearest neighbor)
44+ const lastIndex = audioData . length - 1 ;
4345 for ( let i = 0 ; i < newLength ; i ++ ) {
44- const srcIndex = Math . floor ( i * this . resampleRatio ) ;
45- resampled [ i ] = audioData [ srcIndex ] ; // Nearest neighbor resampling
46+ const srcPos = i * this . resampleRatio ;
47+ const srcIndex = Math . floor ( srcPos ) ;
48+ const nextIndex = Math . min ( srcIndex + 1 , lastIndex ) ;
49+ const frac = srcPos - srcIndex ;
50+ resampled [ i ] =
51+ audioData [ srcIndex ] * ( 1 - frac ) + audioData [ nextIndex ] * frac ;
4652 }
4753 return resampled ;
4854 }
0 commit comments