Add streaming mode to JS client
This commit is contained in:
parent
b0415af4b4
commit
be1bd04b50
@ -30,6 +30,7 @@ parser.addArgument(['--scorer'], {help: 'Path to the external scorer file'});
|
||||
parser.addArgument(['--audio'], {required: true, help: 'Path to the audio file to run (WAV format)'});
|
||||
parser.addArgument(['--version'], {action: VersionAction, nargs: 0, help: 'Print version and exits'});
|
||||
parser.addArgument(['--extended'], {action: 'storeTrue', help: 'Output string from extended metadata'});
|
||||
parser.addArgument(['--stream'], {action: 'storeTrue', help: 'Use streaming code path (for tests)'});
|
||||
let args = parser.parseArgs();
|
||||
|
||||
function totalTime(hrtimeValue: number[]): string {
|
||||
@ -86,8 +87,7 @@ function bufferToStream(buffer: Buffer) {
|
||||
return stream;
|
||||
}
|
||||
|
||||
let audioStream = new MemoryStream();
|
||||
bufferToStream(buffer).
|
||||
let conversionStream = bufferToStream(buffer).
|
||||
pipe(Sox({
|
||||
global: {
|
||||
'no-dither': true,
|
||||
@ -102,27 +102,38 @@ bufferToStream(buffer).
|
||||
compression: 0.0,
|
||||
type: 'raw'
|
||||
}
|
||||
})).
|
||||
pipe(audioStream);
|
||||
}));
|
||||
|
||||
audioStream.on('finish', () => {
|
||||
let audioBuffer = audioStream.toBuffer();
|
||||
if (!args['stream']) {
|
||||
let audioStream = new MemoryStream();
|
||||
conversionStream.pipe(audioStream);
|
||||
audioStream.on('finish', () => {
|
||||
let audioBuffer = audioStream.toBuffer();
|
||||
|
||||
const inference_start = process.hrtime();
|
||||
console.error('Running inference.');
|
||||
const audioLength = (audioBuffer.length / 2) * (1 / desired_sample_rate);
|
||||
const inference_start = process.hrtime();
|
||||
console.error('Running inference.');
|
||||
const audioLength = (audioBuffer.length / 2) * (1 / desired_sample_rate);
|
||||
|
||||
// sphinx-doc: js_ref_inference_start
|
||||
if (args['extended']) {
|
||||
let metadata = model.sttWithMetadata(audioBuffer, 1);
|
||||
console.log(candidateTranscriptToString(metadata.transcripts[0]));
|
||||
Ds.FreeMetadata(metadata);
|
||||
} else {
|
||||
console.log(model.stt(audioBuffer));
|
||||
}
|
||||
// sphinx-doc: js_ref_inference_stop
|
||||
const inference_stop = process.hrtime(inference_start);
|
||||
console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4));
|
||||
Ds.FreeModel(model);
|
||||
process.exit(0);
|
||||
});
|
||||
// sphinx-doc: js_ref_inference_start
|
||||
if (args['extended']) {
|
||||
let metadata = model.sttWithMetadata(audioBuffer, 1);
|
||||
console.log(candidateTranscriptToString(metadata.transcripts[0]));
|
||||
Ds.FreeMetadata(metadata);
|
||||
} else {
|
||||
console.log(model.stt(audioBuffer));
|
||||
}
|
||||
// sphinx-doc: js_ref_inference_stop
|
||||
const inference_stop = process.hrtime(inference_start);
|
||||
console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4));
|
||||
Ds.FreeModel(model);
|
||||
process.exit(0);
|
||||
});
|
||||
} else {
|
||||
let stream = model.createStream();
|
||||
conversionStream.on('data', (chunk: Buffer) => {
|
||||
stream.feedAudioContent(chunk);
|
||||
});
|
||||
conversionStream.on('end', () => {
|
||||
console.log(stream.finishStream());
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user