Skip to content

Instantly share code, notes, and snippets.

@astagi
Created August 21, 2015 09:11
Show Gist options
  • Save astagi/284ab5bd048a21e7bf4f to your computer and use it in GitHub Desktop.
Save astagi/284ab5bd048a21e7bf4f to your computer and use it in GitHub Desktop.

Revisions

  1. astagi created this gist Aug 21, 2015.
    14 changes: 14 additions & 0 deletions appdemo.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    var app = angular.module('demo', ['textToSpeech']);
    app.controller('demoCtrl', function($scope, $timeout, textToSpeech) {
    textToSpeech.onVoiceReady(function(){
    textToSpeech.speak(
    'You worked 9 hours!!',
    'UK English Male',
    {pitch: 2}
    ).then(function() {
    alert('Finish!');
    }, function(err) {
    alert('Error! ' + err);
    });
    });
    });
    12 changes: 12 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!DOCTYPE html>
    <html ng-app="demo">
    <head>
    <meta charset="utf-8">
    <title>ng-nephila demo</title>
    <script src="lib/angular/angular.min.js"></script>
    <script src='http://code.responsivevoice.org/responsivevoice.js'></script>
    <script src="js/text-to-speech.js"></script>
    <script src="js/appdemo.js"></script>
    </head>
    <body ng-controller="demoCtrl"></body>
    </html>
    41 changes: 41 additions & 0 deletions text-to-speech.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    angular.module('textToSpeech', [])
    .factory('textToSpeech', ['$timeout', '$q', function($timeout, $q) {
    var ready = false;
    var readyCallback;
    responsiveVoice.OnVoiceReady = function() {
    if (ready === false) {
    if (readyCallback !== undefined) {
    readyCallback.call();
    }
    ready = true;
    }
    }
    return {
    isReady: function() {
    return ready;
    },
    onVoiceReady: function(callback) {
    readyCallback = callback;
    },
    speak: function(text, voice, options) {
    var q = $q.defer();
    if (voice === undefined) {
    voice = 'UK English Male';
    }
    if (options === undefined) {
    options = {};
    }
    options.onend = function() {
    q.resolve();
    }
    $timeout(function() {
    try {
    responsiveVoice.speak(text, voice, options);
    } catch (err) {
    q.reject(err);
    }
    }, 1);
    return q.promise;
    }
    }
    }]);