UNPKG

watson-speech

Version:

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

108 lines (84 loc) 4.39 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 contentType = require('./content-type'); function getContentType(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 = contentType(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 * @param 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; } this.stop = function stop() { audio.pause(); audio.currentTime = 0; } } function playFile(file) { return getContentType(file).then(function (contentType) { return new FilePlayer(file, contentType); }); } module.exports = FilePlayer; module.exports.getContentType = getContentType; 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_recognize-element.html">watson-speech/speech-to-text/recognize-element</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="MediaElementAudioStream.html">MediaElementAudioStream</a></li><li><a href="RecognizeStream.html">RecognizeStream</a></li><li><a href="TimingStream.html">TimingStream</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:connection-close">connection-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:results">results</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Feb 24 2016 18:53:09 GMT+0000 (UTC) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>