-
Notifications
You must be signed in to change notification settings - Fork 81
/
structure_plugin.js
101 lines (89 loc) · 3.18 KB
/
structure_plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* structure_plugin.js (c) 2010 by Christian Mayer [CometVisu at ChristianMayer dot de]
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/**
* Use the Web Speech API (https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API)
* to make text-to-speech service available. This plugin listens to a address and forwards the
* incoming data to the browser TTS engine (if the browser supports it)
*
* Example:
* <speech lang="de">
* <address transform="OH:string" mode="read">Speak</address>
* </speech>
*/
define( ['structure_custom' ], function( VisuDesign_Custom ) {
"use strict";
var lastSpeech = {};
/**
* This is a custom function that extends the available widgets.
* It's purpose is to change the design of the visu during runtime
* to demonstrate all available
*/
VisuDesign_Custom.prototype.addCreator("speech", {
create: function( element, path) {
if (!window.speechSynthesis) {
console.log("this browser does not support the Web Speech API");
return "";
}
var $e = $(element);
var address = templateEngine.design.makeAddressList($e, false, path);
templateEngine.widgetDataInsert( path, {
'language' : $e.attr('lang') ? $e.attr('lang').toLowerCase() : null,
'address' : address,
'type' : 'speech'
});
return "";
},
update: function(address, text) {
if (!templateEngine.visu.dataReceived) {
// first call -> skipping
console.log("skipping initial TTS for "+text);
return;
}
if (!text || text.length === 0) {
// nothing to say
return;
}
if (lastSpeech[address] == text) {
// do not repeat
console.log("skipping TTS because of repetition "+text);
return;
}
lastSpeech[address] = text;
var element = $(this);
var path = element.attr('id');
var data = templateEngine.widgetDataGet(path);
var synth = window.speechSynthesis;
// speak
var utterThis = new SpeechSynthesisUtterance(text);
var selectedVoice, defaultVoice;
var voices = synth.getVoices();
for (var i = 0, l = voices.length; i < l; i++) {
if (data.language && voices[i].lang.substr(0, 2).toLowerCase() === data.language) {
selectedVoice = voices[i];
}
if (voices[i].default) {
defaultVoice = voices[i];
}
}
if (!selectedVoice) {
selectedVoice = defaultVoice;
}
utterThis.voice = selectedVoice;
synth.speak(utterThis);
}
});
});