This is an example app that combines WebRTC with OpenAI's Text-to-Speech API to stream audio in real-time.
- Go 1.20 or later
- An OpenAI API key
- Web browser with WebRTC support (Chrome, Firefox, Safari, etc.)
- Clone the repository:
git clone <https://github.com/pion/example-webrtc-applications>
cd tts-to-webrtc
- Install module dependencies:
Resampler and opus encoder packages are using cgo modules and need to setup. Follow the instructions below to install the required packages.
Linux: using apt (Ubuntu), yum (Centos)...etc.
$ sudo apt install libsamplerate0 pkg-config libopus-dev libopusfile-dev
MacOS using Homebrew:
$ brew install libsamplerate pkg-config opus opusfile
- Install Go dependencies:
export GO111MODULE=on
go install github.com/pion/example-webrtc-applications/v4/tts-to-webrtc@latest
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY=your_api_key_here
- Start the server:
go run main.go
- Open your web browser and navigate to:
http://localhost:8080
- Click the "Connect" button to establish a WebRTC connection
- Wait for the connection status to show "connected"
- Type some text in the textarea
- Click "Convert to Speech" to hear the text being spoken
- The application uses OpenAI's TTS API to convert text to speech
- Audio is streamed using WebRTC with Opus codec
- Sample rate conversion is handled automatically (24kHz to 48kHz)
- The server implements a simple audio buffer to handle streaming
This project is licensed under the MIT License - see the LICENSE file for details.