modified lib-jitsi-meet dev repo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SphinxTranscriptionService.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* global config */
  2. import Word from '../word';
  3. import audioRecorder from './../audioRecorder';
  4. import AbstractTranscriptionService from './AbstractTranscriptionService';
  5. /**
  6. * Implements a TranscriptionService for a Sphinx4 http server
  7. */
  8. export default class SphinxService extends AbstractTranscriptionService {
  9. /**
  10. * Implements a TranscriptionService for a Sphinx4 http server
  11. */
  12. constructor() {
  13. super();
  14. // set the correct url
  15. this.url = getURL();
  16. }
  17. /**
  18. * Overrides the sendRequest method from AbstractTranscriptionService
  19. * it will send the audio stream the a Sphinx4 server to get the transcription
  20. *
  21. * @param audioFileBlob the recorder audio stream an a single Blob
  22. * @param callback the callback function retrieving the server response
  23. */
  24. sendRequest(audioFileBlob, callback) {
  25. console.log(`sending an audio file to ${this.url}`);
  26. console.log(`the audio file being sent: ${audioFileBlob}`);
  27. const request = new XMLHttpRequest();
  28. request.onreadystatechange = function() {
  29. if (request.readyState === XMLHttpRequest.DONE
  30. && request.status === 200) {
  31. callback(request.responseText);
  32. } else if (request.readyState === XMLHttpRequest.DONE) {
  33. throw new Error(
  34. `unable to accept response from sphinx server. status: ${request.status}`);
  35. }
  36. // if not ready no point to throw an error
  37. };
  38. request.open('POST', this.url);
  39. request.setRequestHeader('Content-Type',
  40. audioRecorder.determineCorrectFileType());
  41. request.send(audioFileBlob);
  42. console.log(`send ${audioFileBlob}`);
  43. }
  44. /**
  45. * Overrides the formatResponse method from AbstractTranscriptionService
  46. * It will parse the answer from the server in the expected format
  47. *
  48. * @param response the JSON body retrieved from the Sphinx4 server
  49. */
  50. formatResponse(response) {
  51. const result = JSON.parse(response).objects;
  52. // make sure to delete the session id object, which is always
  53. // the first value in the JSON array
  54. result.shift();
  55. const array = [];
  56. result.forEach(
  57. word => word.filler
  58. || array.push(new Word(word.word, word.start, word.end)));
  59. return array;
  60. }
  61. /**
  62. * checks wether the reply is empty, or doesn't contain a correct JSON object
  63. * @param response the server response
  64. * @return {boolean} whether the response is valid
  65. */
  66. verify(response) {
  67. console.log(`response from server:${response.toString()}`);
  68. // test if server responded with a string object
  69. if (typeof response !== 'string') {
  70. return false;
  71. }
  72. // test if the string can be parsed into valid JSON
  73. let json;
  74. try {
  75. json = JSON.parse(response);
  76. } catch (error) {
  77. console.log(error);
  78. return false;
  79. }
  80. // check if the JSON has a "objects" value
  81. if (json.objects === undefined) {
  82. return false;
  83. }
  84. // get the "objects" value and check for a session ID
  85. const array = json.objects;
  86. if (!(array[0] && array[0]['session-id'])) {
  87. return false;
  88. }
  89. // everything seems to be in order
  90. return true;
  91. }
  92. }
  93. /**
  94. * Gets the URL to the Sphinx4 server from the config file. If it's not there,
  95. * it will throw an error
  96. *
  97. * @returns {string} the URL to the sphinx4 server
  98. */
  99. function getURL() {
  100. const message = 'config does not contain an url to a Sphinx4 https server';
  101. if (config.sphinxURL === undefined) {
  102. console.log(message);
  103. } else {
  104. const toReturn = config.sphinxURL;
  105. if (toReturn.includes !== undefined && toReturn.includes('https://')) {
  106. return toReturn;
  107. }
  108. console.log(message);
  109. }
  110. }