@@ -1769,6 +1769,12 @@ more clearly how screen readers interact with the accessibility tree, our
17691769discussion of screen reader support will instead include a minimal
17701770screen reader integrated directly into the browser.
17711771
1772+ [^why-diff] : Screen readers need to help the user with operating
1773+ system actions such as logging in, starting applications, and
1774+ switching between them, so it makes sense for the screen reader to
1775+ be outside any application and to integrate with them through the
1776+ operating system.
1777+
17721778But should our built-in screen reader live in the `Browser` or each `Tab`?
17731779Modern browsers generally talk to screen readers from something like the
17741780` Browser` , so we'll do that too.^[And therefore the browser thread in our
@@ -1813,54 +1819,22 @@ browser thread, much like with the display list, to avoid a data race.
18131819[ch12-commit] : scheduling.html#committing-a-display-list
18141820
18151821Now that the tree is in the browser thread, let's implement the screen
1816- reader. We'll use two Python libraries to actually read text out loud :
1817- [`gtts`][gtts] (which wraps the Google [text-to-speech service][tts])
1818- and [`playsound`][playsound]. You can install them using `pip` :
1819-
1820- [^why-diff] : Screen readers need to help the user with operating
1821- system actions such as logging in, starting applications, and
1822- switching between them, so it makes sense for the screen reader to
1823- be outside any application and to integrate with them through the
1824- operating system.
1825-
1826- [gtts] : https://pypi.org/project/gTTS/
1827-
1828- [tts] : https://cloud.google.com/text-to-speech/docs/apis
1822+ reader. Normally screen readers read text aloud, often much faster
1823+ than a normal speaking pace. Let's skip that---it'd add all sorts of
1824+ complexities around audio mixing and playback---and just have the
1825+ screen reader print the text it's going to say to the screen.[^playsound]
18291826
1830- [playsound] : https://pypi.org/project/playsound/
1831-
1832- ` ` ` {.sh}
1833- python3 -m pip install gtts
1834- python3 -m pip install playsound
1835- ` ` `
1827+ [^playsound] : Older editions of this book implemented an actual speech
1828+ engine using the `playsound` and `gtts` libraries, but it was
1829+ perhaps too simple to be useful; see Exercise 14-11.
18361830
18371831You can use these libraries to convert text to an audio file, and then
18381832play it :
18391833
18401834` ` ` {.python}
1841- import os
1842- import gtts
1843- import playsound
1844-
1845- SPEECH_FILE = "/tmp/speech-fragment.mp3"
1846-
18471835def speak_text(text):
18481836 print("SPEAK:", text)
1849- tts = gtts.gTTS(text)
1850- tts.save(SPEECH_FILE)
1851- playsound.playsound(SPEECH_FILE)
1852- os.remove(SPEECH_FILE)
1853- ` ` `
1854-
1855- :: : {.quirk}
1856- You may need to adjust the `SPEECH_FILE` path to fit your system
1857- better. If you have trouble importing any of the libraries, you may
1858- need to consult the [`gtts`][gtts] or [`playsound`][playsound]
1859- documentation. If you can't get these libraries working, just delete
1860- everything in `speak_text` except the `print` statement. You won't
1861- hear things being spoken, but you can at least debug by watching the
1862- console output.
1863- :: :
1837+ ` ` `
18641838
18651839To start with, we'll want a key binding that turns the screen reader
18661840on and off. While real operating systems typically use more obscure
@@ -2724,3 +2698,13 @@ accessibility feature to web developers, plus it allows applying it only
27242698to designated HTML subtrees.
27252699
27262700[zoom-property] : https://developer.mozilla.org/en-US/docs/Web/CSS/zoom
2701+
2702+ 14-11 *Speaking out loud*. Add code to actually read web pages aloud.
2703+ The [`pyttsx`][pyttsx] library can speak text in "non-blocking" mode,
2704+ though it will require some integration with the browser event loop.
2705+ Make sure to interrupt speech when the user hovers over nodes or tabs
2706+ throughout the document. Implement the `aria-live` attribute so the
2707+ author can create either `assertive` alerts that interrupt current
2708+ speech or `polite` ones that don't.
2709+
2710+ [pyttsx] : https://pypi.org/project/pyttsx3/
0 commit comments