Google Cloud
Use Google Cloud Speech-to-Text and Text-to-Speech with google(): streaming recognition, many languages, and Google's voices.
google() wraps the official @google-cloud/speech and @google-cloud/text-to-speech clients, so it authenticates the way the rest of your Google Cloud code does.
Install
npm install voiceai-sdk @google-cloud/speech @google-cloud/text-to-speechpnpm add voiceai-sdk @google-cloud/speech @google-cloud/text-to-speechyarn add voiceai-sdk @google-cloud/speech @google-cloud/text-to-speechbun add voiceai-sdk @google-cloud/speech @google-cloud/text-to-speechSet GOOGLE_APPLICATION_CREDENTIALS to a service account key file, or run where Application Default Credentials are available.
Quickstart
import { createSession } from "voiceai-sdk";
import { google } from "voiceai-sdk/google";
await using session = await createSession(google());
const result = await session.transcribe(await Bun.file("meeting.wav").bytes());
console.log(result.ok, result.text);This prints true and the transcript. If the file has no speech, ok is false and reason is "no_speech". await using closes the session when the block ends.
What it supports
| Feature | Support | Notes |
|---|---|---|
Speech-to-text (transcribe) | Yes | |
Streaming transcripts (listen) | Yes | |
Text-to-speech (speak) | Yes | |
| Streaming speech | No | Speech arrives as one chunk per speak call |
| Speech-to-speech | No | |
| Word timings | Yes | |
| Speaker labels | Yes | Depends on the model and language |
| Interruptions | No | |
Voice list (listVoices) | Yes | |
pause() and resume() | No |
The capability matrix compares these with every other provider.
Stream transcripts
Iterate session.listen to get partial transcripts while someone speaks and a final one when they stop.
import { createSession } from "voiceai-sdk";
import { google } from "voiceai-sdk/google";
await using session = await createSession(google(), { language: "en", sampleRate: 16000 });
for await (const t of session.listen(microphone())) {
if (t.type === "final") console.log(`[${t.startMs}ms] ${t.text}`);
}microphone() stands for any AsyncIterable<Uint8Array> of 16-bit PCM, such as a transport or a file stream.
Speak
Await session.speak for the whole clip, or iterate it for chunks.
import { createSession } from "voiceai-sdk";
import { google } from "voiceai-sdk/google";
await using session = await createSession(google());
for await (const chunk of session.speak("Let me check that for you.")) {
player.write(chunk.data);
}Voices
listVoices returns the voices you can pass as voice. The default is en-US-Neural2-F.
import { listVoices } from "voiceai-sdk";
import { google } from "voiceai-sdk/google";
const voices = await listVoices(google());
console.log(voices.filter((v) => v.language.startsWith("en")).map((v) => v.id));Hold a conversation
converse calls your handler for each final transcript and speaks what it returns. Return an async iterable to start speaking before the reply is complete.
import { createSession } from "voiceai-sdk";
import { google } from "voiceai-sdk/google";
await using session = await createSession(google());
session.connect(transport);
await session.converse(async (text, { signal }) => model.stream(text, { signal }));Configuration
Pass options to google(). Options that have an environment variable fall back to it.
| Option | Type | Default | Env var |
|---|---|---|---|
projectId | string | From credentials | GOOGLE_CLOUD_PROJECT |
location | string | "global" | |
model | string | Provider default | |
voice | string | "en-US-Neural2-F" |
Call the API directly
session.native holds the two Google clients.
import { createSession } from "voiceai-sdk";
import { google } from "voiceai-sdk/google";
await using session = await createSession(google());
const [voices] = await session.native.tts.listVoices({ languageCode: "de-DE" });Limitations
speakreturns its audio in one chunk, so playback starts after the whole sentence is synthesized.pause()andresume()fail withunsupported.- Streaming recognition streams are limited in length by Google. The SDK reopens the stream before the limit, which can split a sentence.
How it works
- Each session opens one streaming recognize call per
listenand maps results topartialandfinalevents. - Text-to-speech requests ask for
LINEAR16orMULAWto match the session'sencoding.
Next steps
- Lifecycle and cleanup
- Give your agent a voice
- Compare providers: the same features on every other provider