1.0.1 • Published 4 years ago

tts-player v1.0.1

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

tts player

description

a fast, simple, and cross-platform solution for play tts

Installation

npm i tts-player
yarn add tts-player

usage

  • init tts, for ios compatibility, please call tts.init() on a user interaction callback like onclick, normally, the ttsSampleRate is 8000 or 16000
import TTS from 'tts-player';
const tts = new TTS();

tts.init({
    ttsSampleRate: 8000
});
  • cache tts chunks, the ttsChunk must be pcm buffer which encoded with base64, normally, we get ttsChunk from other server, and in most cases, the ttsChunk is base64 encode for transportation comfortability
// the caching may take a while before it's ready to play
const ws = new WebSocket();
ws.onmessage = (data) => {
    if (typeof data === 'string') {
        const { ttsChunk, textId } = JSON.parse(data);
        tts.add(ttsChunk, textId);
    }
}
  • play 1 sentence, the return value should be a duration if tts is ready
const playTTSDuration = tts.play1Sentence(textId);
if (!playTTSDuration) throw new Error('tts is not ready !');
console.log(playTTSDuration); // 3.2
  • stop play 1 sentence
tts.stop();