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.
6 * @fileoverview A set of classes to support aural CSS.
10 goog.provide('cvox.AuralProperty');
11 goog.provide('cvox.AuralStyleConverter');
12 goog.provide('cvox.AuralStyleUtil');
14 // This seems the only way to lay out an enum and use it as a key.
18 cvox.AuralProperty = {
21 PAUSE_BEFORE: 'PAUSE_BEFORE',
22 PAUSE_AFTER: 'PAUSE_AFTER',
24 CUE_BEFORE: 'CUE_BEFORE',
25 CUE_AFTER: 'CUE_AFTER',
27 PLAY_DURING: 'PLAY_DURING',
29 ELEVATION: 'ELEVATION',
30 SPEECH_RATE: 'SPEECH_RATE',
31 VOICE_FAMILY: 'VOICE_FAMILY',
33 PITCH_RANGE: 'PITCH_RANGE',
36 SPEAK_PUNCTUATION: 'SPEAK_PUNCTUATION',
37 SPEAK_NUMERIAL: 'SPEAK_NUMERIAL',
38 SPEAK_HEADER: 'SPEAK_HEADER',
43 /* A series of conversion functions. */
45 * An identity conversion.
46 * @param {number} value The aural CSS value to convert.
47 * @return {number} The resulting tts property value.
49 cvox.AuralStyleConverter.identity = function(value) {
55 * Conversion from an aural style property to Chrome TTS property.
56 * TODO(dtseng): no-op's below need to be supported by the extension API itself
58 * @type {Object<cvox.AuralProperty, string>}
60 cvox.AuralStyleConverter.propertyTable = {
63 PAUSE_BEFORE: 'no-op',
72 SPEECH_RATE: 'relativeRate',
73 VOICE_FAMILY: 'no-op',
74 PITCH: 'relativePitch',
78 SPEAK_PUNCTUATION: 'no-op',
79 SPEAK_NUMERIAL: 'no-op',
80 SPEAK_HEADER: 'no-op',
86 * Conversion from an aural style value to Chrome TTS value.
87 * TODO(dtseng): Conversion of aural CSS values is incomplete; everything is an
88 * identity conversion at the moment.
89 * @type {Object<cvox.AuralProperty, function(*)>}
91 cvox.AuralStyleConverter.valueTable = {
92 VOLUME: cvox.AuralStyleConverter.identity,
93 SPEAK: cvox.AuralStyleConverter.identity,
94 PAUSE_BEFORE: cvox.AuralStyleConverter.identity,
95 PAUSE_AFTER: cvox.AuralStyleConverter.identity,
96 PAUSE: cvox.AuralStyleConverter.identity,
97 CUE_BEFORE: cvox.AuralStyleConverter.identity,
98 CUE_AFTER: cvox.AuralStyleConverter.identity,
99 CUE: cvox.AuralStyleConverter.identity,
100 PLAY_DURING: cvox.AuralStyleConverter.identity,
101 AZIMUTH: cvox.AuralStyleConverter.identity,
102 ELEVATION: cvox.AuralStyleConverter.identity,
103 SPEECH_RATE: cvox.AuralStyleConverter.identity,
104 VOICE_FAMILY: cvox.AuralStyleConverter.identity,
105 PITCH: cvox.AuralStyleConverter.identity,
106 PITCH_RANGE: cvox.AuralStyleConverter.identity,
107 STRESS: cvox.AuralStyleConverter.identity,
108 RICHNESS: cvox.AuralStyleConverter.identity,
109 SPEAK_PUNCTUATION: cvox.AuralStyleConverter.identity,
110 SPEAK_NUMERIAL: cvox.AuralStyleConverter.identity,
111 SPEAK_HEADER: cvox.AuralStyleConverter.identity,
112 NONE: cvox.AuralStyleConverter.identity
117 * Converts a given aural property/value rule to a tts property/value.
118 * @param {cvox.AuralProperty} property The property.
119 * @param {*} value The CSS-based value.
120 * @return {Object} An object holding tts property and value.
122 cvox.AuralStyleConverter.convertRule = function(property, value) {
124 property: cvox.AuralStyleConverter.propertyTable[property],
125 value: cvox.AuralStyleConverter.valueTable[property](value)
131 * Converts an aural CSS style block to a TTS property object.
132 * @param {Object<cvox.AuralProperty, *>} style The style.
133 * @return {Object} The tts property object.
135 cvox.AuralStyleConverter.convertStyle = function(style) {
136 var ttsProperties = {};
137 for (var property in style) {
139 cvox.AuralStyleConverter.convertRule(property, style[property]);
140 ttsProperties[ttsProperty.property] = ttsProperty.value;
142 return ttsProperties;
147 * Gets the aural style for a node.
148 * @param {Node} node The node.
149 * @return {Object} The aural style, converted to tts properties.
151 cvox.AuralStyleUtil.getStyleForNode = function(node) {
152 var style = cvox.AuralStyleUtil.defaultStyles[node.tagName];
156 return cvox.AuralStyleConverter.convertStyle(style);
161 * A list of default aural styles.
163 cvox.AuralStyleUtil.defaultStyles = {