選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SphinxTranscriptionService.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* global config, XMLHttpRequest, console, APP, JSON */
  2. var TranscriptionService = require("./AbstractTranscriptionService");
  3. var Word = require( "../word");
  4. var audioRecorder = require("./../audioRecorder");
  5. /**
  6. * Implements a TranscriptionService for a Sphinx4 http server
  7. */
  8. var 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. var request = new XMLHttpRequest();
  31. request.onreadystatechange = function() {
  32. if(request.readyState === XMLHttpRequest.DONE && request.status === 200)
  33. {
  34. callback(request.responseText);
  35. }
  36. else if (request.readyState === XMLHttpRequest.DONE) {
  37. throw new Error("unable to accept response from sphinx server." +
  38. "status: " + 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. var 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. var array = [];
  60. result.forEach(function(word){
  61. if(!word.filler) {
  62. array.push(new Word(word.word, word.start, word.end));
  63. }
  64. });
  65. return array;
  66. };
  67. /**
  68. * checks wether the reply is empty, or doesn't contain a correct JSON object
  69. * @param response the server response
  70. * @return {boolean} whether the response is valid
  71. */
  72. SphinxService.prototype.verify = function(response){
  73. console.log("response from server:" + response.toString());
  74. //test if server responded with a string object
  75. if(typeof(response) !== "string"){
  76. return false;
  77. }
  78. //test if the string can be parsed into valid JSON
  79. var json;
  80. try{
  81. json = JSON.parse(response);
  82. }
  83. catch (error){
  84. console.log(error);
  85. return false;
  86. }
  87. //check if the JSON has a "objects" value
  88. if(json.objects === undefined){
  89. return false;
  90. }
  91. //get the "objects" value and check for a session ID
  92. var array = json.objects;
  93. if(!(array[0] && array[0]["session-id"])){
  94. return false;
  95. }
  96. //everything seems to be in order
  97. return true;
  98. };
  99. /**
  100. * Gets the URL to the Sphinx4 server from the config file. If it's not there,
  101. * it will throw an error
  102. *
  103. * @returns {string} the URL to the sphinx4 server
  104. */
  105. function getURL() {
  106. var message = "config does not contain an url to a " +
  107. "Sphinx4 https server";
  108. if(config.sphinxURL === undefined){
  109. console.log(message);
  110. }
  111. else {
  112. var toReturn = config.sphinxURL;
  113. if(toReturn.includes !== undefined && toReturn.includes("https://")){
  114. return toReturn;
  115. }
  116. else{
  117. console.log(message);
  118. }
  119. }
  120. }
  121. module.exports = SphinxService;