Skip to content

Commit c92ecc3

Browse files
committed
Convert index.js to TypeScript
1 parent 8582fac commit c92ecc3

13 files changed

+263
-174
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
"rules": {
88
"no-console": 0
99
}
10-
}
10+
}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Distribution directory
2+
dist/
3+
4+
# Typescript typings files
5+
typings/
6+
17
# Logs
28
logs
39
*.log

examples/annyang-example.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const ROOT_DIR = __dirname + '/../'
4-
const Sonus = require(ROOT_DIR + 'index.js')
4+
const Sonus = require(ROOT_DIR + 'dist/src/sonus.js')
55
const speech = require('@google-cloud/speech')({
66
projectId: 'streaming-speech-sample',
77
keyFilename: ROOT_DIR + 'keyfile.json'
@@ -23,16 +23,16 @@ const commands = {
2323
}
2424
}
2525

26-
Sonus.annyang.addCommands(commands)
26+
sonus.annyang.addCommands(commands)
2727

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

3333
sonus.on('final-result', result => {
3434
console.log("Final", result)
3535
if (result.includes("stop")) {
36-
Sonus.stop()
36+
sonus.stop()
3737
}
3838
})

examples/example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

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

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

1919
sonus.on('hotword', (index, keyword) => console.log("!" + keyword))
@@ -23,6 +23,6 @@ sonus.on('partial-result', result => console.log("Partial", result))
2323
sonus.on('final-result', result => {
2424
console.log("Final", result)
2525
if (result.includes("stop")) {
26-
Sonus.stop()
26+
sonus.stop()
2727
}
2828
})

examples/trigger-example.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

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

1414
try{
15-
Sonus.trigger(sonus, 1)
15+
sonus.trigger(1)
1616
} catch (e) {
1717
console.log('Triggering Sonus before starting it will throw the following exception:', e)
1818
}
1919

20-
Sonus.start(sonus)
20+
sonus.start()
2121

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

@@ -28,15 +28,15 @@ sonus.on('error', (error) => console.log(error))
2828
sonus.on('final-result', result => {
2929
console.log("Final", result)
3030
if (result.includes("stop")) {
31-
Sonus.stop()
31+
sonus.stop()
3232
}
3333
})
3434

3535
try{
36-
Sonus.trigger(sonus, 2)
36+
sonus.trigger(2)
3737
} catch (e) {
3838
console.log('Triggering Sonus with an invalid index will throw the following error:', e)
3939
}
4040

4141
//Will use index 0 with a hotword of "triggered" and start streaming immedietly
42-
Sonus.trigger(sonus, 0, "some hotword")
42+
sonus.trigger(0, "some hotword")

index.js

Lines changed: 0 additions & 152 deletions
This file was deleted.

lib/annyang-core.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//! https://www.TalAter.com/annyang/
77
"use strict";
88

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

5453
const initIfNeeded = () => {
5554
if (!isInitialized()) {
56-
annyang.init({}, false);
55+
module.exports.annyang.init({}, false);
5756
}
5857
};
5958

@@ -97,7 +96,7 @@ const parseResults = function (results) {
9796
invokeCallbacks(callbacks.resultNoMatch, results);
9897
};
9998

100-
annyang = {
99+
module.exports.annyang = {
101100

102101
init: (commands, resetCommands) => {
103102
if (resetCommands === undefined) {
@@ -202,5 +201,3 @@ annyang = {
202201
parseResults(sentences);
203202
}
204203
};
205-
206-
module.exports = annyang

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"name": "sonus",
33
"version": "0.1.7",
44
"description": "Open source cross platform decentralized always-on speech recognition framework",
5-
"main": "index.js",
5+
"main": "dist/src/sonus.js",
66
"scripts": {
7-
"test": "eslint ."
7+
"test": "eslint . && tslint -c tslint.json src/**/*.ts",
8+
"build": "tsc",
9+
"example": "npm run build && node examples/example.js",
10+
"prepublish": "npm run build"
811
},
912
"repository": {
1013
"type": "git",
@@ -32,6 +35,9 @@
3235
"stream": "0.0.2"
3336
},
3437
"devDependencies": {
35-
"eslint": "^3.7.0"
38+
"eslint": "^3.7.0",
39+
"ts-node": "^2.1.0",
40+
"tslint": "^4.5.1",
41+
"typescript": "^2.2.1"
3642
}
3743
}

src/cloud-speech-recognizer.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Writable } from 'stream';
2+
3+
export class CloudSpeechRecognizer {
4+
private _listening: boolean;
5+
private _recognizer: any;
6+
private _stream: Writable;
7+
8+
constructor(recognizer) {
9+
this._recognizer = recognizer;
10+
this._stream = new Writable();
11+
this._listening = false;
12+
}
13+
14+
public startStreaming(options, audioStream) {
15+
if (this._listening) {
16+
return;
17+
}
18+
19+
this._listening = true;
20+
21+
const recognitionStream = this._recognizer.createRecognizeStream({
22+
config: {
23+
encoding: 'LINEAR16',
24+
sampleRate: 16000,
25+
languageCode: options.language,
26+
speechContext: options.speechContext || null
27+
},
28+
singleUtterance: true,
29+
interimResults: true,
30+
verbose: true
31+
});
32+
33+
recognitionStream.on('error', err => this._stream.emit('error', err));
34+
recognitionStream.on('data', data => {
35+
if (data) {
36+
this._stream.emit('data', data);
37+
if (data.endpointerType === 'END_OF_UTTERANCE') {
38+
this._listening = false;
39+
audioStream.unpipe(recognitionStream);
40+
}
41+
}
42+
});
43+
44+
audioStream.pipe(recognitionStream);
45+
}
46+
47+
public on(event, handler) {
48+
this._stream.on(event, handler);
49+
}
50+
}

0 commit comments

Comments
 (0)