snowboy_ros
snowboy.cc
Go to the documentation of this file.
1 #include <nan.h>
2 #include <snowboy-detect.h>
3 #include <iostream>
4 
5 class SnowboyDetect : public Nan::ObjectWrap {
6  public:
7  static NAN_MODULE_INIT(Init);
8 
9  private:
10  explicit SnowboyDetect(const std::string& resource_filename,
11  const std::string& model_str);
13 
14  static NAN_METHOD(New);
15  static NAN_METHOD(Reset);
16  static NAN_METHOD(RunDetection);
17  static NAN_METHOD(SetSensitivity);
18  static NAN_METHOD(GetSensitivity);
19  static NAN_METHOD(SetAudioGain);
20  static NAN_METHOD(UpdateModel);
21  static NAN_METHOD(NumHotwords);
22  static NAN_METHOD(SampleRate);
23  static NAN_METHOD(NumChannels);
24  static NAN_METHOD(BitsPerSample);
25 
26  static Nan::Persistent<v8::Function> constructor;
27 
29 };
30 
31 Nan::Persistent<v8::Function> SnowboyDetect::constructor;
32 
33 SnowboyDetect::SnowboyDetect(const std::string& resource_filename,
34  const std::string& model_str) {
35  try {
36  this->detector = new snowboy::SnowboyDetect(resource_filename, model_str);
37  } catch (std::runtime_error e) {
38  Nan::ThrowError(e.what());
39  }
40 }
42  if (this->detector) {
43  delete this->detector;
44  }
45 }
46 
47 NAN_MODULE_INIT(SnowboyDetect::Init) {
48  v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
49  tpl->SetClassName(Nan::New("SnowboyDetect").ToLocalChecked());
50  tpl->InstanceTemplate()->SetInternalFieldCount(1);
51 
52  SetPrototypeMethod(tpl, "Reset", Reset);
53  SetPrototypeMethod(tpl, "RunDetection", RunDetection);
54  SetPrototypeMethod(tpl, "SetSensitivity", SetSensitivity);
55  SetPrototypeMethod(tpl, "GetSensitivity", GetSensitivity);
56  SetPrototypeMethod(tpl, "SetAudioGain", SetAudioGain);
57  SetPrototypeMethod(tpl, "UpdateModel", UpdateModel);
58  SetPrototypeMethod(tpl, "NumHotwords", NumHotwords);
59  SetPrototypeMethod(tpl, "SampleRate", SampleRate);
60  SetPrototypeMethod(tpl, "NumChannels", NumChannels);
61  SetPrototypeMethod(tpl, "BitsPerSample", BitsPerSample);
62 
63  constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
64  Nan::Set(target, Nan::New("SnowboyDetect").ToLocalChecked(),
65  Nan::GetFunction(tpl).ToLocalChecked());
66 }
67 
68 NAN_METHOD(SnowboyDetect::New) {
69  if (!info.IsConstructCall()) {
70  Nan::ThrowError("Cannot call constructor as function, you need to use "
71  "'new' keyword");
72  return;
73  } else if (!info[0]->IsString()) {
74  Nan::ThrowTypeError("resource must be a string");
75  return;
76  } else if (!info[1]->IsString()) {
77  Nan::ThrowTypeError("model must be a string");
78  return;
79  }
80 
81  Nan::MaybeLocal<v8::Object> resource = Nan::To<v8::Object>(info[0]);
82  Nan::MaybeLocal<v8::Object> model = Nan::To<v8::Object>(info[1]);
83  Nan::Utf8String resourceString(resource.ToLocalChecked());
84  Nan::Utf8String modelString(model.ToLocalChecked());
85  SnowboyDetect* obj = new SnowboyDetect(*resourceString, *modelString);
86  obj->Wrap(info.This());
87  info.GetReturnValue().Set(info.This());
88 }
89 
90 NAN_METHOD(SnowboyDetect::Reset) {
91  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
92  bool ret = ptr->detector->Reset();
93  info.GetReturnValue().Set(Nan::New(ret));
94 }
95 
96 NAN_METHOD(SnowboyDetect::RunDetection) {
97  if (!info[0]->IsObject()) {
98  Nan::ThrowTypeError("data must be a buffer");
99  return;
100  }
101 
102  Nan::MaybeLocal<v8::Object> buffer = Nan::To<v8::Object>(info[0]);
103  char* bufferData = node::Buffer::Data(buffer.ToLocalChecked());
104  size_t bufferLength = node::Buffer::Length(buffer.ToLocalChecked());
105 
106  std::string data(bufferData, bufferLength);
107 
108  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
109  int ret = ptr->detector->RunDetection(data);
110  info.GetReturnValue().Set(Nan::New(ret));
111 }
112 
113 NAN_METHOD(SnowboyDetect::SetSensitivity) {
114  if (!info[0]->IsString()) {
115  Nan::ThrowTypeError("sensitivity must be a string");
116  return;
117  }
118 
119  Nan::MaybeLocal<v8::Object> sensitivity = Nan::To<v8::Object>(info[0]);
120  Nan::Utf8String sensitivityString(sensitivity.ToLocalChecked());
121 
122  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
123  ptr->detector->SetSensitivity(*sensitivityString);
124 }
125 
126 NAN_METHOD(SnowboyDetect::GetSensitivity) {
127  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
129  info.GetReturnValue().Set(Nan::New(sensitivity).ToLocalChecked());
130 }
131 
132 NAN_METHOD(SnowboyDetect::SetAudioGain) {
133  if (!info[0]->IsNumber()) {
134  Nan::ThrowTypeError("gain must be a number");
135  return;
136  }
137 
138  Nan::MaybeLocal<v8::Number> gain = Nan::To<v8::Number>(info[0]);
139  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
140  ptr->detector->SetAudioGain(gain.ToLocalChecked()->Value());
141 }
142 
143 NAN_METHOD(SnowboyDetect::UpdateModel) {
144  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
145  ptr->detector->UpdateModel();
146 }
147 
148 NAN_METHOD(SnowboyDetect::NumHotwords) {
149  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
150  int numHotwords = ptr->detector->NumHotwords();
151  info.GetReturnValue().Set(Nan::New(numHotwords));
152 }
153 
154 NAN_METHOD(SnowboyDetect::SampleRate) {
155  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
156  int sampleRate = ptr->detector->SampleRate();
157  info.GetReturnValue().Set(Nan::New(sampleRate));
158 }
159 
160 NAN_METHOD(SnowboyDetect::NumChannels) {
161  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
162  int numChannels = ptr->detector->NumChannels();
163  info.GetReturnValue().Set(Nan::New(numChannels));
164 }
165 
166 NAN_METHOD(SnowboyDetect::BitsPerSample) {
167  SnowboyDetect* ptr = Nan::ObjectWrap::Unwrap<SnowboyDetect>(info.Holder());
168  int bitsPerSample = ptr->detector->BitsPerSample();
169  info.GetReturnValue().Set(Nan::New(bitsPerSample));
170 }
171 
172 
173 NODE_MODULE(SnowboyDetect, SnowboyDetect::Init)
snowboy-detect.h
SnowboyDetect::detector
snowboy::SnowboyDetect * detector
Definition: snowboy.cc:28
SnowboyDetect::NAN_METHOD
static NAN_METHOD(New)
std::string
snowboy::SnowboyDetect::SetAudioGain
void SetAudioGain(const float audio_gain)
snowboy::SnowboyDetect::NumHotwords
int NumHotwords() const
snowboy::SnowboyDetect::UpdateModel
void UpdateModel() const
demo.model
model
Definition: demo.py:22
SnowboyDetect::~SnowboyDetect
~SnowboyDetect()
Definition: snowboy.cc:41
snowboy::SnowboyDetect::Reset
bool Reset()
SnowboyDetect::NAN_MODULE_INIT
static NAN_MODULE_INIT(Init)
snowboy::SnowboyDetect::SampleRate
int SampleRate() const
NAN_METHOD
NAN_METHOD(SnowboyDetect::New)
Definition: snowboy.cc:68
iostream
snowboy::SnowboyDetect::RunDetection
int RunDetection(const std::string &data)
training_service.data
dictionary data
END OF MODIFY ##################.
Definition: training_service.py:31
SnowboyDetect
Definition: snowboy.cc:5
std::runtime_error
demo2.sensitivity
list sensitivity
Definition: demo2.py:29
snowboy::SnowboyDetect::SetSensitivity
void SetSensitivity(const std::string &sensitivity_str)
snowboy::SnowboyDetect::GetSensitivity
std::string GetSensitivity() const
snowboy::SnowboyDetect::NumChannels
int NumChannels() const
snowboy::SnowboyDetect
Definition: snowboy-detect.h:22
SnowboyDetect::constructor
static Nan::Persistent< v8::Function > constructor
Definition: snowboy.cc:26
NAN_MODULE_INIT
NAN_MODULE_INIT(SnowboyDetect::Init)
Definition: snowboy.cc:47
SnowboyDetect::SnowboyDetect
SnowboyDetect(const std::string &resource_filename, const std::string &model_str)
Definition: snowboy.cc:33
snowboy::SnowboyDetect::BitsPerSample
int BitsPerSample() const
std::runtime_error::what
T what(T... args)