Autoformat

This commit is contained in:
SillyLossy
2023-04-21 23:41:37 +03:00
parent 54ec3c0840
commit 2cedeba102

View File

@ -11,13 +11,13 @@ let audioControl
let lastCharacterId = null; let lastCharacterId = null;
let lastGroupId = null; let lastGroupId = null;
let lastChatId = null; let lastChatId = null;
async function moduleWorker() { async function moduleWorker() {
// Primarily determinign when to add new chat to the TTS queue // Primarily determinign when to add new chat to the TTS queue
const enabled = $("#elevenlabs_enabled").is(':checked'); const enabled = $("#elevenlabs_enabled").is(':checked');
if (!enabled){ if (!enabled) {
return; return;
} }
@ -34,7 +34,7 @@ async function moduleWorker() {
} }
// Chat/character/group changed // Chat/character/group changed
if ((context.groupId && lastGroupId !== context.groupId) || (context.characterId !== lastCharacterId) || (context.chatId !== lastChatId)) { if ((context.groupId && lastGroupId !== context.groupId) || (context.characterId !== lastCharacterId) || (context.chatId !== lastChatId)) {
currentMessageNumber = context.chat.length ? context.chat.length : 0 currentMessageNumber = context.chat.length ? context.chat.length : 0
saveLastValues(); saveLastValues();
return; return;
@ -135,7 +135,7 @@ async function fetchTtsHistory() {
async function findTtsGenerationInHistory(message, voiceId) { async function findTtsGenerationInHistory(message, voiceId) {
const ttsHistory = await fetchTtsHistory(); const ttsHistory = await fetchTtsHistory();
for (const history of ttsHistory) { for (const history of ttsHistory) {
const text = history.text; const text = history.text;
const itemId = history.history_item_id; const itemId = history.history_item_id;
if (message === text && history.voice_id == voiceId) { if (message === text && history.voice_id == voiceId) {
@ -160,9 +160,9 @@ let queueProcessorReady = true
let lastAudioPosition = 0 let lastAudioPosition = 0
async function playAudioData(audioBlob){ async function playAudioData(audioBlob) {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = function(e) { reader.onload = function (e) {
const srcUrl = e.target.result; const srcUrl = e.target.result;
audioElement.src = srcUrl; audioElement.src = srcUrl;
}; };
@ -186,14 +186,14 @@ function completeCurrentAudioJob() {
*/ */
async function addAudioJob(response) { async function addAudioJob(response) {
const audioData = await response.blob() const audioData = await response.blob()
if (audioData.type != "audio/mpeg"){ if (audioData.type != "audio/mpeg") {
throw `TTS received HTTP response with invalid data format. Expecting audio/mpeg, got ${audioData.type}` throw `TTS received HTTP response with invalid data format. Expecting audio/mpeg, got ${audioData.type}`
} }
audioJobQueue.push(audioData) audioJobQueue.push(audioData)
console.debug("Pushed audio job to queue.") console.debug("Pushed audio job to queue.")
} }
async function processAudioJobQueue(){ async function processAudioJobQueue() {
// Nothing to do, audio not completed, or audio paused - stop processing. // Nothing to do, audio not completed, or audio paused - stop processing.
if (audioJobQueue.length == 0 || !queueProcessorReady || audioPaused) { if (audioJobQueue.length == 0 || !queueProcessorReady || audioPaused) {
return; return;
@ -202,7 +202,7 @@ async function processAudioJobQueue(){
queueProcessorReady = false queueProcessorReady = false
currentAudioJob = audioJobQueue.pop() currentAudioJob = audioJobQueue.pop()
playAudioData(currentAudioJob) playAudioData(currentAudioJob)
} catch(error) { } catch (error) {
console.error(error) console.error(error)
queueProcessorReady = true queueProcessorReady = true
} }
@ -217,12 +217,12 @@ let ttsJobQueue = []
let currentTtsJob let currentTtsJob
let currentMessageNumber = 0 let currentMessageNumber = 0
function completeTtsJob(){ function completeTtsJob() {
console.info(`Current TTS job for ${currentTtsJob.name} completed.`) console.info(`Current TTS job for ${currentTtsJob.name} completed.`)
currentTtsJob = null currentTtsJob = null
} }
function saveLastValues(){ function saveLastValues() {
const context = getContext() const context = getContext()
lastGroupId = context.groupId; lastGroupId = context.groupId;
lastCharacterId = context.characterId; lastCharacterId = context.characterId;
@ -252,7 +252,7 @@ async function processTtsQueue() {
console.debug("New message found, running TTS") console.debug("New message found, running TTS")
currentTtsJob = ttsJobQueue.shift() currentTtsJob = ttsJobQueue.shift()
const text = currentTtsJob.mes.replaceAll('*','...'); const text = currentTtsJob.mes.replaceAll('*', '...');
const char = currentTtsJob.name const char = currentTtsJob.name
try { try {
@ -261,7 +261,7 @@ async function processTtsQueue() {
} }
const voice = await getTtsVoice(voiceMap[char]) const voice = await getTtsVoice(voiceMap[char])
const voiceId = voice.voice_id const voiceId = voice.voice_id
if (voiceId == null){ if (voiceId == null) {
throw (`Unable to attain voiceId for ${char}`) throw (`Unable to attain voiceId for ${char}`)
} }
tts(text, voiceId) tts(text, voiceId)
@ -318,7 +318,7 @@ async function updateApiKey() {
// Using this call to validate API key // Using this call to validate API key
API_KEY = String(value) API_KEY = String(value)
await fetchTtsVoiceIds().catch((error => { await fetchTtsVoiceIds().catch((error => {
API_KEY = null API_KEY = null
throw `ElevenLabs TTS API key invalid` throw `ElevenLabs TTS API key invalid`
})) }))
@ -338,12 +338,12 @@ function parseVoiceMap(voiceMapString) {
return parsedVoiceMap return parsedVoiceMap
} }
async function getTtsVoice(name){ async function getTtsVoice(name) {
// We're caching the list of voice_ids. This might cause trouble if the user creates a new voice without restarting // We're caching the list of voice_ids. This might cause trouble if the user creates a new voice without restarting
if (elevenlabsTtsVoices.length == 0) { if (elevenlabsTtsVoices.length == 0) {
elevenlabsTtsVoices = await fetchTtsVoiceIds(); elevenlabsTtsVoices = await fetchTtsVoiceIds();
} }
const match = elevenlabsTtsVoices.filter((elevenVoice) => elevenVoice.name == name)[0] ; const match = elevenlabsTtsVoices.filter((elevenVoice) => elevenVoice.name == name)[0];
if (!match) { if (!match) {
throw `TTS Voice name ${name} not found in ElevenLabs account`; throw `TTS Voice name ${name} not found in ElevenLabs account`;
} }
@ -354,9 +354,9 @@ async function voicemapIsValid(parsedVoiceMap) {
let valid = true let valid = true
for (const characterName in parsedVoiceMap) { for (const characterName in parsedVoiceMap) {
const parsedVoiceName = parsedVoiceMap[characterName]; const parsedVoiceName = parsedVoiceMap[characterName];
try{ try {
await getTtsVoice(parsedVoiceName); await getTtsVoice(parsedVoiceName);
} catch(error) { } catch (error) {
console.error(error) console.error(error)
valid = false; valid = false;
} }
@ -400,23 +400,23 @@ function onElevenlabsEnableClick() {
} }
function updateUiAudioPlayState() { function updateUiAudioPlayState() {
if (extension_settings.elevenlabstts.enabled == true){ if (extension_settings.elevenlabstts.enabled == true) {
audioControl.style.display = 'flex' audioControl.style.display = 'flex'
const img = !audioElement.paused? "fa-solid fa-circle-pause": "fa-solid fa-circle-play" const img = !audioElement.paused ? "fa-solid fa-circle-pause" : "fa-solid fa-circle-play"
audioControl.className = img audioControl.className = img
} else { } else {
audioControl.style.display = 'none' audioControl.style.display = 'none'
} }
} }
function onAudioControlClicked(){ function onAudioControlClicked() {
audioElement.paused? audioElement.play(): audioElement.pause() audioElement.paused ? audioElement.play() : audioElement.pause()
updateUiAudioPlayState() updateUiAudioPlayState()
} }
function addAudioControl() { function addAudioControl() {
$('#send_but_sheld').prepend('<div id="tts_media_control"/>') $('#send_but_sheld').prepend('<div id="tts_media_control"/>')
$('#send_but_sheld').on('click',onAudioControlClicked) $('#send_but_sheld').on('click', onAudioControlClicked)
audioControl = document.getElementById('tts_media_control'); audioControl = document.getElementById('tts_media_control');
updateUiAudioPlayState(); updateUiAudioPlayState();
} }