Skip to content

Commit

Permalink
Convert index.js to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
pbalaram committed Mar 26, 2017
1 parent 8582fac commit c92ecc3
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"rules": {
"no-console": 0
}
}
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Distribution directory
dist/

# Typescript typings files
typings/

# Logs
logs
*.log
Expand Down
8 changes: 4 additions & 4 deletions examples/annyang-example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const ROOT_DIR = __dirname + '/../'
const Sonus = require(ROOT_DIR + 'index.js')
const Sonus = require(ROOT_DIR + 'dist/src/sonus.js')
const speech = require('@google-cloud/speech')({
projectId: 'streaming-speech-sample',
keyFilename: ROOT_DIR + 'keyfile.json'
Expand All @@ -23,16 +23,16 @@ const commands = {
}
}

Sonus.annyang.addCommands(commands)
sonus.annyang.addCommands(commands)

Sonus.start(sonus)
sonus.start();
console.log('Say "' + hotwords[0].hotword + '"...')
sonus.on('hotword', (index, keyword) => console.log("!" + keyword))
sonus.on('partial-result', result => console.log("Partial", result))

sonus.on('final-result', result => {
console.log("Final", result)
if (result.includes("stop")) {
Sonus.stop()
sonus.stop()
}
})
6 changes: 3 additions & 3 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const ROOT_DIR = __dirname + '/../'
const Sonus = require(ROOT_DIR + 'index.js')
const Sonus = require(ROOT_DIR + 'dist/src/sonus.js')
const speech = require('@google-cloud/speech')({
projectId: 'streaming-speech-sample',
keyFilename: ROOT_DIR + 'keyfile.json'
Expand All @@ -13,7 +13,7 @@ const language = "en-US"
//recordProgram can also be 'arecord' which works much better on the Pi and low power devices
const sonus = Sonus.init({ hotwords, language, recordProgram: "rec" }, speech)

Sonus.start(sonus)
sonus.start();
console.log('Say "' + hotwords[0].hotword + '"...')

sonus.on('hotword', (index, keyword) => console.log("!" + keyword))
Expand All @@ -23,6 +23,6 @@ sonus.on('partial-result', result => console.log("Partial", result))
sonus.on('final-result', result => {
console.log("Final", result)
if (result.includes("stop")) {
Sonus.stop()
sonus.stop()
}
})
12 changes: 6 additions & 6 deletions examples/trigger-example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const ROOT_DIR = __dirname + '/../'
const Sonus = require(ROOT_DIR + 'index.js')
const Sonus = require(ROOT_DIR + 'dist/src/sonus.js')
const speech = require('@google-cloud/speech')({
projectId: 'streaming-speech-sample',
keyFilename: ROOT_DIR + 'keyfile.json'
Expand All @@ -12,12 +12,12 @@ const language = "en-US"
const sonus = Sonus.init({ hotwords, language }, speech)

try{
Sonus.trigger(sonus, 1)
sonus.trigger(1)
} catch (e) {
console.log('Triggering Sonus before starting it will throw the following exception:', e)
}

Sonus.start(sonus)
sonus.start()

sonus.on('hotword', (index, keyword) => console.log("!" + keyword))

Expand All @@ -28,15 +28,15 @@ sonus.on('error', (error) => console.log(error))
sonus.on('final-result', result => {
console.log("Final", result)
if (result.includes("stop")) {
Sonus.stop()
sonus.stop()
}
})

try{
Sonus.trigger(sonus, 2)
sonus.trigger(2)
} catch (e) {
console.log('Triggering Sonus with an invalid index will throw the following error:', e)
}

//Will use index 0 with a hotword of "triggered" and start streaming immedietly
Sonus.trigger(sonus, 0, "some hotword")
sonus.trigger(0, "some hotword")
152 changes: 0 additions & 152 deletions index.js

This file was deleted.

7 changes: 2 additions & 5 deletions lib/annyang-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//! https://www.TalAter.com/annyang/
"use strict";

let annyang;
let commandsList = [];
const callbacks = { start: [], error: [], end: [], result: [], resultMatch: [], resultNoMatch: [], errorNetwork: [], errorPermissionBlocked: [], errorPermissionDenied: [] };
let recognition;
Expand Down Expand Up @@ -53,7 +52,7 @@ const logMessage = (text, extraParameters) => {

const initIfNeeded = () => {
if (!isInitialized()) {
annyang.init({}, false);
module.exports.annyang.init({}, false);
}
};

Expand Down Expand Up @@ -97,7 +96,7 @@ const parseResults = function (results) {
invokeCallbacks(callbacks.resultNoMatch, results);
};

annyang = {
module.exports.annyang = {

init: (commands, resetCommands) => {
if (resetCommands === undefined) {
Expand Down Expand Up @@ -202,5 +201,3 @@ annyang = {
parseResults(sentences);
}
};

module.exports = annyang
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"name": "sonus",
"version": "0.1.7",
"description": "Open source cross platform decentralized always-on speech recognition framework",
"main": "index.js",
"main": "dist/src/sonus.js",
"scripts": {
"test": "eslint ."
"test": "eslint . && tslint -c tslint.json src/**/*.ts",
"build": "tsc",
"example": "npm run build && node examples/example.js",
"prepublish": "npm run build"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -32,6 +35,9 @@
"stream": "0.0.2"
},
"devDependencies": {
"eslint": "^3.7.0"
"eslint": "^3.7.0",
"ts-node": "^2.1.0",
"tslint": "^4.5.1",
"typescript": "^2.2.1"
}
}
50 changes: 50 additions & 0 deletions src/cloud-speech-recognizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Writable } from 'stream';

export class CloudSpeechRecognizer {
private _listening: boolean;
private _recognizer: any;
private _stream: Writable;

constructor(recognizer) {
this._recognizer = recognizer;
this._stream = new Writable();
this._listening = false;
}

public startStreaming(options, audioStream) {
if (this._listening) {
return;
}

this._listening = true;

const recognitionStream = this._recognizer.createRecognizeStream({
config: {
encoding: 'LINEAR16',
sampleRate: 16000,
languageCode: options.language,
speechContext: options.speechContext || null
},
singleUtterance: true,
interimResults: true,
verbose: true
});

recognitionStream.on('error', err => this._stream.emit('error', err));
recognitionStream.on('data', data => {
if (data) {
this._stream.emit('data', data);
if (data.endpointerType === 'END_OF_UTTERANCE') {
this._listening = false;
audioStream.unpipe(recognitionStream);
}
}
});

audioStream.pipe(recognitionStream);
}

public on(event, handler) {
this._stream.on(event, handler);
}
}
Loading

0 comments on commit c92ecc3

Please sign in to comment.