mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-05 01:22:11 +00:00
31 lines
858 B
JavaScript
31 lines
858 B
JavaScript
const fs = require('fs/promises');
|
|
const {noopLogger, makeSynthKey} = require('./utils');
|
|
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 4 * 60) * 60; // cache tts for 4 hours
|
|
|
|
async function addFileToCache(client, logger, path,
|
|
{account_sid, vendor, language, voice, deploymentId, engine, text}) {
|
|
let key;
|
|
logger = logger || noopLogger;
|
|
|
|
try {
|
|
key = makeSynthKey({
|
|
account_sid,
|
|
vendor,
|
|
language: language || '',
|
|
voice: voice || deploymentId,
|
|
engine,
|
|
text,
|
|
});
|
|
const audioBuffer = await fs.readFile(path);
|
|
await client.setex(key, EXPIRES, audioBuffer.toString('base64'));
|
|
} catch (err) {
|
|
logger.error(err, 'addFileToCache: Error');
|
|
return;
|
|
}
|
|
|
|
logger.debug(`addFileToCache: added ${path} to cache with key ${key}`);
|
|
return key;
|
|
}
|
|
|
|
module.exports = addFileToCache;
|