Weilbyte / tiktok-tts

Generate TikTok Text-to-Speech voices in your browser
https://weilbyte.github.io/tiktok-tts/
MIT License
395 stars 84 forks source link

The character limit be more ? #26

Closed alexdo83 closed 1 year ago

alexdo83 commented 1 year ago

Can the character limit be more ? i'm seeing it limited to 300 characters? too short... maybe to limited? the "singing" part doesn't read all the content?

DevTest69 commented 1 year ago

First modify TEXT_BYTE_LIMIT with a big number or your desired character limit. Then replace the try statement from submitForm() with this:

try {
        const req = new XMLHttpRequest()
        var text_array=[];
        var decoded_audio;
        var final_audio;
        while (text.length > 0) {
            text_array.push(text.substring(0, 300));
            text = text.substring(300);
          }
        for (var i = 0; i < text_array.length; i++) {
            req.open('POST', `${ENDPOINT}/api/generation`, false)
            req.setRequestHeader('Content-Type', 'application/json')
            req.send(JSON.stringify({
                text: text_array[i],
                voice: voice
            }))

            let resp = JSON.parse(req.responseText)
            if (resp.data === null) {
                setError(`<b>Generation failed</b><br/> ("${resp.error}")`)
            } else {
                var decoded=atob(resp.data)
                decoded_audio=decoded_audio+decoded
            }  
          }
          final_audio=btoa(decoded_audio)
          setAudio(final_audio, text)
    }
Weilbyte commented 1 year ago

@anhdodotnet TikTok's API errors if the text is over 300 bytes. I have not tested the above code segment, but I suspect it will not split properly if you are using a multi-byte UTF-8 character.

DevTest69 commented 1 year ago

I made a function that splits the text into 300-word strings and then adds them into an array without breaking words, like in the previous response. I tested it and it works as expected.

Here is the function:

function splitIntoSubstrings(str, maxLength) {
    const substrings = [];
    const words = str.split(/([^\w])/);
    let currentSubstring = '';

    for (const word of words) {
      if (currentSubstring.length + word.length > maxLength) {
        substrings.push(currentSubstring);
        currentSubstring = '';
      }
      currentSubstring += word;
    }
    if (currentSubstring.length > 0) {
      substrings.push(currentSubstring);
    }
    return substrings;
  }

And here is how it should be used:

try {
        const req = new XMLHttpRequest()
        var text_array=splitIntoSubstrings(text,300);
        var decoded_audio;
        var final_audio;

        for (var i = 0; i < text_array.length; i++) {
            req.open('POST', `${ENDPOINT}/api/generation`, false)
            req.setRequestHeader('Content-Type', 'application/json')
            req.send(JSON.stringify({
                text: text_array[i],
                voice: voice
            }))

            let resp = JSON.parse(req.responseText)
            if (resp.data === null) {
                setError(`<b>Generation failed</b><br/> ("${resp.error}")`)
            } else {
                var decoded=atob(resp.data)
                decoded_audio=decoded_audio+decoded
            }  
          }
          console.log(text_array)
          final_audio=btoa(decoded_audio)
          setAudio(final_audio, text)
    }
alexdo83 commented 1 year ago

@DevTest69 I'm not a coder, can you edit the complete tools into tools? thanks