-
Notifications
You must be signed in to change notification settings - Fork 17
/
App.js
192 lines (173 loc) · 5.92 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
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
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, TouchableOpacity, SafeAreaView, ActivityIndicator } from 'react-native';
import { FontAwesome } from '@expo/vector-icons';
import FadeInView from './components/FadeInView';
import { Audio } from 'expo-av';
import * as Permissions from 'expo-permissions';
import * as FileSystem from 'expo-file-system';
import { InstantSearch } from 'react-instantsearch-native';
import algoliasearch from 'algoliasearch/lite';
import config from './config';
import SearchBox from './components/SearchBox';
import Hits from './components/Hits';
const searchClient = algoliasearch(
config.ALGOLIA_APP_ID,
config.ALGOLIA_API_KEY,
);
const recordingOptions = {
// android not currently in use. Not getting results from speech to text with .m4a
// but parameters are required
android: {
extension: '.m4a',
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
},
ios: {
extension: '.wav',
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH,
sampleRate: 44100,
numberOfChannels: 1,
bitRate: 128000,
linearPCMBitDepth: 16,
linearPCMIsBigEndian: false,
linearPCMIsFloat: false,
},
};
const App = () => {
const [recording, setRecording] = useState(null);
const [isFetching, setIsFetching] = useState(false);
const [isRecording, setIsRecording] = useState(false);
const [query, setQuery] = useState('');
useEffect(() => {
Permissions.askAsync(Permissions.AUDIO_RECORDING);
}, []);
const deleteRecordingFile = async () => {
try {
const info = await FileSystem.getInfoAsync(recording.getURI());
await FileSystem.deleteAsync(info.uri)
} catch(error) {
console.log("There was an error deleting recording file", error);
}
}
const getTranscription = async () => {
setIsFetching(true);
try {
const info = await FileSystem.getInfoAsync(recording.getURI());
console.log(`FILE INFO: ${JSON.stringify(info)}`);
const uri = info.uri;
const formData = new FormData();
formData.append('file', {
uri,
type: 'audio/x-wav',
name: 'speech2text'
});
const response = await fetch(config.CLOUD_FUNCTION_URL, {
method: 'POST',
body: formData
});
const data = await response.json();
console.log(data);
setQuery(data.transcript);
} catch(error) {
console.log('There was an error reading file', error);
stopRecording();
resetRecording();
}
setIsFetching(false);
}
const startRecording = async () => {
const { status } = await Permissions.getAsync(Permissions.AUDIO_RECORDING);
if (status !== 'granted') return;
setIsRecording(true);
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
playThroughEarpieceAndroid: true,
});
const recording = new Audio.Recording();
try {
await recording.prepareToRecordAsync(recordingOptions);
await recording.startAsync();
} catch (error) {
console.log(error);
stopRecording();
}
setRecording(recording);
}
const stopRecording = async () => {
setIsRecording(false);
try {
await recording.stopAndUnloadAsync();
} catch (error) {
// Do nothing -- we are already unloaded.
}
}
const resetRecording = () => {
deleteRecordingFile();
setRecording(null);
};
const handleOnPressIn = () => {
startRecording();
};
const handleOnPressOut = () => {
stopRecording();
getTranscription();
};
const handleQueryChange = (query) => {
setQuery(query);
};
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
{isRecording &&
<FadeInView>
<FontAwesome name="microphone" size={32} color="#48C9B0" />
</FadeInView>
}
{!isRecording &&
<FontAwesome name="microphone" size={32} color="#48C9B0" />
}
<Text>Voice Search</Text>
<TouchableOpacity
style={styles.button}
onPressIn={handleOnPressIn}
onPressOut={handleOnPressOut}
>
{isFetching && <ActivityIndicator color="#ffffff" />}
{!isFetching && <Text>Hold for Voice Search</Text>}
</TouchableOpacity>
</View>
<View style={{paddingHorizontal: 20}}>
<InstantSearch
indexName={config.ALGOLIA_INDEX}
searchClient={searchClient}
>
<SearchBox query={query} onChange={handleQueryChange} />
<Hits />
</InstantSearch>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
marginTop: 40,
backgroundColor: '#fff',
alignItems: 'center',
},
button: {
backgroundColor: '#48C9B0',
paddingVertical: 20,
width: '90%',
alignItems: 'center',
borderRadius: 5,
marginTop: 20,
}
});
export default App;