[feat] add tts

This commit is contained in:
kwaroran
2023-05-13 21:24:54 +09:00
parent 801a3d5f70
commit f7d7eb3ab9
6 changed files with 133 additions and 15 deletions

View File

@@ -181,6 +181,9 @@ export function setDatabase(data:Database){
if(checkNullish(data.showUnrecommended)){
data.showUnrecommended = false
}
if(checkNullish(data.elevenLabKey)){
data.elevenLabKey = ''
}
if(checkNullish(data.sdConfig)){
data.sdConfig = {
width:512,
@@ -262,6 +265,8 @@ export interface character{
creator?:string
character_version?:number
}
ttsMode?:string
ttsSpeech?:string
}
@@ -386,6 +391,7 @@ export interface Database{
requestmet: string
requestproxy: string
showUnrecommended:boolean
elevenLabKey:string
}

View File

@@ -10,6 +10,7 @@ import { requestChatData } from "./request";
import { stableDiff } from "./stableDiff";
import { processScript, processScriptFull } from "./scripts";
import { exampleMessage } from "./exampleMessages";
import { sayTTS } from "./tts";
export interface OpenAIChat{
role: 'system'|'user'|'assistant'
@@ -165,7 +166,7 @@ export async function sendChat(chatProcessIndex = -1):Promise<boolean> {
}).join('\n\n')) + db.maxResponse) + 150
let chats:OpenAIChat[] = exampleMessage(currentChar)
chats.push({
role: 'system',
content: '[Start a new chat]'
@@ -214,9 +215,6 @@ export async function sendChat(chatProcessIndex = -1):Promise<boolean> {
currentTokens += (await tokenize(systemMsg) + 1)
}
console.log(currentTokens)
console.log(maxContextTokens)
while(currentTokens > maxContextTokens){
if(chats.length <= 1){
alertError(language.errors.toomuchtoken)
@@ -228,8 +226,6 @@ export async function sendChat(chatProcessIndex = -1):Promise<boolean> {
chats.splice(0, 1)
}
console.log(currentTokens)
let bias:{[key:number]:number} = {}
for(let i=0;i<currentChar.bias.length;i++){
@@ -318,6 +314,7 @@ export async function sendChat(chatProcessIndex = -1):Promise<boolean> {
data: result,
saying: currentChar.chaId
})
await sayTTS(currentChar, result)
setDatabase(db)
}

View File

@@ -1,6 +1,71 @@
export async function sayTTS(text:string) {
const utterThis = new SpeechSynthesisUtterance(text);
const voices = speechSynthesis.getVoices();
utterThis.voice = voices[0]
speechSynthesis.speak(utterThis)
import { get } from "svelte/store";
import { alertError } from "../alert";
import { DataBase, type character } from "../database";
export async function sayTTS(character:character,text:string) {
let db = get(DataBase)
switch(character.ttsMode){
case "webspeech":{
if(speechSynthesis && SpeechSynthesisUtterance){
const utterThis = new SpeechSynthesisUtterance(text);
const voices = speechSynthesis.getVoices();
let voiceIndex = 0
for(let i=0;i<voices.length;i++){
if(voices[i].name === character.ttsSpeech){
voiceIndex = i
}
}
utterThis.voice = voices[voiceIndex]
speechSynthesis.speak(utterThis)
}
break
}
case "elevenlab": {
const audioContext = new AudioContext();
const da = await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${character.ttsSpeech}`, {
body: JSON.stringify({
text: text
}),
method: "POST",
headers: {
"Content-Type": "application/json",
'xi-api-key': db.elevenLabKey || undefined
}
})
if(da.status >= 200 && da.status < 300){
const audioBuffer = await audioContext.decodeAudioData(await da.arrayBuffer())
const sourceNode = audioContext.createBufferSource();
sourceNode.buffer = audioBuffer;
sourceNode.connect(audioContext.destination);
sourceNode.start();
}
else{
alertError(await da.text())
}
}
}
}
export function getWebSpeechTTSVoices() {
return speechSynthesis.getVoices().map(v => {
return v.name
})
}
export async function getElevenTTSVoices() {
let db = get(DataBase)
const data = await fetch('https://api.elevenlabs.io/v1/voices', {
headers: {
'xi-api-key': db.elevenLabKey || undefined
}
})
const res = await data.json()
console.log(res)
return res.voices
}