Skip to content

Commit

Permalink
Merge pull request #439 from peuter/speech
Browse files Browse the repository at this point in the history
add simple text to speech plugin
  • Loading branch information
ChristianMayer committed Oct 22, 2016
2 parents 7113f6c + 2fb5c63 commit ceb41d1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/lib/CometVisuClient.js
Expand Up @@ -175,6 +175,7 @@ define( ['jquery'], function( $ ) {
this.readResendHeaderValues();
session.update(data);
this.retryCounter = 0;
session.dataReceived = true;
}

if (self.running) { // keep the requests going
Expand Down Expand Up @@ -210,6 +211,7 @@ define( ['jquery'], function( $ ) {
if (json && !this.doRestart) {
this.readResendHeaderValues();
session.update(json.d);
session.dataReceived = true;
}
if (self.running) { // keep the requests going, but only
// request
Expand Down Expand Up @@ -394,6 +396,7 @@ define( ['jquery'], function( $ ) {
var data = json.d;
session.watchdog.ping();
session.update(data);
session.dataReceived = true;
};

/**
Expand Down Expand Up @@ -539,7 +542,8 @@ define( ['jquery'], function( $ ) {
callbackAfterLoggedIn : null,
context : null,
loginOnlyMode : false // login only for backend configuration, do not start address subscription
}
};
this.dataReceived = false; // needed to be able to check if the incoming update is the initial answer or a successing update

Object.defineProperty(this, 'backend', {
get: function () {
Expand Down Expand Up @@ -674,6 +678,7 @@ define( ['jquery'], function( $ ) {
if (json.c) {
self.backend = $.extend(self.backend, json.c); // assign itself to run setter
}
this.dataReceived = false;
if (this.loginSettings.loginOnly) {
this.currentTransport.handleSession(json, false);
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/TemplateEngine.js
Expand Up @@ -199,8 +199,9 @@ define([
var children = element.children;
if( children[0] )
updateFn.call( children[0], key, data );
else
console.log( element, children, type ); // DEBUG FIXME
else {
updateFn.call( element, key, data );
}
}
//console.log( element, type, updateFn );
} else if( typeof id === 'function' ) {
Expand Down
101 changes: 101 additions & 0 deletions src/plugins/speech/structure_plugin.js
@@ -0,0 +1,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);
}
});

});
14 changes: 14 additions & 0 deletions src/visu_config.xsd
Expand Up @@ -472,6 +472,7 @@
<xsd:element name="gauge" type="gauge" />
<xsd:element name="calendarlist" type="calendarlist" />
<xsd:element name="infoaction" type="infoaction" />
<xsd:element name="speech" type="speech" />
</xsd:choice>
</xsd:group>

Expand Down Expand Up @@ -1753,4 +1754,17 @@
<xsd:element name="info" type="info" />
</xsd:choice>
</xsd:complexType>

<xsd:complexType name="speech">
<xsd:sequence>
<xsd:element name="address" type="address" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="lang" use="optional">
<xsd:annotation>
<xsd:documentation xml:lang="en">The language that should be used to speak the text.</xsd:documentation>
<xsd:documentation xml:lang="de">Die Stimme die benutzt werden soll, um den Text zu sprechen.</xsd:documentation>
</xsd:annotation>
</xsd:attribute>

</xsd:complexType>
</xsd:schema>

0 comments on commit ceb41d1

Please sign in to comment.