Commit ccd57627 authored by Sami BENHAMICHE's avatar Sami BENHAMICHE
Browse files

Merge branch 'master' into 'master'

Add speech-to-text function to hublot client side

Closes #6

See merge request !8
parents 8f10465f 1d41d275
'use strict';
/* global robotLib:true WebSocket */
robotLib.stt = function (config) {
return {
getTranscriptSocket: onSegment => {
const ws = new WebSocket(config.gstreamerURL + '/client/ws/speech?content-type=audio/x-matroska,,+rate=(int)48000,+channels=(int)1');
ws.onopen = function () {
console.info('ws to stt module open');
};
ws.onclose = function () {
console.info('ws to stt module closed');
};
ws.onerror = function (event) {
console.info('ws to stt module error: ' + event);
};
ws.onmessage = function (event) {
const hyp = JSON.parse(event.data);
if (hyp.status === 0) {
if (hyp.result !== undefined && hyp.result.final) {
const trans = ((hyp.result.hypotheses)[0]).transcript;
let start;
let end;
if (hyp['segment-start'] && hyp['segment-length']) {
start = JSON.parse(hyp['segment-start']);
end = parseFloat(hyp['segment-start']) + parseFloat(hyp['segment-length']);
} else {
const time = new Date().getTime() / 1000;
start = time;
end = time + 1;
}
onSegment({
from: start,
until: end,
text: trans
});
}
}
};
return ws;
}
};
};
'use strict';
function WebSocket() {
return {
type: 'Websocket'
};
}
const robotLib = {};
const config = {gstreamerURL: 'ws://testurl'};
describe('client/lib/stt', () => {
beforeEach(() => {
global.robotLib = robotLib;
global.WebSocket = WebSocket;
/* eslint-disable import/no-unassigned-import */
require('./speech-to-text.js');
/* eslint-enable */
});
test('should define robotLib.stt', () => {
expect(global.robotLib.stt).toBeDefined();
});
test('should return a Websocket on getTranscriptSocket', () => {
const stt = global.robotLib.stt(config);
const res = stt.getTranscriptSocket(() => {});
expect(res.type).toBe('Websocket');
});
test('should call callback on valid segement', () => {
const stt = global.robotLib.stt(config);
const callback = jest.fn();
const ws = stt.getTranscriptSocket(callback);
const dataContent = {
status: 0,
result: {
final: true,
hypotheses: [
{
transcript: 'this is a transcript'
}
]
}
};
ws.onmessage({
data: JSON.stringify(dataContent)
});
expect(callback).toHaveBeenCalled();
});
test('should call callback with actual transcript', () => {
const stt = global.robotLib.stt(config);
const callback = jest.fn();
const ws = stt.getTranscriptSocket(callback);
const dataContent = {
status: 0,
result: {
final: true,
hypotheses: [
{
transcript: 'this is a transcript'
}
]
}
};
ws.onmessage({
data: JSON.stringify(dataContent)
});
expect(callback).toHaveBeenCalledWith(expect.objectContaining({
text: 'this is a transcript'
}));
});
test('should not call callback with incorrect value of status', () => {
const stt = global.robotLib.stt(config);
const callback = jest.fn();
const ws = stt.getTranscriptSocket(callback);
const dataContent = {
status: 1,
result: {
final: true,
hypotheses: [
{
transcript: 'this is a transcript'
}
]
}
};
ws.onmessage({
data: JSON.stringify(dataContent)
});
expect(callback).not.toHaveBeenCalled();
});
test('should not call callback when transcription is not final', () => {
const stt = global.robotLib.stt(config);
const callback = jest.fn();
const ws = stt.getTranscriptSocket(callback);
const dataContent = {
status: 0,
result: {
final: false,
hypotheses: [
{
transcript: 'this is a transcript'
}
]
}
};
ws.onmessage({
data: JSON.stringify(dataContent)
});
expect(callback).not.toHaveBeenCalled();
});
});
......@@ -16,7 +16,8 @@
}
}
},
"client": {
"name": "hublot"
}
"client": {
"name": "hublot",
"gstreamerURL": "ws://localhost:8888"
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment