Este artículo solo está disponible en inglés.

Amazon finally gave Alexa an LLM, but it is not as smart as I expected, and it feels clunky next to ChatGPT's voice mode. OpenAI has published the API behind that voice mode, and I have a pile of Raspberry Pis gathering dust at home. So: let's build an Alexa on top of ChatGPT.

The Realtime API

OpenAI's Realtime API offers two transports: WebSockets and WebRTC. WebSockets is the easy route, but WebRTC gives the lowest latency and avoids the head-of-line blocking that any TCP-based transport suffers from.

The WebRTC flavour is simple. The client sets up a PeerConnection with one sendrecv audio transceiver, which carries your microphone one way and the agent's voice the other, plus one DataChannel for control messages. The SDP negotiation is a single HTTP request/response where the client is the offerer, and the same request carries extra configuration such as the initial prompt.

Why implement WebRTC?

Why not?

The trick is to not implement all of WebRTC. The standard is huge because it covers every case: any peer, any network, any number of tracks, any codec. Drop everything this project doesn't need and what remains is a small slice that fits in a side project.

I've seen this work before. The first time I dealt with WebRTC I was working at a company with a mature low-latency streaming server, and we wanted to add web clients. The project's development experience wasn't great: lots of manual build steps, and adding a dependency was a nightmare. When we saw what building the official WebRTC stack involved, we immediately looked for alternatives.

That's how I found a project (which, unfortunately, I've never been able to find again) that advertised itself as a "UDP socket for the web". It was tiny, because it didn't try to implement the whole standard, and so easy to compile and read that I used it as a starting point. After learning how WebRTC actually works, I built a simple RTP/SRTP transport on top of it. Our server already produced H.264 and Opus, both supported by WebRTC, so a packetizer and some RTCP handling were enough for a working prototype.

That is the plan here too: implement WebRTC, but only the parts needed to talk to the Realtime API.

How

In plain C

I've spent my whole career in C++, and I loved it, even its weirdest corners, but I've come to value the simplicity and portability of C. It is also the lingua franca of programming languages: a C API can later be exposed to virtually any other language.

With a few third-party dependencies

A word on what "from scratch" means here. I'll use a few small dependencies, each one vendored and hidden behind an explicit interface so they are easy to swap:

usrsctp looks barely maintained and most WebRTC projects are migrating to other SCTP implementations, so if I feel motivated I may end up writing my own.

With AI for the boring parts

Yes, I plan to use AI, especially for the unglamorous parts: this is a side project, and I want it to have unit tests, fuzzers and the rest of a "modern project" without typing all of that by hand. Still, every line will be reviewed by me, the design will be done by a human (me), and I'll explain every design decision along the way.

Stripping WebRTC down

Peer-to-peer (ICE)

ICE has a reduced variant, ICE Lite, meant for servers with a public IP that don't need NAT traversal. It is the minimum a server must implement to connect with a client, and it is probably what OpenAI runs. Following the same philosophy, we can design a "lite client" that only connects to lite servers (or to any server with a public IP).

A lite client behaves like a lite server, responding to connectivity checks, and additionally sends a connectivity check with the USE-CANDIDATE attribute to the server's endpoint.

Audio

Make the pipeline Opus-centric. Opus gives us tools that simplify the hardest part, the receiver:

  • Packet loss concealment: Opus can generate concealment audio when a packet is lost.
  • Pitch estimation: Opus computes the pitch of the signal and exposes it through its API, which is exactly what you need for time stretching.

With these two, a good jitter buffer is easy to build without all the signal processing that WebRTC ships with.

Send plenty of redundancy by default, and don't make the sender adaptive (at least for now). Assume the network is congested, even if that costs some bitrate on a clean one.

Only audio and one data channel

No video, no multiple audio tracks, a single data channel. Instead of a generic SDP parser and negotiation, we'll implement exactly what we need: two m-lines, one for audio (Opus only) and one for the data channel.

That's the whole scope: a C library that speaks just enough WebRTC to put a Raspberry Pi on a call with the Realtime API. In the next post I'll draft its public API and the structure of the project.