Source: speech-to-text/file-player.js

'use strict';

var getContentTypeFromHeader = require('./content-type');

/**
 * Reads the first few bytes of a binary file and resolves to the content-type if recognized & supported
 * @param {File|Blob} file
 * @returns {Promise}
 */
function getContentTypeFromFile(file) {
  return new Promise(function(resolve, reject) {
    var blobToText = new Blob([file]).slice(0, 4);
    var r = new FileReader();
    r.readAsText(blobToText);
    r.onload = function() {
      var ct = getContentTypeFromHeader(r.result);
      if (ct) {
        resolve(ct);
      } else {
        var err = new Error('Unable to determine content type from file header; only wav, flac, and ogg/opus are supported.');
        err.name = 'UNRECOGNIZED_FORMAT';
        reject(err);
      }
    };
  });
}

/**
 * Plays audio from File/Blob instances
 * @param {File|Blob} file
 * @param {String} contentType
 * @constructor
 */
function FilePlayer(file, contentType) {
  var audio = this.audio = new Audio();
  if (audio.canPlayType(contentType)) {
    audio.src = URL.createObjectURL(new Blob([file], {type: contentType}));
    audio.play();
  } else {
    // if we emit an error, it prevents the promise from returning the actual result
    // however, most browsers do not support flac, so this is a reasonably scenario
    var err = new Error('Current browser is unable to play back ' + contentType);
    err.name = 'UNSUPPORTED_FORMAT';
    err.contentType = contentType;
    throw err;
  }
  /**
   * Stops the audio
   */
  this.stop = function stop() {
    audio.pause();
    audio.currentTime = 0;
  };
}

/**
 * Determines the file's content-type and then resolves to a FilePlayer instance
 * @param {File|Blob} file
 * @returns {Promise.<FilePlayer>}
 */
function playFile(file) {
  return getContentTypeFromFile(file).then(function(contentType) {
    return new FilePlayer(file, contentType);
  });
}

module.exports = FilePlayer;
module.exports.getContentType = getContentTypeFromFile;
module.exports.playFile = playFile;