|
|
1 year ago | |
|---|---|---|
| .. | ||
| src | 1 year ago | |
| .gitignore | 1 year ago | |
| ENV_CONFIG.md | 1 year ago | |
| QUICKSTART.md | 1 year ago | |
| README.md | 1 year ago | |
| index.html | 1 year ago | |
| package-lock.json | 1 year ago | |
| package.json | 1 year ago | |
| postcss.config.js | 1 year ago | |
| tailwind.config.js | 1 year ago | |
| tsconfig.json | 1 year ago | |
| tsconfig.node.json | 1 year ago | |
| vite.config.ts | 1 year ago | |
这是Tolan语音助手的前端应用,使用React + TypeScript + Vite构建。
npm install
创建 .env.local 文件:
VITE_WS_URL=ws://localhost:8000/ws/realtime
npm run dev
应用将在 http://localhost:5173 启动
npm run build
构建产物将输出到 dist/ 目录
frontend/
├── src/
│ ├── components/ # React组件
│ │ ├── MainPage.tsx # 主页面
│ │ ├── AnimatedCharacter.tsx # 动画IP组件
│ │ └── RecordButton.tsx # 录音按钮
│ │
│ ├── stores/ # Zustand状态管理
│ │ └── appStore.ts # 应用状态
│ │
│ ├── utils/ # 工具模块
│ │ ├── websocket.ts # WebSocket通信
│ │ ├── recorder.ts # 音频录音
│ │ └── audioPlayer.ts # 音频播放
│ │
│ ├── config.ts # 配置文件
│ ├── App.tsx # 根组件
│ ├── main.tsx # 入口文件
│ └── index.css # 全局样式
│
├── public/ # 静态资源
├── index.html # HTML模板
├── package.json
├── tsconfig.json
├── vite.config.ts
└── tailwind.config.js
使用 recorder-core 实现跨平台录音,支持:
const recorder = new VoiceRecorder({
sampleRate: 16000,
bitRate: 16,
onProcess: (pcmData) => {
// 实时发送PCM数据
websocket.sendAudio(pcmData.buffer)
}
})
使用 MediaSource API 实现流式播放:
const player = new StreamAudioPlayer({
onPlay: () => console.log('播放开始'),
onEnded: () => console.log('播放结束')
})
// 追加音频块
player.appendAudioChunk(audioData)
实时双向通信:
const ws = new RealtimeWebSocket({
url: 'ws://localhost:8000/ws/realtime',
onMessage: handleControlMessage,
onAudioData: handleAudioData
})
使用 Zustand 管理应用状态:
HTTPS要求: 现代浏览器要求HTTPS才能访问麦克风
iOS兼容: iOS需要在用户交互中初始化录音
音频格式:
# 安装依赖
npm install
# 启动开发服务器
npm run dev
# 生产构建
npm run build
# 预览生产构建
npm run preview
# 代码检查
npm run lint
MIT