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 4.0KB

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