Skip to content

Commit

Permalink
Refactor the decreaseSampleRate function
Browse files Browse the repository at this point in the history
Use `Math.ceil()` instead of `Math.round()` to ensure there is enough space for
all samples.
  • Loading branch information
jinmiaoluo committed Jun 2, 2024
1 parent 723e46f commit ae74f7d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function sendAudioConfig(language) {
type: 'config',
data: {
sampleRate: context.sampleRate,
channels: 1, // Assuming mono channel
channels: 1,
language: language,
processing_strategy: selectedStrategy.value,
processing_args: processingArgs
Expand All @@ -208,16 +208,14 @@ function sendAudioConfig(language) {
}

function processAudio(sampleData) {
const inputSampleRate = context.sampleRate;
const outputSampleRate = 16000;

// ASR (Automatic Speech Recognition) and VAD (Voice Activity Detection)
// models typically require mono audio with a sampling rate of 16 kHz,
// represented as a signed int16 array type.
//
// Implementing changes to the sampling rate using JavaScript can reduce
// computational costs on the server.
const decreaseResultBuffer = decreaseSampleRate(sampleData, inputSampleRate, outputSampleRate);
const outputSampleRate = 16000;
const decreaseResultBuffer = decreaseSampleRate(sampleData, context.sampleRate, outputSampleRate);
const audioData = convertFloat32ToInt16(decreaseResultBuffer);

if (websocket && websocket.readyState === WebSocket.OPEN) {
Expand All @@ -226,11 +224,15 @@ function processAudio(sampleData) {
}

function decreaseSampleRate(buffer, inputSampleRate, outputSampleRate) {
if (inputSampleRate === outputSampleRate) {
return buffer;
if (inputSampleRate < outputSampleRate) {
console.error("Sample rate too small.");
return;
} else if (inputSampleRate === outputSampleRate) {
return;
}

let sampleRateRatio = inputSampleRate / outputSampleRate;
let newLength = Math.round(buffer.length / sampleRateRatio);
let newLength = Math.ceil(buffer.length / sampleRateRatio);
let result = new Float32Array(newLength);
let offsetResult = 0;
let offsetBuffer = 0;
Expand Down

0 comments on commit ae74f7d

Please sign in to comment.