-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode-red-contrib-snowboy.html
212 lines (199 loc) · 8.62 KB
/
node-red-contrib-snowboy.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<script type="text/javascript">
RED.nodes.registerType('Snowboy', {
category: 'input',
color: '#a6bbcf',
icon: "feed.png",
label: function() {
return this.name || "Snowboy";
},
defaults: {
name: { value: "" },
detectorFile: { value: "", required: true },
models: { value: [{}], required: true },
multipleOutput: { value: "false", required: true},
debug: { value: "false" }
},
inputs: 1,
outputs: 1,
outputLabels: function(index) {
var label = "";
for( var i = this.models.length - 1; i >= 0; i-- ) {
if( this.models[i].idxOutput <= index ) {
var hotwords = this.models[i].hotwords.split(',');
label = hotwords[index - this.models[i].idxOutput];
break;
}
}
return label;
},
oneditprepare: function() {
var node = this;
function resizeModel(model) {
var newWidth = model.width();
var hotwordsField = model.find(".node-input-model-hotwords-value");
var sensitivityField = model.find(".node-input-model-sensitivity-value");
var fileField = model.find(".node-input-model-file-value");
var fixedWidth = hotwordsField.width() + sensitivityField.width() + 90;
fileField.width(newWidth - fixedWidth);
}
function sortItems(models) {
var models = $("#node-input-model-container").editableList('items');
var multipleOutput = $("#node-input-multipleOutput").val();
if( multipleOutput == true || multipleOutput == "true" )
models.each(function(i) { $(this).find(".node-input-model-index").html(i+1); });
else
models.each(function(i) { $(this).find(".node-input-model-index").html(1); });
}
$("#node-input-model-container").css('min-height','250px').css('min-width','450px').editableList({
addItem: function(container,index,opt) {
if (!opt.hasOwnProperty('data')) {
opt.data = {};
}
var row = $('<div/>').appendTo(container);
var hotwordsField = $('<input/>',{placeholder:"Hotwords",required:true,class:"node-input-model-hotwords-value",type:"text",style:"width:100px; margin-left: 5px;"}).appendTo(row);
var sensitivityField = $('<input/>',{placeholder:"0-1",required:true,class:"node-input-model-sensitivity-value",type:"text",style:"width:35px; margin-left: 5px;"}).appendTo(row);
var fileField = $('<input/>',{placeholder:"*.pmdl, *.umdl",required:true,class:"node-input-model-file-value",type:"text",style:"width:220px; margin-left: 5px;"}).appendTo(row);
var finalspan = $('<span/>',{style:"float: right;margin-top: 6px;"}).appendTo(row);
if( node.multipleOutput == true || node.multipleOutput == "true" )
finalspan.append(' → <span class="node-input-model-index">'+(index+1)+'</span> ');
else
finalspan.append(' → <span class="node-input-model-index">1</span> ');
hotwordsField.val(opt.data.hotwords);
sensitivityField.val(opt.data.sensitivity);
fileField.val(opt.data.file);
},
removeItem: function(opt) {
if (opt.hasOwnProperty('index')) {
var removedList = $("#node-input-model-container").data('removedList')||[];
removedList.push(opt.index);
$("#node-input-model-container").data('removedList',removedList);
}
var models = $("#node-input-model-container").editableList('items');
models.each(function(i) { $(this).find(".node-input-model-index").html(i+1); });
},
resizeItem: resizeModel,
sortItems: sortItems,
sortable: true,
removable: true
});
$("#node-input-multipleOutput").change(sortItems);
for (var i=0;i<this.models.length;i++) {
var model = this.models[i];
$("#node-input-model-container").editableList('addItem',{data:model,index:i});
}
},
oneditsave: function() {
var items = $("#node-input-model-container").editableList('items');
var node = this;
var models = [];
var nbOutputs = 0;
var changedOutputs = {};
var shift = 0;
items.each(function(i) {
var item = $(this);
var itemData = $(this).data('data');
var hotwords = item.find(".node-input-model-hotwords-value").val();
var sensitivity = item.find(".node-input-model-sensitivity-value").val();
var file = item.find(".node-input-model-file-value").val();
var n = hotwords.split(',').length;
models.push({
'idxOutput': nbOutputs,
'nbOutputs': n,
'hotwords': hotwords,
'sensitivity': sensitivity,
'file': file
});
if( itemData.hasOwnProperty('index')) {
if( itemData.index !== i ) { // Item order moved so outputs are moved to the new position
for( var j = 0 ; j < node.models[itemData.index].nbOutputs; j++ ) {
changedOutputs[node.models[itemData.index].idxOutput + j] = nbOutputs + j;
}
} else if( shift ) { // Previous items changed their outputs so outputs are shifted
for( var j = node.models[itemData.index].idxOutput; j < node.models[itemData.index].idxOutput + node.models[itemData.index].nbOutputs; j++ ) {
changedOutputs[j] = j + shift;
}
}
if( models[i].nbOutputs < node.models[itemData.index].nbOutputs ) { // Less hotwords are declared
var diff = models[i].nbOutputs - node.models[itemData.index].nbOutputs;
var begin = node.models[itemData.index].idxOutput + node.models[itemData.index].nbOutputs;
for( var j = begin + diff; j < begin; j++ ) {
changedOutputs[j] = -1;
}
shift += diff;
} else if( models[i].nbOutputs > node.models[itemData.index].nbOutputs ) { // More hotwords are declared
var diff = models[i].nbOutputs - node.models[itemData.index].nbOutputs;
shift += diff;
}
}
nbOutputs += n;
});
var multipleOutput = $("#node-input-multipleOutput").val();
if( multipleOutput == true || multipleOutput == "true" ) {
var removedList = $("#node-input-model-container").data('removedList')||[];
removedList.forEach(function(i) {
for( var j = 0 ; j < node.models[i].nbOutputs; j++ ) {
changedOutputs[node.models[i].idxOutput + j] = -1;
}
});
} else {
for( var i = 1; i < node.outputs; i++ ) {
changedOutputs[i] = -1;
}
nbOutputs = 1;
}
node._outputs = changedOutputs;
node.outputs = nbOutputs;
node.models = models;
},
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-input-model-container-row)");
var height = size.height;
for (var i=0;i<rows.size();i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-model-container-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
$("#node-input-model-container").editableList('height',height);
}
});
</script>
<script type="text/x-red" data-template-name="Snowboy">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i>Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-detectorFile"><i class="icon-tag"></i>Detector file</label>
<input type="text" id="node-input-detectorFile" placeholder="*.res">
</div>
<div class="form-row node-input-model-container-row">
<label for="form-row node-input-model-container-row"><i class="icon-tag"></i>Models</label>
<ol id="node-input-model-container"></ol>
</div>
<div class="form-row">
<label for="node-input-multipleOutput"><i class="icon-tag"></i> Multiple output</label>
<select id="node-input-multipleOutput">
<option value=false>false</option>
<option value=true>true</option>
</select>
</div>
<div class="form-row">
<label for="node-input-debug"><i class="icon-tag"></i> debug</label>
<select id="node-input-debug">
<option value=false>false</option>
<option value=true>true</option>
</select>
</div>
</script>
<script type="text/x-red" data-help-name="Snowboy">
<p><h2>Snowboy</h2></p>
<p>This API listen for a hotword and then send a message</p>
<p><u>Input</u></p>
<ul>
<li><code>msg.payload</code>: Readable stream or Buffer</li>
</ul>
<p><u>Output</u></p>
<ul>
<li><code>msg.payload</code>: Hotword detected</li>
</ul>
</script>