Skip to content

Commit

Permalink
using int for hotword trigger
Browse files Browse the repository at this point in the history
remove console.log
  • Loading branch information
evancohen committed Dec 18, 2016
1 parent 8dcd68b commit 8cb562d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
22 changes: 16 additions & 6 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,31 @@ Sonus.stop(sonus)
Note that after recognition is stopped it can not be started again without creating an enterly new sonus instance.

### Trigger keyword/hotword manually
You can manuall trigger a hotword by passing your initialized sonus object into `Sonus.trigger`
This will throw a `NOT_STARTED` exception if you have not started sonus when this is called.
You can manuall trigger a hotword by passing your initialized sonus object and an index into `Sonus.trigger`
The indexes of your hotwords are base 1 and are deturmined by the order in which the hotwords are passed into `Sonus.init`

**Exceptions**
- `NOT_STARTED` - will be thrown if you have not started sonus when this is called.
- `INVALID_INDEX` - will be thrown if you pass an invalid index.

**Example:**
``` javascript
Sonus.trigger(sonus)
Sonus.trigger(sonus, 1)
```
sonus will be triggered with a hotword index of `0` and a hotword of `"triggered"`
sonus will be triggered with a hotword index of `1`

While it's not officially supported, If you want to trigger a specific index/hotword you can call `trigger` directly on the initialized sonus object
You can also optionally specify an index of `0` and an arbitrary hotword that will be returned in the `hotword` event
**Example:**
``` javascript
sonus.trigger(0, 'hotword')
sonus.trigger(sonus, 0, 'some hotword')
```
sonus will be triggered with a hotword index of `1` and a hotword of `some hotword`

Passing a hotword with a valid index will override the hotword name and trigger that hotword
**Example:**
``` javascript
sonus.trigger(sonus, 1, 'override')
```
## Events
hotword
partial-result
Expand Down
10 changes: 8 additions & 2 deletions examples/trigger-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const language = "en-US"
const sonus = Sonus.init({ hotwords, language }, speech)

try{
Sonus.trigger(sonus)
Sonus.trigger(sonus, 1)
} catch (e) {
console.log('Triggering Sonus before starting it will throw the following exception:', e)
}
Expand All @@ -32,5 +32,11 @@ sonus.on('final-result', result => {
}
})

try{
Sonus.trigger(sonus, 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)
Sonus.trigger(sonus, 0, "some hotword")
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const stream = require('stream')
const {Detector, Models} = require('snowboy')

const ERROR = {
NOT_STARTED : "NOT_STARTED"
NOT_STARTED : "NOT_STARTED",
INVALID_INDEX : "INVALID_INDEX"
}

const CloudSpeechRecognizer = {}
Expand Down Expand Up @@ -104,8 +105,13 @@ Sonus.init = (options, recognizer) => {

sonus.trigger = (index, hotword) => {
if(sonus.started){
sonus.emit('hotword', index || "0", hotword || "triggered")
CloudSpeechRecognizer.startStreaming(opts, sonus.mic, csr)
try{
let triggerHotword = (index == 0)? hotword : models.lookup(index)
sonus.emit('hotword', index, triggerHotword)
CloudSpeechRecognizer.startStreaming(opts, sonus.mic, csr)
} catch (e) {
throw ERROR.INVALID_INDEX
}
} else {
throw ERROR.NOT_STARTED
}
Expand Down

0 comments on commit 8cb562d

Please sign in to comment.