Skip to content

Commit 9f8d3e5

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

File tree

11 files changed

+240
-174
lines changed

11 files changed

+240
-174
lines changed

.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: 3 additions & 6 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) {
@@ -201,6 +200,4 @@ annyang = {
201200

202201
parseResults(sentences);
203202
}
204-
};
205-
206-
module.exports = annyang
203+
};

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
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 .",
8+
"compile": "tsc",
9+
"example": "npm run compile && node examples/annyang-example.js"
810
},
911
"repository": {
1012
"type": "git",
@@ -32,6 +34,8 @@
3234
"stream": "0.0.2"
3335
},
3436
"devDependencies": {
35-
"eslint": "^3.7.0"
37+
"eslint": "^3.7.0",
38+
"ts-node": "^2.1.0",
39+
"typescript": "^2.2.1"
3640
}
3741
}

src/cloud-speech-recognizer.ts

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

0 commit comments

Comments
 (0)