Skip to content

Instantly share code, notes, and snippets.

@seatedro
Created September 16, 2022 17:08
Show Gist options
  • Save seatedro/ad2a8f29d39c451e7f5dc5b937f77d39 to your computer and use it in GitHub Desktop.
Save seatedro/ad2a8f29d39c451e7f5dc5b937f77d39 to your computer and use it in GitHub Desktop.
/**
* Receives streaming data and writes it to the recognizeStream for transcription
*
* @param {Buffer} data A section of audio data
*/
receiveData(data: DataView) {
if (
this.newStream &&
this.lastAudioInput.length !== 0 &&
this.recognizeStream
) {
// Approximate math to calculate time of chunks
const chunkTime = this.streamingLimit / this.lastAudioInput.length;
if (chunkTime !== 0) {
if (this.bridgingOffset < 0) {
this.bridgingOffset = 0;
}
if (this.bridgingOffset > this.finalRequestEndTime) {
this.bridgingOffset = this.finalRequestEndTime;
}
const chunksFromMS = Math.floor(
(this.finalRequestEndTime - this.bridgingOffset) / chunkTime
);
this.bridgingOffset = Math.floor(
(this.lastAudioInput.length - chunksFromMS) * chunkTime
);
for (let i = chunksFromMS; i < this.lastAudioInput.length; i++) {
this.recognizeStream.write(this.lastAudioInput[i]);
}
}
this.newStream = false;
}
this.audioInput.push(data);
if (this.recognizeStream) {
this.recognizeStream.write(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment