aural_style_util.js revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 A set of classes to support aural CSS.
7 */
8
9
10goog.provide('cvox.AuralProperty');
11goog.provide('cvox.AuralStyleConverter');
12goog.provide('cvox.AuralStyleUtil');
13
14// This seems the only way to lay out an enum and use it as a key.
15/**
16 * @enum {string}
17 */
18cvox.AuralProperty = {
19  VOLUME: 'VOLUME',
20  SPEAK: 'SPEAK',
21  PAUSE_BEFORE: 'PAUSE_BEFORE',
22  PAUSE_AFTER: 'PAUSE_AFTER',
23  PAUSE: 'PAUSE',
24  CUE_BEFORE: 'CUE_BEFORE',
25  CUE_AFTER: 'CUE_AFTER',
26  CUE: 'CUE',
27  PLAY_DURING: 'PLAY_DURING',
28  AZIMUTH: 'AZIMUTH',
29  ELEVATION: 'ELEVATION',
30  SPEECH_RATE: 'SPEECH_RATE',
31  VOICE_FAMILY: 'VOICE_FAMILY',
32  PITCH: 'PITCH',
33  PITCH_RANGE: 'PITCH_RANGE',
34  STRESS: 'STRESS',
35  RICHNESS: 'RICHNESS',
36  SPEAK_PUNCTUATION: 'SPEAK_PUNCTUATION',
37  SPEAK_NUMERIAL: 'SPEAK_NUMERIAL',
38  SPEAK_HEADER: 'SPEAK_HEADER',
39  NONE: 'NONE'
40};
41
42
43/* A series of conversion functions. */
44/**
45 * An identity conversion.
46 * @param {number} value The aural CSS value to convert.
47 * @return {number} The resulting tts property value.
48 */
49cvox.AuralStyleConverter.identity = function(value) {
50  return value;
51};
52
53
54/**
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
57 * or by ChromeVox.
58 * @type {Object.<cvox.AuralProperty, string>}
59 */
60cvox.AuralStyleConverter.propertyTable = {
61  VOLUME: 'volume',
62  SPEAK: 'no-op',
63  PAUSE_BEFORE: 'no-op',
64  PAUSE_AFTER: 'no-op',
65  PAUSE: 'no-op',
66  CUE_BEFORE: 'no-op',
67  CUE_AFTER: 'no-op',
68  CUE: 'no-op',
69  PLAY_DURING: 'no-op',
70  AZIMUTH: 'no-op',
71  ELEVATION: 'no-op',
72  SPEECH_RATE: 'relativeRate',
73  VOICE_FAMILY: 'no-op',
74  PITCH: 'relativePitch',
75  PITCH_RANGE: 'no-op',
76  STRESS: 'no-op',
77  RICHNESS: 'no-op',
78  SPEAK_PUNCTUATION: 'no-op',
79  SPEAK_NUMERIAL: 'no-op',
80  SPEAK_HEADER: 'no-op',
81  NONE: 'no-op'
82};
83
84
85/**
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(*)>}
90 */
91cvox.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
113};
114
115
116/**
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.
121 */
122cvox.AuralStyleConverter.convertRule = function(property, value) {
123  return {
124    property: cvox.AuralStyleConverter.propertyTable[property],
125    value: cvox.AuralStyleConverter.valueTable[property](value)
126  };
127};
128
129
130/**
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.
134 */
135cvox.AuralStyleConverter.convertStyle = function(style) {
136  var ttsProperties = {};
137  for (var property in style) {
138    var ttsProperty =
139        cvox.AuralStyleConverter.convertRule(property, style[property]);
140    ttsProperties[ttsProperty.property] = ttsProperty.value;
141  }
142  return ttsProperties;
143};
144
145
146/**
147 * Gets the aural style for a node.
148 * @param {Node} node The node.
149 * @return {Object} The aural style, converted to tts properties.
150*/
151cvox.AuralStyleUtil.getStyleForNode = function(node) {
152  var style = cvox.AuralStyleUtil.defaultStyles[node.tagName];
153  if (!style) {
154    return null;
155  }
156  return cvox.AuralStyleConverter.convertStyle(style);
157};
158
159
160/**
161 * A list of default aural styles.
162 */
163cvox.AuralStyleUtil.defaultStyles = {
164  'ARTICLE': {
165    PITCH: -0.1
166  },
167  'ASIDE': {
168    PITCH: -0.1
169  },
170  'FOOTER': {
171    PITCH: -0.1
172  },
173  'H1': {
174    PITCH: -0.3
175  },
176  'H2': {
177    PITCH: -0.25
178  },
179  'H3': {
180    PITCH: -0.2
181  },
182  'H4': {
183    PITCH: -0.15
184  },
185  'H5': {
186    PITCH: -0.1
187  },
188  'H6': {
189    PITCH: -0.05
190  },
191  'HEADER': {
192    PITCH: -0.1
193  },
194  'HGROUP': {
195    PITCH: -0.1
196  },
197  'MARK': {
198    PITCH: -0.1
199  },
200  'NAV': {
201    PITCH: -0.1
202  },
203  'SECTION': {
204    PITCH: -0.1
205  },
206  'TIME': {
207    PITCH: -0.1
208  }
209};
210