OLD | NEW |
1 <p id="classSummary"> | 1 <p id="classSummary"> |
2 Use the <code>chrome.experimental.tts</code> module to play synthesized | 2 Use the <code>chrome.experimental.tts</code> module to play synthesized |
3 text-to-speech (TTS) from your extension or packaged app. | 3 text-to-speech (TTS) from your extension or packaged app. |
4 See also the related | 4 See also the related |
5 <a href="experimental.ttsEngine.html">experimental.ttsEngine</a> | 5 <a href="experimental.ttsEngine.html">experimental.ttsEngine</a> |
6 module which allows an extension to implement a speech engine. | 6 module, which allows an extension to implement a speech engine. |
7 </p> | 7 </p> |
8 | 8 |
9 <p class="note"><b>Give us feedback:</b> If you have suggestions, | 9 <p class="note"><b>Give us feedback:</b> If you have suggestions, |
10 especially changes that should be made before stabilizing the first | 10 especially changes that should be made before stabilizing the first |
11 version of this API, please send your ideas to the | 11 version of this API, please send your ideas to the |
12 <a href="http://groups.google.com/a/chromium.org/group/chromium-extensions">chro
mium-extensions</a> | 12 <a href="http://groups.google.com/a/chromium.org/group/chromium-extensions">chro
mium-extensions</a> |
13 group.</p> | 13 group.</p> |
14 | 14 |
15 <h2 id="overview">Overview</h2> | 15 <h2 id="overview">Overview</h2> |
16 | 16 |
(...skipping 21 matching lines...) Expand all Loading... |
38 such as its rate, pitch, and more. For example:</p> | 38 such as its rate, pitch, and more. For example:</p> |
39 | 39 |
40 <pre>chrome.experimental.tts.speak('Hello, world.', {'rate': 2.0});</pre> | 40 <pre>chrome.experimental.tts.speak('Hello, world.', {'rate': 2.0});</pre> |
41 | 41 |
42 <p>It's also a good idea to specify the language so that a synthesizer | 42 <p>It's also a good idea to specify the language so that a synthesizer |
43 supporting that language (and regional dialect, if applicable) is chosen.</p> | 43 supporting that language (and regional dialect, if applicable) is chosen.</p> |
44 | 44 |
45 <pre>chrome.experimental.tts.speak( | 45 <pre>chrome.experimental.tts.speak( |
46 'Hello, world.', {'lang': 'en-US', 'rate': 2.0});</pre> | 46 'Hello, world.', {'lang': 'en-US', 'rate': 2.0});</pre> |
47 | 47 |
48 <p>By default, each call to <code>speak()</code> will interrupt any | 48 <p>By default, each call to <code>speak()</code> interrupts any |
49 ongoing speech and speak immediately. To determine if a call would be | 49 ongoing speech and speaks immediately. To determine if a call would be |
50 interrupting anything, you can call <code>isSpeaking()</code>, or | 50 interrupting anything, you can call <code>isSpeaking()</code>. In |
51 you can use the <code>enqueue</code> option to cause this utterance to | 51 addition, you can use the <code>enqueue</code> option to cause this |
52 be added to a queue of utterances that will be spoken when the current | 52 utterance to be added to a queue of utterances that will be spoken |
53 utterance has finished. | 53 when the current utterance has finished.</p> |
54 | 54 |
55 <pre>chrome.experimental.tts.speak( | 55 <pre>chrome.experimental.tts.speak( |
56 'Speak this first.'); | 56 'Speak this first.'); |
57 chrome.experimental.tts.speak( | 57 chrome.experimental.tts.speak( |
58 'Speak this next, when the first sentence is done.', {'enqueue': true}); | 58 'Speak this next, when the first sentence is done.', {'enqueue': true}); |
59 </pre> | 59 </pre> |
60 | 60 |
61 <p>A complete description of all options can be found in the | 61 <p>A complete description of all options can be found in the |
62 <a href="#method-speak">speak() method documentation</a> below. | 62 <a href="#method-speak">speak() method documentation</a> below. |
63 Not all speech engines will support all options.</p> | 63 Not all speech engines will support all options.</p> |
64 | 64 |
65 <p>To catch errors and make sure you're calling <code>speak()</code> | 65 <p>To catch errors and make sure you're calling <code>speak()</code> |
66 correctly, pass a callback function that takes no arguments. Inside | 66 correctly, pass a callback function that takes no arguments. Inside |
67 the callback, check | 67 the callback, check |
68 <a href="extension.html#property-lastError">chrome.extension.lastError</a> | 68 <a href="extension.html#property-lastError">chrome.extension.lastError</a> |
69 to see if there were any errors.</p> | 69 to see if there were any errors.</p> |
70 | 70 |
71 <pre>chrome.experimental.tts.speak( | 71 <pre>chrome.experimental.tts.speak( |
72 utterance, | 72 utterance, |
73 options, | 73 options, |
74 function() { | 74 function() { |
75 if (chrome.extension.lastError) { | 75 if (chrome.extension.lastError) { |
76 console.log('Error: ' + chrome.extension.lastError.message); | 76 console.log('Error: ' + chrome.extension.lastError.message); |
77 } | 77 } |
78 });</pre> | 78 });</pre> |
79 | 79 |
80 <p>The callback returns right away, before the speech engine has started | 80 <p>The callback returns right away, before the engine has started |
81 generating speech. The purpose of the callback is to alert you to syntax | 81 generating speech. The purpose of the callback is to alert you to |
82 errors in your use of the TTS API, not all possible errors that might occur | 82 syntax errors in your use of the TTS API, not to catch all possible |
83 in the process of synthesizing and outputting speech. To catch these errors | 83 errors that might occur in the process of synthesizing and outputting |
84 too, you need to use an event listener, described below. | 84 speech. To catch these errors too, you need to use an event listener, |
| 85 described below.</p> |
85 | 86 |
86 <h2 id="events">Listening to events</h2> | 87 <h2 id="events">Listening to events</h2> |
87 | 88 |
88 <p>To get more real-time information about the status of synthesized speech, | 89 <p>To get more real-time information about the status of synthesized speech, |
89 pass an event listener in the options to <code>speak()</code>, like this:</p> | 90 pass an event listener in the options to <code>speak()</code>, like this:</p> |
90 | 91 |
91 <pre>chrome.experimental.tts.speak( | 92 <pre>chrome.experimental.tts.speak( |
92 utterance, | 93 utterance, |
93 { | 94 { |
94 'onevent': function(event) { | 95 onEvent: function(event) { |
95 console.log('Event ' + event.type ' at position ' + event.charIndex); | 96 console.log('Event ' + event.type ' at position ' + event.charIndex); |
96 if (event.type == 'error') { | 97 if (event.type == 'error') { |
97 console.log('Error: ' + event.errorMessage); | 98 console.log('Error: ' + event.errorMessage); |
98 } | 99 } |
99 } | 100 } |
100 }, | 101 }, |
101 callback);</pre> | 102 callback);</pre> |
102 | 103 |
103 <p>Each event includes an event type, the character index of the current | 104 <p>Each event includes an event type, the character index of the current |
104 speech relative to the utterance, and for error events, an optional | 105 speech relative to the utterance, and for error events, an optional |
105 error message. The event types are:</p> | 106 error message. The event types are:</p> |
106 | 107 |
107 <ul> | 108 <ul> |
108 <li><code>'start'</code>: the engine has started speaking the utterance. | 109 <li><code>'start'</code>: The engine has started speaking the utterance. |
109 <li><code>'word'</code>: a word boundary was reached. Use | 110 <li><code>'word'</code>: A word boundary was reached. Use |
110 <code>event.charIndex</code> to determine the current speech | 111 <code>event.charIndex</code> to determine the current speech |
111 position. | 112 position. |
112 <li><code>'sentence'</code>: a sentence boundary was reached. Use | 113 <li><code>'sentence'</code>: A sentence boundary was reached. Use |
113 <code>event.charIndex</code> to determine the current speech | 114 <code>event.charIndex</code> to determine the current speech |
114 position. | 115 position. |
115 <li><code>'marker'</code>: an SSML marker was reached. Use | 116 <li><code>'marker'</code>: An SSML marker was reached. Use |
116 <code>event.charIndex</code> to determine the current speech | 117 <code>event.charIndex</code> to determine the current speech |
117 position. | 118 position. |
118 <li><code>'end'</code>: the engine has finished speaking the utterance. | 119 <li><code>'end'</code>: The engine has finished speaking the utterance. |
119 <li><code>'interrupted'</code>: this utterance was interrupted by another | 120 <li><code>'interrupted'</code>: This utterance was interrupted by another |
120 call to <code>speak()</code> or <code>stop()</code> and did not | 121 call to <code>speak()</code> or <code>stop()</code> and did not |
121 finish. | 122 finish. |
122 <li><code>'cancelled'</code>: this utterance was queued, but then | 123 <li><code>'cancelled'</code>: This utterance was queued, but then |
123 cancelled by another call to <code>speak()</code> or | 124 cancelled by another call to <code>speak()</code> or |
124 <code>stop()</code> and never began to speak at all. | 125 <code>stop()</code> and never began to speak at all. |
125 <li><code>'error'</code>: An engine-specific error occurred and | 126 <li><code>'error'</code>: An engine-specific error occurred and |
126 this utterance cannot be spoken. | 127 this utterance cannot be spoken. |
127 Check <code>event.errorMessage</code> for details. | 128 Check <code>event.errorMessage</code> for details. |
128 </ul> | 129 </ul> |
129 | 130 |
130 <p>Four of the event types, <code>'end'</code>, <code>'interrupted'</code>, | 131 <p>Four of the event types—<code>'end'</code>, <code>'interrupted'</code>, |
131 <code>'cancelled'</code>, and <code>'error'</code>, are <i>final</i>. After | 132 <code>'cancelled'</code>, and <code>'error'</code>—are <i>final</i>. |
132 one of those events is received, this utterance will no longer speak and | 133 After one of those events is received, this utterance will no longer |
133 no new events from this utterance will be received.</p> | 134 speak and no new events from this utterance will be received.</p> |
134 | 135 |
135 <p>Some TTS engines may not support all event types, and some may not even | 136 <p>Some voices may not support all event types, and some voices may not |
136 support any events at all. To require that the speech engine used sends | 137 send any events at all. If you do not want to use a voice unless it sends |
137 the events you're interested in, you can pass a list of event types in | 138 certain events, pass the events you require in the |
138 the <code>requiredEventTypes</code> member of the options object, or use | 139 <code>requiredEventTypes</code> member of the options object, or use |
139 <code>getVoices</code> to choose a voice that has the events you need. | 140 <code>getVoices()</code> to choose a voice that meets your requirements. |
140 Both are documented below. | 141 Both are documented below.</p> |
141 | 142 |
142 <h2 id="ssml">SSML markup</h2> | 143 <h2 id="ssml">SSML markup</h2> |
143 | 144 |
144 <p>Utterances used in this API may include markup using the | 145 <p>Utterances used in this API may include markup using the |
145 <a href="http://www.w3.org/TR/speech-synthesis">Speech Synthesis Markup | 146 <a href="http://www.w3.org/TR/speech-synthesis">Speech Synthesis Markup |
146 Language (SSML)</a>. If you use SSML, the first argument to | 147 Language (SSML)</a>. If you use SSML, the first argument to |
147 <code>speak()</code> should be a complete SSML document with an XML | 148 <code>speak()</code> should be a complete SSML document with an XML |
148 header and a top-level <code><speak></code> tag, not a document | 149 header and a top-level <code><speak></code> tag, not a document |
149 fragment. | 150 fragment.</p> |
150 | 151 |
151 For example: | 152 <p>For example:</p> |
152 | 153 |
153 <pre>chrome.experimental.tts.speak( | 154 <pre>chrome.experimental.tts.speak( |
154 '<?xml version="1.0"?>' + | 155 '<?xml version="1.0"?>' + |
155 '<speak>' + | 156 '<speak>' + |
156 ' The <emphasis>second</emphasis> ' + | 157 ' The <emphasis>second</emphasis> ' + |
157 ' word of this sentence was emphasized.' + | 158 ' word of this sentence was emphasized.' + |
158 '</speak>');</pre> | 159 '</speak>');</pre> |
159 | 160 |
160 <p>Not all speech engines will support all SSML tags, and some may not support | 161 <p>Not all speech engines will support all SSML tags, and some may not support |
161 SSML at all, but all engines are required to ignore any SSML they don't | 162 SSML at all, but all engines are required to ignore any SSML they don't |
162 support and still speak the underlying text.</p> | 163 support and to still speak the underlying text.</p> |
163 | 164 |
164 <h2 id="choosing_voice">Choosing a voice</h2> | 165 <h2 id="choosing_voice">Choosing a voice</h2> |
165 | 166 |
166 <p>By default, Chrome will choose the most appropriate voice for each | 167 <p>By default, Chrome chooses the most appropriate voice for each |
167 utterance you want to speak, based on the language and gender. On most | 168 utterance you want to speak, based on the language and gender. On most |
168 Windows, Mac OS X, and Chrome OS systems, speech synthesis provided by | 169 Windows, Mac OS X, and Chrome OS systems, speech synthesis provided by |
169 the operating system should be able to speak any text in at least one | 170 the operating system should be able to speak any text in at least one |
170 language. Some users may have a variety of voices available, though, | 171 language. Some users may have a variety of voices available, though, |
171 from their operating system and from speech engines implemented by other | 172 from their operating system and from speech engines implemented by other |
172 Chrome extensions. In those cases, you can implement custom code to choose | 173 Chrome extensions. In those cases, you can implement custom code to choose |
173 the appropriate voice, or present the user with a list of choices.</p> | 174 the appropriate voice, or to present the user with a list of choices.</p> |
174 | 175 |
175 <p>To get a list of all voices, call <code>getVoices()</code> and pass it | 176 <p>To get a list of all voices, call <code>getVoices()</code> and pass it |
176 a function that receives an array of <code>TtsVoice</code> objects as its | 177 a function that receives an array of <code>TtsVoice</code> objects as its |
177 argument:</p> | 178 argument:</p> |
178 | 179 |
179 <pre>chrome.experimental.tts.getVoices( | 180 <pre>chrome.experimental.tts.getVoices( |
180 function(voices) { | 181 function(voices) { |
181 for (var i = 0; i < voices.length; i++) { | 182 for (var i = 0; i < voices.length; i++) { |
182 console.log('Voice ' + i + ':'); | 183 console.log('Voice ' + i + ':'); |
183 console.log(' name: ' + voices[i].voiceName); | 184 console.log(' name: ' + voices[i].voiceName); |
184 console.log(' lang: ' + voices[i].lang); | 185 console.log(' lang: ' + voices[i].lang); |
185 console.log(' gender: ' + voices[i].gender); | 186 console.log(' gender: ' + voices[i].gender); |
186 console.log(' extension id: ' + voices[i].extensionId); | 187 console.log(' extension id: ' + voices[i].extensionId); |
187 console.log(' event types: ' + voices[i].eventTypes); | 188 console.log(' event types: ' + voices[i].eventTypes); |
188 } | 189 } |
189 });</pre> | 190 });</pre> |
OLD | NEW |