@lls/lls-audio 0.0.64 → 0.0.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/stories/wordDetector.js +8 -17
- package/wordDetector/index.js +79 -31
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lls/lls-audio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.65",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@kadira/storybook": "2.21.0",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"conventional-changelog": "1.1.0",
|
|
16
16
|
"conventional-changelog-cli": "1.2.0",
|
|
17
17
|
"css-loader": "0.26.1",
|
|
18
|
-
"file-saver": "
|
|
18
|
+
"file-saver": "1.3.3",
|
|
19
19
|
"react": "15.3.1",
|
|
20
20
|
"react-dom": "15.3.1",
|
|
21
21
|
"release-it": "2.4.0",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"inline-worker": "1.1.0",
|
|
29
29
|
"is_js": "0.9.0",
|
|
30
30
|
"react-input-range": "0.9.3",
|
|
31
|
+
"react-speech-recognizer": "0.0.9",
|
|
31
32
|
"waveform-data": "2.0.0"
|
|
32
33
|
},
|
|
33
34
|
"peerDependencies": {
|
package/stories/wordDetector.js
CHANGED
|
@@ -16,12 +16,6 @@ var _player2 = _interopRequireDefault(_player);
|
|
|
16
16
|
|
|
17
17
|
var _wordDetector = require('../wordDetector');
|
|
18
18
|
|
|
19
|
-
var _wordDetector2 = _interopRequireDefault(_wordDetector);
|
|
20
|
-
|
|
21
|
-
var _Visualizer = require('../recorder/Visualizer');
|
|
22
|
-
|
|
23
|
-
var _Visualizer2 = _interopRequireDefault(_Visualizer);
|
|
24
|
-
|
|
25
19
|
var _recorder = require('../recorder');
|
|
26
20
|
|
|
27
21
|
var _recorder2 = _interopRequireDefault(_recorder);
|
|
@@ -44,6 +38,12 @@ var onFailure = function onFailure(res, attempts) {
|
|
|
44
38
|
return console.log({ res: res, attempts: attempts });
|
|
45
39
|
};
|
|
46
40
|
|
|
41
|
+
var CONFIG = {
|
|
42
|
+
API_URL: 'http://localhost:3004/speechtotext'
|
|
43
|
+
|
|
44
|
+
// Instanciate component with config
|
|
45
|
+
};var WordDetectorComponent = (0, _wordDetector.createWordDetector)(CONFIG);
|
|
46
|
+
|
|
47
47
|
var WordDetectorExample = function (_Component) {
|
|
48
48
|
_inherits(WordDetectorExample, _Component);
|
|
49
49
|
|
|
@@ -55,18 +55,10 @@ var WordDetectorExample = function (_Component) {
|
|
|
55
55
|
_this.state = {
|
|
56
56
|
volume: 0
|
|
57
57
|
};
|
|
58
|
-
_this.setVolume = _this.setVolume.bind(_this);
|
|
59
58
|
return _this;
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
_createClass(WordDetectorExample, [{
|
|
63
|
-
key: 'setVolume',
|
|
64
|
-
value: function setVolume(volume) {
|
|
65
|
-
this.setState({
|
|
66
|
-
volume: volume
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}, {
|
|
70
62
|
key: 'render',
|
|
71
63
|
value: function render() {
|
|
72
64
|
var volume = this.state.volume;
|
|
@@ -79,14 +71,13 @@ var WordDetectorExample = function (_Component) {
|
|
|
79
71
|
null,
|
|
80
72
|
'Say "Hello"'
|
|
81
73
|
),
|
|
82
|
-
_react2.default.createElement(
|
|
74
|
+
_react2.default.createElement(WordDetectorComponent, {
|
|
83
75
|
word: 'hello',
|
|
84
76
|
onVolumeChange: this.setVolume,
|
|
85
77
|
onError: onError,
|
|
86
78
|
onSuccess: onSuccess,
|
|
87
79
|
onFailure: onFailure
|
|
88
|
-
})
|
|
89
|
-
_react2.default.createElement(_Visualizer2.default, { volume: volume })
|
|
80
|
+
})
|
|
90
81
|
);
|
|
91
82
|
}
|
|
92
83
|
}]);
|
package/wordDetector/index.js
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.createWordDetector = undefined;
|
|
7
|
+
|
|
8
|
+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
|
6
9
|
|
|
7
10
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
8
11
|
|
|
@@ -22,6 +25,8 @@ var _fileSaver = require('file-saver');
|
|
|
22
25
|
|
|
23
26
|
var _fileSaver2 = _interopRequireDefault(_fileSaver);
|
|
24
27
|
|
|
28
|
+
var _reactSpeechRecognizer = require('react-speech-recognizer');
|
|
29
|
+
|
|
25
30
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
26
31
|
|
|
27
32
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
@@ -30,21 +35,19 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
|
|
|
30
35
|
|
|
31
36
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
32
37
|
|
|
33
|
-
var API_URL = 'http://localhost:3004/speechtotext';
|
|
34
38
|
var SIMILARITY_THRESHOLD = 0.8;
|
|
35
39
|
|
|
36
40
|
var debug = function debug(wav) {
|
|
37
41
|
_fileSaver2.default.saveAs(wav, 'test.wav');
|
|
38
42
|
};
|
|
39
43
|
|
|
40
|
-
var sendFileToSpeechToTextApi = function sendFileToSpeechToTextApi(wav) {
|
|
44
|
+
var sendFileToSpeechToTextApi = function sendFileToSpeechToTextApi(API_URL, wav) {
|
|
41
45
|
return new Promise(function (resolve, reject) {
|
|
42
46
|
var xhr = new XMLHttpRequest();
|
|
43
47
|
xhr.open('POST', API_URL, true);
|
|
44
48
|
|
|
45
49
|
xhr.onreadystatechange = function () {
|
|
46
50
|
if (xhr.readyState == 4 && xhr.status == 200) {
|
|
47
|
-
console.log('Speech to text: ', xhr.responseText);
|
|
48
51
|
resolve(xhr.responseText);
|
|
49
52
|
}
|
|
50
53
|
};
|
|
@@ -71,26 +74,46 @@ var WordDetector = function (_Component) {
|
|
|
71
74
|
var _this = _possibleConstructorReturn(this, (WordDetector.__proto__ || Object.getPrototypeOf(WordDetector)).call(this, props));
|
|
72
75
|
|
|
73
76
|
_this.state = {
|
|
74
|
-
attempts: 0
|
|
77
|
+
attempts: 0,
|
|
78
|
+
browserApiAvailable: true
|
|
75
79
|
};
|
|
76
80
|
_this.onError = _this.onError.bind(_this);
|
|
77
|
-
_this.
|
|
81
|
+
_this.onNotAvailable = _this.onNotAvailable.bind(_this);
|
|
82
|
+
_this.onSuccess = _this.onSuccess.bind(_this);
|
|
83
|
+
_this.onFailure = _this.onFailure.bind(_this);
|
|
84
|
+
_this.onApiResult = _this.onApiResult.bind(_this);
|
|
78
85
|
_this.onRecordComplete = _this.onRecordComplete.bind(_this);
|
|
79
86
|
return _this;
|
|
80
87
|
}
|
|
81
88
|
|
|
82
89
|
_createClass(WordDetector, [{
|
|
83
|
-
key: '
|
|
84
|
-
value: function
|
|
85
|
-
|
|
90
|
+
key: 'onNotAvailable',
|
|
91
|
+
value: function onNotAvailable() {
|
|
92
|
+
this.setState({
|
|
93
|
+
browserApiAvailable: false
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}, {
|
|
97
|
+
key: 'onSuccess',
|
|
98
|
+
value: function onSuccess(res, attempts) {
|
|
99
|
+
var _props$onSuccess = this.props.onSuccess,
|
|
100
|
+
onSuccess = _props$onSuccess === undefined ? function () {} : _props$onSuccess;
|
|
86
101
|
|
|
87
|
-
|
|
102
|
+
this.setState({
|
|
103
|
+
attempts: attempts
|
|
104
|
+
});
|
|
105
|
+
onSuccess(res, attempts);
|
|
106
|
+
}
|
|
107
|
+
}, {
|
|
108
|
+
key: 'onFailure',
|
|
109
|
+
value: function onFailure(res, attempts) {
|
|
110
|
+
var _props$onFailure = this.props.onFailure,
|
|
111
|
+
onFailure = _props$onFailure === undefined ? function () {} : _props$onFailure;
|
|
88
112
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}).catch(function (e) {
|
|
92
|
-
_this2.onError(e);
|
|
113
|
+
this.setState({
|
|
114
|
+
attempts: attempts
|
|
93
115
|
});
|
|
116
|
+
onFailure(res, attempts);
|
|
94
117
|
}
|
|
95
118
|
}, {
|
|
96
119
|
key: 'onError',
|
|
@@ -101,39 +124,64 @@ var WordDetector = function (_Component) {
|
|
|
101
124
|
onError(e);
|
|
102
125
|
}
|
|
103
126
|
}, {
|
|
104
|
-
key: '
|
|
105
|
-
value: function
|
|
106
|
-
var
|
|
127
|
+
key: 'onApiResult',
|
|
128
|
+
value: function onApiResult(_ref) {
|
|
129
|
+
var _ref$transcript = _ref.transcript,
|
|
130
|
+
transcript = _ref$transcript === undefined ? '' : _ref$transcript;
|
|
107
131
|
var attempts = this.state.attempts;
|
|
108
132
|
var _props = this.props,
|
|
109
133
|
word = _props.word,
|
|
110
|
-
_props$onSuccess = _props.onSuccess,
|
|
111
|
-
onSuccess = _props$onSuccess === undefined ? function () {} : _props$onSuccess,
|
|
112
|
-
_props$onFailure = _props.onFailure,
|
|
113
|
-
onFailure = _props$onFailure === undefined ? function () {} : _props$onFailure,
|
|
114
134
|
_props$compare = _props.compare,
|
|
115
135
|
compare = _props$compare === undefined ? defaultCompare : _props$compare;
|
|
116
136
|
|
|
117
137
|
var incrementedAttempts = attempts + 1;
|
|
118
|
-
var success = compare(word,
|
|
119
|
-
this.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
138
|
+
var success = compare(word, transcript);
|
|
139
|
+
var cb = success ? this.onSuccess : this.onFailure;
|
|
140
|
+
cb(transcript, incrementedAttempts);
|
|
141
|
+
}
|
|
142
|
+
}, {
|
|
143
|
+
key: 'onRecordComplete',
|
|
144
|
+
value: function onRecordComplete(wav) {
|
|
145
|
+
var _this2 = this;
|
|
146
|
+
|
|
147
|
+
//debug(wav)
|
|
148
|
+
var _props$config = this.props.config,
|
|
149
|
+
config = _props$config === undefined ? {} : _props$config;
|
|
150
|
+
var API_URL = config.API_URL;
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
if (!API_URL) {
|
|
154
|
+
return this.onError(new Error('Missing API_URL in CONF'));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
sendFileToSpeechToTextApi(API_URL, wav).then(function (res) {
|
|
158
|
+
_this2.onApiResult(res);
|
|
159
|
+
}).catch(function (e) {
|
|
160
|
+
_this2.onError(e);
|
|
124
161
|
});
|
|
125
162
|
}
|
|
126
163
|
}, {
|
|
127
164
|
key: 'render',
|
|
128
165
|
value: function render() {
|
|
129
|
-
var
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
166
|
+
var _props2 = this.props,
|
|
167
|
+
_props2$config = _props2.config,
|
|
168
|
+
config = _props2$config === undefined ? {} : _props2$config,
|
|
169
|
+
word = _props2.word,
|
|
170
|
+
_props2$compare = _props2.compare,
|
|
171
|
+
compare = _props2$compare === undefined ? defaultCompare : _props2$compare;
|
|
172
|
+
var API_URL = config.API_URL;
|
|
173
|
+
var browserApiAvailable = this.state.browserApiAvailable;
|
|
174
|
+
|
|
175
|
+
return browserApiAvailable ? _react2.default.createElement(_reactSpeechRecognizer.WordDetector, { word: word, compare: compare, onNotAvailable: this.onNotAvailable, onFailure: this.onFailure, onSuccess: this.onSuccess }) : API_URL ? _react2.default.createElement(_recorder2.default, { onError: this.onError, onRecordComplete: this.onRecordComplete }) : null;
|
|
133
176
|
}
|
|
134
177
|
}]);
|
|
135
178
|
|
|
136
179
|
return WordDetector;
|
|
137
180
|
}(_react.Component);
|
|
138
181
|
|
|
139
|
-
exports.default = WordDetector;
|
|
182
|
+
exports.default = WordDetector;
|
|
183
|
+
var createWordDetector = exports.createWordDetector = function createWordDetector(config) {
|
|
184
|
+
return function (props) {
|
|
185
|
+
return _react2.default.createElement(WordDetector, _extends({}, props, { config: config }));
|
|
186
|
+
};
|
|
187
|
+
};
|