Commit a332e791 authored by Hélène Spring's avatar Hélène Spring

Update text-to-speech function to include voice selection

parent 33404d67
......@@ -112,16 +112,26 @@ replacing `PUT` with a proper `http` directive.
If you’re running long simulations and want some sound notification when they’ve finished, [someone worked out how to do it](https://gist.github.com/parente/41a13f4c8fa6165d345cc7703be291a3). All you need is one handy function:
```
def speak(text):
def speak(text, voice=0):
'''
You can select a voice by putting in an integer. If you overshoot the length of the voice list, the default voice (0) is used.
'''
from IPython.display import Javascript as js, clear_output
# Escape single quotes
text = text.replace("'", r"\'")
display(js('''
display(
js(
f"""
if(window.speechSynthesis) {{
var synth = window.speechSynthesis;
synth.speak(new window.SpeechSynthesisUtterance('{text}'));
var voices = synth.getVoices();
var utterThis = new window.SpeechSynthesisUtterance('{text}');
utterThis.voice = voices[{voice}];
synth.speak(utterThis);
}}
'''.format(text=text)))
"""
)
)
# Clear the JS so that the notebook doesn't speak again when reopened/refreshed
clear_output(False)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment