@lls/lls-audio 0.0.70 → 0.0.72

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lls/lls-audio",
3
- "version": "0.0.70",
3
+ "version": "0.0.72",
4
4
  "main": "index.js",
5
5
  "devDependencies": {
6
6
  "@kadira/storybook": "2.21.0",
package/recorder/index.js CHANGED
@@ -115,6 +115,7 @@ var Recorder = function (_Component) {
115
115
  // Data
116
116
  _this.recordStart = null;
117
117
  _this.audioBuffer = [];
118
+ _this.audioStream = null;
118
119
 
119
120
  // Methods
120
121
  _this.init = _this.init.bind(_this);
@@ -127,6 +128,20 @@ var Recorder = function (_Component) {
127
128
  value: function componentDidMount() {
128
129
  this.init(this.props);
129
130
  }
131
+ }, {
132
+ key: 'componentWillUnmount',
133
+ value: function componentWillUnmount() {
134
+ if (this.audioStream.getTracks) {
135
+ var tracks = this.audioStream.getTracks();
136
+ tracks.forEach(function (track) {
137
+ return track.stop();
138
+ });
139
+ }
140
+
141
+ this.audioStream.stop && this.audioStream.stop();
142
+
143
+ this.speechEvents.stop();
144
+ }
130
145
  }, {
131
146
  key: 'init',
132
147
  value: function init(props) {
@@ -166,6 +181,7 @@ var Recorder = function (_Component) {
166
181
  onRecordComplete = _ref4.onRecordComplete;
167
182
 
168
183
  // Init worker that will do heavy calculation
184
+ this.audioStream = stream;
169
185
  this.worker = initWorker({ onMessage: function onMessage(e) {
170
186
  switch (e.data.command) {
171
187
  case 'send_buffers':
@@ -37,15 +37,15 @@ var onNotAvailable = function onNotAvailable() {
37
37
  var onError = function onError(e) {
38
38
  return console.log("onError", e);
39
39
  };
40
- var onSuccess = function onSuccess(res, attempts) {
41
- return console.log({ res: res, attempts: attempts });
40
+ var onSuccess = function onSuccess(res) {
41
+ return console.log("onSucces", res);
42
42
  };
43
- var onFailure = function onFailure(res, attempts) {
44
- return console.log({ res: res, attempts: attempts });
43
+ var onFailure = function onFailure(res) {
44
+ return console.log("onFailure", res);
45
45
  };
46
46
 
47
47
  var CONFIG = {
48
- API_URL: 'http://localhost:3004/speechtotext'
48
+ API_URL: 'https://api-dev.lelivrescolaire.fr/speechtotext'
49
49
 
50
50
  // Instanciate component with config
51
51
  };var WordDetectorComponent = (0, _wordDetector.createWordDetector)(CONFIG);
@@ -84,7 +84,6 @@ var WordDetectorExample = function (_Component) {
84
84
  onError: onError,
85
85
  onSuccess: onSuccess,
86
86
  onFailure: onFailure,
87
- record: true,
88
87
  onRecordComplete: onRecordComplete
89
88
  })
90
89
  );
@@ -73,8 +73,7 @@ var WordDetector = function (_Component) {
73
73
  var _this = _possibleConstructorReturn(this, (WordDetector.__proto__ || Object.getPrototypeOf(WordDetector)).call(this, props));
74
74
 
75
75
  _this.state = {
76
- attempts: 0,
77
- browserApiAvailable: true
76
+ attempts: 0
78
77
  };
79
78
  _this.onError = _this.onError.bind(_this);
80
79
  _this.onSuccess = _this.onSuccess.bind(_this);
@@ -86,23 +85,27 @@ var WordDetector = function (_Component) {
86
85
 
87
86
  _createClass(WordDetector, [{
88
87
  key: 'onSuccess',
89
- value: function onSuccess(res, attempts) {
88
+ value: function onSuccess(res, attempts, url) {
90
89
  var onSuccess = this.props.onSuccess;
91
90
 
92
91
  this.setState({
93
- attempts: attempts
92
+ attempts: 0
93
+ });
94
+ onSuccess({
95
+ res: res, attempts: attempts, url: url
94
96
  });
95
- onSuccess(res, attempts);
96
97
  }
97
98
  }, {
98
99
  key: 'onFailure',
99
- value: function onFailure(res, attempts) {
100
+ value: function onFailure(res, attempts, url) {
100
101
  var onFailure = this.props.onFailure;
101
102
 
102
103
  this.setState({
103
104
  attempts: attempts
104
105
  });
105
- onFailure(res, attempts);
106
+ onFailure({
107
+ res: res, attempts: attempts, url: url
108
+ });
106
109
  }
107
110
  }, {
108
111
  key: 'onError',
@@ -114,8 +117,10 @@ var WordDetector = function (_Component) {
114
117
  }, {
115
118
  key: 'onApiResult',
116
119
  value: function onApiResult(_ref) {
117
- var _ref$transcript = _ref.transcript,
118
- transcript = _ref$transcript === undefined ? '' : _ref$transcript;
120
+ var speechToText = _ref.speechToText,
121
+ url = _ref.url;
122
+
123
+ var transcript = speechToText && speechToText[0];
119
124
  var attempts = this.state.attempts;
120
125
  var _props = this.props,
121
126
  word = _props.word,
@@ -124,7 +129,7 @@ var WordDetector = function (_Component) {
124
129
  var incrementedAttempts = attempts + 1;
125
130
  var success = compare(word, transcript);
126
131
  var cb = success ? this.onSuccess : this.onFailure;
127
- cb(transcript, incrementedAttempts);
132
+ cb(transcript, incrementedAttempts, url);
128
133
  }
129
134
  }, {
130
135
  key: 'onRecordComplete',
@@ -133,46 +138,27 @@ var WordDetector = function (_Component) {
133
138
 
134
139
  //debug(wav)
135
140
  var _props2 = this.props,
136
- alwaysUseApi = _props2.alwaysUseApi,
137
141
  config = _props2.config,
138
142
  onRecordComplete = _props2.onRecordComplete;
139
143
  var API_URL = config.API_URL;
140
- var browserApiAvailable = this.state.browserApiAvailable;
141
144
 
142
145
 
143
- var useHtmlDetector = isSpeechRecognitionAvailable && !alwaysUseApi;
144
-
145
- // Upload to Speech to text Api
146
- if (!useHtmlDetector) {
147
- if (!API_URL) {
148
- return this.onError(new Error('Missing API_URL in CONF'));
149
- }
150
-
151
- sendFileToSpeechToTextApi(API_URL, wav).then(function (res) {
152
- _this2.onApiResult(res);
153
- }).catch(function (e) {
154
- _this2.onError(e);
155
- });
146
+ if (!API_URL) {
147
+ return this.onError(new Error('Missing API_URL in CONF'));
156
148
  }
157
149
 
150
+ sendFileToSpeechToTextApi(API_URL, wav).then(function (res) {
151
+ _this2.onApiResult(JSON.parse(res));
152
+ }).catch(function (e) {
153
+ _this2.onError(e);
154
+ });
155
+
158
156
  onRecordComplete(wav);
159
157
  }
160
158
  }, {
161
159
  key: 'render',
162
160
  value: function render() {
163
- var _props3 = this.props,
164
- record = _props3.record,
165
- word = _props3.word,
166
- compare = _props3.compare,
167
- alwaysUseApi = _props3.alwaysUseApi;
168
-
169
- var useHtmlDetector = isSpeechRecognitionAvailable && !alwaysUseApi;
170
- return _react2.default.createElement(
171
- 'div',
172
- null,
173
- useHtmlDetector ? _react2.default.createElement(_reactSpeechRecognizer.WordDetector, { word: word, compare: compare, onFailure: this.onFailure, onSuccess: this.onSuccess }) : _react2.default.createElement(_recorder2.default, { onError: this.onError, onRecordComplete: this.onRecordComplete }),
174
- useHtmlDetector && record && _react2.default.createElement(_recorder2.default, { onError: this.onError, onRecordComplete: this.onRecordComplete })
175
- );
161
+ return _react2.default.createElement(_recorder2.default, { onError: this.onError, onRecordComplete: this.onRecordComplete });
176
162
  }
177
163
  }]);
178
164
 
@@ -180,9 +166,7 @@ var WordDetector = function (_Component) {
180
166
  }(_react.Component);
181
167
 
182
168
  WordDetector.defaultProps = {
183
- alwaysUseApi: true,
184
169
  config: {},
185
- record: false,
186
170
  word: '',
187
171
  compare: defaultCompare,
188
172
  onRecordComplete: function onRecordComplete() {},