Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

SphinxTranscriptionService.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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('unable to accept response from sphinx server.'
  37. + 'status: ' + request.status);
  38. }
  39. // if not ready no point to throw an error
  40. };
  41. request.open('POST', this.url);
  42. request.setRequestHeader('Content-Type',
  43. audioRecorder.determineCorrectFileType());
  44. request.send(audioFileBlob);
  45. console.log('send ' + audioFileBlob);
  46. };
  47. /**
  48. * Overrides the formatResponse method from AbstractTranscriptionService
  49. * It will parse the answer from the server in the expected format
  50. *
  51. * @param response the JSON body retrieved from the Sphinx4 server
  52. */
  53. SphinxService.prototype.formatResponse = function(response) {
  54. const result = JSON.parse(response).objects;
  55. // make sure to delete the session id object, which is always
  56. // the first value in the JSON array
  57. result.shift();
  58. const array = [];
  59. result.forEach(function(word) {
  60. if(!word.filler) {
  61. array.push(new Word(word.word, word.start, word.end));
  62. }
  63. });
  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 '
  105. + 'Sphinx4 https server';
  106. if(config.sphinxURL === undefined) {
  107. console.log(message);
  108. } else {
  109. const toReturn = config.sphinxURL;
  110. if(toReturn.includes !== undefined && toReturn.includes('https://')) {
  111. return toReturn;
  112. }
  113. console.log(message);
  114. }
  115. }
  116. module.exports = SphinxService;