Text-to-Speech

andy@payne.org
Updated: March 11, 2016

This notebook shows how to use Chrome's built-in text-to-speech engine.

References:

Here is the basic form:


In [3]:
%%javascript
var msg = new SpeechSynthesisUtterance("I Python is fun!");
window.speechSynthesis.speak(msg);


Now, we can make a Python function that returns a spoken result:


In [4]:
from IPython.display import Javascript
import json

def speak(msg):
    return Javascript("""
            var msg = new SpeechSynthesisUtterance(%s);
            window.speechSynthesis.cancel();
            window.speechSynthesis.speak(msg);
        """ % json.dumps(msg))

For example, if you have a long-running notebook cell, you could finish it with:


In [6]:
import random
answer = int(random.random() * 100)
speak("Computation is complete.  The answer is: %d." % answer)


Out[6]:

In [ ]: