Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert index.js to TypeScript #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="./sonus.png" alt="sonus" />
<img src="./resources/sonus.png" alt="sonus" />
</p>
<p align="center">
<a href="https://travis-ci.org/evancohen/sonus"><img src="https://api.travis-ci.org/evancohen/sonus.svg?branch=master" alt="Build Status"/></a>
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll need tslint instead now.

"ts-node": "^2.1.0",
"tslint": "^4.5.1",
"typescript": "^2.2.1"
}
}
File renamed without changes
File renamed without changes
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