Created
September 25, 2023 15:19
-
-
Save thebwt/b5c662c52a26081f2dc4c9032bbfdb5f to your computer and use it in GitHub Desktop.
Elevenlabs foundry - voice actor storage and retrieval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if a token is selected | |
let selectedActor = game.actors.get(canvas.tokens.controlled[0]?.actor?.id); | |
if (!selectedActor) { | |
ui.notifications.warn("You must select a token first."); | |
return; | |
} | |
// Fetch the voice name from the custom flag, or prompt for a new one if not set | |
let voiceName = selectedActor.getFlag("world", "voiceName"); | |
if (!voiceName) { | |
let dialogContent = ` | |
<form> | |
<div class="form-group"> | |
<label for="voiceName">Set Voice Name:</label> | |
<input id="voiceName" name="voiceName" type="text"/> | |
</div> | |
</form> | |
`; | |
new Dialog({ | |
title: "Set Voice for Actor", | |
content: dialogContent, | |
buttons: { | |
set: { | |
label: "Set Voice", | |
callback: async (html) => { | |
voiceName = html.find("#voiceName")[0].value; | |
await selectedActor.setFlag("world", "voiceName", voiceName); | |
ui.notifications.info(`Voice for ${selectedActor.name} set to ${voiceName}`); | |
} | |
} | |
}, | |
default: "set", | |
close: () => { | |
if (voiceName) { | |
// If voiceName is set, proceed with the Sound Text Input | |
showSoundTextInput(); | |
} | |
} | |
}).render(true); | |
} else { | |
showSoundTextInput(); | |
} | |
function showSoundTextInput() { | |
// Create dialog content for Sound Text Input | |
let dialogContent = ` | |
<form> | |
<div class="form-group"> | |
<label for="soundText">Enter the sound text:</label> | |
<input id="soundText" name="soundText" type="text"/> | |
</div> | |
</form> | |
`; | |
// Create a dialog for Sound Text Input | |
new Dialog({ | |
title: "Sound Text Input", | |
content: dialogContent, | |
buttons: { | |
ok: { | |
label: "Submit", | |
callback: (html) => { | |
// Get the sound text from the dialog | |
const text = html.find("#soundText")[0].value; | |
// Use the voice name from the custom flag or the newly set voice | |
ui.chat.processMessage(`/playsound [${voiceName}] ${text}`); | |
// Make the actor "speak" in the chat | |
ChatMessage.create({ | |
speaker: { | |
alias: selectedActor.name, | |
actor: selectedActor.id | |
}, | |
content: text | |
}); | |
} | |
}, | |
cancel: { | |
label: "Cancel", | |
callback: () => console.log("Sound text input cancelled") | |
} | |
}, | |
default: "ok", | |
render: html => { | |
// Automatically focus on the text input | |
html.find("#soundText").focus(); | |
} | |
}).render(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment