UNPKG

watson-speech

Version:

IBM Watson Speech to Text and Text to Speech SDK for web browsers.

144 lines (119 loc) 6 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: speech-to-text/file-player.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: speech-to-text/file-player.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; var getContentTypeFromHeader = require('./content-type'); /** * Plays audio from a URL * Compatible with Mobile Safari if triggered in direct response to a user interaction (e.g. click) * @param {String} url * @constructor */ function UrlPlayer(url) { var audio = this.audio = new Audio(); audio.src = url; audio.play(); /** * Stops the audio */ this.stop = function stop() { audio.pause(); audio.currentTime = 0; }; } /** * 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 = FilePlayer.ERROR_UNSUPPORTED_FORMAT; err.contentType = contentType; throw err; } /** * Stops the audio */ this.stop = function stop() { audio.pause(); audio.currentTime = 0; }; } FilePlayer.ERROR_UNSUPPORTED_FORMAT = 'UNSUPPORTED_FORMAT'; /** * Reads the first few bytes of a binary file and resolves to the content-type if recognized &amp; supported * @param {File|Blob} file * @return {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 = FilePlayer.ERROR_UNSUPPORTED_FORMAT; reject(err); } }; }); } /** * Determines the file's content-type and then resolves to a FilePlayer instance * @param {File|Blob|String} file - binary data or URL of audio file (binary data playback may not work on mobile Safari) * @return {Promise.&lt;FilePlayer>} */ function playFile(file) { if (typeof file === 'string') { return Promise.resolve(new UrlPlayer(file)); } return getContentTypeFromFile(file).then(function(contentType) { return new FilePlayer(file, contentType); }); } module.exports = FilePlayer; module.exports.getContentType = getContentTypeFromFile; module.exports.playFile = playFile; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-watson-speech.html">watson-speech</a></li><li><a href="module-watson-speech_speech-to-text.html">watson-speech/speech-to-text</a></li><li><a href="module-watson-speech_speech-to-text_get-models.html">watson-speech/speech-to-text/get-models</a></li><li><a href="module-watson-speech_speech-to-text_recognize-file.html">watson-speech/speech-to-text/recognize-file</a></li><li><a href="module-watson-speech_speech-to-text_recognize-microphone.html">watson-speech/speech-to-text/recognize-microphone</a></li><li><a href="module-watson-speech_text-to-speech.html">watson-speech/text-to-speech</a></li><li><a href="module-watson-speech_text-to-speech_get-voices.html">watson-speech/text-to-speech/get-voices</a></li><li><a href="module-watson-speech_text-to-speech_synthesize.html">watson-speech/text-to-speech/synthesize</a></li></ul><h3>Classes</h3><ul><li><a href="FilePlayer.html">FilePlayer</a></li><li><a href="FormatStream.html">FormatStream</a></li><li><a href="RecognizeStream.html">RecognizeStream</a></li><li><a href="ResultStream.html">ResultStream</a></li><li><a href="SpeakerStream.html">SpeakerStream</a></li><li><a href="TimingStream.html">TimingStream</a></li><li><a href="UrlPlayer.html">UrlPlayer</a></li><li><a href="WebAudioL16Stream.html">WebAudioL16Stream</a></li><li><a href="WritableElementStream.html">WritableElementStream</a></li></ul><h3>Events</h3><ul><li><a href="RecognizeStream.html#event:close">close</a></li><li><a href="RecognizeStream.html#event:data">data</a></li><li><a href="RecognizeStream.html#event:error">error</a></li><li><a href="RecognizeStream.html#event:listening">listening</a></li><li><a href="RecognizeStream.html#event:message">message</a></li><li><a href="RecognizeStream.html#event:open">open</a></li><li><a href="RecognizeStream.html#event:send-data">send-data</a></li><li><a href="RecognizeStream.html#event:send-json">send-json</a></li><li><a href="RecognizeStream.html#event:stop">stop</a></li><li><a href="SpeakerStream.html#event:data">data</a></li></ul><h3>Global</h3><ul><li><a href="global.html#getContentTypeFromFile">getContentTypeFromFile</a></li><li><a href="global.html#playFile">playFile</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Feb 21 2017 17:41:51 GMT+0000 (UTC) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>