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

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