-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
283 lines (235 loc) · 6.81 KB
/
App.js
File metadata and controls
283 lines (235 loc) · 6.81 KB
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
import React, {useState, useEffect} from 'react';
import {StyleSheet, View, Button, Text} from 'react-native';
import {Buffer} from 'buffer';
import Permissions from 'react-native-permissions';
import OpenAudioRx from 'react-native-open-audio-rx';
import Sound from 'react-native-sound';
let sound = null;
//An alternative: '@react-native-community/audio-toolkit';
var RNFS = require('react-native-fs');
const ilog = console.log;
const elog = console.error;
const wlog = console.warn;
const maxDuration = 5; //Seconds
const sampleRate = 16000;
const numChannels = 1;
const byteDepth = 2;
const recordToFile = true;
const reportVolume = false;
const reportFrameData = false;
const packetSize = byteDepth * numChannels;
const options = {
sampleRate,
numChannels,
byteDepth,
maxDuration, //Seconds
recordToFile,
reportVolume,
reportFrameData,
};
function App() {
[receiving, setReceiving] = useState(false);
[playing, setPlaying] = useState(false);
[loaded, setLoaded] = useState(false);
[filePath, setFilePath] = useState(null);
[displayVolume, setDisplayVolume] = useState(0);
[frameDataSummary, setFrameDataSummary] = useState(0);
[transcript, setTranscript] = useState('');
useEffect(() => {
//On mount...
(async () => {
ilog('App() mounted.');
await checkPermission(); // <--- Need to have useEffect hold and call an async function
OpenAudioRx.init(options);
OpenAudioRx.subscribe('frameDataEvent', (frameDataB64) => {
const frameData = Buffer.from(frameDataB64, 'base64');
const numBytes = frameData.byteLength;
const numSamples = numBytes / packetSize;
console.log('frameDataReport: ' + numSamples + ' samples.');
const samples = [];
for (let i = 0; i < numSamples; i++) {
let si = i * byteDepth * numChannels;
let s =
byteDepth == 2
? frameData.readInt16LE(si)
: frameData.readUInt8(si);
samples.push(s);
//ilog(s);
}
setFrameDataSummary(samples[0]);
});
OpenAudioRx.subscribe('volumeEvent', (volume) => {
ilog('volume:', volume);
setDisplayVolume(volume.toFixed(0));
});
OpenAudioRx.subscribe('stopEvent', (stopEvent) => {
const stopEventCode = stopEvent.code;
const stopEventFilePath = stopEvent.filePath === 'FILE_PATH_NA' ? null : stopEvent.filePath;
ilog('stopEventCode:' + stopEventCode);
ilog('stopEventFilePath:' + stopEventFilePath);
setReceiving(false);
setFilePath(stopEventFilePath);
if (stopEventCode === 'STOP_CODE_ERROR') {
alert('Error: stopped receiving audio unexpectedly.');
}
/////////////////////////////////
// RNFS.exists(stopEventFilePath).then((status) => {
// if (status) {
// console.log('Yay! File exists');
// } else {
// console.log('File not exists');
// }
// });
// RNFS.copyFile(stopEventFilePath, '/sdcard/Documents/sample-file.wav')
// .then((success) => {
// console.log('file moved!');
// })
// .catch((err) => {
// console.log('Error: ' + err.message);
// });
getTranscript();
/////////////////////////////////
});
})();
//On unmount...
return () => {
ilog('App() unmounted.');
};
}, []);
const checkPermission = async () => {
const p = await Permissions.check('microphone');
console.log('permission check', p);
if (p === 'authorized') {
return;
}
return requestPermission();
};
const requestPermission = async () => {
const p = await Permissions.request('microphone');
console.log('permission request', p);
};
const getTranscript = async () => {
console.log('started getTranscript');
const outputPath = filePath;
console.log(outputPath);
const formData = new FormData();
let filename = outputPath.substring(
outputPath.lastIndexOf('/') + 1,
outputPath.length,
);
console.log(filename);
formData.append('file', {
uri: 'file://' + outputPath,
type: 'audio/wav',
name: filename,
});
console.log(formData);
const response = await fetch(
'https://us-central1-mytestagent-btgj.cloudfunctions.net/audio-to-text-gcloud',
{
method: 'POST',
headers: {
Accept: '*/*',
'Content-Type': 'multipart/form-data',
},
body: formData,
},
).catch((e) => console.log(e));
console.log('JSON.stringify(response)');
console.log(JSON.stringify(response));
const data = await response.json();
console.log(data);
setTranscript(data.transcript);
};
const start = async () => {
setFilePath(null);
setReceiving(true);
setLoaded(false);
OpenAudioRx.start();
setTranscript('');
};
const stop = async () => {
if (!receiving) {
return;
}
OpenAudioRx.stop();
};
const load = () => {
return new Promise((resolve, reject) => {
if (!filePath) {
return reject('file path is empty');
}
sound?.release();
sound = new Sound(filePath, '', (error) => {
if (error) {
console.log('Failed to load the file', error);
return reject(error);
}
setLoaded(true);
return resolve();
});
});
};
const play = async () => {
if (!filePath) {
return;
}
if (!loaded) {
try {
await load();
} catch (error) {
ilog(error);
return;
}
}
setPlaying(true);
Sound.setCategory('Playback');
sound?.play((success) => {
if (success) {
ilog('Successfully finished playing');
} else {
ilog('Playback failed due to audio decoding errors');
}
setPlaying(false);
});
};
const pause = () => {
sound?.pause();
setPlaying(false);
};
function PlayOrPauseButton() {
return playing ? (
<Button onPress={pause} title="Pause" disabled={!filePath} />
) : (
<Button onPress={play} title="Play" disabled={!filePath} />
);
}
return (
<View style={styles.container}>
<View style={styles.row}>
<Button
onPress={start}
title={recordToFile ? 'Record' : 'Receive'}
disabled={receiving}
/>
<Button onPress={stop} title="Stop" disabled={!receiving} />
{recordToFile ? <PlayOrPauseButton /> : <></>}
</View>
<View style={{paddingTop: 50}} />
<View style={styles.row}>
<Text>transcript: {transcript}</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
row: {
flexDirection: 'row',
justifyContent: 'space-evenly',
},
});
export default App;