voice handler

This commit is contained in:
Dobromir Popov 2024-02-14 21:31:15 +02:00
parent 8070babf1d
commit 627b5badb2
3 changed files with 44 additions and 5 deletions

View File

@ -0,0 +1 @@
TTS_BACKEND_URL=http://192.168.0.10:9008/asr

View File

@ -2,6 +2,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { View, Text, Button } from 'react-native'; import { View, Text, Button } from 'react-native';
import Voice from '@react-native-voice/voice'; import Voice from '@react-native-voice/voice';
import Config from 'react-native-config';
class VoiceHandler extends Component { class VoiceHandler extends Component {
constructor(props) { constructor(props) {
@ -10,6 +11,7 @@ class VoiceHandler extends Component {
recognized: '', recognized: '',
started: '', started: '',
results: [], results: [],
isRecording: false,
}; };
Voice.onSpeechStart = this.onSpeechStart.bind(this); Voice.onSpeechStart = this.onSpeechStart.bind(this);
@ -44,22 +46,57 @@ class VoiceHandler extends Component {
recognized: '', recognized: '',
started: '', started: '',
results: [], results: [],
isRecording: true,
}); });
try { try {
await Voice.start('en-US'); await Voice.start('en-US'); // Start the voice recognition
} catch (error) {
console.error('There was an error starting voice recognition:', error);
this.setState({
isRecording: false,
});
}
}
async _stopRecognizing() {
try {
await Voice.stop();
this.setState({
isRecording: false,
});
// Assuming you have the audio data, send it to your backend
this._sendAudioToBackend(this.state.results);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
} }
_sendAudioToBackend(results) {
// Placeholder: Convert `results` or actual audio data to a format acceptable by your backend
const formData = new FormData();
// formData.append('audio', {uri: 'path_to_audio_file', type: 'audio/x-m4a', name: 'audio.m4a'});
fetch(process.env.TTS_BACKEND_URL, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(response => response.text())
.then(body => {
console.log('Audio sent to backend, response:', body);
})
.catch(error => {
console.error('Failed to send audio:', error);
});
}
render() { render() {
return ( return (
<View> <View>
<Text>Press the button and start speaking.</Text> <Text>Press the button and start speaking.</Text>
<Button <Button
onPress={this._startRecognizing.bind(this)} onPress={() => this.state.isRecording ? this._stopRecognizing() : this._startRecognizing()}
title="Start Recognizing" title={this.state.isRecording ? "Stop Recognizing" : "Start Recognizing"}
/> />
<Text>Recognized: {this.state.recognized}</Text> <Text>Recognized: {this.state.recognized}</Text>
<Text>Started: {this.state.started}</Text> <Text>Started: {this.state.started}</Text>

View File

@ -12,7 +12,8 @@
"dependencies": { "dependencies": {
"@react-native-voice/voice": "^3.2.4", "@react-native-voice/voice": "^3.2.4",
"react": "18.2.0", "react": "18.2.0",
"react-native": "0.73.4" "react-native": "0.73.4",
"react-native-config": "^1.5.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.20.0", "@babel/core": "^7.20.0",