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.

AbstractTranscriptionService.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Abstract class representing an interface to implement a speech-to-text
  3. * service on.
  4. */
  5. var TranscriptionService = function() {
  6. throw new Error("TranscriptionService is abstract and cannot be" +
  7. "created");
  8. };
  9. /**
  10. * This method can be used to send the recorder audio stream and
  11. * retrieve the answer from the transcription service from the callback
  12. *
  13. * @param {RecordingResult} recordingResult a recordingResult object which
  14. * includes the recorded audio stream as a blob
  15. * @param {Function} callback which will retrieve the a RecordingResult with
  16. * the answer as a WordArray
  17. */
  18. TranscriptionService.prototype.send = function send(recordingResult, callback){
  19. var t = this;
  20. this.sendRequest(recordingResult.blob, function(response){
  21. if(!t.verify(response)){
  22. console.log("the retrieved response from the server" +
  23. " is not valid!");
  24. recordingResult.wordArray = [];
  25. callback(recordingResult);
  26. }
  27. else{
  28. recordingResult.wordArray = t.formatResponse(response);
  29. callback(recordingResult);
  30. }
  31. });
  32. };
  33. /**
  34. * Abstract method which will rend the recorder audio stream to the implemented
  35. * transcription service and will retrieve an answer, which will be
  36. * called on the given callback method
  37. *
  38. * @param {Blob} audioBlob the recorded audio stream as a single Blob
  39. * @param {function} callback function which will retrieve the answer
  40. * from the service
  41. */
  42. TranscriptionService.prototype.sendRequest = function(audioBlob, callback) {
  43. throw new Error("TranscriptionService.sendRequest is abstract");
  44. };
  45. /**
  46. * Abstract method which will parse the output from the implemented
  47. * transcription service to the expected format
  48. *
  49. * The transcriber class expect an array of word objects, where each word
  50. * object is one transcribed word by the service.
  51. *
  52. * The expected output of this method is an array of word objects, in
  53. * the correct order. That is, the first object in the array is the first word
  54. * being said, and the last word in the array is the last word being said
  55. *
  56. * @param response the answer from the speech-to-text server which needs to be
  57. * formatted
  58. * @return {Array<Word>} an array of Word objects
  59. */
  60. TranscriptionService.prototype.formatResponse = function(response){
  61. throw new Error("TranscriptionService.format is abstract");
  62. };
  63. /**
  64. * Abstract method which will verify that the response from the server is valid
  65. *
  66. * @param response the response from the server
  67. * @return {boolean} true if response is valid, false otherwise
  68. */
  69. TranscriptionService.prototype.verify = function(response){
  70. throw new Error("TranscriptionService.verify is abstract");
  71. };
  72. module.exports = TranscriptionService;