Created
September 28, 2016 10:23
-
-
Save sunnygleason/f4cf9eadf7a27048151082e1952d13aa to your computer and use it in GitHub Desktop.
AngularJS Speech Recognition to PubNub Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="https://cdn.pubnub.com/pubnub-3.15.1.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> | |
<script src="https://cdn.pubnub.com/sdk/pubnub-angular/pubnub-angular-3.2.1.min.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!</pre> | |
<h3>MySpeech to Text</h3> | |
<input type="button" ng-click="dictateIt()" value="Dictate a message!" /> | |
<br /><br /> | |
<ul> | |
<li ng-repeat="message in messages track by $index"> | |
{{message.data}} | |
<a ng-click="sayIt(message.data)">(speak again)</a> | |
</li> | |
</ul> | |
</div> | |
<script> | |
angular.module('PubNubAngularApp', ["pubnub.angular.service"]) | |
.controller('MySpeechCtrl', function($rootScope, $scope, Pubnub) { | |
$scope.messages = [{data:"testing 1 2 3"}]; | |
$scope.msgChannel = 'MySpeech'; | |
if (!$rootScope.initialized) { | |
Pubnub.init({ | |
publish_key: 'YOUR_PUB_KEY', | |
subscribe_key: 'YOUR_SUB_KEY', | |
ssl:true | |
}); | |
$rootScope.initialized = true; | |
} | |
var msgCallback = function(payload) { | |
$scope.$apply(function() { | |
$scope.messages.push(payload); | |
}); | |
$scope.sayIt(payload.data); | |
}; | |
Pubnub.subscribe({ channel: [$scope.msgChannel, $scope.prsChannel], message: msgCallback }); | |
$scope.sayIt = function (theText) { | |
window.speechSynthesis.speak(new SpeechSynthesisUtterance(theText)); | |
}; | |
$scope.dictateIt = function () { | |
var theText = ""; | |
var recognition = new webkitSpeechRecognition(); | |
recognition.onresult = function (event) { | |
for (var i = event.resultIndex; i < event.results.length; i++) { | |
if (event.results[i].isFinal) { | |
theText += event.results[i][0].transcript; | |
} | |
} | |
Pubnub.publish({ | |
channel: $scope.msgChannel, | |
message: {data:theText} | |
}); | |
}; | |
recognition.start(); | |
}; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment