-
Notifications
You must be signed in to change notification settings - Fork 511
/
App.tsx
329 lines (303 loc) · 8.29 KB
/
App.tsx
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// Copyright 2020-2023 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import {
PorcupineManager,
BuiltInKeywords,
PorcupineErrors,
} from '@picovoice/porcupine-react-native';
import {Picker} from '@react-native-picker/picker';
import {language, keywords} from './params.json';
type Props = {};
type State = {
currentKeyword: string | BuiltInKeywords;
keywordOptions: (string | BuiltInKeywords)[];
buttonText: string;
buttonDisabled: boolean;
isListening: boolean;
backgroundColour: string;
isError: boolean;
errorMessage: string | null;
};
export default class App extends Component<Props, State> {
_porcupineManager: PorcupineManager | undefined;
_detectionColour: string = '#00E5C3';
_defaultColour: string = '#F5FCFF';
_accessKey: string = '${YOUR_ACCESS_KEY_HERE}'; // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
constructor(props: Props) {
super(props);
const keywordOptions =
language === 'en' ? Object.values(BuiltInKeywords) : keywords;
this.state = {
keywordOptions: keywordOptions,
currentKeyword: keywordOptions[0],
buttonText: 'Start',
buttonDisabled: false,
isListening: false,
backgroundColour: this._defaultColour,
isError: false,
errorMessage: null,
};
}
async componentDidMount() {
await this._loadNewKeyword(this.state.currentKeyword);
}
async componentWillUnmount() {
if (this.state.isListening) {
await this._stopProcessing();
}
this._porcupineManager?.delete();
}
async _startProcessing() {
this.setState({
buttonDisabled: true,
});
try {
await this._porcupineManager?.start();
this.setState({
buttonText: 'Stop',
buttonDisabled: false,
isListening: true,
});
} catch (e: any) {
this.setState({
isError: true,
errorMessage: e.message,
});
}
}
async _stopProcessing() {
this.setState({
buttonDisabled: true,
});
try {
await this._porcupineManager?.stop();
this.setState({
buttonText: 'Start',
buttonDisabled: false,
isListening: false,
});
} catch (e: any) {
this.setState({
isError: true,
errorMessage: e.message,
});
}
}
async _toggleListening() {
if (this.state.isListening) {
await this._stopProcessing();
} else {
await this._startProcessing();
}
}
async _loadNewKeyword(keyword: string | BuiltInKeywords) {
if (this.state.isListening) {
await this._stopProcessing();
}
this._porcupineManager?.delete();
this.setState({currentKeyword: keyword});
const detectionCallback = (keywordIndex: number) => {
if (keywordIndex >= 0) {
this.setState({
backgroundColour: this._detectionColour,
});
setTimeout(() => {
this.setState({
backgroundColour: this._defaultColour,
});
}, 1000);
}
};
const processErrorCallback = (error: PorcupineErrors.PorcupineError) => {
this._stopProcessing();
this.setState({
isError: true,
errorMessage: error.message,
});
};
try {
if (language === 'en') {
this._porcupineManager = await PorcupineManager.fromBuiltInKeywords(
this._accessKey,
[keyword as BuiltInKeywords],
detectionCallback,
processErrorCallback,
);
} else {
const keywordPath = `keywords/${keyword}_${Platform.OS}.ppn`;
const modelPath = `models/porcupine_params_${language}.pv`;
this._porcupineManager = await PorcupineManager.fromKeywordPaths(
this._accessKey,
[keywordPath],
detectionCallback,
processErrorCallback,
modelPath,
);
}
} catch (err: any) {
let errorMessage: string;
if (err instanceof PorcupineErrors.PorcupineInvalidArgumentError) {
errorMessage = `${err.message}`;
} else if (err instanceof PorcupineErrors.PorcupineActivationError) {
errorMessage = 'AccessKey activation error';
} else if (err instanceof PorcupineErrors.PorcupineActivationLimitError) {
errorMessage = 'AccessKey reached its device limit';
} else if (
err instanceof PorcupineErrors.PorcupineActivationRefusedError
) {
errorMessage = 'AccessKey refused';
} else if (
err instanceof PorcupineErrors.PorcupineActivationThrottledError
) {
errorMessage = 'AccessKey has been throttled';
} else {
errorMessage = err.toString();
}
this.setState({
isError: true,
errorMessage: errorMessage,
});
}
}
render() {
let keywordOptions = this.state.keywordOptions.map((s, i) => {
return <Picker.Item key={i} value={s} label={s} />;
});
return (
<View
style={[
styles.container,
{backgroundColor: this.state.backgroundColour},
]}>
<View style={styles.statusBar}>
<Text style={styles.statusBarText}>Porcupine</Text>
</View>
<View style={styles.keyword}>
<Text style={styles.keywordText}>Keyword</Text>
<View style={styles.picker}>
<Picker
selectedValue={this.state.currentKeyword}
mode="dropdown"
style={{}}
itemStyle={styles.itemStyle}
onValueChange={(keyword) => this._loadNewKeyword(keyword)}>
{keywordOptions}
</Picker>
</View>
</View>
<View style={styles.button}>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => this._toggleListening()}
disabled={this.state.buttonDisabled || this.state.isError}>
<Text style={styles.buttonText}>{this.state.buttonText}</Text>
</TouchableOpacity>
</View>
{this.state.isError && (
<View style={styles.errorBox}>
<Text style={styles.errorText}>{this.state.errorMessage}</Text>
</View>
)}
<View style={styles.footerView}>
<Text style={styles.instructions}>
Made in Vancouver, Canada by Picovoice
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
backgroundColor: '#F5FCFF',
},
subContainer: {
flex: 1,
justifyContent: 'center',
},
statusBar: {
flex: 0.4,
backgroundColor: '#377DFF',
justifyContent: 'flex-end',
},
statusBarText: {
fontSize: 18,
color: 'white',
fontWeight: 'bold',
marginLeft: 15,
marginBottom: 15,
},
keyword: {
flex: 1,
paddingTop: '10%',
},
keywordText: {
color: '#666666',
marginLeft: 15,
marginBottom: 5,
},
picker: {
width: '90%',
height: '40%',
alignContent: 'center',
justifyContent: 'center',
alignSelf: 'center',
},
button: {
flex: 1,
justifyContent: 'center',
alignContent: 'center',
},
buttonStyle: {
width: '50%',
height: '50%',
alignSelf: 'center',
justifyContent: 'center',
backgroundColor: '#377DFF',
borderRadius: 100,
},
buttonText: {
fontSize: 30,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
},
itemStyle: {
fontWeight: 'bold',
fontSize: 20,
textAlign: 'center',
},
footerView: {
flex: 1,
justifyContent: 'flex-end',
paddingBottom: 25,
},
instructions: {
textAlign: 'center',
color: '#666666',
},
errorBox: {
backgroundColor: 'red',
borderRadius: 5,
margin: 20,
padding: 20,
textAlign: 'center',
},
errorText: {
color: 'white',
fontSize: 16,
},
});