downsampleAudio
Downsamples audio data from one sample rate to a lower sample rate.
function downsampleAudio(
buffer,
fromSampleRate,
toSampleRate): Float32Array;
Defined in: src/utils/audio.ts:164
Downsamples audio data from one sample rate to a lower sample rate.
Parameters
| Parameter | Type | Description |
|---|---|---|
buffer | Float32Array | The input audio samples as a Float32Array. |
fromSampleRate | number | The current sample rate of the input audio in Hz. |
toSampleRate | number | The desired output sample rate in Hz. |
Returns
Float32Array
A new Float32Array with the downsampled audio data, or the original buffer if the rates are equal.
Remarks
Uses simple averaging of sample windows to reduce the sample rate. This is adequate for speech audio but may introduce aliasing for music or complex audio. For speech applications (STT), this produces acceptable results.
If fromSampleRate equals toSampleRate, the original buffer is returned without copying.
Example
// Downsample from 48 kHz to 16 kHz for STT processing
const input = new Float32Array(48000); // 1 second at 48 kHz
const output = downsampleAudio(input, 48000, 16000);
// output.length === 16000