OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview Useful abstraction when speaking messages. | |
7 * | |
8 * Usage: | |
9 * $m('aria_role_link') | |
10 * .andPause() | |
11 * .andMessage('aria_role_forms') | |
12 * .speakFlush(); | |
13 * | |
14 */ | |
15 | |
16 goog.provide('cvox.SpokenMessages'); | |
17 | |
18 goog.require('cvox.AbstractTts'); | |
19 goog.require('cvox.ChromeVox'); | |
20 goog.require('cvox.SpokenMessage'); | |
21 | |
22 /** | |
23 * @type {Array} | |
24 */ | |
25 cvox.SpokenMessages.messages = []; | |
26 | |
27 /** | |
28 * Speaks the message chain and interrupts any on-going speech. | |
29 */ | |
30 cvox.SpokenMessages.speakFlush = function() { | |
31 cvox.SpokenMessages.speak(cvox.QueueMode.FLUSH); | |
32 }; | |
33 | |
34 /** | |
35 * Speaks the message chain after on-going speech finishes. | |
36 */ | |
37 cvox.SpokenMessages.speakQueued = function() { | |
38 cvox.SpokenMessages.speak(cvox.QueueMode.QUEUE); | |
39 }; | |
40 | |
41 /** | |
42 * Speak the message chain. | |
43 * @param {cvox.QueueMode} mode The speech queue mode. | |
44 */ | |
45 cvox.SpokenMessages.speak = function(mode) { | |
46 for (var i = 0; i < cvox.SpokenMessages.messages.length; ++i) { | |
47 var message = cvox.SpokenMessages.messages[i]; | |
48 | |
49 // An invalid message format. | |
50 if (!message || !message.id) | |
51 throw 'Invalid message received.'; | |
52 | |
53 var finalText = cvox.ChromeVox.msgs.getMsg.apply(cvox.ChromeVox.msgs, | |
54 message.id); | |
55 cvox.ChromeVox.tts.speak(finalText, mode, | |
56 cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT); | |
57 | |
58 // Always queue after the first message. | |
59 mode = cvox.QueueMode.QUEUE; | |
60 } | |
61 | |
62 cvox.SpokenMessages.messages = []; | |
63 }; | |
64 | |
65 /** | |
66 * The newest message. | |
67 * @return {cvox.SpokenMessage} The newest (current) message. | |
68 */ | |
69 cvox.SpokenMessages.currentMessage = function() { | |
70 if (cvox.SpokenMessages.messages.length == 0) | |
71 throw 'Invalid usage of SpokenMessages; start the chain using $m()'; | |
72 return cvox.SpokenMessages.messages[cvox.SpokenMessages.messages.length - 1]; | |
73 }; | |
74 | |
75 /** | |
76 * Adds a message. | |
77 * @param {string|Array} messageId The id of the message. | |
78 * @return {Object} This object, useful for chaining. | |
79 */ | |
80 cvox.SpokenMessages.andMessage = function(messageId) { | |
81 var newMessage = new cvox.SpokenMessage(); | |
82 newMessage.id = typeof(messageId) == 'string' ? [messageId] : messageId; | |
83 cvox.SpokenMessages.messages.push(newMessage); | |
84 return cvox.SpokenMessages; | |
85 }; | |
86 | |
87 /** | |
88 * Pauses after the message, with an appropriate marker. | |
89 * @return {Object} This object, useful for chaining. | |
90 */ | |
91 cvox.SpokenMessages.andPause = function() { | |
92 return cvox.SpokenMessages.andMessage('pause'); | |
93 }; | |
94 | |
95 /** | |
96 * Adds a message. | |
97 * @param {string|Array} messageId The id of the message. | |
98 * @return {Object} This object, useful for chaining. | |
99 */ | |
100 cvox.$m = cvox.SpokenMessages.andMessage; | |
OLD | NEW |