UNPKG

watson-speech

Version:

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

58 lines (51 loc) 2.1 kB
'use strict'; var extname = require('path').extname; // This module attempts to identify common content-types based on the filename or header. // It is not exhaustive, and for best results, you should always manually specify the content-type option. // See the complete list of supported content-types at // https://console.bluemix.net/docs/services/speech-to-text/input.html#formats // *some* file types can be identified by the first 3-4 bytes of the file var headerContentTypes = { fLaC: 'audio/flac', RIFF: 'audio/wav', OggS: 'audio/ogg', ID3: 'audio/mp3', '\u001aEߣ': 'audio/webm' // String for first four hex's of webm: [1A][45][DF][A3] (https://www.matroska.org/technical/specs/index.html#EBML) }; /** * Takes the beginning of an audio file and returns the associated content-type / mime type * * @param {Buffer} buffer with at least the first 4 bytes of the file * @return {String|undefined} - the contentType of undefined */ exports.fromHeader = function contentTypeFromHeader(buffer) { var headerStr = buffer .slice(0, 4) .toString() .substr(0, 4); // mp3's are only consistent for the first 3 characters return headerContentTypes[headerStr] || headerContentTypes[headerStr.substr(0, 3)]; }; var filenameContentTypes = { '.mp3': 'audio/mp3', '.wav': 'audio/wav', '.flac': 'audio/flac', '.ogg': 'audio/ogg', '.oga': 'audio/ogg', '.opus': 'audio/ogg; codec=opus', '.webm': 'audio/webm' }; /** * Guess the content type from the filename * * Note: Blob and File objects include a .type property, but we're ignoring it because it's frequently either * incorrect (e.g. video/ogg instead of audio/ogg) or else a different format than what's expected (e.g. audio/x-wav) * * @param {String|Blob|File} file - string filename or url, or binary File/Blob object * @return {String|undefined} */ exports.fromFilename = function contentTypeFromFilename(file) { // todo: consider retrying with querystring & hash stripped from URLs var ext = extname((typeof file === 'string' && file) || file.name || ''); return filenameContentTypes[ext]; };