On this page

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-speech

Set GOOGLE_APPLICATION_CREDENTIALS to a service account key file, or run where Application Default Credentials are available.

Quickstart

TypeScript
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

FeatureSupportNotes
Speech-to-text (transcribe)Yes
Streaming transcripts (listen)Yes
Text-to-speech (speak)Yes
Streaming speechNoSpeech arrives as one chunk per speak call
Speech-to-speechNo
Word timingsYes
Speaker labelsYesDepends on the model and language
InterruptionsNo
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.

TypeScript
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.

TypeScript
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.

TypeScript
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.

TypeScript
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.

OptionTypeDefaultEnv var
projectIdstringFrom credentialsGOOGLE_CLOUD_PROJECT
locationstring"global"
modelstringProvider default
voicestring"en-US-Neural2-F"

Call the API directly

session.native holds the two Google clients.

TypeScript
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

  • speak returns its audio in one chunk, so playback starts after the whole sentence is synthesized.
  • pause() and resume() fail with unsupported.
  • 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 listen and maps results to partial and final events.
  • Text-to-speech requests ask for LINEAR16 or MULAW to match the session's encoding.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview