On this page

Azure Speech

Use Azure AI Speech with azure(): continuous recognition, neural voices with word boundaries, and regional endpoints.

azure() wraps microsoft-cognitiveservices-speech-sdk. Each session holds one recognizer and one synthesizer, created in the region you configure.

Install

npm install voiceai-sdk microsoft-cognitiveservices-speech-sdk

Set AZURE_SPEECH_KEY and AZURE_SPEECH_REGION on your server, and keep them out of browser bundles.

Quickstart

TypeScript
import { createSession } from "voiceai-sdk";
import { azure } from "voiceai-sdk/azure";

await using session = await createSession(azure());
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 speechYes
Speech-to-speechNo
Word timingsYesFrom word boundary events
Speaker labelsYesUses conversation transcription; depends on the region
InterruptionsYes
Voice list (listVoices)Yes
pause() and resume()Yes

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 { azure } from "voiceai-sdk/azure";

await using session = await createSession(azure(), { 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

Iterate session.speak to send audio as it is produced. Pass an async iterable of text, such as a model's output, and speech starts before the text is finished.

TypeScript
import { createSession } from "voiceai-sdk";
import { azure } from "voiceai-sdk/azure";

await using session = await createSession(azure());

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

TypeScript
import { listVoices } from "voiceai-sdk";
import { azure } from "voiceai-sdk/azure";

const voices = await listVoices(azure());
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 { azure } from "voiceai-sdk/azure";

await using session = await createSession(azure());
session.connect(transport);
await session.converse(async (text, { signal }) => model.stream(text, { signal }));

Configuration

Pass options to azure(). Options that have an environment variable fall back to it.

OptionTypeDefaultEnv var
keystringAZURE_SPEECH_KEY
regionstringAZURE_SPEECH_REGION
voicestring"en-US-AvaMultilingualNeural"
endpointstringRegional default

Call the API directly

session.native holds the recognizer and synthesizer.

TypeScript
import { createSession } from "voiceai-sdk";
import { azure } from "voiceai-sdk/azure";

await using session = await createSession(azure());
session.native.recognizer.properties.setProperty("SpeechServiceResponse_PostProcessingOption", "TrueText");

Limitations

  • Diarization is not available in every region. An unsupported region fails with unsupported.
  • The factory option is key, not apiKey, to match Azure's naming. The session option key is unrelated.

How it works

  • listen runs continuous recognition. recognizing events become partial, recognized events become final.
  • interrupt() stops the synthesizer and drops audio it has not sent yet.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview