Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

「WIP」:sparkles: feat: 支持 TTS 的组件 & Demo 构造 #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 66 additions & 2 deletions src/ProChat/demos/control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,82 @@
import { ChatMessage, ProChat } from '@ant-design/pro-chat';

import { useTheme } from 'antd-style';
import { useState } from 'react';

import { example } from '../mocks/basic';
import { MockResponse } from '../mocks/streamResponse';

import { Button } from 'antd';
import { useEffect, useState } from 'react';

const SpeechToText = () => {
const [isListening, setIsListening] = useState(false);
const [transcript, setTranscript] = useState('');

useEffect(() => {
const speechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;

Check failure on line 18 in src/ProChat/demos/control.tsx

View workflow job for this annotation

GitHub Actions / test

Property 'SpeechRecognition' does not exist on type 'Window & typeof globalThis'.

Check failure on line 18 in src/ProChat/demos/control.tsx

View workflow job for this annotation

GitHub Actions / test

Property 'webkitSpeechRecognition' does not exist on type 'Window & typeof globalThis'.

Check failure on line 18 in src/ProChat/demos/control.tsx

View workflow job for this annotation

GitHub Actions / test

Property 'SpeechRecognition' does not exist on type 'Window & typeof globalThis'.

Check failure on line 18 in src/ProChat/demos/control.tsx

View workflow job for this annotation

GitHub Actions / test

Property 'webkitSpeechRecognition' does not exist on type 'Window & typeof globalThis'.
if (!speechRecognition) {
alert('Sorry, your browser does not support Speech Recognition.');
return;
}

const recognition = new speechRecognition();
recognition.continuous = true; // Keep listening even after voice stops
recognition.interimResults = true; // Show interim results
recognition.lang = 'zh-CN'; // Set the language of the recognizer

recognition.onstart = () => {
console.log('Voice recognition started');
};

recognition.onresult = (event) => {
console.log('event', event);

for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
setTranscript((transcript) => transcript + event.results[i][0].transcript + ' ');
}
}
};

recognition.onerror = (event) => {
console.error('Voice recognition error', event.error);
};

recognition.onend = () => {
console.log('Voice recognition ended');
setIsListening(false);
};

if (isListening) {
recognition.start();
} else {
recognition.stop();
}

// Clean up function
return () => {
recognition.stop();
};
}, [isListening]);

return (
<div>
<h1>Speech to Text Conversion</h1>
<Button onClick={() => setIsListening((prevState) => !prevState)}>
{isListening ? 'Stop Listening' : 'Start Listening'}
</Button>
<p>{transcript}</p>
</div>
);
};

export default () => {
const theme = useTheme();

const [chats, setChats] = useState<ChatMessage<Record<string, any>>[]>(example.chats);

return (
<div style={{ background: theme.colorBgLayout }}>
<SpeechToText />
<ProChat
chats={chats}
onChatsChange={(chats) => {
Expand Down