GithubHelp home page GithubHelp logo

choo-tts's Introduction

choo-tts stability

npm version build status downloads js-standard-style

Simple text-to-speech in the browser for choo

Usage

var choo = require('choo')
var html = require('choo/html')

var app = choo()
app.use(require('choo-tts')())
app.route('/', mainView)
app.mount('body')

function mainView (state, emit) {
  return html`
    <body>
      <button onclick=${onclick}>Say something</button>
    </body>
  `

  function onclick () {
    // speak with default voice
    emit('tts:speak', 'Hello world')
  }
}

Events

tts:error | tts.events.ERROR

Emitted when there is an error. It will get the error thrown as argument.

tts:speak | tts.events.SPEAK

Emit this event to speak some text. Can get a string and speak it with the default settings, or can get an object with text, rate, pitch and volume properties, only text is mandatory (lang is set in the voice, and the voice to speak is set in the state.selectedVoice property or with the tts:set-voice event). If you pass an object you can set an additional id property that can be used to identify this speech for the speech events.

tts:pause | tts.events.PAUSE

Emit this event to pause an ongoing speak. If there is no speaking taking place, it will do nothing.

tts:resume | tts.events.RESUME

Emit this event to resume an ongoing speak. If there is no paused speaking, it will do nothing.

tts:cancel | tts.events.CANCEL

Emit this event to cancel all qeued speechs.

tts:voices-changed | tts.events.VOICES_CHANGED

Set this event to handle every change on the availaible voices.

tts:set-voice | tts.events.SET_VOICE

Set the voice you want passing a string with the name of the voice. Use this event if you are sure of the name of the voice and that the voice is present in the client, otherwise it will throw. If you are unsure of the name, directly select the voice object from the state.tts.voices array.

tts:speech-start | tts.events.SPEECH_START

Emitted when a speech starts. The argument object has two properties, the event itself and an id. This event will be fired with every speech, but you can set this for a specific speech, if you set an id property to the object passed to the speak event. For example,

function speech (state, emitter) {
  emitter.on('tts:speech-start', function ({ event }) {
    console.log('Speech started!')
  })
  emitter.on('tts:speech-start', function ({ event, id }) {
    if (id === 'my-speech') console.log('My speech just started!')
  })
}

// later in your view
function view (state, emit) {
  return html`<body>
    <button onclick="${regularSpeech}">regular speech</button>
    <button onclick="${customSpeech}">custom speech</button>
  </body>`

  function regularSpeech () {
    emit('tts:speak', 'Just some regular speaking')
  }
  function customSpeech () {
    emit('tts:speak', {
      text: 'This speech is really special!',
      id: 'my-speech'
    })
  }
}

tts:speech-boundary | tts.events.SPEECH_BOUNDARY

Emitted when a word or sentence boundary is reched during a speech. The argument object has two properties, the event itself and an id. This event will be fired with every speech, but you can set this for a specific speech, if you set an id property to the object passed to the speak event.

tts:speech-end | tts.events.SPEECH_END

Emitted when a speech finish being spoken. The argument object has two properties, the event itself and an id. This event will be fired with every speech, but you can set this for a specific speech, if you set an id property to the object passed to the speak event.

API

plugin = tts([opts])

The plugin accept an options object and return the plugin. The opts object can have the following properties:

  • voice: A string with the name of the voice to be selected as selectedVoice. if not set, or not found, will use the browser default voice.
  • rate: The speed of the utterance to be spoken, can be a value from 0.1 to 10, defaults to 1.
  • pitch: The pitch of the utterance to be spoken, can be a value from 0 to 2, defaults to 1.
  • volume: The volume of the utterance to be spoken, can be a value from 0 to 1, defaults to 0.5.

If everything goes right, then the plugin will populate a tts object to the state.

  • tts.state: Get the state of text-to-speech object. Can be any of the following strings: PAUSED, PENDING, SPEAKING or READY.
  • tts.voices: The list of availaible voices. Notice that this object isn't allways populated until the dom has loaded, so you should use it there and re render your app if you are using it in the body of your html, check the example for more info.
  • tts.selectedVoice: The actual voice to be used for the next speaking, unless you pass an object to the speak event.
  • tts.rate: The speed of the utterance to be spoken, can be a value from 0.1 to 10, defaults to 1.
  • tts.pitch: The pitch of the utterance to be spoken, can be a value from 0 to 2, defaults to 1.
  • tts.volume: The volume of the utterance to be spoken, can be a value from 0 to 1, defaults to 0.5.

License

MIT

choo-tts's People

Contributors

yerkopalma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

the-cc-dev

choo-tts's Issues

enable selectedVoice switch

hi, very neat example.
just a reminder to enable selectedVoice switch at onclick() uncommenting
state.tts.selectedVoice = state.tts.voices[document.querySelector('select').value]

  function onclick () {
    // speak with default voice
    state.tts.selectedVoice = state.tts.voices[document.querySelector('select').value]
    emit('tts:speak', document.getElementById('text').value)
  }

1.0.0

Version 1.0.0 should fix #5 and #2.
Also will try to include an experimental output option that will record the speech using https://github.com/guest271314/SpeechSynthesisRecorder

Having the speech as data (blob, buffer, stream, etc) would allow us to

  • Save to an audio file.
  • Use some sort of cache on any following tts:speak event.

event to select voice

I was hacking on a TTS project with a friend today, and it's been really fun! — got a bit of feedback tho:

Right now it's a bit awkward to select a new voice — it involves looking up properties, and finding the indexOf property. It'd be cool if we could emit an event to change the voice, and it'd return a boolean of something to signal if it was successful.

Thanks! 😁

Initialize as a constructor

Was thinking it might make sense to make this module be a constructor. I'd imagine there could be a thing where we can for example include a list of voices, that are checked for availability, and the first one in the list is picked. Just like with fonts. But it'd require the API to change, and include a constructor. Thoughts?

var voices = [ 'kyla', 'james', 'junior' ]
app.use(require('choo-tts')({ voice: voices })

Select lang

Right now, selecting a language can't be done directly. That's because the utterance object and the voice, both have a lang property and I'm not sure which one to set. Now, I could internally just set them both to the same value (I think I'll go this way), but I would like to know before the difference between them and/or what happen if I set them to different values, how would that sounds?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.