Skip to content

Commit

Permalink
You no longer need to call annyang.init() manually (but you can). You…
Browse files Browse the repository at this point in the history
… can now simply start() and addCommands() as often as you like, and let annyang worry about initialization. Bumped version to 1.1.0 - Fully backwards compatible with 1.0.0
  • Loading branch information
TalAter committed Jan 7, 2014
1 parent 7361af1 commit 39b10f4
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 33 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ Demo

Usage
-----
It's as easy as adding [one javascript file](//cdnjs.cloudflare.com/ajax/libs/annyang/1.0.0/annyang.min.js) to your document, and defining the commands you want.
It's as easy as adding [one javascript file](//cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js) to your document, and defining the commands you want.
````html
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/1.0.0/annyang.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js"></script>
<script>
if (annyang) {
// Let's define a command.
var commands = {
'show tps report': function() { $('#tpsreport').show(); }
};
// Initialize annyang with our commands
annyang.init(commands);
// Add our commands to annyang
annyang.addCommands(commands);
// Start listening.
annyang.start();
Expand Down
41 changes: 30 additions & 11 deletions annyang.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! annyang
//! version : 1.0.0
//! version : 1.1.0
//! author : Tal Ater @TalAter
//! license : MIT
//! https://www.TalAter.com/annyang/
Expand All @@ -23,9 +23,8 @@
return undefined;
}

var commandsList;
var commandsList = [];
var recognition;
var lang = 'en-US';
var callbacks = { start: [], error: [], end: [], result: [], resultMatch: [], resultNoMatch: [], errorNetwork: [], errorPermissionBlocked: [], errorPermissionDenied: [] };
var autoRestart;
var lastStartedAt = 0;
Expand Down Expand Up @@ -56,11 +55,24 @@
});
};

var initIfNeeded = function() {
if (recognition === undefined) {
root.annyang.init({}, false);
}
};

root.annyang = {
// Initialize annyang with a list of commands to recognize.
// e.g. annyang.init({'hello :name': helloFunction})
// annyang understands commands with named variables, splats, and optional words.
init: function(commands) {
init: function(commands, resetCommands) {

// resetCommands defaults to true
if (resetCommands === undefined) {
resetCommands = true;
} else {
resetCommands = !!resetCommands;
}

// Abort previous instances of recognition already running
if (recognition && recognition.abort) {
Expand All @@ -74,7 +86,7 @@
recognition.maxAlternatives = 5;
recognition.continuous = true;
// Sets the language to the default 'en-US'. This can be changed with annyang.setLanguage()
recognition.lang = lang;
recognition.lang = 'en-US';

recognition.onstart = function() { invokeCallbacks(callbacks.start); };

Expand Down Expand Up @@ -147,15 +159,20 @@
};

// build commands list
commandsList = [];
this.addCommands(commands);
if (resetCommands) {
commandsList = [];
}
if (commands.length) {
this.addCommands(commands);
}
},

// Start listening (asking for permission first, if needed).
// Call this after you've initialized annyang with commands.
// Receives an optional options object:
// { autoRestart: true }
start: function(options) {
initIfNeeded();
options = options || {};
if (options.autoRestart !== undefined) {
autoRestart = !!options.autoRestart;
Expand All @@ -168,6 +185,7 @@

// abort the listening session (aka stop)
abort: function() {
initIfNeeded();
autoRestart = false;
recognition.abort();
},
Expand All @@ -184,16 +202,17 @@
// Set the language the user will speak in. If not called, defaults to 'en-US'.
// e.g. 'fr-FR' (French-France), 'es-CR' (Español-Costa Rica)
setLanguage: function(language) {
lang = language;
if (recognition && recognition.abort) {
recognition.lang = language;
}
initIfNeeded();
recognition.lang = language;
},

// Add additional commands that annyang will respond to. Similar in syntax to annyang.init()
addCommands: function(commands) {
var cb,
command;

initIfNeeded();

for (var phrase in commands) {
if (commands.hasOwnProperty(phrase)) {
cb = root[commands[phrase]] || commands[phrase];
Expand Down
4 changes: 2 additions & 2 deletions annyang.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "annyang",
"version": "1.0.0",
"version": "1.1.0",
"description": "A javascript library for adding voice commands to your site, using speech recognition",
"main": "annyang.js",
"keywords": [
Expand Down
12 changes: 6 additions & 6 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta property="og:title" content="annyang! Easily add speech recognition to your site"/>
<meta property="og:url" content="https://www.talater.com/annyang/"/>
<meta property="og:site_name" content="annyang"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/1.0.0/annyang.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
"use strict";
Expand Down Expand Up @@ -74,8 +74,8 @@
// OPTIONAL: activate debug mode for detailed logging in the console
annyang.debug();

// Initialize annyang
annyang.init(commands);
// Add voice commands to respond to
annyang.addCommands(commands);

// OPTIONAL: Set a language for speech recognition (defaults to English)
annyang.setLanguage('en');
Expand Down Expand Up @@ -126,7 +126,7 @@ <h2>annyang supports multiple languages, has no dependencies, weighs just 2kb an
<section id="section_code_sample_1">
<p><em>How did you do that?</em></p>
<p>Simple. Here is all the code needed to achieve that:</p>
<pre><code>&lt;script src="<a href="//cdnjs.cloudflare.com/ajax/libs/annyang/1.0.0/annyang.min.js">//cdnjs.cloudflare.com/ajax/libs/annyang/1.0.0/annyang.min.js</a>"&gt;&lt;/script&gt;
<pre><code>&lt;script src="<a href="//cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js">//cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js</a>"&gt;&lt;/script&gt;
&lt;script&gt;
if (annyang) {
// Let's define our first command. First the text we expect, and then the function it should call
Expand All @@ -136,8 +136,8 @@ <h2>annyang supports multiple languages, has no dependencies, weighs just 2kb an
}
};

// Initialize annyang with our commands
annyang.init(commands);
// Add our commands to annyang
annyang.addCommands(commands);

// Start listening. You can call this here, or attach this call to an event, button, etc.
annyang.start();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "annyang",
"version": "1.0.0",
"version": "1.1.0",
"description": "A javascript library for adding voice commands to your site, using speech recognition",
"homepage": "https://www.talater.com/annyang/",
"main": "annyang.js",
Expand Down
2 changes: 1 addition & 1 deletion sites/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
}
};

annyang.init({
annyang.addCommands({
'back': goBack,
'go back': goBack,
'go home': stream,
Expand Down
6 changes: 3 additions & 3 deletions sites/facebook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sites/geektime.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
return false;
};

annyang.init({
annyang.addCommands({
'(דף) (ה)בית': gotoHome,
'(דף) הבא': nextPage,
'(דף) (ה)קודם': prevPage,
Expand Down
Loading

0 comments on commit 39b10f4

Please sign in to comment.