Ashley Buckingham
Notes / Projects / Building Ada (part two): the time and the weather

Building Ada (part two): the time and the weather

Published:
Intended audience:
Developers, tinkerers

This is part two of the series. Part one covers why I chose an old Echo Show over a Raspberry Pi, how I flashed it with TECHO5, and how Ada became three small servers on my Mac.

At the end of part one, Ada didn't understand anything. Every request came back as the same recording. Now she hears what I say and answers two questions: what's the time, and what's the weather. Ada needs three things before she can replace my Alexa: music, the time and the weather. This part covers two of them, and music is next.

The rule for now is to copy what Alexa does in a country where it's fully supported, as closely as I can. That means the same questions, answered the same way. I want Ada to match the Alexa I'd normally use before she gets any features Alexa doesn't have.

Three fakes, three swaps

Part one left a fake in each of Ada's three servers. This part replaces all three, because an answer like "It's 10:41 am" can't come from a recording.

ServerPart onePart two
Speech to textSaved the audio, always answered "hello ada"whisper.cpp transcribes the saved audio
ChatAlways replied "Hi, I'm Ada."Keyword rules for the time and the weather
Text to speechPlayed one recorded WAVmacOS's say speaks the reply

The Show didn't need any changes. It still calls the same three addresses.

Speech to text with whisper.cpp

I wanted speech to text to run on my Mac, with a model small enough to stay out of the way. whisper.cpp is a C++ port of OpenAI's Whisper that runs on Apple Silicon's GPU. Homebrew has it, and the English-only base.en model is 148 MB:

terminal
brew install whisper.cpp
mkdir -p models
curl -L -o models/ggml-base.en.bin \
  https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin

Ada already saved every request as a WAV, so I could hear what the Show heard. Now she runs whisper's command-line tool on that same file:

src/whisper.ts
export function whisper(modelPath: string): Transcribe {
  return async (wavPath) => {
    const { stdout } = await run("whisper-cli", ["-m", modelPath, "-f", wavPath, "-l", "en", "-nt", "-np"]);
    return cleanTranscript(stdout);
  };
}

cleanTranscript removes whisper's markers for things that aren't speech, like [BLANK_AUDIO] and (music), and joins the lines into one sentence.

Part one left me with a handful of recordings from the Show, so I had a test set before writing any code. Whisper got every request right: "Are you there?", "Can you hear me?", "What's the time?" and "What time is it?".

Ten seconds, once

The first file took 10.6 seconds. Every other file took about 0.2 seconds. Running the first file again took 0.3 seconds, so the slow part was whisper's first run, not that file. The first run loads the model onto the GPU.

fig. 1: whisper's first run, and every run after

Nobody should wait ten seconds for the time, so Ada transcribes one second of silence when she starts. The model is loaded before the first real question arrives.

src/main.ts
writeFileSync(warmup, encodeWav({ rate: 16000, width: 2, channels: 1, pcm: Buffer.alloc(16000 * 2) }));
await transcribe(warmup);

Whisper heard the TV

That slow first file was 10.7 seconds of a TV in the background. I hadn't said anything. Whisper wrote down the TV's lines anyway: "This is Spanish 101. I know how to say 'Hello'…". Whisper always tries to find words, so if the TV is loud enough when I ask a question, Ada might hear the TV's words instead.

Understanding, without a model

The chat server gets the words and has to decide what to say back. Eventually that will be a language model. For two questions, a table of keyword rules is enough:

src/skills.ts
const skills: Skill[] = [
  { heard: /\b(weather|temperature|hot|cold|rain(ing)?)\b/i, answer: weather },
  { heard: /\btime\b/i, answer: time },
];

The first rule that matches answers. Anything else gets "Sorry, I can only tell you the time or the weather so far."

fig. 2: how Ada picks an answer

The time

Ada formats the Mac's clock in Dubai's time zone, not the server's. That matters if Ada ever runs on a server set to UTC.

src/skills.ts
const clock = new Intl.DateTimeFormat("en-GB", {
  hour: "numeric",
  minute: "2-digit",
  hour12: true,
  timeZone: place.timeZone,
}).format(now);
return `It's ${clock}.`;

The weather

The weather comes from Open-Meteo. It's free and needs no API key or account. One request gets the current conditions and today's forecast.

My first version said "It's 27 degrees and clear in Dubai". It worked, but it wasn't what Alexa says. Matching Alexa is the rule, so I copied the way Alexa words the weather in a country where it works:

The current weather is 26 degrees Celsius with mostly sunny skies. Today, you can expect intermittent clouds, with a high of 30 degrees and a low of 21 degrees.

Open-Meteo describes the sky with a WMO weather code, a number from 0 to 99. Ada turns each code into Alexa's words. Clear skies are "sunny" in the day and "clear" at night, using the is_day flag Open-Meteo returns:

CodeDayNight
0sunny skiesclear skies
1mostly sunny skiesmostly clear skies
2intermittent cloudsintermittent clouds
3cloudy skiescloudy skies
61–67rainrain

Fog, drizzle, showers, snow and thunderstorms each get their own words too.

The tests fake the weather response, so they check exact sentences. One of them is Alexa's sentence above, word for word.

A voice from say

macOS has a text-to-speech command built in. say -v Samantha speaks any text, and -o writes it to a file instead of the speakers. TECHO5 only plays 16-bit mono audio, so Ada asks for exactly that:

src/voice.ts
await run("say", ["-v", voice, "-o", file, "--data-format=LEI16@22050", text]);
return parseWav(await readFile(file));

Samantha sounds robotic. She's fine for the time and the weather, but Ada will get a better voice in a later part. The only British voice on my Mac is Daniel, a male voice.

What leaves the house

Speech to text and text to speech both run on my Mac now. So does the wake word, which the Show detects itself. My voice goes from the Show to the Mac over my home Wi-Fi and stops there.

fig. 3: the only request that leaves the house

The weather is the only request that goes out, and all it sends is Dubai's coordinates. Privacy is part of why I'm building Ada, but it doesn't matter much for the time and the weather. It will matter once Ada can do more.

On the Show

It works. I asked the Show the time and the weather, and both answers came back right. I also asked from across the room with music playing on another speaker, and it still heard me. I haven't tried it with music playing from the Show itself yet.

I expected running speech to text on my own Mac to be slow. It isn't. Next to my Alexa, I can't hear any difference in how long the answer takes.

The wake word is the weak spot. My other Alexa kept answering when I used "Alexa", so I switched the Show to "Computer". The Show catches "Alexa" much more reliably than "Computer" or any of its other built-in words. TECHO5's docs say the built-in words run on two different engines: "Alexa", "Okay Nabu", "Hey Jarvis" and "Hey Mycroft" on microWakeWord, and the rest, including "Computer", on openWakeWord. I don't know yet whether that's why. Either way, the real fix is a wake word of my own.

What's next

Ada now handles the time and the weather. Part three was meant to be music, but a better voice made Ada slower, and fixing that came first.

TECHO5 already lets the Show act as a Spotify Connect speaker, so I can play to it from the Spotify app. What's missing is the voice part. "Play some Fleetwood Mac" means Ada has to search Spotify and tell it to play on the Show. That needs Spotify Premium, a Spotify developer app, and a test to show the Show is a device Ada can send music to.