-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
152 lines (133 loc) · 4.68 KB
/
App.js
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
import { StatusBar } from 'expo-status-bar';
import React, {useCallback, useEffect, useState, useRef} from 'react';
import { StyleSheet, Text, View, Button, VStack, } from 'react-native';
import _ from 'lodash';
import LiveAudioStream from 'react-native-live-audio-stream';
// import { mediaDevices } from 'react-native-webrtc';
// import RecordRTC from 'recordrtc';
// import useWebSocket from 'react-native-use-websocket';
const options = {
sampleRate: 32000, // default is 44100 but 32000 is adequate for accurate voice recognition
channels: 1, // 1 or 2, default 1
bitsPerSample: 16, // 8 or 16, default 16
audioSource: 6, // android only (see below)
bufferSize: 4096 // default is 2048
};
export default function App() {
const [onSubmit, setOnSubmit] = useState(false);
// const getSocketUrl = useCallback(() => {
// return new Promise(resolve => {
// setTimeout(() => {
// resolve('http://127.0.0.1:5000');
// }, 2000);
// });
// }, []);
// const {
// sendMessage,
// lastMessage,
// readyState,
// getWebSocket
// } = useWebSocket(getSocketUrl, STATIC_OPTIONS);
LiveAudioStream.init(options);
LiveAudioStream.on('data', data => {
// base64-encoded audio data chunks
// console.log(data);
console.log('sending data');
console.log(data);
// sendMessage({'name':'hello', 'data': data});
fetch('https://28077.gradio.app/api/predict', { method: "POST", body: JSON.stringify({"data":[ {'data':`data:audio/mp3;base64,${data}`, 'name':'hello'},null, null, 'spanish', null]}), headers: { "Content-Type": "application/json" } }).then(function(response) { return response.json(); }).then(function(json_response){ console.log(json_response) })
// var chunk = Buffer.from(data, 'base64');
});
/**
*
*/
const handleOnSubmit = useCallback(async () => {
LiveAudioStream.start();
console.log('starting');
setOnSubmit(true);
// mediaDevices.getUserMedia({audio: true, video: false})
// .then(stream =>{
// console.log("Stream is working now", stream)
// //5)
// recordAudio = RecordRTC(stream, {
// type: 'audio',
// //6)
// mimeType: 'audio/webm',
// sampleRate: 44100,
// // used by StereoAudioRecorder
// // the range 22050 to 96000.
// // let us force 16khz recording:
// desiredSampRate: 16000,
// // MediaStreamRecorder, StereoAudioRecorder, WebAssemblyRecorder
// // CanvasRecorder, GifRecorder, WhammyRecorder
// recorderType: StereoAudioRecorder,
// // Dialogflow / STT requires mono audio
// numberOfAudioChannels: 1,
// //1)
// // get intervals based blobs
// // value in milliseconds
// // as you might not want to make detect calls every seconds
// timeSlice: 4000,
// //2)
// // as soon as the stream is available
// ondataavailable: function(blob) {
// // 3
// // making use of socket.io-stream for bi-directional
// // streaming, create a stream
// var stream = ss.createStream();
// // stream directly to server
// // it will be temp. stored locally
// ss(socket).emit('stream', stream, {
// name: 'stream.wav',
// size: blob.size
// });
// // pipe the audio blob to the read stream
// ss.createBlobReadStream(blob).pipe(stream);
// }
// });
// recordAudio.startRecording();
// setOnSubmit(true);
// })
// .catch(e =>{console.log(e)})
}, []);
/**
* stop sending
*/
const handleOnStop = useCallback(async () => {
console.log('stopping');
setOnSubmit(false);
LiveAudioStream.stop();
}, []);
return (
<View style={styles.container}>
<Text>Send Live Audio Data</Text>
<Button
// flex={1}
title="Start"
size="lg"
onPress={handleOnSubmit}
isDisabled={onSubmit}
// isLoading={onSubmit}
>
Start
</Button>
<Button
title="Stop"
size="lg"
onPress={handleOnStop}
isDisabled={!onSubmit}
// isLoading={onSubmit}
>
Stop
</Button>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});