UNPKG

@jackdbd/eleventy-plugin-text-to-speech

Version:

Eleventy plugin that uses text-to-speech to generate audio assets for your website, then injects audio players in your HTML.

60 lines 2.09 kB
import { createHash } from 'node:crypto'; import { z } from 'zod'; import defDebug from 'debug'; import { DEBUG_PREFIX } from './constants.js'; import { validatedResult } from './validation.js'; import { hosting } from './hosting/schemas.js'; import { synthesis, text_to_synthesize } from './synthesis/schemas.js'; const debug = defDebug(`${DEBUG_PREFIX}:text-to-audio-asset`); export const text_to_audio_asset_config = z .object({ text: text_to_synthesize, synthesis, hosting }) .describe('Text to audio asset config'); /** * Synthesizes some text into a readable stream representing the speech. Then it * generates an audio asset from that speech. * * @param config * @returns */ export const textToAudioAsset = async (config) => { const result = validatedResult(config, text_to_audio_asset_config); if (result.error) { return { error: result.error }; } // TODO: make this function an async transducer? const { text, hosting, synthesis } = result.value; const s_res = await synthesis.synthesize(text); if (s_res.error) { return { error: s_res.error }; } const readable = s_res.value; // should I make configurable the code that computes the asset name? const md5 = createHash('md5'); const contents = [ text, JSON.stringify(synthesis.config), JSON.stringify(hosting.config) ]; const contentHash = md5.update(contents.join('_')).digest('hex'); const assetName = `${contentHash}.${synthesis.extension}`; debug(`write audio asset ${assetName}`); const w_res = await hosting.write({ assetName, readable }); if (w_res.error) { if (!readable.closed) { // should I destroy the readable stream manually? readable.destroy(); } return { error: w_res.error }; } if (!readable.closed) { // should I destroy the readable stream manually? readable.destroy(); } debug(`wrote audio asset ${assetName}`); return { value: w_res.value }; }; //# sourceMappingURL=text-to-audio-asset.js.map