this post was submitted on 29 Jan 2025
1 points (100.0% liked)

Perchance - Create a Random Text Generator

529 readers
5 users here now

⚄︎ Perchance

This is a Lemmy Community for perchance.org, a platform for sharing and creating random text generators.

Feel free to ask for help, share your generators, and start friendly discussions at your leisure :)

This community is mainly for discussions between those who are building generators. For discussions about using generators, especially the popular AI ones, the community-led Casual Perchance forum is likely a more appropriate venue.

See this post for the Complete Guide to Posting Here on the Community!

Rules

1. Please follow the Lemmy.World instance rules.

2. Be kind and friendly.

  • Please be kind to others on this community (and also in general), and remember that for many people Perchance is their first experience with coding. We have members for whom English is not their first language, so please be take that into account too :)

3. Be thankful to those who try to help you.

  • If you ask a question and someone has made a effort to help you out, please remember to be thankful! Even if they don't manage to help you solve your problem - remember that they're spending time out of their day to try to help a stranger :)

4. Only post about stuff related to perchance.

  • Please only post about perchance related stuff like generators on it, bugs, and the site.

5. Refrain from requesting Prompts for the AI Tools.

  • We would like to ask to refrain from posting here needing help specifically with prompting/achieving certain results with the AI plugins (text-to-image-plugin and ai-text-plugin) e.g. "What is the good prompt for X?", "How to achieve X with Y generator?"
  • See Perchance AI FAQ for FAQ about the AI tools.
  • You can ask for help with prompting at the 'sister' community Casual Perchance, which is for more casual discussions.
  • We will still be helping/answering questions about the plugins as long as it is related to building generators with them.

6. Search through the Community Before Posting.

  • Please Search through the Community Posts here (and on Reddit) before posting to see if what you will post has similar post/already been posted.

founded 2 years ago
MODERATORS
 

I am trying to use my PlayHT API in order to add voice to my character. I am using the custom javascript section in advanced options in order to achieve this. I have a very rudimentary understanding of code so that combined with a bit of ChatGPT for help, I ended up with this:

async function getCustomPlayHTVoices() { const apiKey = "Removed for security"; // Your PlayHT API key const userId = "Removed for security"; // Your PlayHT User ID

try { const response = await fetch("https://api.play.ht/api/v2/cloned-voices", { headers: { "Authorization": Bearer ${apiKey}, "X-User-ID": userId } });

if (!response.ok) throw new Error("Failed to fetch PlayHT voices");

const voices = await response.json();
const customVoices = voices.filter(v => v.name.toLowerCase().includes("arlecchino"));

if (customVoices.length === 0) console.warn("Custom voice 'Arlecchino' not found.");

return customVoices;

} catch (error) { console.error(error); return []; } }

// Populate voice selection dropdown (async () => { const playHTVoices = await getCustomPlayHTVoices(); if (playHTVoices.length === 0) { document.body.innerHTML = "Error fetching PlayHT voices. Check your API key or voice availability."; return; }

const voiceOptions = playHTVoices .map(v => <option value="${v.id}">${v.name}</option>) .join("");

document.body.innerHTML = <p>Please choose a voice:</p> <select id="voiceSelect">${voiceOptions}</select> <br> <button onclick="setVoice()">Submit</button>;

window.chosenVoiceId = playHTVoices[0].id; // Default voice })();

function setVoice() { window.chosenVoiceId = document.getElementById("voiceSelect").value; oc.window.hide(); }

oc.window.show();

// Listen for streamed text and convert it to speech using PlayHT let sentence = ""; oc.thread.on("StreamingMessage", async function (data) { for await (let chunk of data.chunks) { sentence += chunk.text;

// Check for end of sentence OR limit to 200 characters to prevent buffering too much text
let endOfSentenceIndex = sentence.search(/[.!?]/);
if (endOfSentenceIndex !== -1 || sentence.length > 200) {
  let sentenceToSpeak = sentence.slice(0, endOfSentenceIndex + 1 || sentence.length);
  console.log("Speaking sentence:", sentenceToSpeak);
  await textToSpeech({ text: sentenceToSpeak, voiceId: window.chosenVoiceId });
  sentence = sentence.slice(endOfSentenceIndex + 1).trim(); // Reset for next sentence
}

} });

// Function to send text to PlayHT API and play the generated audio async function textToSpeech({ text, voiceId }) { const apiKey = "Removed for security"; // Your PlayHT API key const userId = "Removed for security"; // Your PlayHT User ID

try { const response = await fetch("https://api.play.ht/api/v2/tts/stream", { method: "POST", headers: { "Authorization": Bearer ${apiKey}, "X-User-ID": userId, "Content-Type": "application/json" }, body: JSON.stringify({ text: text, voice: voiceId, format: "mp3" }) });

if (!response.ok) throw new Error("Failed to fetch audio from PlayHT");

const result = await response.json();
console.log("PlayHT Response:", result);

if (!result.audioUrl) throw new Error("No audio URL returned from PlayHT");

return new Promise(resolve => {
  const audio = new Audio(result.audioUrl);
  audio.onended = resolve;
  audio.play();
});

} catch (error) { console.error(error); } }

I would also like to understand where I went wrong so please explain if you know the solution. Apologies as well, I can't get the code to be in a single sheet in preview. I guess that tells me a lot about my ability to code.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 3 days ago

Thank you for the help. One thing to note is window.chosenVoiceName = playHTVoices[0].id; I had to put it after the html body due to it not appearing if it was before, but otherwise this works perfectly. Thank you again.