@jackdbd/eleventy-plugin-text-to-speech
Version:
Eleventy plugin for the Google Cloud Text-to-Speech API
76 lines • 2.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clientLibraryCredentials = exports.mediaType = exports.audioExtension = void 0;
const debug_1 = __importDefault(require("debug"));
const constants_js_1 = require("./constants.js");
const debug = (0, debug_1.default)(`${constants_js_1.DEBUG_PREFIX}/utils`);
const audioExtension = (audioEncoding) => {
switch (audioEncoding) {
case 'ALAW': {
return 'alaw';
}
case 'LINEAR16': {
return 'l16';
}
case 'MP3': {
return 'mp3';
}
case 'MULAW': {
return 'mulaw';
}
case 'OGG_OPUS': {
return 'opus';
}
default: {
return 'wav';
}
}
};
exports.audioExtension = audioExtension;
/**
* The <audio> tag supports 3 audio formats: MP3 (audio/mpeg), WAV (audio/wav), and OGG (audio/ogg).
* https://stackoverflow.com/questions/36866611/html5-audio-browsers-unable-to-decode-wav-file-encoded-with-ima-adpcm
*
* Supported media types in various browsers:
* https://en.wikipedia.org/wiki/HTML5_audio
*/
const mediaType = (ext) => {
switch (ext) {
case '.mp3': {
return { value: 'audio/mpeg' };
}
case '.opus': {
return { value: 'audio/ogg' };
}
case '.wav': {
return { value: 'audio/wav' };
}
default: {
return {
error: new Error(`file extension "${ext}" does not have a matching media type for the <code>audio</code> element`)
};
}
}
};
exports.mediaType = mediaType;
const clientLibraryCredentials = ({ keyFilename, what }) => {
let credentials = undefined;
if (keyFilename) {
debug(`initialize ${what} using keyFilename`);
credentials = keyFilename;
}
else {
debug(`initialize ${what} using environment variable GOOGLE_APPLICATION_CREDENTIALS`);
credentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
}
if (!credentials) {
const message = `${constants_js_1.ERROR_MESSAGE_PREFIX.invalidConfiguration}: neither keyFilename nor GOOGLE_APPLICATION_CREDENTIALS are set. Cannot initialize ${what}.`;
throw new Error(message);
}
return credentials;
};
exports.clientLibraryCredentials = clientLibraryCredentials;
//# sourceMappingURL=utils.js.map