Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sunnygleason/5af7eae53d7204bfb197d797ff77cccc to your computer and use it in GitHub Desktop.

Select an option

Save sunnygleason/5af7eae53d7204bfb197d797ff77cccc to your computer and use it in GitHub Desktop.
PubNub Translation plus Text-to-Speech UI with IBM Watson
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.5.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://cdn.pubnub.com/sdk/pubnub-angular/pubnub-angular-4.0.2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
</head>
<body>
<div class="container" ng-app="PubNubAngularApp" ng-controller="MySpeechCtrl">
<pre>
NOTE: make sure to update the PubNub keys below with your keys,
and ensure that the translation/text-to-speech BLOCK is configured properly!
</pre>
<h3>MyText to Speech</h3>
<audio id="theAudio"></audio>
<input ng-model="toSend" />
<input type="button" ng-click="publish()" value="Send!" />
<br /><br />
<audio id="theAudio"></audio>
<ul>
<li ng-repeat="message in messages track by $index">
{{message.text_en}}
<br />
<a ng-click="sayIt(message.speech_en)">play audio</a>
<br />
{{message.text_es}}
<br />
<a ng-click="sayIt(message.speech_es)">play audio</a>
</li>
</ul>
</div>
<script>
angular.module('PubNubAngularApp', ["pubnub.angular.service"])
.controller('MySpeechCtrl', function($rootScope, $scope, Pubnub) {
$scope.messages = [];
$scope.msgChannel = 'translate-text-to-speech-chat';
if (!$rootScope.initialized) {
Pubnub.init({
publishKey: 'YOUR_PUB_KEY',
subscribeKey: 'YOUR_SUB_KEY',
ssl:true
});
$rootScope.initialized = true;
}
var msgCallback = function(payload) {
$scope.$apply(function() {
$scope.messages.push(payload);
});
$scope.sayIt(payload.speech_es);
};
$scope.publish = function() {
Pubnub.publish({
channel: $scope.msgChannel,
message: {text_en:$scope.toSend}
});
$scope.toSend = "";
};
Pubnub.subscribe({ channels: [$scope.msgChannel] });
Pubnub.addListener({ message: msgCallback });
$scope.sayIt = function (url) {
var audioElement = $("#theAudio")[0];
audioElement.src = url;
audioElement.play();
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment