A full-stack application that records user voice input and translates each spoken sentence using AI API via a real-time WebSocket connection.
webapi-test/
├── frontend/ # React + TypeScript + Vite
│ ├── src/
│ │ ├── components/ # UI components
│ │ ├── context/ # Context API for state management
│ │ ├── hooks/ # Custom React hooks
│ │ ├── services/ # API/WebSocket services
│ │ └── types/ # TypeScript interfaces
│ ├── package.json
│ └── vite.config.ts
├── backend/ # Node.js + Express + Socket.io
│ ├── src/
│ │ ├── config/ # Configuration files
│ │ ├── services/ # Business logic (translation service)
│ │ ├── types/ # TypeScript interfaces
│ │ └── index.ts # Server entry point
│ ├── package.json
│ └── tsconfig.json
└── README.md
- Voice Recording: Uses MediaRecorder API to capture audio from user's microphone
- Real-time Communication: WebSocket connection via Socket.io for instant translation updates
- Sentence Detection: Pause-based detection to identify complete sentences
- AI Translation: Integration with OpenAI API (Whisper for transcription, GPT for translation)
- Mock Mode: Works without OpenAI API key using mock translations
- Error Handling: Comprehensive error handling for microphone permissions, connection issues, and API failures
- Modern UI: Clean and responsive interface with real-time status indicators
- Node.js: Version 18 or higher (required for File API support in backend)
- npm: Package manager
- Microphone: For voice recording functionality
- OpenAI API Key (optional): For real transcription and translation. Without it, the app uses mock mode.
- Navigate to the backend directory:
cd backend- Install dependencies:
npm install- Create a
.envfile in the backend directory (optional):
PORT=3001
OPENAI_API_KEY=your_openai_api_key_here
CORS_ORIGIN=http://localhost:5173
TRANSLATION_TARGET_LANGUAGE=enNote: If you don't provide an OPENAI_API_KEY, the application will run in mock mode with simulated translations.
- Start the development server:
npm run devThe backend server will start on http://localhost:3001 (or the port specified in your .env file).
- Navigate to the frontend directory:
cd frontend- Install dependencies:
npm install- Start the development server:
npm run devThe frontend application will start on http://localhost:5173 (default Vite port).
- Open your browser and navigate to
http://localhost:5173
- Start the Backend: Ensure the backend server is running on port 3001
- Start the Frontend: Run the frontend development server
- Allow Microphone Access: When prompted, allow the browser to access your microphone
- Check Connection: Verify the connection status indicator shows "Connected" (green)
- Start Recording: Click the "Start Recording" button
- Speak: Speak clearly into your microphone. The app will detect sentence pauses and send audio for translation
- View Translations: Translated sentences will appear in real-time below the recording controls
- Stop Recording: Click "Stop Recording" when finished
- Clear: Use the "Clear" button to remove all translations
- Decision: Use MediaRecorder API with pause-based sentence detection
- Trade-off: Simpler implementation but less accurate than advanced speech recognition. Sentence detection relies on silence thresholds which may not always align with natural speech patterns.
- Decision: Use Socket.io for WebSocket communication
- Trade-off: Socket.io adds overhead compared to native WebSockets but provides better cross-browser compatibility and automatic reconnection handling.
- Decision: Use React Context API for global state
- Trade-off: Context API is simpler for this use case but could become less performant with very large state. For a production app with more complexity, Zustand or Redux might be better.
- Decision: Support both OpenAI API and mock mode
- Trade-off: Mock mode allows development without API costs but doesn't provide real transcription. The mock mode generates random sentences rather than actual speech-to-text.
- Decision: Pause-based detection (1.5 seconds of silence)
- Trade-off: Simple and works for most cases, but may split sentences incorrectly or combine multiple sentences if pauses are inconsistent.
- Decision: Convert audio to base64 for transmission
- Trade-off: Base64 encoding increases payload size by ~33%, but it's simpler than streaming binary data. For production, consider using binary WebSocket frames or chunked uploads.
-
Input Language: The application assumes input speech is in English. The backend is configured to transcribe English audio. This can be changed in the translation service.
-
Browser Support: The application requires modern browsers with:
- MediaRecorder API support
- WebSocket support
- Microphone access permissions
-
Network: Assumes frontend and backend are on the same network or localhost during development.
-
Node.js Version: Backend requires Node.js 18+ for File API support. For older versions, the app will fall back to mock mode for audio transcription.
-
Microphone Quality: Assumes reasonable microphone quality. Poor quality microphones may result in inaccurate transcriptions.
The application includes comprehensive error handling for:
- Microphone Permissions: Clear error messages when access is denied
- Connection Issues: Automatic reconnection attempts with status indicators
- API Failures: Graceful fallback to mock mode if OpenAI API fails
- Browser Compatibility: Checks for required APIs before attempting to use them
npm run dev: Start development server with hot reloadnpm run build: Build TypeScript to JavaScriptnpm start: Run production build
npm run dev: Start Vite development servernpm run build: Build for productionnpm run preview: Preview production build
- Speech Recognition: Integrate Web Speech API for client-side transcription before sending to backend
- Language Selection: Allow users to select source and target languages
- Audio Visualization: Add waveform visualization during recording
- History: Persist translation history in local storage or database
- Export: Allow users to export translations as text files
- Better Sentence Detection: Implement more sophisticated NLP-based sentence detection
- Streaming: Use streaming audio for lower latency
- Multiple Language Support: Support multiple source languages for transcription
ISC