cmusphinx / node-pocketsphinx

Pocketsphinx bindings for Node.JS
Other
242 stars 47 forks source link

Using Mic on Raspberry PI #30

Closed imomin closed 8 years ago

imomin commented 8 years ago

For those who would like to use the audio directly from mic. They can use npm microphone. (NOTE: I was having issue installing Lame v1.0.2, therefore, I installed latest (v1.2.4) and copied the source)

Usage would look something like.

var fs = require('fs');
var ps = require('./build/Release/PocketSphinx.node');
var sb = require('./build/Release/SphinxBase.node');
var mic = require('./microphone'); // OR require('microphone');
modeldir = "../pocketsphinx-5prealpha/model/en-us/";

var config = new ps.Decoder.defaultConfig();
config.setString("-hmm", modeldir + "en-us");
config.setString("-dict", modeldir + "cmudict-en-us.dict");
config.setString("-lm", modeldir + "en-us.lm.bin");

var decoder = new ps.Decoder(config);

mic.startCapture();
mic.audioStream.on('data', function(data) {
        decoder.startUtt();
        decoder.processRaw(data, false, false);
        decoder.endUtt();
        console.log(decoder.hyp())
});
//mic.stopCapture();
nshmyrev commented 8 years ago

Thanks for the suggestion. You probably should not start utterance every time you get data. So it should be something like

mic.startCapture();
decoder.startUtt();
mic.audioStream.on('data', function(data) {
        decoder.processRaw(data, false, false);
        console.log(decoder.hyp());
        if (decoder.hyp() == "something") {
                decoder.endUtt();
                mic.stopCapture();
        }
});
imomin commented 8 years ago

Ah... OK. Thanks!